var videoGaleryLength = 0;
var itemsPerPage = 5;
var totalGalleryPage = 0;
var currentGalleryPage = 1;
var curretnVideoID = "";

function inittv()
{	
	$('.streamingItem').hover(
		function() 
		{	
			$(this).css({"color": "#111"});
			$(this).children('.videoThumb').children('.playOverVideo').css({"display": "block"});
			$(this).children('.videoThumb').children('.playOverVideo').stop(true, true).animate({"opacity": 1}, 200);
			$(this).children('.videoThumb').stop(true, true).animate({"borderBottomColor": "#e96b00", "borderTopColor": "#e96b00", "borderLeftColor": "#e96b00" , "borderRightColor": "#e96b00"}, 200);	
		},
		function() 
		{	
			$(this).css({"color": "#999"});
			$(this).children('.videoThumb').children('.playOverVideo').stop(true, true).animate({"opacity": 0}, 200);
			if(curretnVideoID!=$(this).attr('ID'))
			{
				$(this).children('.videoThumb').stop(true, true).animate({"borderBottomColor": "#CCC", "borderTopColor": "#CCC", "borderLeftColor": "#CCC" , "borderRightColor": "#CCC"}, 200);
			}
		}
	);
	$('.streamingItem').click(
		function() 
		{	
			var id = $(this).attr('ID');
			var video = $(this).attr('data-video');
			var image = $(this).attr('data-image');
			changeVideo(id, video, image);	
		}
	);
	
	$('.setanavle').hover(
		function() 
		{	
			$(this).children('img').stop(true, true).animate({"left": 2}, 200);			
		},
		function() 
		{	
			$(this).children('img').stop(true, true).animate({"left": 5}, 200);
		}
	);
	$('.setanavle').click(
		function() 
		{	
			prevGalleryPage();			
		}
	);
	
	$('.setanavld').hover(
		function() 
		{	
			$(this).children('img').stop(true, true).animate({"left": 3}, 200);			
		},
		function() 
		{	
			$(this).children('img').stop(true, true).animate({"left": 0}, 200);
		}
	);
	$('.setanavld').click(
		function() 
		{	
			nextGalleryPage();			
		}
	);
	
	videoGaleryLength = $('.streamingItem').length;
	totalGalleryPage = Math.ceil(videoGaleryLength / itemsPerPage);
	$('.streamingItemContainer').css({"width": 145 * videoGaleryLength});
	videoGalleryNavigate(1, 0);
}

function nextGalleryPage()
{
	var page = currentGalleryPage + 1;
	page = (page>totalGalleryPage) ? totalGalleryPage : page;
	videoGalleryNavigate(page, 1200);
}

function prevGalleryPage()
{
	var page = currentGalleryPage - 1;
	page = (page<1) ? 1 : page;
	videoGalleryNavigate(page, 1200);
}

function videoGalleryNavigate(page, time)
{
	currentGalleryPage = page;
	$('.streamingItemContainer').animate({"left": -((page-1)*(140*itemsPerPage))}, {queue:false, duration: time, easing:"quartEaseInOut"});
	setArrowGallery();	
}

function setArrowGallery()
{
	if(currentGalleryPage>=totalGalleryPage)
	{
		$('.setanavld').fadeOut();
	}else
	{
		$('.setanavld').fadeIn();
	}
	
	if(currentGalleryPage<=1)
	{
		$('.setanavle').fadeOut();
	}else
	{
		$('.setanavle').fadeIn();
	}
}

function setVideo(id, url, image)
{
	curretnVideoID = id;
	
	$('.streamingItem').each(function()
	{
		$(this).children('.videoThumb').stop(true, true).animate({"borderBottomColor": "#CCC", "borderTopColor": "#CCC", "borderLeftColor": "#CCC" , "borderRightColor": "#CCC"}, 0);
	});
	$("#" + curretnVideoID).children('.videoThumb').stop(true, true).animate({"borderBottomColor": "#e96b00", "borderTopColor": "#e96b00", "borderLeftColor": "#e96b00" , "borderRightColor": "#e96b00"}, 200);
	jwplayer("videoPlayerTv").setup(
	{ 
		flashplayer: "player/player.swf", 
		file: url, 
		height: 392, 
		width: 642,
		skin: "player/minima.zip",
		image: image
	});
}

function changeVideo(id, url, image)
{
	jwplayer("videoPlayerTv").remove();
	setVideo(id, url, image);
}
