$(document).ready(function() {
	
	/* 	--------------------------------------------------------
			Image viewer (viewer.php / viewer-large.php)
		--------------------------------------------------------  */	
		
	// Get the hash 
	var hash = window.location.hash;
	// Remove #	
	var start = hash.substring(1)
	// If start is empty or if it's not a number (eg. #biography) set to zero
	if(!start || isNaN(start)) { start = 0; }
	
	// Set N of N caption w/ the number of images in the #art div
	var caption = '1 of ' + $("#art").find("div").length;
	$('#count').html(caption);
	
	// Get the current pages URI
	var enlargeHref = $('.enlarge-img').attr('href');
	var closeHref = $('.close-img').attr('href');	
	
	// Get current title
	var currTitle = document.title;
		
	// JQuery Cycle
	
	// Fade in for first slide
	//$('.viewer').fadeIn('slow', function() {
   		  
   		  // Cycle itself     		
   		  $('#art').cycle({ 
		  	 	startingSlide: start,
			    fx:     'fade', 
			    speed:  'slow', 
			    timeout: 0, 
			    next:   '.next-img', 
			    prev:   '.prev-img',
			    after: 	onAfter
			});
	//});
	

	function preventDefault(e) {
	    e.preventDefault();
	}
	
	$('.next-img').click(function(){
	
		
		//$('.next-img').bind("click", preventDefault);
		
		//$('.next-img').attr("href"," ");
		
		//$('.next-img').css("color","green");
		
		setTimeout(function(){
			//$('.next-img').unbind("click", preventDefault);
			//$('.next-img').css("color","black");

		},3000);
		
	
	});

	
/*
	function locationHashChanged() {
   		
   		//window.alert(window.location.hash);
   
   		// Get the hash 
		var hash = window.location.hash;
		// Remove #	
		var id = parseInt(hash.substring(1));
	
		//	var id = parseInt(href.substring(1));
   
   		 $('#art').cycle(id); 

    }

	window.onhashchange = locationHashChanged;
*/
	
	
	// Called after the silde advances (including the first slide)	
	function onAfter(curr,next,opts) {	
	
			// Set the count caption
			var caption = ' ' + (opts.currSlide + 1) + ' <b>of</b> ' + opts.slideCount;
			$('#count').html(caption); 
			
			// Set the hash to the new current id		
			var currId = opts.currSlide;
			window.location.hash = '#' + currId;		
		
		// Set the link on viewer.php to lead to viewer-large.php w/ the hash		
		$('.enlarge-img').attr('href', enlargeHref + 'large/#' + currId);
		// Set the link on viewer-large.php back to viewer.php w/ the hash
		$('.close-img').attr('href', closeHref + '#' + currId);		
		
	}
	
	// To reveal grid 		
	$('.grid-img').click(function(){
		$('#art').fadeToggle(function(){
			$('#grid-container').toggle();
		});
		
		// Toggle the nav links to prevent clicking on them
		$('#img-nav a').toggle(200); 
		$('#img-nav').fadeTo(200,0); 
	
	});

	// Trigger slide in grid view
	$('.grid-thumbnail a').click(function(){
		
		// The href is the id so get that
		var href = $(this).attr('href');
		// Make sure it's a number for Cycle below
		var id = parseInt(href.substring(1));
		
		// Hide the grid			
		$('#grid-container').toggle(function(){
			$('#art').fadeToggle();
		});

		// Toggle the nav links back on
		$('#img-nav a').toggle(200); 
		$('#img-nav').fadeTo(200,100); 


		// Noy sure what this is...
		//$('#img-nav a, span').click(function() { return true; }); 

		 // Trigger Cycle to go to the the appropriate slide
		 $('#art').cycle(id); 
		 // Don't follow the link
		 return false; 
	});
	
	// Keypress functions for cycling through the images
	$(document).keydown(function(e) {
		if (e.keyCode == 38) {  window.location = "./"; }
		if (e.keyCode == 37) { $('#art').cycle('prev'); }
		if (e.keyCode == 39) { $('#art').cycle('next'); }
	});
	
	
	/* 	--------------------------------------------------------
			Artists Index (artists.php)
		--------------------------------------------------------  */		
	
	// Break the list of names into two columns w/ autocolumn.min.js
	$('#names-list').columnize({ 
		lastNeverTallest: true, 
		columns: 2
	});
	
	// Rollovers name to show image
	$('#names-list a').mouseover(function(){
		
		// Get the id for the image from the id of the name
		var id = $(this).attr('id');
		
		// Hide the default image (of the gallery)
		$('#img-list-container').css('background-image','none');
		
		// Hide whatever image might be currently showing
		$('#img-list img').css('display','none');
		
		// Show the image that corresponds to the name
		$('img#'+id).css('display','block');
			
	});
	
	// Reverse for the mouseout
	$('#names-list a').mouseout(function(){
	
		// Show the default image (of the gallery)
		$('#img-list-container').css('background-image','url(/i/gallery_install_01_320_bw_02.jpg)');
		
		// Hide the image of the art
		$('#img-list img').css('display','none');
				
	});
	
	
	/* 	--------------------------------------------------------
			Global
		--------------------------------------------------------  */		

	// If the div.inner inside div#mid is empty, change the body background color to the same as div#top so it "disappears"	
	 if ($("#mid .inner:has(*)").length) {
	 	// Not empty, do nothing	 	
	 }else{
	  	// Empty, so change the bg color to the same as #top
	 	$('body').css('background-color','#eff0f1');
	 }
 
});



