var spRemain = "";
var spTime = 10;
var spSec = 60;
var reloadCap = 0;
var spWait;
var spTries = 0;
var nDat = new Date;
var uTime = parseInt(nDat.getTime() / 1000);



function ctDwn(){
	if(spTime>1){
		document.getElementById("spTmsg").innerHTML=("Updating in <span class='countdown minutes'>" + spTime + " minutes</span>");
		spTime--;
		setTimeout("ctDwn()",60000);
	}else{
		if(spTime<=2){
			if(spSec>0){
				document.getElementById("spTmsg").innerHTML=("Updating in <span class='countdown seconds'>" + spSec + " seconds</span>");
				spSec--;
				setTimeout("ctDwn()",1000);
			}else{
				if(reloadCap<12){
					spTime = 10;
					spSec = 60;
					reGrab();
					reloadCap++
				}else{
					spMsg('Please refresh the page for updated stories.')
					document.getElementById("spTmsg").style.visibility="hidden";
				}
			}
		}
	}
}

function spMsg(x){
	ggMsgTmp = document.getElementById("spTmsg")
	ggMsgTmp.innerHTML=x;
	ggMsgTmp.style.display="block";
}

function in_array(n,h){
    var k = '';
    for (k in h) {
        if (h[k] == n){return true}
    }
    return false;
}

