/* ____________________________________ openAuth - no dependencies */ 

/* __________________________ JSON script object */ 
/*constructor function for creating a new scriptJSON object*/
var scriptJSON = function (fullUrl) {
	this.fullUrl = fullUrl; 
	this.scriptId = 'scriptId' + this.scriptCounter++;
	this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
	this.documentHead = document.getElementsByTagName("head").item(0);	
}
scriptJSON.prototype = {
	constructor : scriptJSON,
	scriptCounter : 1,
	buildScriptTag : function () {
		this.scriptTag = document.createElement("script");
		this.scriptTag.setAttribute("type", "text/javascript");
		this.scriptTag.setAttribute("charset", "utf-8");
		this.scriptTag.setAttribute("src", this.fullUrl + this.noCacheIE);
		this.scriptTag.setAttribute("id", this.scriptId);
	},
	removeScriptTag : function () {
		this.documentHead.removeChild(this.scriptTag);  
	},
	addScriptTag : function () {
		this.documentHead.appendChild(this.scriptTag);
	}
}

/* __________________________ openauth object */ 
openAuth = {
	init : function() {
		this.token = null;
		this.loggedIn = false;
		this.userId = null;
		this.referer = "";
		this.loginStatusDiv = document.getElementById("loginStatus");
		this.getToken();
	},
	createTokenCookie : function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readTokenCookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	eraseTokenCookie : function(name) {
		this.createTokenCookie(name,"",-1);
	},
	getToken : function() {	
		if (this.readTokenCookie('__s03t19m01_')) {
			this.token = this.readTokenCookie('__s03t19m01_');
			if (this.token) {
				this.getInfo();
			}
		}
		else {
			requestUrl = "https://api.screenname.aol.com/auth/getToken?devId="+openAuth.devId+"&f=json&c=openAuth.gotToken";
			tokenJSONScript = new scriptJSON(requestUrl);
			tokenJSONScript.buildScriptTag();
			tokenJSONScript.addScriptTag();	
		}	
	},
	gotToken : function(objJSON) {
		if (objJSON.response.statusCode != 200) {
			this.loggedIn = false;
			tokenJSONScript.removeScriptTag();
			this.makeLoginLink();
		} else {
			this.loggedIn = true;
			this.token = objJSON.response.data.token.a;
			tokenJSONScript.removeScriptTag();			
    		if (this.token) {
				this.getInfo();
			}
		}
	},
	getInfo : function() {
		requestUrl = "https://api.screenname.aol.com/auth/getInfo?a="+openAuth.token+"&devId="+openAuth.devId+"&referer="+openAuth.referer+"&f=json&c=openAuth.gotInfo";
		infoJSONScript = new scriptJSON(requestUrl);
		infoJSONScript.buildScriptTag();
		infoJSONScript.addScriptTag();	
	},
	gotInfo : function(objJSONB){
		if (objJSONB.response.statusCode !== 200) {
			this.eraseTokenCookie('__s03t19m01_');
			this.makeLoginLink();
		} else {
			if (!this.readTokenCookie('__s03t19m01_')) {
				this.createTokenCookie('__s03t19m01_', this.token, 1);	
			}	
			/*sitesocial*/
			siteSocialIntegration.init();

			if(window.AIM) {
				AIM.params.token = this.token;
				AIM.params.authenticationMode = 0;
				AIM.params.callbacks.endSession.push("siteSocialIntegration.logoutUser")
				if(!AIM.params.sessionId) AIM.transactions.startSession();
			}	
			this.userId = objJSONB.response.data.userData.loginId;
			this.removeCurrentLink();
			this.makeLogOutLink();
		}
	},
	makeLoginLink : function() {
		if (this.loginStatusDiv) {
			if(this.loginStatusDiv.firstChild){this.removeCurrentLink();}
			var loginLink = document.createElement("SPAN");
			loginLink.setAttribute("id", "loginLink"); 			
			loginLink.onclick = this.openAuthWindow;			
			var loginText = document.createTextNode("Sign In / Register"); 
			loginLink.appendChild(loginText); 
			this.loginStatusDiv.appendChild(loginLink);
		}
	},
	openAuthWindow : function (){
			var loginUrl = "http://api.screenname.aol.com/auth/login?devId="+openAuth.devId+"&f=qs&succUrl="+openAuth.loginSuccURL;	
			var x = (screen.width - 490)/2; var y = (screen.height -490)/2;								 
			window.open(loginUrl,'name','resizable=yes,width=490,height=490,directories=no,titlebar=no,status=no,menubar=no,toolbar=no,location=yes,left='+x+',top='+y);
	},
	makeLogOutLink : function() {
		if (this.loginStatusDiv && this.userId && (!(document.getElementById("logoutLink")))) {
			if(this.loginStatusDiv.firstChild){this.removeCurrentLink();}
			var loggedInText = document.createTextNode("Logged In: ");
			var userName = document.createElement("span");   
			userName.setAttribute("id", "userName"); 	
			var userNameText = document.createTextNode(this.userId); 
			userName.appendChild(userNameText);
			this.loginStatusDiv.appendChild(loggedInText);
			this.loginStatusDiv.appendChild(userName);
			var logoutLink = document.createElement("SPAN");
			logoutLink.style.cursor = "pointer";
			logoutLink.setAttribute("id", "logoutLink");
			logoutLink.onclick = function (){
				var logoutUrl = "https://api.screenname.aol.com/auth/logout?a="+openAuth.token+"&devId="+openAuth.devId+"&doSNSLogout=1&f=qs&succUrl="+openAuth.logoutSuccURL;
				AIM.transactions.endSession(true) /*sitesocial*/ 	
				var x = (screen.width - 490)/2; var y = (screen.height -490)/2;								 
				window.open(logoutUrl,'name','resizable=yes,width=490,height=490,directories=no,titlebar=no,status=no,menubar=no,toolbar=no,location=yes,left='+x+',top='+y);	
			}	
			var logoutText = document.createTextNode("Log Out"); 
			logoutLink.appendChild(logoutText); 
			this.loginStatusDiv.appendChild(logoutLink);	
		}
	},
	removeCurrentLink : function(){
		while (this.loginStatusDiv.firstChild) {
			this.loginStatusDiv.removeChild(this.loginStatusDiv.firstChild);
		 };
	},
	/*this function calls Blogsmith's sns javasvript to populate Blogsmith's comment's module fields*/
	sns : function () {
		if (document.getElementById("sns")) {
			sns.calls.getToken();
		}
	},
	/*The function de-populates Blogsmith's comment's module fields*/
	clearForm: function(){
		if (document.getElementById('cmtuinfo_sns')){
			var snsInput = getElementsByClassName(document, "input", "formtext");
			for(a=0; a<snsInput.length; a++) {
				snsInput[a].value = '';
			}
		}
	}

}
/*sitesocial */
siteIM_params_launchMethod = function() {
	if(!AIM.params.token) {
		openAuth.openAuthWindow();
	} else {
		AIM.transactions.startSession();
	}
}
siteSocialIntegration = {
	init : function () {
 
	},
	logoutUser:function() {
				openAuth.removeCurrentLink();
			openAuth.makeLoginLink();
			openAuth.clearForm();
			openAuth.loggedIn = false;
			openAuth.eraseTokenCookie('blogToken');
		}
}

