var changeInterval = 4000;
var fadeInterval = 700;

var currentPage = 1;

$(function(){
	$('#recomandations_pages_wrapper').everyTime(changeInterval, "timer", nextPage, 0);
	
	$('a.recomandatios_page_trigger').bind('click', function(){
		setPage($(this).html());
	});
	
	$('a.recomandatios_page_next').bind('click', function(){
		nextPage();
	});
	
	$('a.recomandatios_page_prev').bind('click', function(){
		previousPage();
	});
	
	$('a.category_box_pagination_prev').bind('click', scrollRight);
	$('a.category_box_pagination_next').bind('click', scrollLeft);
    
    $('a.sort_link').bind('click', function(){
        if (!$(this).hasClass('active')) {
        	var id = $(this).attr('rel');
        	var scroller = $('#category_list_'+id).find('div.category_video_box_wrapper').find('div.category_video_box_scroller');
        	var top = parseFloat(scroller.css('top').replace('/([\d.]+)(px|pt|em|%)/','$1'));
            if (top == -238) {
                scroller.animate({top:0, left:0}, 400);
            } else {
                scroller.animate({top:-238, left:0}, 400);
            }
            $('#category_list_' + id + ' a.sort_link').removeClass('active');
            $(this).addClass('active');
         }
    });
});

function scrollLeft() {
	//$('a.category_box_pagination_next').unbind('click');
	var id = $(this).attr('rel');
	var scroller = $('#category_list_'+id).find('div.category_video_box_wrapper').find('div.category_video_box_scroller');
	var left = parseFloat(scroller.css('left').replace('/([\d.]+)(px|pt|em|%)/','$1'));
	left = left - 800;
	if (left>=-1600)
	{
		if (!scroller.is(':animated'))
		{
			scroller.animate({
				left: left
			}, 400);
		}
	} else {
		if (!scroller.is(':animated'))
		{
			/*scroller.animate({
				left: 0
			}, 400);*/
			document.location = baseUrl + '/video-' + $(this).parent().children(':first-child').attr('rel');
		}
	}
}

function scrollRight() {
	//$('a.category_box_pagination_prev').unbind('click');
	var id = $(this).attr('rel');
	var scroller = $('#category_list_'+id).find('div.category_video_box_wrapper').find('div.category_video_box_scroller');
	var left = parseFloat(scroller.css('left').replace('/([\d.]+)(px|pt|em|%)/','$1'));
	left = left + 800;
	if (left<=0)
	{
		if (!scroller.is(':animated'))
		{
			scroller.animate({
				left: left
			}, 400);
		}
	} else {
		if (!scroller.is(':animated'))
		{
			scroller.animate({
				left: -1600
			}, 400);
		}
	}
}

function nextPage() {
	$('#page_'+currentPage).fadeOut(fadeInterval);
	currentPage++;
	if (currentPage == 4) currentPage = 1;
	$('a.recomandatios_page_trigger').removeClass('active');
	$('#page_trigger_'+currentPage).addClass('active');
	$('#page_'+currentPage).fadeIn(fadeInterval);
}

function previousPage() {
	$('#page_'+currentPage).fadeOut(fadeInterval);
	currentPage--;
	if (currentPage == 0) currentPage = 3;
	$('a.recomandatios_page_trigger').removeClass('active');
	$('#page_trigger_'+currentPage).addClass('active');
	$('#page_'+currentPage).fadeIn(fadeInterval);
	resetTimer();
}

function setPage(page) {
	$('#page_'+currentPage).fadeOut(fadeInterval);
	currentPage = page;
	$('a.recomandatios_page_trigger').removeClass('active');
	$('#page_trigger_'+currentPage).addClass('active');
	$('#page_'+currentPage).fadeIn(fadeInterval);
	resetTimer();
}

function resetTimer() {
	$('#recomandations_pages_wrapper').stopTime("timer");
	$('#recomandations_pages_wrapper').everyTime(changeInterval, "timer", nextPage, 0);
}
