/*
	
	Vanderbilt University Owen Global Javascript Functions
	ISITE Design

*/

// jquery no conflict. use $j or jQuery outside of ready function
var $j = jQuery.noConflict(); 


// jQuery document ready
jQuery(function($) {

	// JS enabled
	$('html').addClass('js');
	
	// setups for IE6
	if($.browser.msie && parseInt($.browser.version) < 7){
		//add class to drop downs and buttons 
	    $('#nav-main>li').hover(
			function() { var c = $(this).attr("class") || ""; $(this).addClass("over over-"+c); },
			function() { var c = $(this).attr("class") || ""; $(this).removeClass("over over-"+c); }
		);
		$('#nav-supplement li:first-child,.callout-group .link-list-block li:first-child').addClass('first');
		$('input,textarea').focus(function() { $(this).addClass('focus'); }).blur(function() { $(this).removeClass('focus'); });
		$('input[type=button], input[type=submit]').not('.btn').addClass('text-btn');		
	}// end ie6
	
	// position clearance for ie6/7
	if($.browser.msie && parseInt($.browser.version) < 8){
		var prop = $.browser.version < 7 ? 'height' : 'minHeight',
			goal = $('#lead').height();			
		$('.page-landing #destinations .inner-wrap').css(prop,goal+"px");
	}// end ie6/7

	$('#s,.login-username,.login-password').inputSetter();
	
	// empower print
	//$('<li id="nav-action-print"><a href="#">Print</a></li>').prependTo('#nav-page-actions').find('a').click(function(){ window.print(); return false; });

	// table stripe
	$("table:not(.no-stripe) tbody").each(function(){	
		$(">tr:even",this).addClass("alt");
	});

	// show drops on focus
	$("#nav-main a").focus(function(){ $(this).parents("li").addClass("over"); })
					.blur(function(){ $(this).parents("li").removeClass("over"); });
	$("#nav-main>li").mouseover(function() { $("#nav-main>li").not($(this)).removeClass("over"); });
		
	// homepage slideshow
	OWEN.slideshow();
	
	// set programs height
	OWEN.setBlockHeight();
	
	// faq expand/collapse
	OWEN.enableExpand("dl.collapsible");

});// document ready


