$(document).ready(function() {
	
	/* Add classes every other image for flexibility
	********************************************************************************/
	
	var photoClass = "even";
	var photoIndex = 0;
	
	$('ul.gallery li').each(function() {
		if(photoIndex % 1 == 0) {
			photoClass = (photoClass == 'even' ? 'odd' : 'even');
		}
		$(this).addClass(photoClass);
		photoIndex++;
	});
	
	/* History Plugin
	********************************************************************************/
	
	$.historyInit(pageload, "jquery_history.html");

	function pageload(hash) {
		
		// PageLoad function
		// This function is called when:
		// 1. after calling $.historyInit();
		// 2. after calling $.historyLoad();
		// 3. after pushing "Go Back" button of a browser
		
		
		if(hash) {
			// restore ajax loaded state
			if($.browser.msie) {
				// jquery's $.load() function does't work when hash include special characters like ŒŠš.
				//hash = encodeURIComponent(hash);
			}
			
			//$('.gallery li').removeClass('selected');
			//$('.gallery li.' + hash).addClass('selected');
			
			var loadObject = $('.gallery li.' + hash);
			var showMedium = loadObject.children('span.medium').text();
			var showLarge = loadObject.children('span.large').text();
			var showTitle = loadObject.children('span.title').text();
			var showDesc = loadObject.children('span.desc').text();
			var showCat = loadObject.children('span.cat').text();
			
			$('.gallery li').removeClass('selected');
			loadObject.addClass('selected');
			
			loadDetail(showMedium,showLarge,showTitle,showDesc);
			
			if(showCat == "Behind The Scenes") {
				$('.gallery2').show();
				$('.nav2').show();
				$('.gallery1').hide();
				$('.nav1').hide();
			} else {
				$('.gallery1').show();			
				$('.nav1').show();
				$('.gallery2').hide();
				$('.nav2').hide();
			}
		
		} else { // DEFAULT SCENARIO WHEN THERE IS NO HASH OR HISTORY //
		
			$('.gallery1').show();	
			$('.nav1').show();		
			$('.gallery2').hide();
			$('.nav2').hide();
			
			var loadObject = $('.gallery1 li:first');
			var showMedium = loadObject.children('span.medium').text();
			var showLarge = loadObject.children('span.large').text();
			var showTitle = loadObject.children('span.title').text();
			var showDesc = loadObject.children('span.desc').text();
			var showCat = loadObject.children('span.cat').text();
			
			$('.gallery li').removeClass('selected');
			loadObject.addClass('selected');
			
			loadDetail(showMedium,showLarge,showTitle,showDesc);
		}
	} // end pageload history function

	
	/* Gallery Nav Bar
	********************************************************************************/

	$('.nav .nav1 a').click(function() {
		$('.nav1').hide();
		$('.gallery1').hide();
		$('.nav2').show();
		$('.gallery2').show();
		//return false;
	});
		
	$('.nav .nav2 a').click(function() {
		$('.nav2').hide();
		$('.gallery2').hide();
		$('.nav1').show();
		$('.gallery1').show();
		//return false;
	});
	
	
	/* Gallery Images
	********************************************************************************/
	
	$('.gallery li img').hover(
		function() { $(this).parents('li').addClass("over"); },
		function() { $(this).parents('li').removeClass("over"); }
	);

	$(".gallery li a").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	});

});


function loadDetail(pMedium,pLarge,pTitle,pDesc)
{
		
	$('a.full_screen').click(function() {
	    Shadowbox.open({
	        content:    pLarge,
	        player:     "img"
	    });
	});
	
	$('object').remove();
	$('p.photo_desc').empty().flash({ 
		swf: '/media/style/style_text_box.swf',
		params: {   
			play: false,
			bgcolor: "#000000"
		},   
		flashvars: {
			message: 'I come from Flash.',
			style_text: pDesc
		},
		height: 340,
		width: 240
    });
    	
	$("#main_image").hide().empty().append('<img src="' + pMedium + '" alt="' + pTitle + '" title="' + pTitle + '" />').fadeIn("slow");
	
}

