/* centered pop up window */
function centeredPopUpWindow(mypage, myname, w, h, scroll){
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// get position of child node
function getPosition(main,spec) {
	var items = main.getElementsByTagName(spec.tagName);
	var found = 0;
	for (p = 0; p < items.length; p++) {
		if (items[p] == spec) {
			found = 1;
			break;
		}
	}
	if (found) {
		return p;
	}
	else {
		return -1;
	}
}

$(document).ready(function(){
	
	// navigation rollovers/selected states
	$("ul#mainNavigation li").hover(
		function(){
			$(this).css({cursor: "pointer"})
			.find("div").css({backgroundPosition: "100% -43px"})
			.find("a").css({backgroundPosition: "0px -43px"})
		},
		function(){
			$(this).css({cursor: "default"})
			.find("div").css({backgroundPosition: "100% 0px"})
			.find("a").css({backgroundPosition: "0px 0px"})
		}
	);
	
	// shut off divider for last sub nav item
	$("ul#subNavigation li:last-child").css({background: "none"});
	
	// testimonials pagination
	createTestimonialNavigation();
	$("ul#testimonialPages li").click(function(){
		
		var testimonialPosition = getPosition(this.parentNode,this);
		
		$("ul#testimonialPages li").removeClass("selectedTestimonial");
		$(this).addClass("selectedTestimonial");
		$("div.testimonials").hide();
		$("div#testimonial" + (testimonialPosition + 1)).fadeIn(750);
		
		return false;
		
	});
	
	// billboard focus area
	$("div#todaysBillboard").click(function(){
		$("div#billboardPopout").fadeIn(500);
		return false;
	});
	
	$("div#billboardPopout a").click(function(){
		$("div#billboardPopout").fadeOut(500);
		return false;
	});
	
	$("div#todaysBillboard").hover(
		function(){
			$(this).find("a.arrowButton").css({backgroundPosition: "0px -13px"});
		},
		function(){
			$(this).find("a.arrowButton").css({backgroundPosition: "0px 0px"});
		}
	);
	
	
	// manufacturers logos
	$("table#logoTable tr td").css({padding: "20px 0"});
	$("table#logoTable tr td:nth-child(1)").css({width: "205px", textAlign: "left"});
	$("table#logoTable tr td:nth-child(2)").css({width: "215px", textAlign: "center"});
	$("table#logoTable tr td:nth-child(3)").css({width: "220px", textAlign: "right"});
	
	$("table#logoTable tr td a").hover(
		function(){ $(this).css({opacity: .5}); },
		function(){ $(this).css({opacity: 1}); }
	);
	
	// enewsletter signup form helper
	$("input#newsletter").focus(function(){
		if( $(this).val() == "email address" ){
			$(this).val("");
		} else { 
		}
	});
	$("input#newsletter").blur(function(){
		if( $(this).val() == "" ){
			$(this).val("email address");
		} else { 
		
		}
	});

	// footer nav
	$("div#footerLinks a:first-child").css({paddingRight: "0px", border: "none"});
	
	// set external links to open in new window
	$("a[rel=external]").click(function(){window.open(this.href);return false;});
	
	setYearInFooter();
});


/* set testimonial navigation */
function createTestimonialNavigation()
{
	testimonialCount = $("div.testimonials").size();
	if ( testimonialCount == 0 )
	{
		// do nothing
	}
	else{
		for ( testimonial=0; testimonial<testimonialCount; testimonial++ )
		{
			$("ul#testimonialPages").append("<li><a href=\"#\">" + (testimonial+1) + "</a></li>");
		}
		$("ul#testimonialPages li:first").addClass("selectedTestimonial");
	}
}


function setYearInFooter(){
	var yy = new Date().getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	$("span.yearWrapper").text(year);
}

