var firstIndex = 0;
var nextEmission = 0;
var timer;
var scrNextCounter = 0;
var timer;
				
function evalProgsData(currentDate) {
	for (i=channel.programs.length-1;i>=0;i--) {
		prog = channel.programs[i];
		emissionDate = new Date(prog.emissionDate);
		if  (emissionDate <= currentDate) {
			firstIndex = i;
			break;
		}
		nextEmission = emissionDate;
	}
	
	if (firstIndex + 1 == channel.programs.length) {
		$('#program').html('');
		return false;
	}
	
	diff = nextEmission - currentDate + 100;
	if (timer) clearTimeout(timer);
	timer = setTimeout(function() {reloadProgramCarousel(); }, diff);
	return true;
}
				
function generateList(currentDate, channel) {
	firstIndex = 0;
	nextEmission = 0;
	scrNextCounter = 0;
	if (evalProgsData(currentDate)) {
		mycarousel_itemList = [];
		count = 0;
		for (i = firstIndex; i < channel.programs.length ; i++) {
			prog = channel.programs[i];
			emissionDate = new Date(prog.emissionDate);
			mycarousel_itemList.push(prog);
			if (count == 8) break;
			count++;
		}
		return true;
	}
	return false;
}
				
var mycarousel_itemList = [];

function mycarousel_itemLoadCallback(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {
        if (carousel.has(i)) {
            continue;
        }

        if (i > mycarousel_itemList.length) {
            break;
        }
        carousel.add(i, mycarousel_getItemHTML(i, mycarousel_itemList[i-1]));
    }
};

function mycarousel_getItemHTML(i, program)
{
    emissionDate = new Date(program.emissionDate);
	min =  emissionDate.getMinutes();
	if (min == 0 || min < 10) {
		min = "0" + min;
	}
	return '<div class="item c' + (i)+'"><p><a href="'+getProgramTvUrl()+'"> <span>' + emissionDate.getHours() + '.<small>' + min +'</small></span><strong>' + program.title + '</strong></a></p>';
};

var programCarousel;

function mycarousel_initCallback(carousel) {
	programCarousel = carousel;
}

function mycaruselStyling() {
	$('#program .wrap .more .prev').hide();
	$('#program .wrap .more .next').show();
	$('#programCarousel li:first').css('border','none');
}

jQuery(document).ready(function() {
	currentTime = getServerTime();
	if (generateList(currentTime,channel)) {
		jQuery('#program .wrap').jcarousel({
		scroll: 1,
		size: mycarousel_itemList.length,
		itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback},
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
		});

		$('#program .wrap .more .next').click(function() { 
			scrNextCounter++;
			if (scrNextCounter == 1)
				$('#program .wrap .more .prev').show();		
			if (scrNextCounter == 5)
				$('#program .wrap .more .next').hide();		
			programCarousel.next(); 
		});

		$('#program .wrap .more .prev').click(function() { 
			scrNextCounter--;
			if (scrNextCounter == 0)
				$('#program .wrap .more1 .prev').hide();
			if (scrNextCounter == 4)
				$('#program .wrap .more .next').show();
			programCarousel.prev(); 
		});

	
		programCarousel.reset();
		programCarousel.reload();
		mycaruselStyling();
	}
});


function reloadProgramCarousel() {
	currentTime = getServerTime();
	generateList(currentTime,channel);
	programCarousel.reset();
	programCarousel.reload();
}
