//Predefine stribUser
var stribUser = "";

function killOASspacer() {
  var div = document.getElementById('OAS_bottomleaderboard');
  if(div != null){
		var a = div.getElementsByTagName('img');
		if (a.length && a[0].width == 1 && a[0].height == 1) {
			 a[0].style.display = 'none';
			 a[0].style.visibility = 'hidden';
		}
	}
}

function show( id )
{
document.getElementById(id).style.display = 'block';
}
function hide( id )
{
document.getElementById(id).style.display = 'none';
}

function showHelp( id )
{
document.getElementById(id).style.visibility = 'visible';
document.getElementById(id).style.left = '-95px';
}
function hideHelp( id )
{
document.getElementById(id).style.visibility = 'hidden';
document.getElementById(id).style.left = '-5000px';
}

function breakPopup(theLink,closePopup)
{
if (! (window.focus && window.opener))return true;
window.opener.focus();
window.opener.location.href=theLink.href;
if (closePopup)window.close();
return false;
}

function timeStamp(){
var elapsedTime = sectionTime - storyTime;
	if( elapsedTime <= 31536000){
		if( elapsedTime <= 86400){
			if(elapsedTime <= 3600){
				if(elapsedTime <= 60){
				document.write(" <span class=\"timeStamp\">Updated a minute ago</span>")
				}
				else{
				document.write(" <span class=\"timeStamp\">Updated " + Math.round(elapsedTime / 60) + " minutes ago</span>")
				}
			}
			// older than 1 hour, print this pubdate
			else{
			document.write(" <span class=\"timeStamp\">Updated " + dayOld + "</span>");
			}
			}
			// older than 1 day, print this pubdate
			else{
			document.write(" <span class=\"timeStamp\">Published " + dayOld + "</span>");
			}
		}
	// older than 1 year, print this pubdate
	else{
	document.write(" <span class=\"timeStamp\">First published " + dayOld + "</span>");
	}
}

// blockToggle is used to simply turn the display of an element to none or block

function blockToggle(id)
{
	if (document.getElementById(id).style.display == document.getElementById('docLayout').style.display)
	{
		document.getElementById(id).style.display = "none";
	}
	else
	{
		document.getElementById(id).style.display = document.getElementById('docLayout').style.display
	}
}

function getURLParameter(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var strURL = URLDecode(window.location.href);
	var results = regex.exec(strURL);
	if( results == null ){
		return null;
	}else{
		result = results[1];
		return result;
	}
}

function killADSpacer(adContainer) {
	var div = $(adContainer);
	if(div != null){
		var hasIframe = div.getElementsByTagName('iframe');
		var hasObject = div.getElementsByTagName('object');
		var hasEmbed = div.getElementsByTagName('embed');
		var hasImage = div.getElementsByTagName('img');
		var hasDiv = div.getElementsByTagName('div');
		if (hasIframe[0] != null || hasObject[0] != null || hasImage[0] != null || hasEmbed[0] != null || hasDiv[0] == null){
			if(adContainer == 'OAS_topleaderboard'||adContainer == 'OAS_bottomleaderboard'){
				div.style.padding = '12px 0 12px 104px';
				if (hasImage[0] != null){
					if (hasImage[0].width == 1){
						div.style.padding = '0px';
						hasImage[0].style.display = 'none';
						hasImage[0].style.visibility = 'hidden';
					}
				}
			}
		}
		if (hasDiv[0] != null){
			if(adContainer == 'OAS_topleaderboard'||adContainer == 'OAS_bottomleaderboard'){
				div.style.padding = '0';
				hasDiv[0].style.margin = '0 0 0 104px';
			}
		}
	}
}

/* Begin encoding decoding functions. */
var keyString = "E43hDYQLP+m7k:c8pyiaO5CuKNz10bt2jv6RwHnMge.Goqx9BfdSsXrTZIlJFAVUW";
var referenceString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+:.";
var encodSig = "KArks";

