﻿$(function() {

	// make anchors with rel="external" open in a new tab or browser window
	$("a[rel='external']").each(function(){
		$(this).click(function(){
			window.open(this.href); 
			return false;
		});
	});

	// add class to last nav list item
	$("#nav li:last-child").addClass("last");
	
	
	// set the page height if sideAdWrapper is longer than main page content
	// var articleHeight, sideAdWrapperHeight = 0;
	// a = $("div.article").height();
	// b = $("#sideAdWrapper").height() + 9; // add 9px top padding to #sideAdWrapper
	// if ( a < b ) {
	// 	$("div.article").height(b);
	// }
	
	// truncate Case study names in Recently Viewed panel
	$("div.recentlyViewed ul > li > span > a").jTruncate({
		length: 24,  
		minTrail: 0,
		moreText: "",
		lessText: ""
	});

	// Truncate case study names in My Selections panel
			$("#mySelections div.selections li span a").jTruncate({
				length: 25,  
				minTrail: 0,
				moreText: "",
				lessText: ""
			});
	
	// style Login input fields when in error
	$("#loginEmail, #loginPassword, #newsEmail").inputFocus();
	if($("#login span.error").length > 0){
		//$("#loginEmail,#loginPassword").css("background","#ffe6e6").css("color","red").css("border-color","red");
	}
	
	/*
	if($("#login span:hidden){
		$("#loginEmail,#loginPassword").css("background","#ffe6e6").css("color","red").css("border-color","red");
		$("#loginEmail, #loginPassword").focus(function(){
			$("#loginPassword").attr("value","");
			$("#loginEmail,#loginPassword").css("border-color","#999999").css("background-image","url('../images/input.png')").css("background-color","white").css("background-repeat","no-repeat").css("color","black");
			$("#login span.error").hide();
		});
	}
	*/
	
	// bodyClass switch
	bodyClass = $("body").attr("class");
	
	switch (bodyClass)
	{
		case "home":
			$("#nav a").removeClass("selected");
			$("#nav a[title='home']").addClass("selected");
			$("#contentWrapper div.section > ul > li").eq(0).addClass("row1").next().addClass("row1");	// add class to first 2 case study categories
			$("#contentWrapper div.section > ul > li:odd").addClass("alt");		// add class to alternate categories
			// add class to stripe alternate case study rows
			$("#contentWrapper div.section > ul > li").each(function(){
				$(this).find("ul li:even").addClass("alt");
			});
			
			// Handle wrapping of <h3> tags on home page
			var hdr = $("div.section ul > li > h3");
			$.each(hdr,function(){
				if($(this).height() > 16){
					if($(this).parent().hasClass("alt")){
						$(this).parent().prev().children("h3").height(32);
					} else {
						$(this).parent().next().children("h3").height(32);
					}
				}
			});
			// Truncate case study names in list blocks
			$("div.section ul > li > ul > li > span a").jTruncate({
				length: 30,  
				minTrail: 0,
				moreText: "",
				lessText: ""
			});
			// Scroll the View Full List link in non-IE browsers
			if ( !$.browser.msie ){
				$.localScroll();
			}
			break;
			
		case "casestudy":
			$("#nav a").removeClass("selected");
			$("ul.nav li:last-child").addClass("last");
			$("#sideAdWrapper div.recentlyViewed > ul > li:even").addClass("alt");
			$("#contentWrapper div.section > ul > li:even").addClass("alt");	// add class to alternate categories
			$("#contentWrapper div.section > ul > li").each(function(){		// add class to stripe alternate case study rows
				$(this).find("ul li:even").addClass("alt");	
			});
			$("#mySelections div.selections ul li:even").addClass("alt");

			//$("#gallery").creamGallery();
			GALLERY.init();
			break;
		
		case "casestudyedit":
			//loadTinyMCE.init();
			break;
		
		case "signin":
			$("#nav a").removeClass("selected");
			$("#newVisitor fieldset:eq(0), #existingUser fieldset:eq(0)").addClass("first");
			$("#existingUser div.error").next().find("li:first-child").addClass("error").next().addClass("error");
			break;
			
		case "about":
			$("#nav a").removeClass("selected");
			$("#nav a[title='about FMAs']").addClass("selected");
			break;

		case "preview":
			$("#nav a").removeClass("selected");
			$("#nav a[title='new cream preview']").addClass("selected");
			break;			
	}
		
});

/* Function to handle input field default value
----------------------------------------------- */
$.fn.inputFocus = function() {
	$(this).focus(function(){
		var defVal = $(this).attr("value");
		$(this).attr("value","").css("color","black");
		$(this).blur(function(){
			if ( $(this).attr("value") == "" ){
				$(this).attr("value",defVal).css("color","#999");
			}
		});
	});
};

/* Function to handle gallery
----------------------------------------------- */
$.fn.creamGallery = function() {
	
	var thisVideo = null,
		videoExists = 0,
		$gallery = $("#gallery");

	// if there is video content, copy the <embed> tag and replace it with an image thumbnail
	if($(this).find("li").hasClass("video")){
		videoExists = 1;
		thisVideo = $gallery.find("li.video > object").clone();
		$gallery.find("li.video").children().remove();
		$("<img src='/assets/images/video_thumb.gif' alt='Video thumbnail image' title='Click to play' />").appendTo($gallery.find("li.video"));
	}
	
	//add containing div for large image
	$(this).before("<div id='gallery-display'></div>");
	
	//default the large image to the first image in the <ul> and show it
	$(this).find("li:first").addClass("selected").find("img").addClass("selected");
	if(videoExists==1){
		$(thisVideo).appendTo("#gallery-display").hide().fadeIn(500);
	} else {
		$(this).find("li:first").find("img").clone().appendTo("#gallery-display").hide().fadeIn(500);
	}
	
	//add class to thumbnails, and fade them in
	$(this).find("li > img").addClass("thumb-image").fadeIn().click(function(){

		// on clicking a thumbnail image, remove the current image
		$("#gallery-display").children().remove();
		$("#gallery li.selected").toggleClass("selected");			
		$("#gallery img.thumb-image").removeClass("selected").fadeTo(0,0.5);

		if($(this).hasClass("selected")){
			// user has clicked on thumbnail for the displayed image, so do nothing
			return;
		} else if( $(this).parent().hasClass("video") ){
			// Clicked on video, so add video content to #gallery-display
			$(thisVideo).appendTo("#gallery-display").hide().fadeIn(500);
			$(this).closest("li").toggleClass("selected");
			return;
		} else {
			// clicked on alternate image, so add new image to #gallery-display
			$(this).addClass("selected").fadeTo(0,1).clone().appendTo("#gallery-display").hide().fadeIn(500);
			$(this).closest("li").toggleClass("selected");
			return;
		}
	});
	
	//handle the thumbnail opacity on hover
	$(this).find("li > img").hover(
		function(){
			if($(this).hasClass(".selected")){
				return;
			} else {
				$(this).fadeTo(500,1);
				$("#gallery img.selected").fadeTo(500,0.5);
			}
		},
		function(){
			if($(this).hasClass("selected")){
				$(this).fadeTo(0,1);
			} else {
				$(this).fadeTo(500,0.5);
				$("#gallery img.selected").fadeTo(500,1);
			}
		}
	);
};