function JSONscriptRequest(fullUrl) {
	this.fullUrl = fullUrl;
	this.noCacheIE = '?noCacheIE=' + (new Date()).getTime();
	this.headLoc = document.getElementsByTagName("head").item(0);
	this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {
	this.scriptObj = document.createElement("script");
	this.scriptObj.setAttribute("type", "text/javascript");
	this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
	this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest.prototype.removeScriptTag = function () {
	this.headLoc.removeChild(this.scriptObj);
}

JSONscriptRequest.prototype.addScriptTag = function () {
	this.headLoc.appendChild(this.scriptObj);
}

function spWireCallback(ynData){
	var tmpStr = "";
	if(ynData.items.length>0){
		/* document.getElementById("spTmsg").style.display="none"; */
	}
	if(upDat==0){
		for(i=0;i<(ynData.items.length);i++){
			var tmpCat = "x"
			for(j=0;j<ynData.items[i].categories.length;j++){
				tmpCatEx = ynData.items[i].categories[j].toLowerCase();
				if ((tmpCatEx == "redsox") || (tmpCatEx == "red sox") || (tmpCatEx == "mlb")){
					tmpCat = "rs"
				}
				if ((tmpCatEx == "patriots") || (tmpCatEx == "nfl")){
					tmpCat = "pt"
				}
				if ((tmpCatEx == "bruins") || (tmpCatEx == "nhl")){
						tmpCat = "br"
				}
				if ((tmpCatEx == "celtics") || (tmpCatEx == "nba")){
					tmpCat = "ct"
				}
			}
			if(ynData.items[i].title.indexOf(" - ")!=-1){
				tmpTitle = ynData.items[i].title.split(" - ")
				tmpTitle = tmpTitle[0]
			}else{
				tmpTitle = ynData.items[i].title
			}
			tmpLink = ynData.items[i].link;
			tmpStr += "<div id='" + tmpLink + "' class='spItem " + tmpCat + "'><div class='spiContainer'><div class='spMdCol'><div class='bx " + tmpCat + "'>&nbsp;</div></div><!-- end spMdCol--><div class='spRtCol'><h3><a href='" + tmpLink + "'>" + tmpTitle + "</a> <span class='timestamp'>" + getDf(ynData.items[i].time) + "</span></h3><div class='tt' onclick='document.location=\"" + tmpLink + "\"'>" + "<span class='leadIn'>" + ynData.items[i].contributor + "</span> " + ynData.items[i].description + "</div></div><div class='cf'></div></div></div>";
		}
		document.getElementById("gg").innerHTML=(tmpStr)
		getStyl();
	}else{
		var tmpLink = ynData.items[0].link;
		if(tmpLink != document.getElementById("gg").getElementsByTagName("a")[0].href){
			var lStop=0;
			for(i=0;i<(ynData.items.length);i++){
				tmpLink = ynData.items[i].link;
				if((tmpLink != document.getElementById("gg").getElementsByTagName("a")[0].href)&&(lStop==0)){
					var tmpCat = "x"
					for(j=0;j<ynData.items[i].categories.length;j++){
						tmpCatEx = ynData.items[i].categories[j].toLowerCase();
						if ((tmpCatEx == "redsox") || (tmpCatEx == "red sox") || (tmpCatEx == "mlb")){
							tmpCat = "rs"
						}
						if ((tmpCatEx == "patriots") || (tmpCatEx == "nfl")){
							tmpCat = "pt"
						}
						if ((tmpCatEx == "bruins") || (tmpCatEx == "nhl")){
							tmpCat = "br"
						}
						if ((tmpCatEx == "celtics") || (tmpCatEx == "nba")){
							tmpCat = "ct"
						}
					}
					if(ynData.items[i].title.indexOf(" - ")!=-1){
						tmpTitle = ynData.items[i].title.split(" - ")
						tmpTitle = tmpTitle[0]
					}else{
						tmpTitle = ynData.items[i].title
					}
					tmpLink = ynData.items[i].link;
					tmpStr += "<div id='" + tmpLink + "' class='spItem " + tmpCat + "'><div class='spiContainer'><div class='spMdCol'><div class='bx " + tmpCat + "'>&nbsp;</div></div><!-- end spMdCol--><div class='spRtCol'><h3><a href='" + tmpLink + "'>" + tmpTitle + "</a> <span class='timestamp'>" + getDf(ynData.items[i].time) + "</span></h3><div class='tt' onclick='document.location=\"" + tmpLink + "\"'>" + "<span class='leadIn'>" + ynData.items[i].contributor + "</span> " + ynData.items[i].description + "</div></div><div class='cf'></div></div></div>";
				}else{
					lStop=1;
				}
			}
			document.getElementById("gg").innerHTML=(tmpStr+document.getElementById("gg").innerHTML)
			$(".spNew").animate({"height": "toggle", "opacity": "toggle"}, { duration: 350 });
			$(".spNew").removeClass("spNew")
		}else{
			spMsg('Nothing new. Next check for updates in 10 minutes.')
			setTimeout('$("#sptMsg").animate({"height": "toggle", "opacity": "toggle"}, { duration: 800 });',4000);
		}
	}
	ctDwn();
}

function tstSp(){
	if(document.getElementById("gg").innerHTML==""){
		if(spTries<3){
			spMsg("We're having a problem retrieving the latest headlines. Please stand by while we try again.")
			spTries++;
			spWire(0);
			setTimeout("tstSp()",5000)
		}
		if(spTries==3){
			spMsg("We're sorry, but something is broken. Please wait a minute and try again.")
		}
	}
}

function spWire(x){
	setTimeout("tstSp()",3000)
	upDat = x;
	
	if($('ul.teamButtons').attr('id')=='red_sox'){
		var request = 'http://www.boston.com/sports/sxWire.json'
		$('#globeWire').addClass('redSox')
		$('.topper p.tt').text('Red Sox updates from The Boston Globe');		
	}

	if($('ul.teamButtons').attr('id')=='patriots'){
		var request = 'http://www.boston.com/sports/ptWire.json'
		$('#globeWire').addClass('patriots')
		$('.topper p.tt').text('Patriots updates from The Boston Globe');		
	}

	if($('ul.teamButtons').attr('id')=='celtics'){
		var request = 'http://www.boston.com/sports/ctWire.json'
		$('#globeWire').addClass('celtics')
		$('.topper p.tt').text('Celtics updates from The Boston Globe');		
	}

	if($('ul.teamButtons').attr('id')=='bruins'){
		var request = 'http://www.boston.com/sports/brWire.json'
		$('#globeWire').addClass('bruins')
		$('.topper p.tt').text('Bruins updates from The Boston Globe');		
	}

	if($('ul.teamButtons').attr('id')=='sports' || $('ul#nav').attr('class')=='gnavContainer') {
		var request = 'http://www.boston.com/sports/spWire.json'
		$('#globeWire').addClass('sportsFront')
		$('.topper p.tt').text('Sports updates from The Boston Globe');	
	}	
		
	
	aObj = new JSONscriptRequest(request);
	aObj.buildScriptTag();
	aObj.addScriptTag();
}

function getStyl(){
	var gg = document.getElementById("gg");
	for(i=0;i<gg.getElementsByTagName("a").length;i++){
		if(((getStyle(gg.getElementsByTagName("a")[i],"color")) == "#6699cc") || (getStyle(gg.getElementsByTagName("a")[i],"color") == "rgb(102, 153, 204)")){
			gg.getElementsByTagName("a")[i].parentNode.style.color="#777";
		}
	}
}

function getDf(x){
	var tmpDif=(uTime-x)
	if(tmpDif<=60){return "<span class='fresh'>1 minute ago</span>"}
	if((tmpDif>=61)&&(tmpDif<=300)){return "<span class='fresh'>5 minutes ago</span>"}
	if((tmpDif>=301)&&(tmpDif<=600)){return "<span class='fresh'>10 minutes ago</span>"}
	if((tmpDif>=601)&&(tmpDif<=900)){return "<span class='fresh'>15 minutes ago</span>"}
	if((tmpDif>=901)&&(tmpDif<=1200)){return "<span class='fresh'>20 minutes ago</span>"}
	if((tmpDif>=1201)&&(tmpDif<=1800)){return "<span class='fresh'>30 minutes ago</span>"}
	if((tmpDif>=1801)&&(tmpDif<=2700)){return "<span class='fresh'>45 minutes ago</span>"}
	if((tmpDif>=2701)&&(tmpDif<=4500)){return "1 hour ago"}
	if((tmpDif>=4501)&&(tmpDif<=9000)){return "2 hours ago"}
	if((tmpDif>=9001)&&(tmpDif<=21600)){return "6 hours ago"}
	if((tmpDif>=21601)&&(tmpDif<=43200)){return "12 hours ago"}
	if((tmpDif>=43201)&&(tmpDif<=86400)){return "1 day ago"}
	if(tmpDif>=86401){return "> 1 day ago"}
}

function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

function getStyle(el, prop){
	if (document.defaultView && document.defaultView.getComputedStyle){
		return document.defaultView.getComputedStyle(el, null)[prop];
	}else if (el.currentStyle){
		return el.currentStyle[prop];
	}else{
		return el.style[prop];
	}
}

function reGrab(){
    spWire(1)
}

spWire(0);
