//Javascript Document
$(function() {
	$("#email_signup_form").submit(function() { 
		_gaq.push(['_trackPageview','/contact/email/signup']);
		return true;
	});
	//Search
	$('[rel=focus]').each(function(){
		this.orig = this.value;
		$(this).focus(function(){
			if(this.value == this.orig){
				this.value = '';
			}
		});
		$(this).blur(function(){
			if(trim(this.value) == '')
				this.value = this.orig;
		});
	});
	var search = 'Keyword(s)';
	var newsletter = 'email address';
	$('input#searchInput').focus(function() {
		if(this.value == search) {
			this.value = '';
		}
	}).blur(function() {
		if(this.value == '') {
			this.value = search;
		}
	});
	$('input#newsletterInput').focus(function() {
		if(this.value == newsletter) {
			this.value = '';
		}
	}).blur(function() {
		if(this.value == '') {
			this.value = newsletter;
		}
	});
	//Map Jump Menu
	$('select#map_jump_dropdown').change(function() {
		$('div#map_jump_dropdown_label').html($('select#map_jump_dropdown option:selected').text());
		window.location = document.getElementById('map_jump_dropdown').options[document.getElementById('map_jump_dropdown').selectedIndex].value;
	});
	//Feedback Button
	var opened = false;
	$('#feedback').click(function() {
		if(!opened){
			_gaq.push(['_trackEvent', 'contact', 'modal','opened']);
			_gaq.push(['_trackPageview','/contact/modal/open']);
			opened = true;
		}
		setFeedbackPosition(getWidthHeight());
	});
});
/* Check readiness of page instead of onLoad in case ads prevent full loading of page */
var wa_domrun = 0;
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", function() { wa_domrun=1; doLoadFx(); }, false);
} else if (document.all && !window.opera) {
	document.write('<script type="text/javascript" id="wa_contentloaded" defer="defer" src="javascript:void(0)"><\/script>');
	var wa_contentloaded = document.getElementById("wa_contentloaded");
	wa_contentloaded.onreadystatechange=function() { if (this.readyState=="complete"){ wa_domrun = 1; doLoadFx(); } }
} // safari uses a slightly different method:
if(/Safari/i.test(navigator.userAgent)) { var _timer = setInterval(function() { if(/loaded|complete/.test(document.readyState)) { clearInterval(_timer); doLoadFx(); } }, 10) };

window.onload = function() { setTimeout("if (!wa_domrun) doLoadFx()", 0); }
function doLoadFx() { resetFeedbackPosition();return true; }

// get width and height of browser viewport
function getWidthHeight() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth; myHeight = window.innerHeight;  // non IE
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;  //IE 6+ in 'standards compliant mode'
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;  //IE 4 compatible
	}
	return new Array(myWidth,myHeight);
}
// determine if obj is array
function isArray(obj) { return (obj.constructor.toString().indexOf("Array") != -1); }
// set the position of the feedback div if hidden, otherwise call func to hide it
function setFeedbackPosition(wharr) {
	// if it's already showing, hide it
	if (document.getElementById("feedbackform").style.display == "block") {
		resetFeedbackPosition();
		$('#form_wrap').hide();
		return;
	}
	// otherwise show it.
	var w = 0, h = 0;
	if (!isArray(wharr)) {
		w = 800; h = 600;
	} else {
		w = wharr[0]; h = wharr[1];
	}
	// dimensions of div are defined in fluid.css: div#feedbackform
	var dw = 440, dh = 347;
	var left, top;
	ow = .5*w - .5*dw;  oh = .5*h - .5*dh;
	// add a check for the scrolled height
	if (navigator.appName == "Microsoft Internet Explorer"){
		position = document.body.scrollTop;
	} else {
		position = window.pageYOffset;
	}
	oh = oh + position;
	document.getElementById("feedbackform").style.display = "block";
	document.getElementById("feedbackform").style.left = ow + "px";
	document.getElementById("feedbackform").style.top  = oh + "px";
	$('#form_wrap').show();
	return;
}
// hide feedback div
function resetFeedbackPosition() {
	document.getElementById("feedbackform").style.display = "none";
	document.getElementById("feedbackform").style.left = "-354px";
	document.getElementById("feedbackform").style.top  = "-621px";
	$('#form_wrap').hide();
	return false;
}
// select the text in the feedback title or description fields
function selectAll(obj) {
	if (obj.value == "Please provide a short title" || obj.value == "Describe your ideas, comments or concerns...") { obj.value=""; }
}
// select the text in the feedback title or description fields if it is empty
function replaceText(obj) {
	if (obj.id == "feedback_title" && trim(obj.value) == "") {
		obj.value = "Please provide a short title";
	} else if (obj.id == "feedback_description" && trim(obj.value) == "") {
		obj.value = "Describe your ideas, comments or concerns..."
	}
}

function trim(str) { var s = str.replace(/^(\s)*/, ''); s = s.replace(/(\s)*$/, ''); return s; }


