var $cur;


$(window).load(function()	{
	// Dropdown and Hover code
	nav();
	
	
	lines();

	
	
});





		





$(document).ready(function()	{







/*Hide this if sitesearch on header is commented out
	showHideFormValues('sitesearch');
	
	function showHideFormValues(frm) {
		var frm = document.forms[frm];
		var num_elems = frm.elements.length;
		var elem_values_orig = new Array();
		
		// for debugging
		//frm.elements['comments'].value = '';
	
		for (i=0; i<num_elems; i++) {
			// loop through form elements and only bind events for text fields and textareas. also build an array of initial values.
			if (frm.elements[i].type.toLowerCase() ==  'text' || frm.elements[i].type.toLowerCase() == 'textarea') {
				elem_values_orig[frm.elements[i].id] = frm.elements[i].value; 
				
				$('#' + frm.elements[i].id).focus(function() {
					if (this.value == elem_values_orig[this.id]) { 						   
						this.value = '';	
					};
				});
				$('#' + frm.elements[i].id).blur(function(){
					if (this.value.length < 1) {
						this.value = elem_values_orig[this.id];
					}
				});
			}// end if
			
		};// end for loop
	
	}// end showHideFormValues(); */









	$(window).resize(function()	{
								lines();
								});
						   });

function nav()	{		
	
	$("#nav ul li.dd").hover(function()	{
		$cur = $(this);													
		
		// Check to make sure there is something to drop down	
		isDD = $cur.children("div").length;
		
		if(isDD == 1)	{												
			
			// Get menu content
			dd_content = $cur.children("div").html();
			
			// Set Menu content
			$("#dd div").html(dd_content);						
			
			// Define needed vars
			content_offset = $("#wrapper #header #nav").offset().left;
			this_offset = $cur.position().left;
			this_width = $cur.width();
			nav_offset = $("#nav").position().left;											
			
			// Set new offset																															
			new_offset = (nav_offset + this_offset);
			
			// Apply offset and show dropdown
			$("#dd").css({
						 display : "block",						 
						 visibility : "hidden"
						 });												
			
			dd_width = $("#dd").width();								
			nav_width = $("#nav").width();
			parent_width = $("#nav").parent().width();
			
			if(dd_width > this_width)	{								
				
				shift_left = (dd_width - this_width) / 2;
				if(shift_left < new_offset)	{
					new_offset -= shift_left;
				}								
				
				if(dd_width + new_offset > parent_width)	{
					new_offset = parent_width - dd_width - 22;	
				}
				
			}
			
			$("#dd").css({
						 left : new_offset,
						 visibility : "visible"
						 });
			
			
			console.log("Content: " + content_offset + ", This: " + this_offset + ", This Width: " + this_width + ", Nav: " + nav_offset + ", New: " + new_offset + ", DD width: " + dd_width);						
			
			
			
		}
	 }, function()	{
		$cur = $(this);		
		$("#dd").hide();
	 });
	
	
	
	// Failsafe to keep dropdown open
	$("#dd").hover(function()	{
		$cur.children("a").css({
			border : "1px solid #c7c8ca",
			borderTop: "1px solid #c7c8ca",
	     	borderBottom : "1px solid #fff",
			background : "#fff",
			color : "#000"
				 });
		$(this).show();					
		
	}, function()	{				
		$cur.children("a").css({
			border : "",
	     	borderBottom : "",
			background : "",
			color : ""
				 });
		$(this).hide();	
	});
}

function lines()	{

	$nav_line = $("#nav_line");
	$title_line = $("#title_line");
	$footer_line = $("#footer_line");
	
	doc_width = $(document).width();
	
	// Nav Line
	nav_offset = $("#nav").offset().left;
	nav_top_offset = $("#nav").offset().top;
	$nav_line.html("&nbsp;").width(nav_offset - 10).css("top", nav_top_offset - 5).show();
	
	// TItle Line
	title_offset = $("#content h1").offset().left;
	title_width = $("#content h1").width();
	title_height = $("#content h1").height();
	title_top_offset = $("#content h1").offset().top;
	title_line_width = doc_width - title_offset;			
	$("#content h1").css("border-bottom", "none");// Hide CSS underline
	if( title_height <= 45 ) {
    $title_line.html("&nbsp;").width(title_line_width - 10).css({top : title_top_offset + 19, right : 0}).show();}	
	if( title_height >= 45 ) {
    $title_line.html("&nbsp;").width(title_line_width - 10).css({top : title_top_offset + 59, right : 0}).show();}	

	
	// Footer Line
	footer_offset = $("#content #footer").offset().left;
	footer_width = $("#content #footer").width();
	footer_height = $("#content #footer").height();
	footer_top_offset = $("#content #footer").offset().top;
	footer_line_width = doc_width - footer_offset;			
	$("#content #footer").css("border-bottom", "none");// Hide CSS underline
	$footer_line.html("&nbsp;").width(footer_line_width - 10).css({top : footer_top_offset + footer_height, right : 0}).show();
}
