/**
 *	MagaliPettier.com
 */
$(document).ready(function()
	{
		// Chose section to activate at start
        
		var $sec = getVariable( window.location, "sec" );
		if( !$sec ) $sec = 3;
		var $makeActive = 0;
		if( $sec == 4 ) $makeActive = 1;
		else if( $sec == 5 ) $makeActive = 2;
		if( window.location.toString().indexOf("publications") != -1) $sec = 7;
		// Unhide options hidden for none JS browsers
		$("#galleryNav").find("div").addClass("subMenu").removeClass("hide");
		$("#galleryNav").accordion(
		{ 
			active: false, 
			header: '.acchead', 
			selectedClass: 'activated',
			navigation: true, 
			event: 'click'
		}).accordion('activate', $makeActive ); // Make active section
       
		/**
		 *	remove scroll bars
		 */
		$("#thumbsBox").css('overflow','hidden');
		getThumbs( $sec );
		/**
		 *	Section links on menu, not subSection links
		 */
		$(".accHead").click(function()
			{
				getThumbs(getVariable(this.href, "sec"), getVariable(this.href, "subsec"));//no sub sec
				return false;
			});
		/**
		 *	When a subsection is clicked
		 */
         
		$(".subSecLink").click(function()
			{
				getThumbs( getVariable(this.href, "sec"), getVariable(this.href, "subsec") );
				return false;//Don't follow link
			});
		/**
		 *	Info box for subCats
		 */
        
		$("#infoBtn").click(function()
			{
				$(".infoContainer").fadeIn("fast");
				return false;
			});

         
	});//dom ready

/**
 *	Get a parameter from a GET link string
 *	eg. www.example.com?param=6
 */
function getVariable( href , param )
{
	href = unescape(href);// &amp; to &
	if( href.indexOf("?") == -1 ) return false; // no getstring
	href = href.split("?");
	var str = href[1]; // only after the ?
	var vars = str.split("&");
	for (var i=0; i<vars.length; i++) 
	{
		var pair = vars[i].split("=");
    	if (pair[0] == param) 
		{
      		return pair[1];
    	}
  	} 
}

/**
 *	Get the get string from an url
 */
function getGetString( href )
{
	href = href.split("?");
	var str = href[1]; // only after the ?
	return str;
}

/**
 *	Get Thumbnails for the given
 *	section/sub section
 */
function getThumbs( section, subSection )
{    
	$thumbsBox = $("#thumbsBox");
	/**
	 *	Request info box
	 */
	$.ajax(
	{
		type: "POST",
		url: "http://www.magalipettier.com/cms/templates/galleryAjax.php",//location.href?
		data: "sec="+section+"&subsec="+subSection+"&request=infoBox",
		success: function( msg )
		{
			$(".infoContainer").remove(); // remove old box IF there is one
			$("#imageCol-inner").prepend( msg );
            $("iframe.youtube").remove();
			$("#flashBox").remove();
			$infoBox = $(".infoContainer");
			if( $infoBox.length == 0 ) $("#infoBtn").hide();
			else
			{
				/**
				 *	Add scrollBars Here .....
				 */
				
				$("#infoBtn").show();
				$(".infoContainer").fadeIn("fast");			
				/**
				 *	Hide info box on click x
				 */
				$(".infoCloseBtn").click(function()
					{
						$(".infoContainer").fadeOut("fast");
						return false;
					});
                
                //$(".imageSlider").empty();
			}
		}
	});
	/**
	 *	reuqest thumbnails
	 */
	$.ajax(
	{
		type: "POST",
		url: "http://www.magalipettier.com/cms/templates/galleryAjax.php",//location.href?
		data: "sec="+section+"&subsec="+subSection+"&request=thumbs",
		beforeSend: function()
		{
			//$thumbsBox.append("<div id=\"thumbsBoxLoader\">Loading...</div>");
			//$("#mainImageLoader").show();
		},
		success: function( msg )
		{
			$thumbsBox.replaceWith( msg );
			showThumbButtons();
			$("#thumbSlider").panelSlider();
			//updateImage();
			/**
			 *	On img load, remove darkened overlay
			 */
			$(".tnail").load(function()
				{
					$(this).parent().parent().find(".thumbOverlay").fadeTo( "slow", 0.40 );// leave a touch of overlay
				});
			$(".thumbsBoxInner:first").find(".thumbOverlay:first").addClass("activeThumb").fadeTo( "fast", 0.0 );// Select first thumb
			/**
			 *	Thumbnail click, load in main image area
			 */
			$(".thumbLink").click(function()
				{
					/**
					 *	Only do event if we're not loading something!!
					 */
					if( $("#mainImageLoader:visible").length == 1 ) $("#mainImageLoader").show();
					var thumbID = getVariable(this.href, "view");
					$(".activeThumb").removeClass("activeThumb");
					$(this).find(".thumbOverlay").addClass("activeThumb"); // mark active thumbnail
					$(".infoContainer:visible").fadeOut("fast"); // Hide visible info box
					$(this).find(".thumbOverlay", this).fadeTo( "fast", 0 ); // remove overlay
					$(".thumbOverlay").not(".activeThumb").fadeTo( "fast", 0.40 ); // leave active one visible, hide the rest
					updateImage( thumbID );
					return false;
				});
		}
	});
}

