function getjs(jsFile){
	var jsCheck = document.getElementById(jsFile);
	if(jsCheck == null){
        var loadScript = document.getElementById(jsFile);
        var head = document.getElementsByTagName("head").item(0);
        loadScript = document.createElement("script");
        loadScript.setAttribute("src","http://alt.coxnewsweb.com/statesman/js/" + jsFile + ".js");
        loadScript.setAttribute("id",jsFile);
		loadScript.setAttribute("type","text/javascript");
        head.appendChild(loadScript);
	}
}
  
  function getUniqueKey4Pluck() {
   var loc = window.location.href;
   var srchstring = "content/";
   var pos = loc.indexOf(srchstring);
   var start = parseInt(pos+srchstring.length);
   var end  = parseInt(loc.indexOf('?'))-start;
   if(loc.indexOf('#') != -1)
   		var end  = parseInt(loc.indexOf('#'))-start;
   if (end < 1){var key = loc.substr(start);}
   else{var key = loc.substr(start,end);}
   return key;
  }

function recommend_widget() {
	//gSiteLife.Recommend("ExternalResource", getUniqueKey4Pluck());
}

function showLoad(elementId){document.getElementById(elementId).style.background = 'url(http://alt.coxnewsweb.com/statesman/img/spinner.gif) no-repeat scroll center center';}

function hideLoad(elementId){
	document.getElementById(elementId).style.backgroundImage= 'none';
}

function showElement(elementId) {
	document.getElementById(elementId).style.display='block';
}

function hideElement(elementId) {
	document.getElementById(elementId).style.display='none';
}

function delayAction(doSomething,trigger){
	if(trigger != ''){
		var triggerCheck = document.getElementById(trigger);
		if(triggerCheck == null){
			callmelater(doSomething, trigger);
			return;
		}
	}
	var actions = doSomething
	if(actions != null){
		try{
			for(var i=0; i < actions.length; i++){
				eval(actions[i]);
			}
		}
		catch(err){
			callmelater(actions,trigger);
		}
	}
}

function callmelater(doAgain,trigger){
	window.setTimeout(function(){delayAction(doAgain,trigger);},300);
}


//------------ Out of box widget functions go below this line ---------------//
function truncateString(objClass,len){
var elements = document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
	if(elements[i].className!="")
	if (elements[i].className==objClass){
		var length = len;
		var p = elements[i].getElementsByTagName("a")[0];
		if (p) {
		  var trunc = p.innerHTML;
		  if (trunc.length > len) {
			trunc = trunc.substring(0, len);
			trunc = trunc.replace(/\w+$/, '');
			trunc += '...';
			p.innerHTML = trunc;
		  }
		 }
  }
}
}

function postComment() {
	// get form elements and page info
	var articleKey = new ArticleKey(getUniqueKey4Pluck());
	var pageUrl = document.location.href;
	var pageTitle = document.title;
	var commentBody = document.getElementById('commentBody').value;

	// create and send request
	var requestBatch = new RequestBatch();            
	var commentAction = new CommentAction(articleKey, pageUrl, pageTitle, commentBody);
	requestBatch.AddToRequest(commentAction); 
	requestBatch.BeginRequest(serverUrl, commentSubmitted); 
}

function commentSubmitted(responseBatch) {
	if (responseBatch.Messages[0].Message == 'ok') {
		document.getElementById("comment_error").innerHTML = "";
		document.getElementById("comment_ok").innerHTML = "<strong>Your comment has been posted.  Please refresh the page to view it.</strong>";
	} else {
		document.getElementById("comment_ok").innerHTML = "";
		document.getElementById("comment_error").innerHTML = "<strong>We restrict rapid posting of multiple comments for quality reasons. You have have already posted a comment within the last several seconds. Please try again later.</strong>";
		}
}

function comments_widget() {
	if(!document.getElementById("no_comments")){
	document.write("<a name='comments'></a><div id='storyComments'><h3>Your Comments</h3><p>Austinites love to be heard, and we're giving you a bullhorn. We just ask that you keep things civil. Leave out the personal attacks. Do not use profanity, ethnic or racial slurs, or take shots at anyone's sexual orientation or religion. If you can't be nice, we reserve the right to remove your material and ban users who violate our <a href='http://www.statesman.com/search/content/standing/visitoragreement.html'>visitor's agreement</a>.</p>");
	var bakery = document.cookie;
	var srchstring = "at=u=";
	var pos = bakery.indexOf(srchstring);
	//if else is redundant, will be fixed in commenting V2
	if(pos != -1){
		document.write('<div id="comment_error" style="color:red;"></div><div id="comment_ok" style="color:green;"></div><div class="Comments_AddTextarea"><form><textarea cols="60" rows="10" name="submitcomment" id="commentBody"></textarea><input type="button" value="Submit" onclick="postComment()"/></form></div>');
	} else {
		document.write('<div id="SiteLife_Login" class="SiteLife_Login">You must be logged in to leave a comment. <a onclick="window.parent.window.location.href = \'https://www.statesman.com/userreg/userreg/userregclass/portal_login.jsp?SendBackURL=\' + encodeURIComponent(window.parent.window.location.href); return false;" href="#">Login</a> | <a onclick="window.parent.window.location.href = \'https://www.statesman.com/userreg/userreg/ursignup/signup.jsp?UrUsecase=800300&SendBackURL=\' + gSiteLife.EscapeValue(window.parent.window.location.href); return false;" href="#">Register</a></div><div class="Comments_AddTextarea"><form><textarea cols="60" rows="10" name="submitcomment" id="commentBody" disabled="true"></textarea><input type="button" value="Submit" disabled="true"/></form></div>');
	}

	gSiteLife.CommentsOutput("ExternalResource",getUniqueKey4Pluck(), true, 5, "TimeStampDescending");
	document.write("</div><div style='clear:both'></div>");
	}
		var pluckMod = new (function(){
			var me = this;
			me.injectStyle = function(){
				var r = document.getElementById('commentsiframe');
				if (r == null) {
					callmelater();
					return;
				}
				r = r.contentWindow || r.contentDocument;
				if (r.document) {
					r = r.document;
				}
				try {
					var myframe = document.getElementById('commentsiframe');
				} 
				catch (err) {
					callmelater();
					return;
				}
				myframe.allowTransparency = "true";
				function callmelater(){
					window.setTimeout(pluckMod.injectStyle, 300);
				}
			};
		});
		pluckMod.injectStyle();
	}