// application logic
var OWEN = {
	
	// initialize the homepage slideshow
	slideshow : function() {
		
		var list = $j(".page-home .slideshow");
		// avoid any errors: bail if there is no slideshow
		if(!list.length) return;
		
		var lis 	= $j('li',list),
			show 	= lis.filter(".show");
		
		// pull the content and insert it before
		// hide all except for the default item based on "show" class
		var slides = $j('div.slide-content',list).detach();
		slides.hide().eq(lis.index(show)).show().end().insertBefore(list);
		
		// create links and show all the li's which are hid by CSS initially
		var wrapped = $j('img',lis).wrap('<a href="#"></a>')
		lis.css("display","block");
		
		// bind the toggle function to each thumbnail
		$j('a',lis).bind('click',showSlide);		
		
		// append navigation button
		addNavigation();		
		
		jQuery("html").addClass("active");
		
		// the workers
		function showSlide(e) {			
			var target = $j(e.target),
				show = lis.filter(".show"),
				next;
			// clicked already current item: exit
			if(target.parents('li').is('.show')) return false;
			
			// determine what is next based on if clicked a thumbnail or the navigator
			if(target.is('.navigate')) {					
				next = show.next().not('.navigator-parent').length ? show.next() : lis.eq(0);					
			} else {			
				next = target.parents('li');				
			}
			// execute
			turnOff(show);
			turnOn(next);		
			
			return false;
		}
		function turnOff(el) {					 
			el.removeClass("show");
			$j('.slide-content').eq(lis.index(el)).fadeOut(750);
		}
		function turnOn(el) {
			el.addClass("show");			
			$j('.slide-content').eq(lis.index(el)).fadeIn(750);
		}
		function addNavigation() {
			$j('<li class="navigator-parent"><a class="navigate" href="#">Next</a></li>').appendTo(list).show().find("a").bind('click',showSlide);
		}		
		
	},
	// create open/close. receives dl.
	enableExpand : function(el) {
		
		if(!$j(el).length) { return; }
		
		// flags for animation helpers
		var moving = 0,
			allmoving = 0;
		
		$j(el).find("dd").hide();
		$j(el).find("dt").prepend('<span class="state">+</span> ').wrapInner('<a href="#"></a>').click(function(e){
			if(moving == 0) {
				if(jQuery(e.target).is("a")) { moving = 1; }
				var textinsert = $j("span.state",this).text() == "+" ? "-" : "+";
				$j(this).toggleClass("open").next("dd").slideToggle("fast",function(){ moving = 0; allmoving = 0; }).end().find("span.state").text(textinsert);	
			}
			return false;
		});	
		
		var showtext = { show : "Show All", hide : "Hide All" };
		$j('<a class="revealer show" href="#">'+showtext.show+'</a>').insertBefore(el).click(function(e){	
			if(allmoving == 0) {
				allmoving = 1;	
				var action = $j(this).is(".show");
				$j(this).next("dl.collapsible").find(action ? "dt:not(.open)" : "dt.open").click().end().end().toggleClass("show").toggleClass("action-hide").text(showtext[action ? "hide" : "show"]);	
			}
			return false;
		});
		
	},
	
	// for programs landing page, grid of programs should all be the same
	setBlockHeight : function(){
		var programs = $j("body.page-landing div.programs");
		if($j("div.block",programs).length) {
			var mh = OWEN.maxHeight($j("div.block:not(.callout-group .block)",programs)),
				type = $j.support.minHeight() ? "minHeight" : "height",
				sb = $j(".column.special div.block",programs).height();
			$j("div.block:not(.callout-group .block)",programs).css(type,mh+"px");
			$j(".column:last div.block:not(.callout-group .block)",programs).eq(-2).css(type,4+mh+"px");
			
			// some math for the hdr calculation
			var h = $j(".column.special h2",programs).height();
			if(sb <= mh-h) {			
				$j(".column.special div.block",programs).css(type,mh-h+"px");
			}
		}		
	},
	
	// utility to return the height of the tallest element in a jQuery object
	maxHeight : function($els) {
		var h = 0;
		$els.each(function(){ h = h < parseInt(jQuery(this).height()) ? parseInt(jQuery(this).height()) : h; });		
		return h;
	}	
};


// jQuery plugins
jQuery.fn.inputSetter = function(lower) {	
	return this.each(function() {
		var $input = jQuery(this),
			$label = jQuery("label[for='"+$input.attr("id")+"']"),
			labeltext = lower && lower==1 ? $label.text().toLowerCase() : $label.text();
			
		$label.hide();	
		
		if($input.is(':password')) {
			var $fake = jQuery('<input />',{ value:labeltext, className:$input.attr('class'), type:"text" });
			$input.hide();			
			$fake.focus(function() { jQuery(this).hide(); $input.show().focus(); }).insertBefore($input);
			$input.blur(function() { if (!this.value.length) { jQuery(this).hide(); $fake.show() } });				
		} else {		
			$input.val(labeltext);		
			$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
				  .blur(function() { if (!this.value.length) { this.value = labeltext; } });			
		}		
	});
};
jQuery.extend(jQuery.support, {
	minHeight : function() {
		var id = "minheightsupport" + (new Date).getTime();
		jQuery("<div></div>").attr("id",id).css({height:"1px",minHeight:"2px",overflow:"hidden",border:"none",padding:"0",margin:"0"}).appendTo("body");
		var correctheight = document.getElementById(id).offsetHeight==2;
		jQuery("#"+id).remove();
		return correctheight;
	}
});

// IE6 fixes
// make sure IE has the abbr and acronym tag
if(document.all){
	document.createElement("abbr");
	document.createElement("acronym");
}
// prevent IE6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

//Inline css labels on email widget; needs to be here because it can't run before noConflict
$j (document).ready(function(){
	$j ('.e2ma_signup_form_element').addClass('margin-overlay');	

	$j(".e2ma_signup_form_label + .e2ma_signup_form_element").each(function (type) {

		$j(this).keypress(function () {
			$j(this).prev(".e2ma_signup_form_label").addClass("has-text");
		});

		$j(this).blur(function () {
			if($j(this).val() == "") {
			$j(this).prev(".e2ma_signup_form_label").removeClass("has-text");
		}
		});
	});
});