/**
 *	Update the main image we want to view
 */
function updateImage( id )
{
	var view = $(".thumbLink:first").attr("href");
	var sec = getVariable( view , "sec");
	if( typeof(id) == 'undefined' ) id = getVariable( view , "view");
    
	$.ajax(
	{ 
		type: "POST",
		url: "http://www.magalipettier.com/cms/templates/galleryAjax.php",
		data: "id="+parseInt(id)+"&request=mainImg&sec="+sec,
		beforeSend: function()
		{
			$("#mainImageLoader").show(); // loading image while we wait
		},
		success: function( msg )
		{
			if( sec == '4' ) // Video section, don't slide, just replace /////////
			{
				$(".imageSlider").empty();
				$(".imageSlider").append( msg );
				$("#mainImageLoader").hide();
			}
			else ////// Photo sections, slide in new photo //////////////////////
			{
				$(".imageSlider").prepend( msg ); // Add new image container
				$(".mainImage:first").load(function() // Wait for the new image to load first
				{
					$("#mainImageLoader").hide();
					$(".imgContainer:last").fadeOut(600,
					function() // Wait for animation to finish then remove from dom
					{
						$(this).remove();
						//if( sec == '5' ) $(".audioCaption").show(); // if project section show audio box
					});	
					
				});
				/**
				 *	Add hover events for photo section, 
				 *	Keep visible for project section
				 */
				if( sec == '3' || sec == '7' || sec == '5' ) // photo section /////////////////////////
				{
					// Hide buttons if we are at start/end
					if( $(".thumbLink").index($(".activeThumb").parent()) >= ($(".thumbLink").length - 1) ) $("#tbNext").hide();
					if( $(".thumbLink").index($(".activeThumb").parent()) == 0 ) $("#tbPrev").hide();
					
					$("#tbInfo").click(function()
						{
							if( sec == '5' ) $(this).parent().parent().find(".audioCaption").slideToggle("fast");
							else $(this).parent().parent().find(".imgCaption").slideToggle("fast");
							return false;
						});
					/**
					 *	Next image load
					 */
					$("#tbNext").click(function()
						{
							/**
							 *	Only do event if we're not loading something!!
							 */
							if( $("#mainImageLoader:visible").length == 1 ) return false;
							$nextLink = $(".activeThumb").parent().parent().parent().next(".thumbOuter").find(".thumbLink");
							if( $nextLink.length == 0 )// end of inner thumb box, move to next one
							{
								$nextLink = $(".activeThumb").parent().parent().parent()
											.parent().next(".thumbsBoxInner").find(".thumbLink:first");// Move to next thumb box
								
								$("#thumbButtonBox > .nextBtn").trigger('click');
							}
							$nextLink.trigger('click');
							return false;
						});
					/**
					 *	Prev Image Load
					 */
					$("#tbPrev").click(function()
						{
							/**
							 *	Only do event if we're not loading something!!
							 */
							if( $("#mainImageLoader:visible").length == 1 ) return false;
							$prevLink = $(".activeThumb").parent().parent().parent().prev(".thumbOuter").find(".thumbLink");
							if( $prevLink.length == 0 )
							{
								$prevLink = $(".activeThumb").parent().parent().parent()
											.parent().prev(".thumbsBoxInner").find(".thumbLink:last");// Move to prev thumb box
								$("#thumbButtonBox > .prevBtn").trigger('click');
							}
							$prevLink.trigger('click');
							return false;
						});
				}
				/**
				 *	 Add Lightbox to the updated image
				 */
				$(function() 
				{
					$('.lightBoxLink:first').lightBox();
					$('.imgToolbar > .lightBoxLink:first').lightBox();
				});
			}// else
		}//sucess
	});
}
/**
 *	Show forward/back buttons
 */
function showThumbButtons()
{
	$("#thumbsBox").append("<div id=\"thumbButtonBox\">"+
						 '<a class="prevBtn" href=""><img src="/images/prev-btn.png" alt="" /></a>'+
						 '<a class="nextBtn" href=""><img src="/images/next-btn.png" alt="" /></a></div>');
	
}



