$(function(){

    //hack change to yui template - should be fixed in CSS file
   // $('#yui-main > .yui-b').css({'margin-left': '12px'});
						   
	//page highlight
	(function(){
		var primaryNav = $("#main-nav a");
		var secondaryNav = $("#secondary-nav a");
		var currentURL = window.location.href;
		var page = currentURL.split("/");
		var page = page[page.length -1];
		$(primaryNav).each(function(){
			var linkURL = $(this).attr("href");
			if(currentURL.indexOf(linkURL)!= -1){
				$(this).addClass("selected");
			}
		});
		$(secondaryNav).each(function(){
			$(this).removeClass("selected");
			var linkURL = $(this).attr("href");
			var u = linkURL.split("/");
			var u = u[u.length - 1];
			if(page == u){
				$(this).addClass("selected");
				//$(this).focus();
			}
		});	
	})();
	
	//"action" triggers
	$("a[rel='action']").click(function(e){
			e.preventDefault();		
	});

	
	(function(){				
		var mainImg = $('#img-frame img');
		
		function addCaption(){
		
			//if there is any caption content
			if($("#img-longdesc p").html().length == 0)
			{
				return false;
			}
			else
			{
				var imgW = mainImg.width();
				var containerW = $("#img-frame").width();
				if (imgW < containerW){ //if the image is not fullsized
					//show the description
					$("#img-longdesc").animate({display: "block", opacity: "show"}, "slow");
				}else{ //if the image is fullsized
					//show the caption trigger
					$("#captionTrigger").show();
					$("#captionTrigger a").animate({opacity: 0.8},"slow");
				}
			}
	
		}

		if(mainImg.length){	
		
			mainImg.animate({opacity: "show"}, "slow");
			//$("#captionTrigger").hide();
			if(mainImg[0].complete){//image is loaded
				addCaption();
			} else { //image is not loaded yet
					//set a handler 
				mainImg.load(function(){
					addCaption();
				});
			}
		}
	})();
	
	
	//center content (if image isn't standard size)
	/*
	(function(){	   
		var defWidth = 580 // default 'max' width for main image
		var contWidth = $("#main .wrap").width();
		var mainImgWidth = $("#primary-content img").width();
		if(mainImgWidth < defWidth) {
		    console.log('small')
			$("#primary-content").width(mainImgWidth);
			$("#main .wrap").width(contWidth - (defWidth - mainImgWidth)).css('margin', '0 auto');
		} else {
			return false //do nothing
		}
	})();
	*/
	
	
	//gallery-captions
	$("#captionTrigger a").hover(function() {
	  $("#img-longdesc").animate({opacity: "show"}, "slow");
	}, function() {
	  $("#img-longdesc").animate({opacity: "hide"}, "fast");
	});
	
	//gallery-nav
	var thumbNav = $(".thumbNav");
	thumbNav.each(function(){
		var linkURL = $(this).attr("href")
		var currentURL = window.location.href;
		if(currentURL.indexOf(linkURL) != -1){
			$(this).addClass("selected");
		}
	});
	
//end of jquery
});
