var counter;
var activeSlide;

var clicked;

function timer(times, activeSlide){
	$("#timer").everyTime(3500, function() {
		
		//console.log('counter = '+counter+' activeSlide = '+activeSlide);	
		if (counter % 5 == 0) {counter = 1;}
		
		id = counter;
        if(id != activeSlide){
		
            $("#home-slides #slide-" + activeSlide).fadeOut('slow');
			$("#thumb-"+activeSlide+" .imgHoverable").attr("src", "/images/thumb"+activeSlide+"_off.gif");
			
			$("#home-slides #slide-" + id).fadeIn('slow');
			$("#thumb-"+id+" .imgHoverable").attr("src", "/images/thumb"+id+"_on.gif");
			
            activeSlide = id;
        } 
                 
      counter = counter + 1;  
    }, times);     
}

$(document).ready(function(){
	
	$(document).pngFix(); 
	
     counter = 2;
	 activeSlide = 1;
     timer(999, activeSlide);

     // Change the image of hoverable images
     $(".imgHoverable").hover( function() {  
         var hoverImg = HoverImgOf($(this).attr("src"));
         $(this).attr("src", hoverImg);
       }, function() {
		 if ( ($(this).parent().attr('id') != clicked) )  {
		 	var normalImg = NormalImgOf($(this).attr("src"));
		 	$(this).attr("src", normalImg);
		 }
	   }
     );
	
	$("#home-slides-thumbs .item").click(function(){
		clicked = $(this).attr("id");

		var i = 1;
		
        id = $(this).attr("id");
        id = id.substring(id.length-1,id.length);
		//$('#home-slides-indicator span').stop();
		//$("#timer").stopTime();
		
		//console.log('id = '+id);
        //if(id != activeSlide){
		
		
		if ( $('#home-slides #slide-' + id).css('display') != 'block') {
		
            $("#home-slides #slide-" + activeSlide).fadeOut('slow');
			
			//$('#home-slides-thumbs .item').fadeOut('slow');
			//$('#home-slides-thumbs .item .imgHoverable').attr("src", "/images/thumb"+i+"_off.gif");
			
			while (i<=5)
			{
				$("#home-slides #slide-" + i).fadeOut('slow');
				$("#thumb-"+i+" .imgHoverable").attr("src", "/images/thumb"+i+"_off.gif");
				i++;
			}
			
			$("#home-slides #slide-" + id).fadeIn('slow');
			$("#thumb-"+id+" .imgHoverable").attr("src", "/images/thumb"+id+"_on.gif");
		}
			activeSlide = parseInt(id);
			counter = parseInt(id);
			//console.log('counter = '+counter+' activeSlide = '+activeSlide);	
        //}

    });

	
	/*$("#home-slides-thumbs").mouseout(function(){
		$('#home-slides-indicator span').stop();
		$("#timer").stopTime();
        timer(999, activeSlide);
    });*/
	
    /*$("#home-slides-thumbs .item").mouseover(function(){
        id = $(this).attr("id");
        id = id.substring(id.length-1,id.length);
        $('#home-slides-indicator span').stop();
        $('#home-slides-indicator span').animate({
            //left: goTo
          },
          { duration: 200 }
        );
    });*/
	
    /*$("#home-slides-thumbs").mouseout(function(){
        //activeSlide = 1;
		//counter = 1;
		//offset = (190 * (activeSlide - 1)) + 85;
        //goTo = offset + "px";
        $('#home-slides-indicator span').stop();
        $('#home-slides-indicator span').animate({
            //left: goTo
          },
          { duration: 200 }
        );
        timer(999, activeSlide);
    });*/
    
	
    /*$("#home-slides-thumbs .item").mouseover(function(){
        $("#timer").stopTime();
        id = $(this).attr("id");
        id = id.substring(id.length-1,id.length);
        if(id != activeSlide){
            $("#home-slides #slide-" + activeSlide).fadeOut('slow');
            $("#home-slides #slide-" + id).fadeIn('slow');
            activeSlide = id;
            
        }
        
    });*/
	
});

    /* ------------------------------------
    Change the image of hoverable images
    --------------------------------------*/
            
    function HoverImgOf(filename)
    {
       var re = new RegExp("(.+)_off\\.(gif)", "g");
       return filename.replace(re, "$1_on.$2");
    }
    function NormalImgOf(filename)
    {
       var re = new RegExp("(.+)_on\\.(gif)", "g");
       return filename.replace(re, "$1_off.$2");
    }


  
