/*******************************************************************************

    CSS on Sails Framework
    Title: SPNHC
    Author: XHTMLized (http://www.xhtmlized.com/)
    Date: October 2010

*******************************************************************************/

$(document).ready(function(){
	
	/** * Initializes main menu on this page after the document has been loaded */
	var mozilla_menu_bar = new YAHOO.widget.MenuBar('yahoo_menu_bar', {
		autosubmenudisplay: true,
		showdelay: 300,
		hidedelay: 750,
		iframe: false,
		lazyload: true,
		monitorresize: false,
		appendtodocumentbody: true
	});
	mozilla_menu_bar.render();
	
	function focusClear(ele) {
		if(ele.value == ele.defaultValue) ele.value = "";
		if(!ele.onblur) ele.onblur = function() { if(this.value == "") this.value = ele.defaultValue }
	}
	
	$('#header input').focus(function() { focusClear(this) });


	if(!$('#sidebar').length){   /********* Homepage *********/
		$('#container').after('<div id="shadowbottom"></div>');
	}
	else {                       /********* Interior pages *********/
		$('#content').before('<div id="shadowtop"></div>')
		$('#sidebar').after('<div id="shadowbottom"></div>');
	}

	$('#yahoo_menu_bar ul ul').each(function() { $(this).find('li').eq(0).css({ borderTop: "none" }) });



	if(typeof DD_belatedPNG !== "undefined"){
		// DD_belatedPNG fix for IE6
		var PNG_fix_selectors = [
			'#container',
			'#shadowtop',
			'#shadowbottom',
			'#content',
			'.slideshow_nav_img',
			'#slideshow_container .nav img'
		];
		DD_belatedPNG.fix(PNG_fix_selectors.join(','));
	}
	function paginate(ind, el){
		if(ind%2==0){return "<div class='even'><img src='/media/img/slide_nav_odd.png'></div>";}
		else{return "<div class='odd'><img src='/media/img/slide_nav_even.png'></div>";}
	};

	if($("#slide").length){
		$("#slide").cycle({
			prev: '#slide-left',
			next: '#slide-right',
			timeout: 0,
			pager: '#slideshow_container .nav',
			pagerAnchorBuilder: paginate
		});
	}
	$('#slideshow_container .nav img').css('top', '-2px');
	$('#slideshow_container .nav img').css('top', '');
	
	$('#slide-right, #slide-left').hover(function(){
		$(this).css('opacity', 1);
	}, function(){
		$(this).css('opacity', 0);		
	}).css('opacity', 0);
	
	// fix the size of the title so that it does not overlap the banner image
	// gives us the width of the text even if it goes outside the parent container
	$.fn.textWidth = function(){
	  var html_org = $(this).html();
	  var html_calc = '<span>' + html_org + '</span>'
	  $(this).html(html_calc);
	  var width = $(this).find('span:first').width();
	  $(this).html(html_org);
	  return width;
	};
	// set the original width
	var page_title = $('#page_title');
	var origWidth = page_title.outerWidth();
	var fontSize = 1.0;
	// decrement the font-size until we hit a width that is not overflowing the parent container
	while(page_title.textWidth() > origWidth){
		fontSize -= 0.1;
		page_title.css('font-size', fontSize+'em');
	};
	
	// sets the content hight so that shadows display properly
	fix_content_height();
	
	fix_footer_height();
	$(window).resize(fix_footer_height);
});

// fix the height of the footer so that the bottom of the page always goes to blue
var fix_footer_height = function(){
	var footer = $('#footer');
	var _window = $(window);
	if (footer.offset().top + footer.outerHeight() < _window.height()) {
		footer.css('height', _window.height() - footer.offset().top - 1); // subtract one to keep the scroll bar from appearing
	};	
}
function fix_content_height(){
	if($("#content").outerHeight() < $("#sidebar").outerHeight()){
		$("#content").css("height", $("#sidebar").outerHeight()+50);
	}
}


