// JavaScript Document

$(document).ready(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();
	}
	


	//var MOSSEditMode = $('.ms-consoletitleareaframe').attr('class');

	setDefaultFontSize();
	setDefaultContrast();
});

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){
		
		//if (MOSSEditMode != null){
			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);

		//}else{
		//	$(divId).innerHTML = "CONTENT DIABLED IN EDIT MODE" ;
		//}
}

function buildFlashVideo (divId, swfLoc, swfName, swfWidth, swfHeight, swfVar){
		var flashVersion = 8;

		var so = new SWFObject(swfLoc, swfName, swfWidth, swfHeight, flashVersion, "#000000");
		so.addParam("wmode", "transparent");
		so.addParam("scale", "noscale");
		so.addParam("allowFullScreen", "true");

		if (swfVar){
			so.addVariable("flv", 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()) +"; path=/";
}

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 setDefaultContrast(){
	var HCactive = getCookie("TLyleHC")
	if(HCactive){
		enableHighContrast();
	}
}


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()
{
	var pageTitle = document.title;
	var pageURL = location.href;
	pageTitle = pageTitle.replace("&", "%26");
	pageURL = pageURL.replace("&", "%26");

	mail_str = "mailto:?subject= www.tateandlyle.com " + pageTitle;
	mail_str += "&body=Have a look at the " + pageTitle + " page on Tate %26 Lyle's website: " + pageURL;
	//mail_str += "%0D%0A"; //line breaks

	location.href = mail_str;
}

function setContrast(){
	var hiContrastLinks = $('li.contrast').find('a');
	$('li.contrast').find('a').unbind("click");
	for(i=0; i<hiContrastLinks.length; i++){
		$('li.contrast a').click(function(){


			enableHighContrast();
		});	
	}
}

function enableHighContrast(){
	var HTMLhead = document.getElementsByTagName("head")[0];         
	var headerCSS = document.createElement('link');
	headerCSS.type = 'text/css';
	headerCSS.rel = 'stylesheet';
	headerCSS.className = 'hc-stylesheet';
	headerCSS.href = '/_layouts/styles/TateAndLyleCSSAssets/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", "/_layouts/inc/TateAndLyleFlashAssets/TandLMapHC.swf", "flash", "710", "375", "/_layouts/inc/TateAndLyleXMLAssets/locations.xml");
	}	
	
	// Check whether on homepage or other page type before replacing SIFR text colors
	if($('.sIFR-replaced').length){		
/*		if($('#flash-navigation').length){
			// Check for homepage SIFR elements before replace
	
			if($('h2.pullout-home').length){
				$.each(sIFR.replacements['h2.pullout-home'], function() {
					this.changeCSS(HCcss);
				});
			}
	
			if($('p.market-solutions').length){
				$.each(sIFR.replacements['p.market-solutions'], function() {
					this.changeCSS(HCcss);
				});
			}
		}
		else{
	
//			if($('h1').length){
//				$.each(sIFR.replacements['h1'], function() {
//					this.changeCSS(HCcss);
//				});
//			}
//	
//			if($('h2').length){
//				$.each(sIFR.replacements['h2'], function() {
//					this.changeCSS(HCcss);
//				});
//			}
		}
*/	}
	setCookie('TLyleHC', 'hc-stylesheet', 30);
	setContrastClose();	
}

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", "/_layouts/inc/TateAndLyleFlashAssets/TandLMap.swf", "flash", "710", "375", "/_layouts/inc/TateAndLyleXMLAssets/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;}';
			/*	if($('.sIFR-replaced').length){			
			if($('#flash-navigation').length){
					// Check for homepage SIFR elements before replace
	
					if($('h2.pullout-home').length){
						$.each(sIFR.replacements['h2.pullout-home'], function() {
							this.changeCSS(Browncss);
						});
					}
	
					if($('p.market-solutions').length){
						$.each(sIFR.replacements['p.market-solutions'], function() {
							this.changeCSS(Greycss);
						});
					}
				}
				else{
	
					if($('h1').length){
						$.each(sIFR.replacements['h1'], function() {
							this.changeCSS(Bluecss);
						});
					}
	
					if($('h2').length){
						$.each(sIFR.replacements['h2'], function() {
							this.changeCSS(Browncss);
						});
					}
				}
			}*/
			deleteCookie('TLyleHC');
			setContrast();
		});
	}
	
}

function deleteCookie( cookie_name ){
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString() +"; path=/";
}