var hub;

/* NB: font size */
function fontSizer(){
	
	var setfs = parseInt(checkFontSize());
	newSize(setfs);
	
	function checkFontSize(){
		var cookie = getCookie("gam_style");
		if(cookie==null){ cookie = "fS=0;"; }
		cookie = cookie.split(";");
		for(x=0;cookie.length>x;x++){
			if(cookie[x].indexOf("fS")!=-1) {	return cookie[x].split("fS=")[1]; }
		}
	}
	
	var nObj = d.getElementById("articleTools");
	var fsHolder = d.createElement("li");
	fsHolder.id = "fontSize";
	fsHolder.appendChild(d.createTextNode("Text Size: "));
	
	var smallerObj = d.createElement("small");
	createFontSizer(smallerObj);
		
	
	var largerObj = d.createElement("big");
	createFontSizer(largerObj);
	
	function createFontSizer(nObj){
		switch(nObj.nodeName.toLowerCase()){
			case "small":
				var parms = [smallerObj,"Smaller","decrease","Decrease",-1,-1];
				break;
			case "big":
				var parms = [largerObj,"Larger","increase","Increase",1,1];
				break;
		}
		nObj.id = "fs"+(setfs+parms[4]);
		nObj.appendChild(d.createTextNode(parms[1]));
		if(parms[5]!=setfs) {
			addEvent(parms[0],"click",changeSize);
			nObj.title = parms[3] +" the text size";
		} else {
			nObj.style.textDecoration = "line-through";
			nObj.title = "You cannot " + parms[2] + " the text size any further";
		}
	}
	
	fsHolder.appendChild(smallerObj);
	fsHolder.appendChild(largerObj);
	nObj.appendChild(fsHolder);
	
	function changeSize(){
	
		switch(this.nodeName.toLowerCase()){
			case "small":
				var parms = [smallerObj,largerObj,changeSize,"decrease","Increase",-1,1];
				break;
			case "big":
				var parms = [largerObj,smallerObj,changeSize,"increase","Decrease",1,-1];
				break;
		}
		var size = parseFloat(this.id.split("fs")[1]);
		newSize(size);
				
		if(Math.abs(size)==3) { 
			removeEvent(parms[0],"click",parms[2]);
			this.style.textDecoration = "line-through";
			this.title = "You cannot " + parms[3] +" the text size any further";
			addEvent(parms[1],"click",parms[2]);
			parms[1].title = parms[4] +" the text size";
		} else { 
			removeEvent(parms[0],"click",parms[2]);
			removeEvent(parms[1],"click",parms[2]);
			addEvent(parms[0],"click",parms[2]);
			addEvent(parms[1],"click",parms[2]);
			size = eval(size+parms[5]);
			othersize = eval(size+parms[6]+parms[6]);
			this.id = "fs"+size; 
			parms[1].id = "fs"+othersize; 
			parms[1].style.textDecoration = "none";
			parms[1].title = parms[4] +" the text size";
		}
		
		if(d.getElementById("skyRHolder") && d.getElementById("skyR") ) {
			placeAC("skyR");
		}	
		if(d.getElementById("boxRHolder") && d.getElementById("boxR") ) {
			placeAC("boxR");
		}		
		
	}
	
	function newSize(size){
		if(size>2) { size = 2; }
		else if(-2>size) { size = -2; }
		switch(size) {
			case -2:
				fs = 72;
				break;
			case -1:
				fs = 85;
				break;
			case 1:
				fs = 115;
				break;
			case 2:
				fs = 130;
				break;
			default:
				fs = 100;
				break;
		}
		d.getElementById("article").style.fontSize = fs+"%";
		setCookie("gam_style", "fS="+size+";");	
	}

}

/* NB: windows print */
function printThis(nObj){
	var c = nObj.childNodes;
	for(x=0;c.length>x;x++){
		if(c[x].className=="print"){
			addEvent(c[x].getElementsByTagName("a")[0],"click",function(){ 
				if(d.getElementById("pagination") && location.search.indexOf("pageRequested=all")==-1){
					qmod = (location.search) ? "\u0026" : "?";
					url = location.href.replace(/pageRequested=([0-9]+)/,"");
					location.href=url+qmod+"pageRequested=all\u0026print=true";
				}	else {
					window.print(); 
				}
				return false; 
			});
			c[x].getElementsByTagName("a")[0].title="Click to print this page";
		}
	}
}

