//Javascript Document
$(function() {
	//Base URL
	var base_url = 'http://worldatlas.com';
	
	//Search
	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;
		}
	});
	//Newsletter
	$('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());
		var loc = document.getElementById('map_jump_dropdown').options[document.getElementById('map_jump_dropdown').selectedIndex].value;
		if(pageTracker) {
			pageTracker._trackEvent('RedesignJumpMenu', 'Click', loc);
		}
		
		window.location = loc;
	});
	
	$('div#feedback_x a img').mouseover(function() {
		this.src = base_url+'/images/feedback_x_over.png';
	}).mouseout(function() {
		this.src = base_url+'/images/feedback_x.png';
	});
	
	//Feedback rules/regs
	$('div#rulesregs').hide();
	$('a#openrulesregs').click(function() {
		$('div#rulesregs').show();
	});
	$('a#rulesregsclose').click(function() {
		$('div#rulesregs').hide();
	});
	
	//Feedback Button
	$('a.feedback').click(function() {
		setFeedbackPosition(getWidthHeight());
		return false;
	});
	
	//Feedback form inputs
	$('input.replaceText, textarea.replaceText').focus(function() {
		if($(this).val() == $(this).attr('rel')) {$(this).val('');}
	}).blur(function() {
		if($(this).val() == '') {$(this).val($(this).attr('rel'));}
	});
	
	
	//Feedback Validation
	$('form#feedback_form').submit(function() {
		var toGo = false;
	
		var requiredFilled = true
		$('form#feedback_form input.required').each(function(i) {
			if($(this).val() == '' || $(this).val() == $(this).attr('rel')) {
				requiredFilled = false;
			}
		});
		
		var selected = false;
		var thevote = '';
		$('form#feedback_form input[type=radio]').each(function(i) {
			if(this.checked) {selected = true; thevote = this.value; }
		});
		if(!selected || !requiredFilled) {
			alert('Please be sure to fill out all fields!');
		} else {
			//Fire google event!
			//Attach a goal to this as well!
			if(pageTracker) {
				pageTracker._trackEvent('Feedback', 'Submitted', thevote);
				pageTracker._trackPageview('/FeedbackSubmit');
			}
		}
		
		if(selected && requiredFilled) {
			toGo = true;
		}
		
		return toGo;
	});
	
	//Screen Resizing
	$(window).resize(function() { setLeftMargin() });
});
/* 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(); tellUsersThanks(); }, 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(); tellUsersThanks(); } }
} // 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(); tellUsersThanks(); } }, 10) };

window.onload = function() { setTimeout("if (!wa_domrun) doLoadFx()", 0); }
function doLoadFx() { /*resetFeedbackPosition();*/ setFluidWidth(); setLeftMargin(); return true; }

var thankonce = false;

function tellUsersThanks() {
	if(sendThanks == true && thankonce == false) {
		thankonce = true;
		alert("Thank you for your feedback!");
	}
};

// 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();
		return false;
	}
	// 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 = 621, dh = 354;
	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";
	if(pageTracker) {
		pageTracker._trackEvent('RedesignFeedback', 'Click', 'Show Feedback Form');
	}
	return false;
}
// hide feedback div
function resetFeedbackPosition() {
	document.getElementById("feedbackform").style.display = "none";
	document.getElementById("feedbackform").style.left = "-354px";
	document.getElementById("feedbackform").style.top  = "-621px";
	if(pageTracker) {
		pageTracker._trackEvent('RedesignFeedback', 'Click', 'Hide Feedback Form');
	}
	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..."
	}
}
// trim whitespace from an input string
function trim(str) { var s = str.replace(/^(\s)*/, ''); s = s.replace(/(\s)*$/, ''); return s; }
// set the width of the map container.
function setFluidWidth() {
	var waImgWidth = 0;
	waImgWidth = getMapWidth();
	//if (waImgWidth <= 463) { waImgWidth = 463; /* min width for full layout */ }
	//document.getElementById("wrapper").style.minWidth = (waImgWidth + 610) + "px";
	document.getElementById("center").style.width = (waImgWidth) + "px";
	document.getElementById("mapContainer").style.width = waImgWidth + "px";
}

function getMapWidth() {
	if ($('#wa_map').length) { 
		waImgWidth = parseInt(document.getElementById('wa_map').getAttribute('width'));
		if(!waImgWidth) {
			waImgWidth = document.getElementById('wa_map').width;
		}
	}
	return waImgWidth;
}

// set the left margin
function setLeftMargin() {
	if($('div#right2').length) {
		var right = $('div#right2').width();
	} else if($('div#right').length) {
		var right = $('div#right').width();
	} else {
		var right = 0;
	}
	var center = $('div#center');
	var container = $('div#container');
	var width = $('div#left').width() + right + center.width() + 6;
	//container.width(width);
	//$('div#footer_content').width(width);
	
	var w = getViewportWidth();
	if($('body').hasClass('wide')) {
		if(w <= 1030) {
			$('body').removeClass('smallWidth').addClass('smallWidth');
			container.width(width);
			center.width(getMapWidth());
			center.css({'margin-left':'0px'});
			if($('div#right2').length) { $('#right2').css({'margin-left':'0px'}); }
		} else {
			$('body').removeClass('smallWidth');
			container.width(width + 18);
			center.width(getMapWidth());
			center.css({'margin-left':'9px'});
			if($('div#right2').length) { $('#right2').css({'margin-left':'9px'}); }
		}

	} else {
		if(w <= 1030) {
			$('body').removeClass('smallWidth').addClass('smallWidth');
			container.width(width);
			center.width(getMapWidth());
			if($('body').hasClass('wide')) {
				center.css({'margin-left':'0px'});
				if($('div#right2').length) { $('#right2').css({'margin-left':'0px'}); }
			} else {
				center.css({'margin-left':'3px'});
				if($('div#right2').length) { $('#right2').css({'margin-left':'3px'}); }
			}
			
		} else {
			$('body').removeClass('smallWidth');
			container.width(width + 28 + 15);
			center.width(getMapWidth() + 28);
			center.css({'margin-left':'9px'});
			if($('div#right2').length) { $('#right2').css({'margin-left':'9px'}); }
		}
	}
	if(container.width() < 1010) {
		var headWidth = 1010;
	} else {
		var headWidth = container.width();
	}
	$('div#header').width(headWidth);
	$('div#header_content').width(headWidth);
}

// get width of the viewport
function getViewportWidth() {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth;
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		viewportwidth = document.documentElement.clientWidth;
	} else {
		// older versions of IE
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	}
	return viewportwidth;
}