// START jQUERY
$(document).ready(function(){  
	
	// ARTIST/EXHIBITION CONTENT DEFAULTS 
	if ($("#content.artist").length>0) {  
		
		// HIDE SIDE NAV ELEMENTS IF PERTINENT CONTENT ISN'T LOADED ON PAGE 
		$(".sidebar .nav a.tab").hide(); 
		$(".multi-content").each(function(){ 
			var contentID = $(this).attr("id");   
			$(".sidebar .nav a.tab").each(function(){      
				if($(this).hasClass(contentID)) { 	
					$(this).show(); 
				}   
			})   
		})
		
		// INIT THE ARTIST/EXHIBIT SIDE NAV
		$(".sidebar .nav a.tab").click(function(){     
			$(".multi-content").hide();    
			$(".paging").hide();
			$(".artwork-details").hide();
			$(".sidebar .nav a").removeClass("on"); // turn off all nav level links
			$(this).addClass("on"); // turn on active nav level link    
			var theContent = $(this).attr("href"); 
            $(theContent).show();
			return false;
		})                
	}
	   
	// ARTIST THUMBNAIL LIST VIEW
	if ($(".artwork-thumbs").length>0) { 
	
		/* ARTIST WORKS PAGE: INITIALIZE CLICKS ON THE THUMBNAILS AND THE PAGING LINKS */
		$(".artwork-thumbs a").click(function(){
			// hide the thumbnails
			$(".artwork-thumbs").addClass("hide-thumbs");
			// keep track of current image
			$(this).addClass("on");
			// show the requested image, its description and paging links
			imagePager($(this).attr("class"),"thumb"); 
			// don't follow the link
			return false;
		});
	
		$(".paging a").click(function(){
			// show the requested image, its description and paging links
			imagePager($(this).attr("class"),"paging");
			// don't follow the link
			return false;
		});
	
		$(".artwork").click(function(){   
			// allow user to click the detail image to advance to the next
			imagePager("next","paging");
		});  
		
		
	} // END RTIST THUMBNAIL LIST PAGE BLOCK   
	
	

 }); 
 
/* ARTIST WORKS HELPERS */   
function imagePager(direction,trigger) {  
	var numImages = ($(".artwork-thumbs a").length - 1);
	var currentImage = getCurrentImage($(".artwork-thumbs a"), "on");
	var goToImage = getPrevNext(currentImage, numImages, direction);  
	if (trigger == "thumb") goToImage = currentImage;  
	var goToImageSrc = $(".artwork-thumbs a:eq("+goToImage+")").attr("href");   
	var goToImageDims = $(".artwork-thumbs a:eq("+goToImage+")").attr("rel");
	var goToImageWidth = parseInt(goToImageDims.split(',')[0]);
	var goToImageHeight = parseInt(goToImageDims.split(',')[1]);  
	var goToImageConstraint = '';            
	// keep track of which image we're on
	$(".artwork-thumbs a").removeClass("on");
	$(".artwork-thumbs a:eq("+goToImage+")").addClass("on");
	// set image constraints if need be
	if (goToImageWidth > goToImageHeight) {   
		if (goToImageWidth > 675) {     
			goToImageConstraint = 'width="675"'; // max width
		}
	} else {   
		if (goToImageHeight > 550) {
			goToImageConstraint = 'height="550"'; // max height  
		}
	}                                    
	// load the requested image   
	$(".artwork").empty();
	$(".artwork").append('<img class="artwork-detail-image" src="'+goToImageSrc+'" '+goToImageConstraint+' />');
	$(".artwork").addClass("artwork-on");
	// show the associated description and paging links     
	//$(".artwork-details li").removeClass("on");
	//$(".artwork-details li:eq("+goToImage+")").addClass("on");  
	var artWorkTitle = $(".artwork-thumbs a.on").children("img").attr("title");     
	var artWorkDescription = $(".artwork-thumbs a.on").siblings(".description").html(); 
	var artWorkInfo = '<em>'+artWorkTitle+'</em><br />'+artWorkDescription;
	$(".artwork-details").show();                                     
	$(".artwork-details .artwork-info").html(artWorkInfo);                                     
	$(".paging").addClass("paging-on");  
} 

/* getCurrentImage returns the array index of the obj passed in based on the the className passed in */
function getCurrentImage(arrayObj, classNameMatch) {
	retVal = 0;
	$.each($(arrayObj), function(i) {
		if (this.className==classNameMatch) {
			retVal = i;
		}
    });
	return retVal;
}

/* getPrevNext returns the previous or next ID based on the currentId, upperLimit and direction supplied */
function getPrevNext(currentId, upperLimit, direction) {
	var retVal = 0;
	if (direction == "prev") {
		retVal = parseInt(currentId) - 1;
	} else {
		retVal = parseInt(currentId) + 1;
	}
	if (retVal > parseInt(upperLimit)) {
		retVal = 0;
	}
	if (retVal == -1) {
		retVal = parseInt(upperLimit);
	}
	return retVal;
}

    
