var contentElementWidth = 688;
var elementCount = 0;
var contentIndex = 1;
var imageIndex = 1;
var rotateTimeout;
var waitTime = 5000;
var animationTime = 500;
var captionWidth = 190;

$(document).ready(function() {

	elementCount = $('.homeGallery ul').children('li').length;
	totalRight = elementCount * contentElementWidth;
       
    firstSrc = $('.homeGallery ul li:eq(0)').html();
    lastSrc = $('.homeGallery ul li:eq('+(elementCount-1)+')').html();

    $('.homeGallery ul').width((elementCount * contentElementWidth)+contentElementWidth);
    $('.homeGallery ul').css({'position': 'relative'});
       
    $('.homeGallery ul').prepend("<li>"+lastSrc+"</li>");
    $('.homeGallery ul').append("<li>"+firstSrc+"</li>");
    //$('.homeGallery ul').prepend("<li class='slide"+imageCount+"'>"+lastSrc+"</li>");
    //$('.homeGallery ul').append('<li>'+$('.homeGallery ul li:eq(0)').html()+'</li>');
    /*$('.channelNumbers a').click(function(e) {
    	e.preventDefault();
        navNumber = $(this).html();
		//console.log(navNumber);
        updateContent(navNumber - 1);
	});*/
    $('.csNext').click(function(e) {
        e.preventDefault();
        clearTimeout(rotateTimeout);
        rotateImage(imageIndex-1);
    });
    
    $('.csPrev').click(function(e) {
        e.preventDefault();
        clearTimeout(rotateTimeout);
        prevContent(imageIndex);
    });

    $('.homeGallery ul').css({display: 'block'});
    $('.homeGallery ul').animate({opacity: 1}, 1000, "swing", function() {
        initGallery();
    });
});

function prevContent(index) {
    imageIndex--;
    //contentIndex = imageIndex;
    //contentIndex = contentIndex == elementCount-1 ? 0 : contentIndex+1;
    if(imageIndex == -1) {
        imageIndex = elementCount-1;
        contentIndex = imageIndex;
        $('.homeGallery ul').css({right: totalRight+'px'});
	    updateText(contentIndex);
    } else {
        imageIndex = index-1;
        contentIndex = imageIndex;
        updateText(contentIndex);
    }
    $('.homeGallery ul').stop(true, true);
    $('.homeGallery ul').animate({right:(imageIndex*contentElementWidth)+'px'}, animationTime, "swing");
    rotateTimeout = setTimeout(function() {
        rotateImage();
    }, waitTime);
};

function initGallery() {
    rotateTimeout = setTimeout(function() {
    	rotateImage();
    }, waitTime);
};

function updateContent(index) {
    imageIndex = index-1;
    contentIndex = index;
    updateText(imageIndex);
    clearTimeout(rotateTimeout);
    rotateImage();
}

function updateText(index) {
    var anchor = $('.homeGallery ul li:eq('+index+')');
    var newTitle = $(anchor).children('img').attr('title');
    $('.csImageTitle').html(newTitle);
    $('.channelNumbers a').removeClass('selected');
    $('.channelNumbers a:eq('+index+')').addClass('selected');
}

function rotateImage() {
    imageIndex++;
    contentIndex = contentIndex == elementCount-1 ? 0 : contentIndex+1;
    updateText(contentIndex);
    if(imageIndex == elementCount+1){
        imageIndex = 1;
        $('.homeGallery ul').css({right: '0px'});
    }
    $('.homeGallery ul').stop(true, true);
    $('.homeGallery ul').animate({right:(imageIndex*contentElementWidth)+'px'}, animationTime, "swing");
    rotateTimeout = setTimeout(function() {
    	rotateImage();
    }, waitTime);
};