function chkPrint(){
	if(location.search.indexOf("print=true")!=-1 && window.print){
		window.print();
		if(ua.indexOf("safari")==-1) { history.go(-1); }
		else { return; }
	}
}


/* NB: widen page on finance sites */
function writeWidenPage(){
	if(d.getElementsByTagName("BODY")[0].className.indexOf("globeinvestor")!=-1 || d.getElementsByTagName("BODY")[0].className.indexOf("globefund")!=-1){ 
		var pt = d.getElementById("articleTools");
		var li = d.createElement("LI");
		var an = d.createElement("A");
		li.className = "widen";
		an.href = "#";
		addEvent(an,"click",function () { widenPage(this); return false; });
		an.appendChild(d.createTextNode("Widen this Page"));
		li.appendChild(an);
		pt.appendChild(li);
	}

	function widenPage(el){
		var nObj = d.getElementById("articleTools").childNodes;
		var text = el.firstChild.nodeValue;
	
		if(text.indexOf("Widen")!=-1){
			d.getElementById("content").className = "widen";
			el.firstChild.nodeValue = "Narrow this Page";
			d.getElementById("boxRHolder").style.visibility = "hidden";		
			d.getElementById("skyRHolder").style.visibility = "hidden";	
			el.parentNode.className = "narrow";
		} else {		
			d.getElementById("content").className = "";
			d.getElementById("boxRHolder").style.visibility = "";			
			d.getElementById("skyRHolder").style.visibility = "";	
			el.firstChild.nodeValue = "Widen this Page";
			el.parentNode.className = "widen";
		}
	}
}



/* NB: duplicate tools within aricles */
function addArticleTools(){
	if(ua.indexOf("mac")!=-1 && ua.indexOf("msie")!=-1) { return; }
	var aTools = d.getElementById("articleTools").cloneNode(true);
	aTools.id = "articleBottomTools";
	aToolsLinks = aTools.getElementsByTagName("a");
	
	if(d.getElementById("articleBottomToolsHolder")){ aToolsHolder = d.getElementById("articleBottomToolsHolder"); }
		for(x=0;aToolsLinks.length>x;x++){
		if(aToolsLinks[x].parentNode.className=="comment" && d.getElementById("commentRSS")) { 
			if(aToolsLinks[x].className=="closed"){
				commentvalue = aToolsLinks[x].firstChild.nodeValue.replace(/Comments \(([0-9]+)/,"Follow conversation ($1");
			} else if(aToolsLinks[x].firstChild.nodeValue.search(/Comment(|[s]) \(([0-9]+)/)!=-1){
				commentvalue = aToolsLinks[x].firstChild.nodeValue.replace(/Comment(|[s]) \(([0-9]+)/,"Join conversation ($2");
			} else {
				commentvalue = aToolsLinks[x].firstChild.nodeValue.replace(/Comment/,"Leave the first comment");
			}
			aToolsLinks[x].firstChild.nodeValue=commentvalue;
		}
	}	
	if(d.getElementById("articleBottomToolsHolder") && aToolsHolder) { aToolsHolder.appendChild(aTools); }
}


/* NB: get stories, and hide if listed */
function getSLinks(i,s_id,count){
	var s_links = d.getElementById(i).getElementsByTagName("a");
	for(x=0;s_links.length>x;x++){
		if((s_links[x].href.indexOf(s_id)>1) || (x>=count)){
			nObj = ((i=="relatedNewsNav" || i=="wiseNav") && s_links[x].parentNode.parentNode.nodeName=="LI") ? s_links[x].parentNode.parentNode : s_links[x].parentNode;
			nObj.style.display = "none";
			return;
		}
	}
}

/* NB: init */
function articleInit(){
	if(d.getElementById("articleTools") && location.href.indexOf("CommentStory")==-1) { 
		addArticleTools();
		if(window.print){ 
			printThis(d.getElementById("articleTools"));
			if(d.getElementById("articleBottomTools")) { printThis(d.getElementById("articleBottomTools")); }
		}	
	}
	if((location.href.indexOf("PPVStoryOwn")==-1) && (d.getElementsByTagName("BODY")[0].className.indexOf("bundle")==-1)) { fontSizer();  }
	writeWidenPage();
	globalNav();
}

if(typeof init != "function"){ function init(){ articleInit(); } }

