var nextTimer = undefined;
var repeatSpeed = 3800;
var imageCount = 25; // actual number of images. probably overwritten by php variable later?
var currentImage = 1; // 
var autoScrollOn = true;    
var $curr;
$(document).ready(function(){
    maxWidth = 410;
    minWidth = 0;	
   
    $curr = $("#a1");
    
  //  $(".pImg").click(function(){ showNext(); });
  //  $(".nImg").click(function(){ showPrev(); });    
    
/*
JS to fade the descriptions in/out. (problematic because hovering over them, hovers out of the LI so there's a flicker)
$(".lrg").children("li").mouseover(function() {
        $("span").fadeIn(200);
    }).mouseout(function() {
        $("span").fadeOut(200); 
    });  
*/    

    //nextTimer = window.setTimeout("autoScroll()", repeatSpeed);	
});

var nextImgPath = "#";
function setNextImage(){
    nextImgPath = $(".nImg").attr('coords'); //variabile per memorizzare il titolo href
    //alert(nextImgPath);
}


    function autoScroll (){   
            
        if (autoScrollOn && nextImageLoaded) {
            //showPrev();
            showNextImage("#a1");
            nextTimer = window.setTimeout("autoScroll()", repeatSpeed);
        }
        else {
            nextTimer = window.setTimeout("autoScroll()", 100);
            //document.title+=" . ";
        }

    }
    
    function killTimer() {
      clearTimeout(nextTimer);
    }
  
    function showNext() {
         clearTimeout(nextTimer);
         var pos = $curr.parent().find('> *').index($curr); 
         if (pos >= 1) {
             
             $(".nImg").css({opacity:""});
             $curr.children().animate({opacity: ".0"}, 1500, function(){$(".previous").addClass('hidden'); $(".hidden").removeClass('previous');});             
             $curr.removeClass('active'); 
             $curr.addClass('previous');       
 
             $curr = $curr.prev();   
             $curr.removeClass('hidden');  
             $curr.addClass('active'); 
             //$curr.children().css({opacity: ".9",});   
             $curr.children().animate({opacity: ".99"}, { queue:false, duration:1500});
             
             var posText = $curr.parent().find('> *').index($curr);
             $("#current").empty().append(posText+1); 
             
             
         }
         if (pos == 1) {
            $(".pImg").css({opacity:".20"});
         }  else {
            //nextTimer = window.setTimeout("showNext()", repeatSpeed);	
         }
    }
    
        function showPrev() {
            clearTimeout(nextTimer);
            var pos = $curr.parent().find('> *').index($curr); 
            $("#current").empty().append(pos);
            if (pos < imageCount-1) {
            
            $curr.children().animate({opacity: ".0"}, 1500, function(){$(".previous").addClass('hidden'); $(".hidden").removeClass('previous');});             
            $curr.removeClass('active'); 
            $curr.addClass('previous');  
            
            $curr = $curr.next();    
            $curr.removeClass('hidden');  
            $curr.addClass('active'); 
            $curr.children().animate({opacity: ".99"}, { queue:false, duration:1500});
            
            $(".pImg").css({opacity:""});
            
             var posText = $curr.parent().find('> *').index($curr);
             $("#current").empty().append(posText+1); 
            
            }
    
         if (pos == imageCount-2) {
            $(".nImg").css({opacity:".20"});
         } else {
            //nextTimer = window.setTimeout("showPrev()", repeatSpeed);	
         }

    }
    
 //document.title="mccaul goldsmiths";   
    
 function SetupAJAXPagination(divID) {
 
  	$("a.ajx").click( function() {
  	    killTimer();
		$.ajax({
		    type: "GET",
			url: this.href,
			cache: true,
			dataType: "html",
			success: function(html){		
			    //document.title="test";
			    //$(divID).animate({opacity: ".0"}, { queue:false, duration:1000}, function(){ showNewContent(html, divID);  } );
			    //$(divID).animate({opacity: ".0"}, { queue:false, duration:1000} );
			    //showNewContent(html, divID);
			    //$("#a1 a img").fadeOut(1000);
				$(divID).fadeOut(500, function(){ showNewContent(html, divID);  } );		
   			}, 
   			complete: function() {
   				SetupAJAXPagination(divID);
   				//alert('test');
   			}
 		});
 		
 		return false;
 
	});
	
	//domUpdated();
	
}

function showNextImage(divID) {
		$.ajax({
		    type: "GET",
			url: $(".nImg").attr("href"),
			cache: true,
			dataType: "html",
			success: function(html){		
				$(divID).fadeOut(1000, function(){ showNewContent(html, divID);  } );		
   			}, 
   			complete: function() {
   				SetupAJAXPagination(divID);
   				//showNewPic(nextImgPath);
   			}
 		});
 		
 		return false;
}

 function showNewContent(html, divID) {
   // alert(html);
    $("#pagination").html($('<div>'+html+'</div>').find("#pagination").html());     
    $(divID).html($('<div>'+html+'</div>').find(divID).html());   
	//$(divID).css({display: "block" , opacity: ".0"});
	//$(divID).animate({opacity: ".99"}, { queue:false, duration:1500});
	$(divID).fadeIn(1000)
	SetupAJAXPagination(divID);
	setNextImage(); 
    showNewPic(nextImgPath);
	nextImageLoaded = false;
	$(".jqzoom").jqzoom();
 }
 
 $(document).ready(function() {
 	SetupAJAXPagination("#large");
 	setNextImage(); 
 	showNewPic(nextImgPath);
 });  

function showNewPic(imageUrl) {
//document.title=imageUrl;
var img = new Image();
img.onload = function(){
   nextImageLoaded=true;
};
img.src = imageUrl;
}

var nextImageLoaded = false;



// carousel start-up code..
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
jQuery(document).ready(function() {
    jQuery('#car').jcarousel({
        auto: 5,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        scroll: 6,
        animation: 2000
        
    });
    
    jQuery('.jcarousel-prev').bind('click', function() {
        pageTracker._trackPageview('/ajax-track/scroll-left');
        return false;
    });
    jQuery('.jcarousel-next').bind('click', function() {
        pageTracker._trackPageview('/ajax-track/scroll-right');
        return false;
    });
    
});
// end 