var defaultIndex;

function GetURL()
{
	var path = window.location.pathname;
	
	path = path.substring(0, path.lastIndexOf('/')+1)
	
	//return(window.location.protocol+'//'+window.location.host+path);
	return(window.location.protocol+'//'+window.location.host+'/');
}


function GetFullURL()
{
	var path = window.location.pathname;
	
	path = path.substring(0, path.lastIndexOf('/')+1)
	
	return(window.location.protocol+'//'+window.location.host+path);
}


function RegisterCheck()
{
	var fields = new Array('username', 'email');
	
	for(var i in fields)
	{
		$('#'+fields[i]).blur(function() {
			
			var id = $(this).attr('id');
			
			$.post(GetURL()+'ajax/register_check.php', {field: id, value: $(this).val()}, function(data) {
				
				var img, txt;
				
				// operacja przebiegła prawidłowo
				if(data.status == 1)
				{
					if(data.correct == 1)
					{
						img = 'field_correct.png';
						txt = 'Dostępne';
					}
					else
					{
						img = 'field_incorrect.png';
						txt = 'Niedostępne';
					}
					
					$('#'+id+'_correct').empty();
					$('#'+id+'_correct').append($.create('img', {'src': 'files/img/'+img, 'alt': ''+txt, 'title': ''+txt}));
				}
				
			}, 'json');
			
		});
	}
}


function Rating(section_id)
{
	defaultIndex = parseInt($('#rating').attr('src').substr($('#rating').attr('src').length-5, 1));
	
	$('#rating').mousemove(function(e){
		var pos = $(this).offset();
		var x = parseInt((e.pageX-pos.left)/13)+1;
			
		$(this).attr('src', rating_imgs[x].src);
	});

	//
	$('#rating').mouseout(function(e){
		$(this).attr('src', rating_imgs[defaultIndex].src);
	});
	
	$('#rating').click(function(e) {
		var pos = $(this).offset();
		var x = parseInt((e.pageX-pos.left)/13)+1;
			
		$(this).attr('src', rating_imgs[x].src);
			
		$(this).unbind('click mouseout mousemove');
			
		// send post request
		$.post(GetURL()+'ajax/rate_it.php', {obj: 'section', id: section_id, rate: parseInt($('#rating').attr('src').substr($('#rating').attr('src').length-5, 1))});
	});
}


function AddComment(section_id)
{
	$('#save').click(function() {
		
		if($('#comment').length)
		{
			// sending request
			$.post(GetURL()+'ajax/add_comment.php', {obj: 'section', id: section_id, comment: $('#comment').val()}, function(data) {
				
				//$('#comment').attr('disable', 'disable');
				// clone first element
				
				var new_comment = $('div[class^="comment_row"]:first').clone();
				
				if(new_comment.hasClass('comment_row1'))
				{
					new_comment.removeClass('comment_row1');
					new_comment.addClass('comment_row2');
				}
				else
				{
					new_comment.removeClass('comment_row2');
					new_comment.addClass('comment_row1');
				}
				
				jQuery.each(new_comment.children().children('span'), function(i, el) {

					switch($(el).attr('class'))
					{
						case 'comment_username':
							$(el).text(data.username);
							break;
							
						case 'comment_date':
							$(el).text('('+data.date+')');
							break;
							
						case 'comment_text':
							$(el).text($('#comment').val());
							break;
					}
					
				});
				
				//var current_class = new_comment.removeClass();
				new_comment.prependTo('div#comments');
				
				//alert($('div[class^="comment_row"]:first').attr('class'));
				
				//$('#comments').prepend($('#comments > div.comment_row:first').html());
			}, "json");
		}
		
		return false;
	});
}