/*end sitesocial*/
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

/* end openauth */


//Cross Promo
var d=document; var w=window;var docHg;
var range=400;
var cod_arr=new Array();
var arrLen = 0;
var modSpan = 6;
var currId = 0;
var opt = {
	ae:function(o,et,fn){if(o.addEventListener)o.addEventListener(et,fn,false);else if(o.attachEvent)o.attachEvent("on"+et,fn);},
	gt:function(t,o){o=o?o:document;return o.getElementsByTagName(t)},
	gc:function(c,t,s,o){var r=new Array();var os=opt.gt(t,o);for(var i=0,j=0,l=os.length;i<l;i++){var sc = s + os[i].className + s;if(sc.indexOf(s+c+s)!=-1){r[j] = os[i];j++;}}return r;},
        nview:function(obj,i) {
                if (obj.loaded==null) {
                    obj.loaded=0;
                }
                if (obj.loaded==0) {

                    if(document.all) {
                        var relTop=obj.offsetParent.offsetTop-(w.scrollY||d.documentElement.scrollTop);
                    } else {
                        var relTop=obj.offsetTop-(w.scrollY||d.documentElement.scrollTop);
                    }

                    if((relTop-docHg-20)<range) {
                      var codUrl=opt.gt('a',obj)[0].href;
                      opt.xhr(codUrl,obj,i);
                    }
                }
        },
        doCod:function(){
                var l=cod_arr.length;
                for (var i=0;i<l;i++) {
                        opt.nview(cod_arr[i],i);
                }
        },
        xhr:function(u,obj,i){
                obj.loaded=1;
                var f,r,m='GET';
                f=function(){if(r.readyState>3)opt.update(obj,r,i);}
                r= window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
                r.onreadystatechange=f;
                r.open(m,u,1);
                r.setRequestHeader('content-type','text/xml');
                r.send('');
        },
        update:function(obj,r,i) {
                if(r.status==200){
                        obj.innerHTML=r.responseText;
                } else {obj.loaded=0;}
                     
				if(document.getElementById('crs_quigoMod')!=null)
				{
					var the_object;
					var quigo_id = document.getElementById('crs_quigoMod');
					var q_ad='';
					var myJSONObject = quigo_id.innerHTML;
					the_object = eval("(" + myJSONObject + ")");;
					

							if(the_object.response.statusCode!="200"){
							q_ad='<p class="dn">Quigo status code: '+the_object.response.statusCode+", status text: "+the_object.response.statusText+"</p>";
							}
							else{
								q_ad+='<H3 style="color:#000;font-weight:bold;width:auto;border-bottom:1px solid #eee;">Sponsored Links</H3>';
								

								

								var list_n=the_object.response.data.numResults;
								for(var k=0;k<list_n;k++)
								{
									q_ad+='<div class="sponser" style="float:left;width:388px;"><div class="sponpg"><div class="sponHdr" style="margin-top:.63em;font-size:13px;">';
									q_ad+='<a target="_blank" style="color:#2965AD;font-weight:bold;font-size:12px;" href="'+the_object.response.data.listing[k].targetUrl+'" title="'+the_object.response.data.listing[k].title+'">'+the_object.response.data.listing[k].title+'</a>';
									

									q_ad+='</div><div class="sponCont">';
									q_ad+='<a target="_blank" style="color:#000;margin-bottom:.5em;font-size:11px;" href="'+the_object.response.data.listing[k].targetUrl+'" title="'+the_object.response.data.listing[k].title+'">'+the_object.response.data.listing[k].description+'</a>';
									q_ad+='</div><div class="sponLnk">';
									q_ad+='<a target="_blank" style="color:#000;font-size:11px;margin-bottom:.5em;" href="'+the_object.response.data.listing[k].targetUrl+'" title="www.'+the_object.response.data.listing[k].domain+'">www.'+the_object.response.data.listing[k].domain+'</a></div>';
									q_ad+='</div></div>';
									

								}
								q_ad+='<div class="sponFtr" style="float:right;width:100px;"><a href="http://aol.adsonar.com/admin/advertisers/indexPl.jsp" target="_blank">Buy a link here</a></div>';
								quigo_id.style.display="block";
							}


					quigo_id.innerHTML=q_ad.trim();
					

				}
                
                if(document.getElementById('footerad')!=null)
					{
						if(set_ad==true)
						{
							var tmp=document.getElementById('footerad');
							 var x_ad = tmp.getElementsByTagName('script');   
							 var test="";
							 var srcobj=document.createElement('script');
							   for( var i=0; i < x_ad.length; i++)
							   {  
								     if(x_ad[i].src!="")
									 {
										
										srcobj.src=x_ad[i].src;
										if(is_IE)
										{
										eval(srcobj.src);
										}
										
									 }
									 if(x_ad[i].text!="")
									 {
										var txtobj=document.createElement('script');
										txtobj.text=x_ad[i].text;
										test+=txtobj.text;
										if(is_IE)
										{
										eval(txtobj.text);
										}
										
									 }
									 
							   }
							
							  if(!is_IE)
								{
								   var txtobj2=document.createElement('script');
								   txtobj2.text=test;
								   document.getElementById('footerad').innerHTML=null;;
								   document.getElementById('footerad').appendChild(srcobj);
								   document.getElementById('footerad').appendChild(txtobj2);
								}
							  
							set_ad=false;
						}
					
					}
					if (document.getElementById("cp-ads")) {
    					document.getElementById("cp-ad-" + Math.floor(Math.random() * 3)).style.display = "block";
					}

        },
        init:function() {
                        cod_arr=opt.gc('cod','div','',document);
                        docHg=w.innerHeight||d.documentElement.offsetHeight;
                        opt.doCod();
                        opt.ae(window,'scroll',opt.doCod);
        },
        res:function(){
                docHg=w.innerHeight||d.documentElement.offsetHeight;
                opt.doCod();
        }

}
opt.ae(window,'resize',opt.res);
opt.ae(window,'load',opt.init);

