function emailPopup(url) {
	window.open(url, '', 'scrollbars=yes,resizable=no,width=650, height=520');
}

function changeSize(size) {
	if ( size == "small" ) {
		size = '1em';
	}
	else if ( size == "medium" ) {
		size = '1.2em';
	}
	else if ( size == "large" ) {
		size = '1.4em';
	}

	//document.body.style.fontSize = size;
	if ( document.getElementById('main_content') != null ) {
		document.getElementById('main_content').style.fontSize = size;
	}

	if ( document.getElementById('related_content') != null ) {
		document.getElementById('related_content').style.fontSize = size;
	}

	if ( document.getElementById('navigation_pane') != null ) {
		document.getElementById('navigation_pane').style.fontSize = size;
	}
	
	if ( document.getElementById('zuBoxContent') != null ) {
		document.getElementById('zuBoxContent').style.fontSize = size;
	}

	this.createCookie("SR_2008_DFS", size, 14);
}

function spawnWindow(url,windowname,features) {
	window.open(url,windowname,features);
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){
				d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
			}
			}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else {
		expires = "";
	}

	document.cookie = name+"="+value+expires+"; path=/";
}

function changeKeyTab(a,b) {
	var img1 = '/sustainability_reports/2008/common/images/interface/tabs/2008-targetandresults.gif';
	var img2 = '/sustainability_reports/2008/common/images/interface/tabs/2009-target.gif';
	
	if ( b == 2) {
		document.getElementById(a+'_img').src = img2;
		
		document.getElementById(a+'_1').style.display = 'none';
		document.getElementById(a+'_2').style.display = 'block';
		
	}
	else {
		document.getElementById(a+'_img').src = img1;
		
		document.getElementById(a+'_2').style.display = 'none';
		document.getElementById(a+'_1').style.display = 'block';
	}
	
	return false;
}

