/* homepage Carousel Script
Requires JQuery 1.3.2 or better
written by David Long for AETN UK
*/


/* Counter Script
//######################################################################################
// Author: ricocheting.com
// For: public release (freeware)
// Date: 4/24/2003 (update: 6/26/2009)
// Description: displays the amount of time until the "dateFuture" entered below.


// NOTE: the month entered must be one less than current month. ie; 0=January, 11=December
// NOTE: the hour is in 24 hour format. 0=12am, 15=3pm etc
// format: dateFuture = new Date(year,month-1,day,hour,min,sec)
// example: dateFuture = new Date(2003,03,26,14,15,00) = April 26, 2003 - 2:15:00 pm

 // dateFuture = new Date(2011,3,7,21,00,00);

// TESTING: comment out the line below to print out the "dateFuture" for testing purposes
//document.write(dateFuture +"<br />");


//###################################
//nothing beyond this point
function GetCount(txYYYY,txMM,txDD,txHH,txMIN,txSS){
	alert(txYYYY,txMM,txDD,txHH,txMIN,txSS);
	dateFuture = new Date(txYYYY,txMM,txDD,txHH,txMIN,txSS);
	dateNow = new Date();									//grab current date
	amount = dateFuture.getTime() - dateNow.getTime();		//calc milliseconds between dates
	delete dateNow;

	// time is already past
	if(amount < 0){
	//	document.getElementById('countbox').innerHTML="Now!";
		document.getElementById('countbox-days').innerHTML="DD";
		document.getElementById('countbox-hours').innerHTML="HH";
		document.getElementById('countbox-mins').innerHTML="MM";
		document.getElementById('countbox-secs').innerHTML="SS";
	}
	// date is still good
	else{
		days=0;hours=0;mins=0;secs=0;out="";

		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

		days=Math.floor(amount/86400);//days
		amount=amount%86400;

		hours=Math.floor(amount/3600);//hours
		amount=amount%3600;

		mins=Math.floor(amount/60);//minutes
		amount=amount%60;

		secs=Math.floor(amount);//seconds

		if(days != 0){out += days +" day"+((days!=1)?"s":"")+", ";}
		if(days != 0 || hours != 0){out += hours +" hour"+((hours!=1)?"s":"")+", ";}
		if(days != 0 || hours != 0 || mins != 0){out += mins +" minute"+((mins!=1)?"s":"")+", ";}
		out += secs +" seconds";
		
		var ddays = days;
		var dhours = hours;
		var dmins = mins;
		var dsecs = secs;
		if(days < 10){ddays = "0" + days;}
		if(hours < 10){dhours = "0" + hours;}
		if(mins < 10){dmins = "0" + mins;}
		if(secs < 10){dsecs = "0" + secs;}
		
		//document.getElementById('countbox').innerHTML=out;
		document.getElementById('countbox-days').innerHTML=ddays;
		document.getElementById('countbox-hours').innerHTML=dhours;
		document.getElementById('countbox-mins').innerHTML=dmins;
		document.getElementById('countbox-secs').innerHTML=secs;

		setTimeout("GetCount()", 1000);
	}
}*/

var	counter = 0;