function countCartridges() {
   var cartCount = 0;
   end = false;
   do {
      if(document.getElementById('cart'+cartCount)) {
         cartCount++;
      } else {
         end = true;
      }
   } while (end == false)
   arrLen = cartCount;
}

function showCartridges( startId ) {
  if(arrLen == 0) {
    countCartridges(); 
  }
  var i,j;
  for (i=0,j=startId; i < modSpan; i++,j++) {
     var strCart = j%arrLen;
     document.getElementById('cartridge'+i).innerHTML = document.getElementById('cart'+strCart).innerHTML;
  }
}

function isModFetched (modId) {
  if(document.getElementById('cart'+modId).innerHTML.length>0) {
      return true;
  } else {
      return false;
  }
}

function nextCartridge () {
  if(arrLen == 0) {
     countCartridges(); 
  }
  currId = currId + 1;
  if ( !isModFetched( (currId+modSpan)%arrLen ) ) {
      fetchModule((currId+modSpan)%arrLen);
  }
  showCartridges(currId);
}

function prevCartridge () {
  if(arrLen == 0) {
    countCartridges(); 
  }  
  if(currId!=0) {
      currId = currId - 1
  } else {
      currId=arrLen-1;
  }

  if ( !isModFetched( (currId+modSpan)%arrLen ) ) {
      fetchModule((currId+modSpan)%arrLen);
  }
  showCartridges(currId);
}
// Crosspromo end