function GetVideoList(s, type, count, page)
{
	// type = {'most_viewed', 'most_popular', 'latest', 'related'}
	// count = 9 
	// page = 1
	
	var c_name = 'tabs' + s;
	var c_val = $.cookie(c_name);

	if(c_val != null)
	{
		c_val = c_val.split(';');
		c_val[1] = page;
		$.cookie(c_name, c_val.join(';'), {path: '/', expires: '0'});
	}
	
	if(typeof(count) == 'undefined')
		count = 9;
	else
		count = parseInt(count);
		
	if(typeof(page) == 'undefined')
		page = 1;
	else
	{
		page = parseInt(page);
		
		if(page < 1)
			page = 1;
	}
		
	$.post(GetURL()+'ajax/get_video_list.php', {section: s, type: type, count: count, page: page}, function(data) {

		var obj1, obj2, obj3;
		
		$('#tabs-'+type).empty();

		for(var i=1; i<=data[0].returned; i++)
		{
			//<div class="item_line">
			//	<div class="hearts">
			//		<img src="files/img/ratings/rating3.jpg" width="65" height="12" />
			//	</div>
			//	<div class=""></div>
			//</div>
			//<div class="item_line">
			//	<div class="video_icon"></div>
			//	<div class="item_views">views: 123</div>
			//</div>
			//<div class="item_desc">
			//	Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sodales varius velit, at interdum quam ullamcorper nec.
			//</div>
			//
			
			data[i].title = html_to_polish(data[i].title);
						
			var url = GetURL()+'wideo/'+data[i].id+'/'+data[i].title_formated;
			var div_item = $.create('div', {'class': 'list_item'});
			

			
			
			if(typeof(data[i].desc_short) != 'undefined')
				data[i].desc_short = html_to_polish(data[i].desc_short);
			else
				data[i].desc_short = '';

			// img
			obj1 = $.create('a', {'href': url, 'class': 'item_img', 'alt': ''+data[i].title, 'title': ''+data[i].title});
			$(obj1).append($.create('img', {'src': data[i].img+''+data[i].unique_id+'_m.jpg', 'width': '130', 'height': '98', 'alt': ''+data[i].title}));
			$(div_item).append($(obj1));
			
			// title link
			$(div_item).append($.create('a', {'href': url, 'class': 'item_title', 'alt': ''+data[i].title, 'title': ''+data[i].title}, [''+data[i].title]));
			
			// episode
			if(data[i].episode && data[i].episode.length > 0)
				$(div_item).append($.create('span', {}, ['odc. '+data[i].episode]));
			
			/*
			// hearts
			obj1 = $.create('div', {'class': 'item_line'});
			obj2 = $.create('div', {'class': 'hearts'});
			
			var rating = (data[i].votes > 0) ? Math.round(data[i].total/data[i].votes) : 0;
			
			obj3 = $.create('img', {'src': 'files/img/ratings/rating'+rating+'.jpg', 'width': '65', 'height': '12'});
			
			$(obj2).append($(obj3));
			$(obj1).append($(obj2));
			$(div_item).append($(obj1));
			//*/
			
			// type & votes
			obj1 = $.create('div', {'class': 'item_line'});
			//$(obj1).append($.create('div', {'class': data[i].content_type+'_icon'}));
			//$(obj1).append($.create('div', {'class': 'video_icon'}));			
			$(obj1).append($.create('div', {'class': 'item_views'}, ['wyświetleń: '+data[i].views]));
			$(div_item).append($(obj1));
			
			// desc
			$(div_item).append($.create('div', {'class': 'item_desc'}, [''+data[i].desc_short]));
			
			$('#tabs-'+type).append($(div_item));
			
			if(i%3 == 0)
				$('#tabs-'+type).append($.create('div', {'class': 'clear'}));
		}
		
		// PAGING
		var pages = Math.ceil(data[0].max/count);
		obj1 = $.create('div', {'class': 'paging_items'});
				
		// first & prev
		if(page > 1)
		{
			// first
			obj2 = $.create('a', {'href': '#', 'class': 'paging_prev_link'}, ['<< pierwsza']);
			$(obj2).click(function() {
				GetVideoList(s, type, count, 1);
				
				return false;
			});
			
			$(obj1).append($(obj2));
			
			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
			
			// prev
			obj2 = $.create('a', {'href': '#', 'class': 'paging_prev_link'}, ['< poprzednia']);
			$(obj2).click(function() {
				GetVideoList(s, type, count, page-1);
				
				return false;
			});
			
			$(obj1).append($(obj2));
			
			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
		}
		else
			$(obj1).append($.create('div', {'class': 'paging_empty'}));
		
		// page X of Y
		$(obj1).append($.create('div', {'class': 'paging_info'}, ['Strona '+page+' z '+pages]))
		
		// next & last
		if(page < pages)
		{
			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
			
			// next
			obj2 = $.create('a', {'href': '#', 'class': 'paging_next_link'}, ['następna >']);
			//$(obj2).unbind('click');
			
			$(obj2).click(function() {
				GetVideoList(s, type, count, page+1);
				
				return false;
			});
			
			$(obj1).append($(obj2));

			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
			
			// last
			obj2 = $.create('a', {'href': '#', 'class': 'paging_next_link'}, ['ostatnia >>']);
			//$(obj2).unbind('click');
			
			$(obj2).click(function() {
				GetVideoList(s, type, count, pages);
				
				return false;
			});
			
			$(obj1).append($(obj2));
		}
			
		$('#tabs-'+type).append($(obj1));
		
		$('#tabs-'+type).append($.create('div', {'class': 'clear'}));

	}, 'json');
}