function decodeString(stringToDecode){
	if(stringToDecode){
		var dbString = "";
		var retDecodeString = "";
		var passedSig = stringToDecode.substring(0, 5);
		var curChar = "";
		if(passedSig == encodSig){
			stringToDecode = stringToDecode.substring(5);
			for (si = 0; si < stringToDecode.length; si++) {
				curChar = stringToDecode.charAt(si);
				if (curChar != "_") {
	
	
	
	
					curIndex = keyString.indexOf(curChar);
					decodeChar = referenceString.charAt(curIndex);
				}else{
					decodeChar = curChar;
				}
				
				//dbString += "|si: " + si + "|curchar:" + curChar + "|decodeChar:" + decodeChar + "\n";
				retDecodeString += decodeChar;
			}
			//alert(dbString);				
			return retDecodeString;					
		}else{
			return false;
		}
	}else{
		return null;
	}
}

function encodeString(stringToEncode){
	var retEncodeString = encodSig;
	//$("debug").innerHTML += "ste: " + stringToEncode.length + "<hr />";
	for(si=0; si < stringToEncode.length; si++){
		//$("debug").innerHTML += "si: " + si + "<hr />";
		curChar = stringToEncode[si];
		curIndex = referenceString.indexOf(curChar);
		if (curIndex == -1) {
			encodeChar = "_";
		}
		else {
			encodeChar = keyString[curIndex];
		}
		retEncodeString += encodeChar;
	}				
	return retEncodeString;
}
/* end encoding decoding functions */

function URLDecode(encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}
//Hid and show comments
arCommentToggleControlls = [];
var arCommentsElements = [];
commentingHidden = false;

// toggles commenting from current state to the opposite state if the parameter state is null
// if state is passed as 1 comments are turned on, passed as 0 comments off.
function toggleCommenting(state){
	//var arCommentsElements = ["featuredComment"];
	if(state != null){
		if(state == 1){
			commentingHidden = true;
			createCookie("hideCommenting", 1, null, "startribune.com");
			for(i=0; i < arCommentsElements.length; i++){
				//document.getElementById(arCommentsElements[i]).style.display = "none";
				$(arCommentsElements[i]).addClass("hidden");
			}
		}else if(state == 0){
			commentingHidden = false;
			createCookie("hideCommenting", 0, null, "startribune.com");
			for(i=0; i < arCommentsElements.length; i++){
				//document.getElementById(arCommentsElements[i]).style.display = "block";
				$(arCommentsElements[i]).removeClass("hidden");
			}		
		}
	}else{
		for(i=0; i < arCommentsElements.length; i++){
			$(arCommentsElements[i]).toggleClass("hidden");
		}
		if($(arCommentsElements[0]).hasClass("hidden")){
			commentingHidden = true;
			createCookie("hideCommenting", 1, null, "startribune.com");
			
		}else{
			commentingHidden = false;
			createCookie("hideCommenting", 0, null, "startribune.com");		
		}
 	}
}

function registerCommentToggle(element){
	if (element) {
		updateCommentControl(element);
		element.onclick = toggleCommentingonClick;		
		arCommentToggleControlls.push(element);
	}
}

function toggleCommentingonClick(){
	toggleCommenting(); 
	updateCommentControls();
	return false;
}

//Changes the text in ALL registered controls
function updateCommentControls(){
	for (i = 0; i < arCommentToggleControlls.length; i++) {
		updateCommentControl(arCommentToggleControlls[i]);
	}
}

//Changes the text in ONE control
function updateCommentControl(element){
		if(commentingHidden){
			element.innerHTML = "Show reader comments";			
		}else{
			element.innerHTML = "Hide reader comments";
		}
}

function checkCommentingState(){
	if(GetCookie('hideCommenting') && GetCookie('hideCommenting') == "1"){
		commentingHidden = true;
	}else{
		commentingHidden = false;
	}
}

function initComments(){
	checkCommentingState();
	if(commentingHidden){
		toggleCommenting(1);
	}else{
		toggleCommenting(0);
	}
	updateCommentControls();
}



function cancelRefresh(){
	window.clearTimeout(reloadTimer);
}