function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(leftNav);
addLoadEvent(ttComma);


function searchState(initDiv) {
		var tmpLst = document.getElementById(initDiv).getElementsByTagName('h4');	
		for (var i=0; i<tmpLst.length; i++) {
			tmpLst[i].onclick = search_tab_slctSte;
			tmpLst[i].onmouseover = searchHighLight;
			tmpLst[i].onmouseout = searchHighLightOff;			
		}	
}

function searchHighLight() { this.className = "hoveredOverTab"; }
function searchHighLightOff() { this.className = ''; }
function search_tab_slctSte() {

		var elemToKill = document.getElementById('selectedTab');
		var oldNodeTxt = document.getElementById('selectedTab').innerHTML;

		var newNode = document.getElementById('selectedTab').cloneNode(true);
		newH4 = document.createElement('h4');
		newH4.innerHTML = oldNodeTxt;
		
		this.parentNode.insertBefore(newH4, elemToKill);
		elemToKill.parentNode.removeChild(elemToKill)
		this.parentNode.insertBefore(newNode, this);
		this.parentNode.removeChild(this);
		newNode.innerHTML = this.innerHTML;
		searchState('searchTabs');
		searchb = this.innerHTML;
		bb_dosrchby('bb_topform',links);
		
}
function doSrchOmni(term,type){
    var s_account="aolsvc,aolmus"
   s_265.prop20=term;
    s_265.prop21=type+": clicked";
   s_265.t();
}
function bb_dosrchby(sFo, links) {
  eval('var sFormObj = document.'+ sFo);
  var searchQuery = sFormObj.query.value.toLowerCase();
  if(searchQuery == "" || searchQuery == defaultSrchTxtVal.toLowerCase()) return;
  // do srch
  doSrchOmni(searchQuery,searchb);
  var url = links[searchb.toLowerCase().replace(" ","")];
  url = url + escape(searchQuery).replace(/\+/g,'%2b');
  location.href = url;
}

var links = new Object;
var defaultSrchTxtVal = 'Search for artists, videos, songs and albums';
links.theboot = 'http://www.theboot.com/search/?q=';
links.music = 'http://search.music.aol.com/search/?query=';
links.web = 'http://search.aol.com/aol/search?invocationType=hdtheboot&query=';
links.images = 'http://search.aol.com/aolcom/image?invocationType=hdtheboot&query=';
links.video = 'http://video.aol.com/searchresults?query=';
links.news ='http://search.aol.com/aolcom/news?invocationType=hdtheboot&query=';
links.local = 'http://sbgw.search.aol.com/kw/exec?lookupType=11&category=fox&sourceType=416&text=&q= ';
searchb='theboot';


function searchStateF(initDivF) {
		var tmpLstF = document.getElementById(initDivF).getElementsByTagName('h4');	
		for (var i=0; i<tmpLstF.length; i++) {
			tmpLstF[i].onclick = search_tab_slctSteF;
			tmpLstF[i].onmouseover = searchHighLightF;
			tmpLstF[i].onmouseout = searchHighLightOffF;			
		}	
}