function changeKeyTab2(a,b) {
	var img1 = '/sustainability_reports/2008/common/images/interface/tabs/2008-targetresult.gif';
	var img2 = '/sustainability_reports/2008/common/images/interface/tabs/2009-target_s.gif';
	
	if ( b == 2) {
		document.getElementById(a+'_img').src = img2;
		
		document.getElementById(a+'_1').style.display = 'none';
		document.getElementById(a+'_2').style.display = 'block';
		
	}
	else {
		document.getElementById(a+'_img').src = img1;
		
		document.getElementById(a+'_2').style.display = 'none';
		document.getElementById(a+'_1').style.display = 'block';
	}
	
	return false;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function googleSearch(input, orig) {
	if (input.value == orig) {
		input.value = "";
	} else if (input.value == "") {
		input.value = orig;
	}
}

function checkMyShortCuts(id) {
	rel = this.readCookie('AR_2008_'+id);
	if ( rel != null)
	{
		document.getElementById('my_shortcuts_link_changer_show').innerHTML = document.getElementById('my_shortcuts_link_changer_hide').innerHTML;
		document.getElementById('my_shortcuts_link_changer_show').className = document.getElementById('my_shortcuts_link_changer_hide').className;		
		document.getElementById('my_shortcuts_link_changer_show').href = document.getElementById('my_shortcuts_link_changer_hide').href;
	}
}

function externalLinks() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

function submitKeywords(num1, num2) {
	var url = document.forms[num1].elements[num2].value;
	if(url.match('/sustainability_reports/2008/what_keywords/')) {
		window.open(url, 'what_are_keywords', 'scrollbars=no,resizable=yes,width=500, height=350');
	}
	else {
		window.open(url, 'keywords', 'scrollbars=yes,resizable=yes,width=347,height=400');
	}

	return false;
}

function submitKeywordsOnKeywordsPage(num1, num2) {
	var url = document.forms[num1].elements[num2].value;
	if(url != '/sustainability_reports/2008/what_keywords/') {
		window.location.href = url;
	}
}

function initializeMain() {
	var font_cookie = readCookie('AR_2008_DFS');
	if ( font_cookie != null )
	{
		changeSize(font_cookie);
	}
	
	if ( document.getElementById('bottom_half') != null && document.getElementById('back_to_top_link') != null )
	{
		pageHeight = YAHOO.util.Dom.getY(document.getElementById('bottom_half'));
		if ( pageHeight > 1500)
		{
			document.getElementById('back_to_top_link').style.display = "block";
		}
	}
}

function changeGraph(a,b,c) {
	document.getElementById("grossMarg_img").src = a;
	document.getElementById("grossMarg_title").innerHTML = b;
	if (c.id == "href_k") {
		document.getElementById("href_k").className = "selected graph zuBox";
		document.getElementById("href_n").className = "normal graph zuBox";
		document.getElementById("href_p").className = "normal graph zuBox";
	}
	else if (c.id == "href_n") {
		document.getElementById("href_n").className = "selected graph zuBox";
		document.getElementById("href_k").className = "normal graph zuBox";
		document.getElementById("href_p").className = "normal graph zuBox";
	}
	else if (c.id == "href_p") {
		document.getElementById("href_p").className = "selected graph zuBox";
		document.getElementById("href_n").className = "normal graph zuBox";
		document.getElementById("href_k").className = "normal graph zuBox";
	}
	else {
		document.getElementById("href_p").className = "normal graph zuBox";
		document.getElementById("href_n").className = "normal graph zuBox";
		document.getElementById("href_k").className = "normal graph zuBox";
	}
	return false;
}

function toggleHelp(helpId) {
	help = document.getElementById(helpId);

	if( help.style.display == "none" ) {
		help.style.display = "block";
	} else {
		help.style.display = "none";
	}

	return false;
}

function closeHelp(helpId) {
	help = document.getElementById(helpId);

	if( help.style.display == "block" ) {
		help.style.display = "none";
	}

	return false;
}

function openHelp(helpId) {
	help = document.getElementById(helpId);

	if( help.style.display == "none" ) {
		help.style.display = "block";
	}

	return false;
}


function submit_feedback_form(form, uri) {
	var yy = YAHOO.util.Dom.getY(document.getElementById('zuBoxMain'));
	window.scrollTo(0,yy);

	YAHOO.util.Connect.setForm(document.getElementById('sr2008Form'));
	var request = YAHOO.util.Connect.asyncRequest('POST', uri, {
		success:handleFeedBackSuccess,
		failure:handleFeedBackFailure
	});
	document.getElementById('feedback_pane').innerHTML = "<div style=\"margin: 100px; text-align: center;\"><img src=\"/sustainability_reports/2008/common/images/content/waiting.gif\" alt=\"Loading\" /></div>";
}

var handleFeedBackSuccess = function(o) {
	if ( o.responseText !== undefined ) {
		document.getElementById('feedback_pane').innerHTML = o.responseText;
		setTimeout("zuBoxHide()", 3000);
	}
}

var handleFeedBackFailure = function(o) {
	document.getElementById('feedback_pane').innerHTML = "<div style=\"margin: 100px; text-align: center;\">Sorry there was an error.  Please try again.</div>";
}

function loadPCSMap(id) {
	var map = new GMap2(document.getElementById("googleMap"));
	map.setCenter(new GLatLng(24.206889622398023, -29.53125), 2);
	map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10)));
	var clickZoomLevel = 10;

	//SQM - Santiago
	var santiago = new GMarker(new GLatLng(-33.45, -70.666667), {
		draggable:false,
		title:"SQM"
	});
	GEvent.addListener(santiago, "click", function() {
		map.setCenter(santiago.getPoint());
		map.setZoom(clickZoomLevel);
		santiago.openInfoWindowHtml("<strong>SQM</strong><br />Santiago, Chile");
	});
	map.addOverlay(santiago);

	//ICL - Tel Aviv
	var tel_aviv = new GMarker(new GLatLng(32.047806,34.814129), {
		draggable:false,
		title:"ICL"
	});
	GEvent.addListener(tel_aviv, "click", function() {
		map.setCenter(tel_aviv.getPoint());
		map.setZoom(clickZoomLevel);
		tel_aviv.openInfoWindowHtml("<strong>ICL</strong><br />Tel Aviv, Israel");
	});
	map.addOverlay(tel_aviv);

	//if ( id == "peer" ) {

	//PotashCorp - Saskatoon
	var saskatoon = new GMarker(new GLatLng(52.128661,-106.665316), {
		draggable:false,
		title:"PotashCorp"
	});
	GEvent.addListener(saskatoon, "click", function() {
		map.setCenter(saskatoon.getPoint());
		map.setZoom(clickZoomLevel);
		saskatoon.openInfoWindowHtml("<strong>PotashCorp</strong><br />Saskatoon, SK, Canada");
	});
	map.addOverlay(saskatoon);

	//Agrium - Calgary
	var calgary = new GMarker(new GLatLng(51.045, -114.057222), {
		draggable:false,
		title:"Agrium"
	});
	GEvent.addListener(calgary, "click", function() {
		map.setCenter(calgary.getPoint());
		map.setZoom(clickZoomLevel);
		calgary.openInfoWindowHtml("<strong>Agrium</strong><br />Calgary, AB, Canada");
	});
	map.addOverlay(calgary);

	//Mosaic - Plymouth
	var plymouth = new GMarker(new GLatLng(45.010556, -93.455556), {
		draggable:false,
		title:"Mosaic"
	});
	GEvent.addListener(plymouth, "click", function() {
		map.setCenter(plymouth.getPoint());
		map.setZoom(clickZoomLevel);
		plymouth.openInfoWindowHtml("<strong>Mosaic</strong><br />Plymouth, MN, USA");
	});
	map.addOverlay(plymouth);

	//Yara - Oslo
	var oslo = new GMarker(new GLatLng(59.933333, 10.75), {
		draggable:false,
		title:"Yara"
	});
	GEvent.addListener(oslo, "click", function() {
		map.setCenter(oslo.getPoint());
		map.setZoom(clickZoomLevel);
		oslo.openInfoWindowHtml("<strong>Yara</strong><br />Oslo, Norway");
	});
	map.addOverlay(oslo);

	//K+S - Kassel
	var kassel = new GMarker(new GLatLng(51.316667, 9.5), {
		draggable:false,
		title:"K+S"
	});
	GEvent.addListener(kassel, "click", function() {
		map.setCenter(kassel.getPoint());
		map.setZoom(clickZoomLevel);
		kassel.openInfoWindowHtml("<strong>K+S</strong><br />Kassel, Germany");
	});
	map.addOverlay(kassel);
		
	//Intrepid
	var intrepid = new GMarker(new GLatLng(39.746125, -104.991457), {
		draggable:false,
		title:"Intrepid"
	});
	GEvent.addListener(intrepid, "click", function() {
		map.setCenter(intrepid.getPoint());
		map.setZoom(clickZoomLevel);
		intrepid.openInfoWindowHtml("<strong>Intrepid</strong><br />Denver, CO, USA");
	});
	map.addOverlay(intrepid);
		
		
	//Terra Sioux City, IA, USA
	var terra = new GMarker(new GLatLng(42.522542, -96.409288), {
		draggable:false,
		title:"Terra"
	});
	GEvent.addListener(terra, "click", function() {
		map.setCenter(terra.getPoint());
		map.setZoom(clickZoomLevel);
		terra.openInfoWindowHtml("<strong>Terra</strong><br />City, IA, USA");
	});
	map.addOverlay(terra);
		
	//CF Industries Deerfield, IL, USA
	var cf = new GMarker(new GLatLng(42.162546, -87.880764), {
		draggable:false,
		title:"CF Industries"
	});
	GEvent.addListener(cf, "click", function() {
		map.setCenter(cf.getPoint());
		map.setZoom(clickZoomLevel);
		cf.openInfoWindowHtml("<strong>CF Industries</strong><br />Deerfield, IL, USA");
	});
	map.addOverlay(cf);
		
	//Uralkali Moscow, Russia
	var uralkali = new GMarker(new GLatLng(55.778504, 37.624741), {
		draggable:false,
		title:"Uralkali"
	});
	GEvent.addListener(uralkali, "click", function() {
		map.setCenter(uralkali.getPoint());
		map.setZoom(clickZoomLevel);
		uralkali.openInfoWindowHtml("<strong>Uralkali</strong><br />Moscow, Russia");
	});
	map.addOverlay(uralkali);
}
