
var introClose = 12000;  // time to close auto-expand 

function closeIndexHeaderAd(m) {    
	document.getElementById('adInsertion_SponsoredHeader').style.display='none'; 
	}

function ads_XMLReqObj() {
	this.initXMLReqObj();
	this.targetElementID = '';
	this.DEAD = false;
}

ads_XMLReqObj.prototype.initXMLReqObj = function () {
	if (window.XMLHttpRequest) {
		this.xmlClient = new XMLHttpRequest();
		if (!this.xmlClient) {
			this.initFailure();
		}
	}
	else if(window.ActiveXObject){
		this.xmlClient = new ActiveXObject("MsXml2.XmlHttp");
		if (!this.xmlClient) {
			this.initFailure();
		}
	}
	else {
		this.initFailure();
	}
}

ads_XMLReqObj.prototype.initFailure = function() {
	this.DEAD = true;
	this.xmlClient = null;
	//alert("***\n\nYour browser does not properly support the XMLHttpRequest Object.\n\nYou will not be able to use the full functionality of this page.\n\n***");
}

ads_XMLReqObj.prototype.processReqStateChange = function() {
	

	if (this.xmlClient.readyState == 4) {
		if (this.xmlClient.status == 200) {			
			var xmlDocument = this.xmlClient.responseXML;
 			//Error parsing XML - display error
			//if(xmlDocument.parseError.errorCode!=0) {
			//	alert("Error parsing XML: " + xmlDocument.parseError.reason + "Source text: " + xmlDocument.parseError.srcText);
			//}
			for (var i=0; i<xmlDocument.getElementsByTagName('InsertionType').length; i++){
					var insertionType = xmlDocument.getElementsByTagName('InsertionType').item(i).firstChild.data;
					//alert(insertionType);
				if(xmlDocument.getElementsByTagName('Ad').item(i).firstChild==null) {
					//alert(this.xmlClient.responseText);
					//this.setContent(insertionType, "No ad returned for insertion type " + insertionType);
				} else {
					var adData = xmlDocument.getElementsByTagName('Ad').item(i).firstChild.data;
					//alert(this.xmlClient.responseText);
					//alert(adData);
				  	this.setContent(insertionType, adData);
				}
			  
			}
			
			//APPEND ANY LOGGING
			for(var j=0; j<xmlDocument.getElementsByTagName('Log').length; j++) {
				if(xmlDocument.getElementsByTagName('Log').item(j).getAttribute("src")) {
					var s = document.createElement('script');
					s.src = xmlDocument.getElementsByTagName('Log').item(j).getAttribute("src");
					s.type = 'text/javascript';
					document.body.appendChild(s); 
				}
			}
			
			this.xmlClient = null;
		}
		else {
			this.setContent("There was a problem retrieving the XML data:\n" + this.xmlClient.statusText);
		}
	}
}

ads_XMLReqObj.prototype.getFile = function(URL) {
	var _this = this;
	if (this.xmlClient && !this.DEAD) {
		this.xmlClient.onreadystatechange = function () {
			_this.processReqStateChange();
		}
		this.xmlClient.open("GET", URL, true);
		this.xmlClient.send(null);
	}
	else if(this.DEAD) {
		this.initFailure();
	}
	else {
		this.initXMLReqObj();
		this.getFile(URL);
	}
}

ads_XMLReqObj.prototype.setContent = function(type, content) {

	var idPrefix = "adInsertion_";
	// var idPrefix = "";
	var elementId = idPrefix + type;
		
	if ( type == "SponsoredByLogoHeader" || type == "SponsoredByLogoHeader2" ) {
		var s=SSB.flashVars;
		s=s+content;
		SSB.flashVars=s;
		SSB.targetElement="mainHeader";
		SSB.useDOM=true;
		SSB.render(true);
    	} else if (document.getElementById(elementId)) {        
    		document.getElementById(elementId).innerHTML=content;        
    		if(type=="SponsoredHeader"){            
    			document.getElementById(elementId).style.display='block';            
    			autoclose=setTimeout("closeIndexHeaderAd('notrack');", introClose);        
    			}    
    		} 
	var sn=type+"JS";
	if (content && content.indexOf(sn)>1){ 
		var sc=document.getElementById(sn);
		if ( sc != null && sc.src != null && sc.src.length > 0) {
			var ns = document.createElement('script');
			ns.src = sc.src;
			ns.type = 'text/javascript';
			document.getElementById(elementId).appendChild(ns); 	
		} else {
			eval(document.getElementById(sn).innerHTML);
		}
	}
}

//var ads_xmlobj = new ads_XMLReqObj();
//xmlobj.overrideMimeType("text/xml");

//This will be called from an onclick to get and display new content in the target DIV
function insertAds(URL) {
	var canGetXML = (document.URL.indexOf('http://games.espn.go.com/') == 0);
	if (canGetXML) {
		new ads_XMLReqObj().getFile(URL);
		return false;
	}	
}

// var ads_thisURI = document.URL.replace('http://games.espn.go.com', '');
// Behaviour.addLoadEvent(function() { insertAds('http://games.espn.go.com/DynamicXMLAd?url=' + ads_thisURI + 'srvc=sz&itype=Banner&itype=Poster'); });
// Behaviour.start();
/*****
END AJAX ADS
*****/