function searchHighLightF() { this.className = "hoveredOverTabF"; }
function searchHighLightOffF() { this.className = ''; }
function search_tab_slctSteF() {

		var elemToKillF = document.getElementById('selectedTabF');
		var oldNodeTxtF = document.getElementById('selectedTabF').innerHTML;
		var newNodeF = document.getElementById('selectedTabF').cloneNode(true);
		newH4F = document.createElement('h4');
		newH4F.innerHTML = oldNodeTxtF;
		this.parentNode.insertBefore(newH4F, elemToKillF);
		elemToKillF.parentNode.removeChild(elemToKillF)
		this.parentNode.insertBefore(newNodeF, this);
		this.parentNode.removeChild(this);
		newNodeF.innerHTML = this.innerHTML;
		searchStateF('searchTabsF');
		searchbF = this.innerHTML;
		bb_dosrchbyF('bb_topformF',linksF);
		
}
function bb_dosrchbyF(sFoF, linksF) {
  eval('var sFormObjF = document.'+ sFoF);
  var searchQueryF = sFormObjF.queryF.value.toLowerCase();
  if(searchQueryF == "" || searchQueryF == defaultSrchTxtValF.toLowerCase()) return;
  // do srch
  doSrchOmniF(searchQueryF,searchbF);
  var urlF = linksF[searchbF.toLowerCase().replace(" ","")];
  urlF = urlF + escape(searchQueryF).replace(/\+/g,'%2b');
  location.href = urlF;
}
function doSrchOmniF(term,type){
    var s_account="aolsvc,aolmus"
   s_265.prop20=term;
    s_265.prop21=type+": clicked";
   s_265.t();
}
var linksF = new Object;
var defaultSrchTxtValF = 'Search for artists, videos, songs and albums';
linksF.theboot = 'http://www.theboot.com/search/?q=';
linksF.music = 'http://search.music.aol.com/search/?query=';
linksF.web = 'http://search.aol.com/aol/search?invocationType=hdtheboot&query=';
linksF.images = 'http://search.aol.com/aolcom/image?invocationType=hdtheboot&query=';
linksF.video = 'http://video.aol.com/searchresults?query=';
linksF.news ='http://search.aol.com/aolcom/news?invocationType=hdtheboot&query=';
linksF.local = 'http://sbgw.search.aol.com/kw/exec?lookupType=11&category=fox&sourceType=416&text=&q= ';
searchbF='theboot';


function srchSub(ref) {
	var frm=p_o("search");
	var queryval = frm.aolMusicSearch.value.trim();
	queryval = queryval.replace( /\+/, " ");
	newurl = ref.href + queryval;
	window.open(newurl,'');
	return false;
}
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}
function leftNav(){
var myRegExp = /\.com\/tag\//;
    var artistNavMusicRef = document.getElementById("artistNavMusic");
	if (location.href.search(myRegExp) != -1 && artistNavMusicRef.getElementsByTagName("li")[0]){
		document.getElementById("artistNav").style.display="block";
		var navArtistHref = artistNavMusicRef.getElementsByTagName("li")[0].getElementsByTagName("a")[0].href;
		navArtistName = navArtistHref.replace(/http...music.aol.com.artist.([^/]+).[^/]+/g, "$1");
		var _navArtistName = navArtistName; 
		navArtistName = navArtistName.replace(/-/," ");
		
		var navArtistName = document.createTextNode(navArtistName);
		var navArtistNameH4 = document.createElement("H4");
		navArtistNameH4A = document.createElement("A");
		navArtistNameH4A.setAttribute("href", navArtistHref);
		navArtistNameH4A.appendChild(navArtistName);
		navArtistNameH4.appendChild(navArtistNameH4A);

		artistNavMusicRef.insertBefore(navArtistNameH4, artistNavMusicRef.firstChild);
		var spinartnavLi = artistNavMusicRef.getElementsByTagName("li");
		for(var b=0; b<spinartnavLi.length; b++){
			spinartnavLi[b].innerHTML = spinartnavLi[b].innerHTML.replace(/\(\)/g, "")
		}
		var songLyricLi = document.createElement("li");
		songLyricLi.innerHTML = '<a target="_blank" title=" Lyrics" href="http://www.metrolyrics.com/'+_navArtistName+'-lyrics.html">Song Lyrics</a>' ;
		spinartnavUl = artistNavMusicRef.getElementsByTagName("ul")[0];
		spinartnavUl.insertBefore(songLyricLi , spinartnavLi[parseInt(spinartnavLi.length/2)]);

		}
}
function displayLISelection(currLI) { 
	if (document.getElementById("tourTrackerWidget")){
		var ttWhen = document.getElementsByName("tb-when")[0];
		ttWhen.value = currLI.getElementsByTagName("A")[0].innerHTML;
		var ttWhen = getElementsByClassName(document, "div", "dropDownWhen")[0];
		ttWhen.style.display = "none";
	}
}
function showDropDown() {
	var ttWhen = getElementsByClassName(document, "div", "dropDownWhen")[0];
	ttWhen.style.display = "block";
}
function ttComma () {
var lstcommaReplace = document.getElementById("ttTopArtists").innerHTML.replace(/\,\s$/,'');
document.getElementById("ttTopArtists").innerHTML = lstcommaReplace;
}