// JavaScript Document

window.onload = function(){
						   
	// Check for email a friend link and call function to set onclick event
	var emailsExist = $('.email').attr('class');				
	if (emailsExist != null){
		setEmails();
	}		
	
	// Check for high-contrast version link and call function to set onclick event
	var hiContrastExist = $('.contrast').attr('class');				
	if (hiContrastExist != null){
		setContrast();
	}	
}

function fixPNG(myImage){
	
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
		
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title=\"" + myImage.title  + "\" " : "title=\"" + myImage.alt + "\" "
	   var imgStyle = "display:block; margin-top:0px;" + myImage.style.cssText
	   var myParent = myImage.parentNode

		if (myParent.nodeName == "A"){
			var aHref=myParent.href;
			var aTarget=""
			var aStyle="style=\" display:block; width:" + myImage.width + "px; height:" + myImage.height + "px;\" "
			if (myParent.target !=="") {
				aTarget = "target=\""+ myParent.target + "\"";
			}
			var strNewHTML = "<span " + imgID + imgClass
				+ imgTitle + " style=\"" + "width:" + myImage.width 
				+ "px; height:" + myImage.height 
				+ "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + myImage.src + "\', sizingMethod='image');\">"
				+ "<a " + aStyle +  "href=\""+aHref + "\" target=\""+ aTarget + "\" ><!-- --></a> </span>"
			myParent.outerHTML = strNewHTML
		}
	   else{
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + myImage.width 
				+ "px; height:" + myImage.height 
				+ "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + myImage.src + "\', sizingMethod='image');\"></span>"
			myImage.outerHTML = strNewHTML
	   }
	   	  
    }
}

function buildFlash (divId, swfLoc, swfName, swfWidth, swfHeight, swfVar){
		var flashVersion = 8;

		var so = new SWFObject(swfLoc, swfName, swfWidth, swfHeight, flashVersion, "#FFFFFF");
		so.addParam("wmode", "transparent");
		so.addParam("scale", "noscale");
		if (swfVar){
			so.addVariable("xmlPath", swfVar);
		}
		so.write(divId);
}

// FONT SIZE CHANGER - COOKIE BASED

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
		    c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function changeFont(fontClass){
	var HTMLBody = document.getElementsByTagName("body")[0];     
	HTMLBody.className = fontClass;
	setCookie("TLyleFontSize", fontClass, 30);
	if(fontClass == ""){
		$('body div.director-bio-container').css('height', '200px');
		var contentheight = $('div.tabbed-content-item').height();
		$('div.tabbed-content').css('height', contentheight);
	}
	else if(fontClass == "medium"){
		$('body.medium div.director-bio-container').css('height', '230px');
		var contentheight = $('div.tabbed-content-item').height();
		$('div.tabbed-content').css('height', contentheight);
	}
	else if(fontClass == "large"){
		$('body.large div.director-bio-container').css('height', '282px');	
		var contentheight = $('div.tabbed-content-item').height();
		$('div.tabbed-content').css('height', contentheight);
	}
}		

function setDefaultFontSize(){
	var fontSize = getCookie("TLyleFontSize")
	if(fontSize){
		var HTMLBody = document.getElementsByTagName("body")[0];    
		HTMLBody.className = fontSize;
	}
}


function setEmails(){
	var emailLinks = $('li.email').find('a');
	for(i=0; i<emailLinks.length; i++){
		$('li.email a').click(function(){
			mailpage();
			return false;
		});	
	}
}

function mailpage()
{
	mail_str = "mailto:?subject=Check out the " + document.title;
	mail_str += "&body=I thought you might be interested in the " + document.title;
	mail_str += ". You can view it at, " + location.href;

	location.href = mail_str;
}

function setContrast(){
	var hiContrastLinks = $('li.contrast').find('a');
	for(i=0; i<hiContrastLinks.length; i++){
		$('li.contrast a').click(function(){
			//alert('Switching to High contrast!');
			
			var HTMLhead = document.getElementsByTagName("head")[0];         
			var headerCSS = document.createElement('link');
			headerCSS.type = 'text/css';
			headerCSS.rel = 'stylesheet';
			headerCSS.className = 'hc-stylesheet';
			headerCSS.href = 'http://localhost/MasterPages/Custom/Tate and Lyle/TateAndLyleStyle/high-contrast.css';
			headerCSS.media = 'screen';
			HTMLhead.appendChild(headerCSS);
			
			var HCcss = '.sIFR-root {color:#ffff00; font-weight:bold;}';

			if($('#flash-map').length){
				buildFlash("flash-map", "common/swf/TandLMapHC.swf", "flash", "710", "375", "common/xml/locations.xml");
			}	
			
			

			setContrastClose();
			
			//return false;
			
		});	
	}
}

function setContrastClose(){
	var hiContrastClose = $('li.contrast').find('a');
	
	$('li.contrast').find('a').unbind("click");
	for(i=0; i<hiContrastClose.length; i++){
		$('li.contrast a').click(function(){
			$('.hc-stylesheet').remove();
			
			if($('#flash-map').length){
				buildFlash("flash-map", "common/swf/TandLMap.swf", "flash", "710", "375", "common/xml/locations.xml");
			}

			// Check whether on homepage or other page type before replacing SIFR text colors
			
			var Browncss = '.sIFR-root {color:#b98860; font-weight:bold;}';
			var Greycss = '.sIFR-root {color:#8e989e; font-weight:bold;}';
			var Bluecss = '.sIFR-root {color:#4e8abe; font-weight:bold;}';

		

			setContrast();
		});
	}
	
}