$(document).ready(function(){
	
	//if($('#countbox-days')!= null){GetCount();}
	
	// Set up carousel 
	//options( 1 - ON , 0 - OFF)  
	var auto_slide = 1;  
	var hover_pause = 1;  
	var key_slide = 0;   
	//get the item width  
    var item_width = $('#carouselMain .carouselContent').outerWidth() ;
	
	//speed of auto slide(  
	var auto_slide_seconds = 8000;  
	// IMPORTANT: i know the variable is called ...seconds but it's 
	// in milliseconds ( multiplied with 1000) '  
	
	// move the last list item before the first item. The purpose of this is 
	// if the user clicks to slide left he will be able to see the last item.  
	//$('#carouselMain .carouselContent:first').before($('#carouselMain .carouselContent:last'));  
	
	//check if auto sliding is enabled  
         if(auto_slide == 1){  
             // set the interval (loop) to call function slide with option 'right' 
             // and set the interval time to the variable we declared previously   
             var timer = setInterval('slide("right")', auto_slide_seconds);  
   
             // and change the value of our hidden field that hold info about 
             // the interval, setting it to the number of milliseconds we declared previously  
            // $('#hidden_auto_slide_seconds').val(auto_slide_seconds);  
         }  
   	 
	 
	 
	  //check if hover pause is enabled  
         if(hover_pause == 1){  
             //when hovered over the list  
             $('#carouselMain').hover(function(){  
                 //stop the interval  
                 clearInterval(timer)  
             },function(){  
                 //and when mouseout start it again  
                 timer = setInterval('slide("right")', auto_slide_seconds);  
             });  
   
         }  
   
         //check if key sliding is enabled  
         if(key_slide == 1){  
   
             //binding keypress function  
             $(document).bind('keypress', function(e) {  
                 //keyCode for left arrow is 37 and for right it's 39 '  
                 if(e.keyCode==37){  
                         //initialize the slide to left function  
                         slide('left');  
                 }else if(e.keyCode==39){  
                         //initialize the slide to right function  
                         slide('right');  
                 }  
             });  
   
         }  
		 
		 // Navigation controls
		 $('#carouselMain ul.carouselNav li').each(function(index) {
	
		$('#carouselMain ul.carouselNav li a:eq(' + index + ')').click(function(event){
			event.preventDefault();
			
	 		$("#carouselMain ul.carouselNav li.selected").removeClass('selected');
	 		$('#carouselMain ul.carouselNav li:eq(' + index + ')').addClass('selected');
			
			//var slideVal = ${#carouselMain .slider).css('left');
			var NavItems = $('#carouselMain ul.carouselNav li').length;
			
			
			if(index < NavItems){
			var navTargetPos = (index * (item_width * -1));
		//	if(navTargetPos < slideVal ){}
			}else {
				navTargetPos = 0;
			}
			$('#carouselMain .slider').animate({left:'' + navTargetPos + 'px'},750, function(){
				counter = index;
				
			});
				
			
				
			});
			
	});
   
   });  
    //FUNCTIONS BELLOW  
   
 //slide function  
 function slide(where){  
   				
             //get the item width  
             var item_width = $('#carouselMain .carouselContent').outerWidth() ;  
   
             // using a if statement and the where variable check 
             // we will check where the user wants to slide (left or right)  
             if(where == 'left'){  
                 //...calculating the new left indent of the unordered list (ul) for left sliding  
                 var left_indent = parseInt($('#carouselMain .slider').css('left')) + item_width;  
             }else{  
                 //...calculating the new left indent of the unordered list (ul) for right sliding  
                 var left_indent = parseInt($('#carouselMain .slider').css('left')) - item_width;  
   
             }  
			 $("#carouselMain ul.carouselNav li.selected").removeClass('selected'); 
				 var numberOfNavItems = $('#carouselMain ul.carouselNav li').length -1;
				counter++;
				if(counter > numberOfNavItems){
					counter = 0;
					left_indent = 0;
				}
				$('#carouselMain ul.carouselNav li:eq(' + counter + ')').addClass('selected');
				//$(".headerAd").html('counter: ' + counter + ', numberOfNavItems: ' + numberOfNavItems  + '.');
   			
             //make the sliding effect using jQuery's animate function... '  
             $('#carouselMain .slider:not(:animated)').animate({'left' : left_indent},1000,function(){  
   				
				/*var slideVal = $("#carouselMain .slider").css('left');
				
				var slideValStripped = slideVal.replace("px","");
				var slideNumber = Math.round( ( (slideValStripped *-1) - 964) / 964);
				//alert('slideNumber:' + slideNumber + ', slideVal:' + slideVal + ', numberOfNavItems:' + numberOfNavItems);
				
				if(slideNumber <= numberOfNavItems){
				
				$('#carouselMain ul.carouselNav li:eq(' + slideNumber + ')').addClass('selected');
				}else {
					$('#carouselMain ul.carouselNav li:eq(' + numberOfNavItems + ')').addClass('selected');
					
				}*/
                 // when the animation finishes use the if statement again, and make an ilussion 
                // of infinity by changing place of last or first item 
                /* if(where == 'left'){  
                     //...and if it slided to left we put the last item before the first item  
                     $('#carouselMain .carouselContent:first').before($('#carouselMain .carouselContent:last'));  
                 }else{  
                     //...and if it slided to right we put the first item after the last item  
                     $('#carouselMain .carouselContent:last').after($('#carouselMain .carouselContent:first'));  
                 } */
				 
				
   
                 //...and then just get back the default left indent  
                // $('#carouselMain .slider').css({'left' : '0'});  
             });  
   
 }


