var contentElementWidth = 980;
var elementCount = 0;
var contentIndex = 0;
var imageIndex = 0;
var rotateTimeout;
var waitTime = 5000;
var animationTime = 500;
var captionWidth = 980; 

ua = navigator.userAgent.toLowerCase(),
check = function(r){
    return r.test(ua);
},
isChrome = check(/\bchrome\b/),
isSafari = !isChrome && check(/safari/),
isSafari5 = isSafari && check(/version\/5/),

$(document).ready(function() {
	
	elementCount = $('.galleryListSet').children('li').length;
	channelNumbersWidth = elementCount * 31;
	channelPanelWidth = captionWidth + channelNumbersWidth; 
	
	$('.galleryListSet').width((elementCount * contentElementWidth)+980);
	$('.galleryListSet').css({'position': 'relative'});
	$('.galleryListSet').append('<li>'+$('.galleryListSet li:eq(0)').html()+'</li>');
	$('.channelControls a').click(function(e) {
		e.preventDefault();
		navNumber = $(this).html();
	    updateContent(navNumber - 1);
	});

    /*$('.channelSelectorWrapper').css({display: 'block'});
	$('.channelSelectorWrapper').animate({opacity: 1}, 1000, "swing", function() {
	    
	});*/
	
	setTimeout('initGallery();', 1000);
	
	$('#whereSubmit').click(function(e) {
	    e.preventDefault();
	    location.href=$('#choose').val();
	});
	
	$('#whatSubmit').click(function(e) {
	    e.preventDefault();
	    location.href=$('#choose2').val();
	});
	
});

function initGallery() {
	rotateTimeout = setTimeout(function() {
		rotateImage();
	}, waitTime);
};

function updateContent(index) {
    imageIndex = index-1;
    contentIndex = index-1;
    clearTimeout(rotateTimeout);
    rotateImage();
}

function updateText(index) {
    var anchor = $('.galleryListSet li img:eq('+index+')');
    //var newLink = $(anchor).attr('href');
    //$('.channelBody h1').html('<a href="'+newLink+'">'+$(anchor).attr('title')+'</a>');
    $('.text-holder p').text($(anchor).attr('alt'));
    //$('.csFeatureImage img').attr('src', 'admin-resources/image-tools.php?w=83&h=77&c=1&e=0&src=' + $('.thumbSrc', anchor).html());
    $('.paging-holder ul li').removeClass('active');
    //$('.csViewMoreBtn').attr('href', newLink);
    //$('.csFeatureImage a').attr('href', newLink);
    $('.paging-holder ul li a:eq('+index+')').parent().addClass('active');
}

function rotateImage() {
    imageIndex++;
	contentIndex = contentIndex == elementCount-1 ? 0 : contentIndex+1;	
	updateText(contentIndex);
    if(imageIndex == elementCount+1){
        imageIndex = 1;
        $('.galleryListSet').css({right: '0px'});
    }
     $('.galleryListSet').stop(true, true);
     if(!isSafari5) {
        $('.galleryListSet').animate({right: (imageIndex*contentElementWidth)+'px'}, animationTime, "swing");
     } else {
        $('.galleryListSet').css({right: (imageIndex*contentElementWidth)+'px'});
     }
	rotateTimeout = setTimeout(function() {
		rotateImage();
	}, waitTime);
};


