jQuery(document).ready(function() {
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
	
	jQuery('#menu span').click(function() {
		checkContentType($(this).attr("rel"));
		activeMenu($(this));
	});		

	$(function()
			{
				var win = $(window);
				// Full body scroll
				var isResizing = false;
				win.bind(
					'resize',
					function()
					{

						if (!isResizing) {
							isResizing = true;
							var container = $('.scroll-pane');
							// Temporarily make the container tiny so it doesn't influence the
							// calculation of the size of the document
							container.css(
								{
									'width': 1,
									'height': 1
								}
							);
							// Now make it the size of the window...
							container.css(
								{
									'width': win.width(),
									'height': "420px"
								}
							);
							isResizing = false;
							container.jScrollPane(
								{
									'showArrows': true
								}
							);
						}
					}
				).trigger('resize');

				// Workaround for known Opera issue which breaks demo (see
				// http://jscrollpane.kelvinluck.com/known_issues.html#opera-scrollbar )
				$('body').css('overflow', 'hidden');

				// IE calculates the width incorrectly first time round (it
				// doesn't count the space used by the native scrollbar) so
				// we re-trigger if necessary.
				if ($('#full-page-container').width() != win.width()) {
					win.trigger('resize');
				}

				// Internal scrollpanes
				$('.scroll-pane').jScrollPane({showArrows: true});
			});

	
	



});

function activeMenu(pSelected){
	$("#menu span").each(function(index) {
    	$(this).removeClass("highLight");
  	});
	pSelected.addClass("highLight");
}

function loadSet(pSetId){
	pSetId =$('[rel|="'+pSetId+'"]').attr("id")
	var materWidth = 0;
	$('#holder').html("<span class='loadingMsg'>Loading ...</span>");
  	jQuery.post("libs/dispatcher.php", { action: "getPhotos",setId : pSetId},
   		function(data){
   				//console.log(data);
     		$('#holder').html(data);
			$('#holder').ajaxComplete(function() {
				
				$(".lightbox").lightbox({
			    	fitToScreen: true,
					imageClickClose: false
		    	});	
				
 				$(".preloading").each(
 				
					function(){
						$(this).find('img').hide();
						$(this).find('img').load(function () {
							var thisWidth = $(this).width();
    						materWidth = materWidth+thisWidth;
    						$(".holder-pane").css("width",materWidth);
							$(this).fadeIn("slow");
							$('.scroll-pane').jScrollPane({showArrows: true});
    					})	
				});
				
			});
	}); 

}


function checkContentType(pVal){
	switch (pVal) {
 		case "about":
 			SWFAddress.setValue(pVal);
 			$("#holder").load('about.php');
 		break;	
 		case "news":
 		 	SWFAddress.setValue(escape(pVal));
 			$("#holder").load('news.php');
 		break;
 		default: 
 			SWFAddress.setValue(pVal);
 			loadSet(pVal);
 		break;
	}
}




function handleChange(event) {
	SWFAddress.removeEventListener(SWFAddressEvent.CHANGE, handleChange);
	if (event.path=="/"){
		loadSet("people-portraits");
	}else{
		checkContentType(event.pathNames[0]);
	}
}


function external(path) { // pass in the correct path to the function so we only need one <A> for infinite amount of calls from  flash                          
	masterPath = path;
	// if the lightbox does not exist we will make it                       
	if ($('a#lightbox').length == 0) {                          
		$("body").append("</a><a id='lightbox' style='visibility: hidden; position: absolute; left: -9999px;' href='"+path+"'>calling js lightbox from flash</a>");                            
  		$("a#lightbox").lightbox({
			    fitToScreen: true,
			    imageClickClose: false
		    });
	                  
	// if it already exists but the path is different we will set the new path                      
 	} else if ($('a#lightbox').attr("href") != path) {                           
		$('.lightbox').attr("href", masterPath+path);                   
 	}      
	// now we will simulate the click here.                         
 	$('a#lightbox').trigger("click");      
}



