// Browser Detection Utility
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function ads_pdResize() { 

	for(var x=0;x<ads_expObjArray.length;x++){
		if(ads_expObjArray[x].pageLoaded && ads_expObjArray.pdAutoExpand){

			var key = ads_expObjArray[x].objKey;

			// Retrieve expand Obj to access all data
			var expandObj = adsPD_getObj(key);
			
			// Retrieve Base Panel DIV Element to control it
			var baseDiv = document.getElementById(expandObj.adsExpand_pdDivName);
			
			// find left mouse position
			var ads_baseXleft = adsPD_getX(baseDiv);	
		
			// find top mouse position by adding the top offset to the Base panel y position
			var ads_baseYtop = adsPD_getY(baseDiv);

			// Get the Floating Div by its id name	
			var adPDSlide = document.getElementById(expandObj.adsExpand_pdDivName + "Float");
			
			// Reposition only if this is Expand Method A
			if(expandObj.expandMethod=='a'){
				// Reposition the Div correctly
				adPDSlide.style.top = ads_baseYtop + "px";
				adPDSlide.style.left = ads_baseXleft + "px";
			}

		}
	}
}

// Add window resize event handler
window.onresize = ads_pdResize;

// UTILITY TO RETURN EXPAND OBJECT
function adsPD_getObj(objKey){
	for (adsExpand_tempX = 0; adsExpand_tempX < ads_expObjArray.length; adsExpand_tempX++){
		if(ads_expObjArray[adsExpand_tempX].objKey == objKey)
			return ads_expObjArray[adsExpand_tempX];
	}
}

// UTILITY TO RETURN EXPAND ARRAY POSITION
function adsPD_getObjNode(objKey){
	for (adsExpand_tempX = 0; adsExpand_tempX < ads_expObjArray.length; adsExpand_tempX++){
		if(ads_expObjArray[adsExpand_tempX].objKey == objKey)
			return adsExpand_tempX;
	}
}
// UTILITY TO RETURN FLASH OBJECT REFERENCE BASED ON BROWSER TYPE //

function getMovieName(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	} else {
		return document[movieName]
	}
}

// X,Y LOCATION DETECTION UTILITIES
function adsPD_getY(element){
	var y = 0;
	for (var e = element; e; e = e.offsetParent) // Iterate the offsetParents
		y += e.offsetTop;

	for (var e = element.parentNode; e && e !=document.body; e = e.parentNode) // Iterate the parent Nodes
		if (e.scrollTop) y -= e.scrollTop; 	// subtract scrollbar values

	//This is the Y coordinate with the doucment-internal scrolling accounted for.
	return y;
}

function adsPD_getX(element){
	var x = 0;
	for (var e = element; e; e = e.offsetParent) // Iterate the offsetParents
		x += e.offsetLeft;

	for (var e = element.parentNode; e && e !=document.body; e = e.parentNode) // Iterate the parent Nodes
		if (e.scrollLeft) x -= e.scrollLeft; 	// subtract scrollbar values

	//This is the Y coordinate with the doucment-internal scrolling accounted for.
	return x;
}

// AUTO-COLLAPSE FUNCTIONS //
function adsPD_checkMouse(){
	// Do Not add handler if creative is on auto expand
	if(!adsPD_gAutoExpandBool){	
		// CHECK MOUSE (make sure the user hasn't left the hotspot while interacting with a rollover expand)
		Handler.add(window.document.body,"mousemove", adsPD_getCoords);
	}
}

// CHECK DELAY (make sure the user hasn't left the hotspot within 1 second)
function adsPD_checkDelay(){
	Handler.remove(window.document.body, "mousemove", adsPD_getDelayCoords);
	Handler.add(window.document.body,"mousemove", adsPD_getDelayCoords);
}

// MOUSE DETECTION FUNCTIONS //
// GET COORDINATES FOR ROLLOVER AUTO-COLLAPSE
function adsPD_getCoords(e){
	// Remove any old handlers before adding new one
	Handler.remove(window.document.body, "mousemove", adsPD_getCoords);	
	// The event object holds the mouse position in window coordinates
	// Convert these to document coordinates using the Geometry module
	var x = e.clientX + Geometry.getHorizontalScroll();
	var y = e.clientY + Geometry.getVerticalScroll();
	adsPD_checkCoords(x,y);
}

// GET COORDINATES FOR ROLLOVER 1 SEC DELAY
function adsPD_getDelayCoords(e){
	// Remove any old handlers before adding new one
	Handler.remove(window.document.body, "mousemove", adsPD_getDelayCoords);
	// The event object holds the mouse position in window coordinates
	// Convert these to document coordinates using the Geometry module
	var x = e.clientX + Geometry.getHorizontalScroll();
	var y = e.clientY + Geometry.getVerticalScroll();
	// Set the Coords of current mouse position
	adsPD_gDelayRecentX = x;
	adsPD_gDelayRecentY = y;
}

// PUSHDOWN
// MOUSE X,Y COORDS CHECK FUNCTIONS
// CHECK COORDINATES FOR ROLLOVER AUTO-COLLAPSE
function adsPD_checkCoords(x,y){

	var collapseBase = false;
	var collapseExpand = false;

	// get expandObj to read its hotspot coordinates
	var expandObj = adsPD_getObj(adsPD_globalCurrentKey);

	if(!((x >= expandObj.ads_baseXleft) && (x <= expandObj.ads_baseXright)) || !((y >= expandObj.ads_baseYtop) && (y <= expandObj.ads_baseYbottom)))
		collapseBase = true;
		
	if(!((x >= expandObj.ads_expandXleft) && (x <= expandObj.ads_expandXright)) || !((y >= expandObj.ads_expandYtop) && (y <= expandObj.ads_expandYbottom)))
		collapseExpand = true;	
				
	if(collapseBase && collapseExpand)	
		cbsiPushup();
}

// CHECK COORDINATES FOR ROLLOVER 1 SEC DELAY
function pdCheckDelayCoords(){

	// Remove the mouse checker for 1 second delay
	window.clearInterval(adsPD_gDelayTimerInterval);

	// Rollover has finished after the 1 second delay
	adsPD_gRolloverActive = false;
		
	// get Base panel as an object to determine its position on the page	
	var expandObj = adsPD_getObj(adsPD_globalCurrentKey);

	if(((adsPD_gDelayRecentX >= expandObj.ads_baseXleft) && (adsPD_gDelayRecentX <= expandObj.ads_baseXright)) && ((adsPD_gDelayRecentY >= expandObj.ads_baseYtop) && (adsPD_gDelayRecentY <= expandObj.ads_baseYbottom))){
		cbsiPushdownDelay();
	}

}
// END AUTO-COLLAPSE AND MOUSE DETECTION SCRIPT //


//CHECK FOR SWF - WHEN FOUND KICK START THE PUSHDOWN
function ads_PDCheckSWF(){
	if(ads_swfReady){
		// SEND AUTO PUSHDOWN SIGNAL TO MOVIE
		// Create a fast Interval to look for the Expand Movie, if movie is found the command will be sent
		adsPD_gCheckTimerInterval=window.setInterval("adsPD_checkExpand('callCBSIAutoPushdown')", 10);	
		window.clearInterval(adsPD_gSWFCheckerInterval);
	}
}

//CHECK FOR EXPAND
function adsPD_checkExpand(flashFunction){

	// Get the current expand object
	adsPD_gExpandObjNode = adsPD_getObjNode(adsPD_globalCurrentKey);

	// Put the expand action call in an error catching loops to avoid potential errors
	try{
		switch(flashFunction){
			case "callCBSIAutoPushdown":
				getMovieName(ads_expObjArray[adsPD_gExpandObjNode].adsExpand_expandName).callCBSIAutoPushdown();
				adsPD_gExpandChecker=true;
				//console.debug(flashFunction);
				break;
			case "callCBSIPushup":
				// send the Expand Panel Flash Object the command to Expand
				getMovieName(ads_expObjArray[adsPD_gExpandObjNode].adsExpand_expandName).callCBSIPushup();
				adsPD_gExpandChecker=true;
				//console.debug(flashFunction);
				break;
			case "callCBSIPushdownComplete":
				// send the Expand Panel Flash Object the command to Expand
				getMovieName(ads_expObjArray[adsPD_gExpandObjNode].adsExpand_expandName).callCBSIPushdownComplete();
				adsPD_gExpandChecker=true;
				//console.debug(flashFunction);
				break;
			case "callCBSIPushupComplete":
				// send the Expand Panel Flash Object the command to Expand
				getMovieName(ads_expObjArray[adsPD_gExpandObjNode].adsExpand_expandName).callCBSIPushupComplete();
				adsPD_gExpandChecker=true;
				//console.debug(flashFunction);
				break;
		}
		
	}
	catch(e){
		// If expand action call fails then set the checker to false and try again
		adsPD_gExpandChecker=false;
	}
	finally{
		// increase count to restrict number of loops the interval will make
		adsPD_gExpandCheckerCount++;
	}

	//If expand action call was successful or the Interval has looped too many times kill the Interval loop
	if(adsPD_gExpandCheckerCount>25 || adsPD_gExpandChecker==true){
		window.clearInterval(adsPD_gCheckTimerInterval);
		adsPD_gExpandCheckerCount=0;
	}

}

function vertPush(){
	if(!adsPD_gExpandState){

		// Determine what Next Pixel Distance will be
		adsPD_gVPNext = adsPD_gVPDistance + adsPD_gVertPixPerPush;
		
		// Determine what temporary height of DIV element will be when Next Pixel is added to it
		adsPD_gVPTempHeight = adsPD_gBaseHeight + adsPD_gVPNext;
		
		// If the distance traveled is greater than the total distance
		if(adsPD_gVPDistance > adsPD_gVertDistance){
			// Set temporary height equal to the base height plus total distance
			adsPD_gVPTempHeight = adsPD_gBaseHeight + adsPD_gVertDistance;
			// Make DIV element equal to that height via CSS
			document.getElementById(adsPD_gVPName).style.height=adsPD_gVPTempHeight+'px';
			document.getElementById(adsPD_gVPFloatName).style.height=adsPD_gVPTempHeight+'px';
			// Stop the Interval calling the Vert Push function
			clearInterval(adsPD_gExpandTimerHandler);

			// SEND PUSHDOWN COMPLETE SIGNAL TO MOVIE
			// Create a fast Interval to look for the Expand Movie, if movie is found the command will be sent
			adsPD_gCheckTimerInterval=window.setInterval("adsPD_checkExpand('callCBSIPushdownComplete')", 10);			

			// Take Expands Off Hold
			adsPD_gOnHold = false;
			// Mark Expand as open
			adsPD_gExpandState = true;

		}else{
			document.getElementById(adsPD_gVPName).style.height=adsPD_gVPTempHeight+'px';
			document.getElementById(adsPD_gVPFloatName).style.height=adsPD_gVPTempHeight+'px';
			adsPD_gVPDistance = adsPD_gVPNext;
		}
	}
}

function vertPull(){

	if(adsPD_gExpandState){
		
		// Determine what Next Pixel Distance will be
		adsPD_gVPNext = adsPD_gVPullDistance + adsPD_gVertPixPerPull;

		// Determine what temporary height of DIV element will be when Next Pixel is subtracted from it
		adsPD_gVPTempHeight = adsPD_gFullHeight - adsPD_gVPNext;

		if((adsPD_gVPullDistance + adsPD_gVertPixPerPull) > adsPD_gVertDistance){

			// Set temporary height equal to the base height
			adsPD_gVPTempHeight = adsPD_gBaseHeight;
			// Make DIV element equal to that height via CSS
			document.getElementById(adsPD_gVPName).style.height=adsPD_gVPTempHeight+'px';
			document.getElementById(adsPD_gVPFloatName).style.height=adsPD_gVPTempHeight+'px';
			
			// Stop the Interval calling the Vert Pull function
			clearInterval(adsPD_gCollapseTimerHandler);

			// SEND PUSHUP COMPLETE SIGNAL TO MOVIE
			// Create a fast Interval to look for the Expand Movie, if movie is found the command will be sent
			adsPD_gCheckTimerInterval=window.setInterval("adsPD_checkExpand('callCBSIPushupComplete')", 10);	
						
			// Take Expands Off Hold
			adsPD_gOnHold = false;
			// Mark Expand as closed
			adsPD_gExpandState = false;
			// Take Rollover Expands off hold
			adsPD_gRolloverActive = false;
			
		} else {		
			document.getElementById(adsPD_gVPName).style.height=adsPD_gVPTempHeight+'px';
			document.getElementById(adsPD_gVPFloatName).style.height=adsPD_gVPTempHeight+'px';			
			adsPD_gVPullDistance = adsPD_gVPNext;
		}
	}
}

function cbsiPushdown(a, b) {

if(!adsPD_gRolloverActive && !adsPD_gAutoExpandBool){

	// Detect Version of IE
	version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	
	// New Component Tracking Class passes 1 variable - key
	if(arguments.length==1){
		var key = a;
		var expand = null;
	// Old Tracking Class passes expand and key
	} else {
		var expand = a;
		var key = b;
	}
	
	// Retrieve expand Obj to access all data
	var expandObj = adsPD_getObj(key);
	
	// Retrieve or set Expand and Collapse Types
	if ( typeof(expandObj.adsExpand_expandType) == "undefined" ) {
		if(expand==null){
			expandObj.adsExpand_expandType = "click";
		} else {
			expandObj.adsExpand_expandType = expand;	
		}
	}		
	
	if ( typeof(expandObj.adsExpand_collapseType) == "undefined" ) {
		if(expand==null){
			expandObj.adsExpand_collapseType = "click";
		} else if(expand=="rollover") {
			expandObj.adsExpand_collapseType = "rolloff";
		} else {
			expandObj.adsExpand_collapseType = "rolloff";
		}
	}
	
	
	// Do Not Initiate Expand unless (adsPD_gOnHold) No other Expand is currently animating and (adsPD_gExpandState) Current Expand is not open
	if(!adsPD_gOnHold && !adsPD_gExpandState && !adsPD_gAutoExpandBool){
	
		// Set Current expand key
		adsPD_globalCurrentKey = key;
	
		// Base Height
		adsPD_gBaseHeight = expandObj.adsExpand_pdBaseHeight;

		// Retrieve Push Down DIV Element to control it
		var pdDiv = document.getElementById(expandObj.adsExpand_pdDivName);
		
		// DETERMINE BASE HOTSPOT COORDINATES
		// find left mouse position
			expandObj.ads_baseXleft = adsPD_getX(pdDiv);
		// find right mouse position by adding the base width to the left position 
			expandObj.ads_baseXright = expandObj.ads_baseXleft + expandObj.pdFullWidth;
		// find top mouse position by adding the top offset to the Base panel y position
			expandObj.ads_baseYtop = adsPD_getY(pdDiv);
		// find bottom mouse position by adding the expand height to the top position 
			expandObj.ads_baseYbottom = expandObj.ads_baseYtop + expandObj.adsExpand_pdBaseHeight;
									
		// DETERMINE EXPAND HOTSPOT COORDINATES
		// find left mouse position by adding the left offset to the Base panel x position
			expandObj.ads_expandXleft = adsPD_getX(pdDiv);
		// find right mouse position by adding the Expand width to the left position 
			expandObj.ads_expandXright = expandObj.ads_baseXleft + expandObj.pdFullWidth;			
		// find top mouse position by adding the top offset to the Base panel y position
			expandObj.ads_expandYtop = adsPD_getY(pdDiv);
		// find bottom mouse position by adding the expand height to the top position 
			expandObj.ads_expandYbottom = expandObj.ads_baseYtop + expandObj.adsExpand_pdFullHeight;				
		
		//alert(expandObj.ads_baseXleft + ', ' + expandObj.ads_baseXright + ', ' + expandObj.ads_baseYtop + ', ' + expandObj.ads_baseYbottom);

		// if the expand action is a mouse rollover, add 1 second delay
		if(expandObj.adsExpand_expandType=="rollover"){
			adsPD_gRolloverActive = true;
			// Begin Auto-Detection for Delay
			adsPD_gDelayRecentX = expandObj.ads_baseXleft;
			adsPD_gDelayRecentY = expandObj.ads_baseYtop;
			adsPD_gDelayTimerInterval=window.setInterval(adsPD_checkDelay, 50);
			setTimeout(pdCheckDelayCoords, 1000);
		} else {
			//launch Pushdown Expand by invoking setInterval on vertPush function
			adsPD_gExpandTimerHandler = setTimeout(cbsiPushdownDelay, 0);
		}

	}
}
}

// EXPAND DELAY (the expand actions triggered after the initial delay)
function cbsiPushdownDelay(){

	// Detect Version of IE
	version = parseFloat(navigator.appVersion.split("MSIE")[1]);

	// Retrieve expand Obj to access all data
	var expandObj = adsPD_getObj(adsPD_globalCurrentKey);
	adsPD_gExpandObjNode = adsPD_getObjNode(adsPD_globalCurrentKey);
	
	// set global Expand state to open
	expandObj.expandState=true;	

	// Put other expands on hold till this one finishes
	adsPD_gOnHold = true;	
	
	// Reset the current distance to 0 for new expand
	adsPD_gVPDistance = 0;

	// Set the Global Close AutoTimer
	adsPD_CloseTimer=expandObj.pdAutoExpandTimer;

	// Find total distance to expand
	adsPD_gVertDistance = expandObj.adsExpand_pdFullHeight - expandObj.adsExpand_pdBaseHeight;

	// Find Number of Pixels to move per second.  Multiply expand length by 1% to convert milliseconds to seconds
	adsPD_gVertDistancePerSecond = Math.round(adsPD_gVertDistance / (expandObj.adsExpand_expandLength * 0.001));
		
	adsPD_gVertPixPerPush = Math.round(adsPD_gVertDistancePerSecond / expandObj.adsExpand_FPS);
	
	// Determine Interval for Vert Push function in milliseconds to equal out to FPS rate
	adsPD_gVertPushTimer =  Math.round(1000 / expandObj.adsExpand_FPS);
	
	//launch Pushdown Expand by invoking setInterval on vertPush function
	adsPD_gExpandTimerHandler = setInterval(vertPush, adsPD_gVertPushTimer);	

	// reload tracking gif to track the expand action
	document.getElementById(expandObj.impTrack_name).src=expandObj.expandImpTrack;
	
	// If 3rd Party Expand Trackign supported
	if(expandObj.expandImpTrack3P){
		// reload tracking gif to track the expand action
		document.getElementById(expandObj.impTrack_name3P).src=expandObj.expandImpTrack3P;
	} 
	
	// Initiate default mouse detection routine during expand
	if(expandObj.adsExpand_collapseType=="rolloff"){

		// Set a Rolloff Key for Auto-Collapse function
		ads_globalRolloffKey = adsPD_globalCurrentKey;

		// Set the Boolean to mark that Auto-Collapse is active
		adsPD_gRolloverActive = true;

		// Begin Auto-Collapse
		adsPD_gTimerInterval=window.setInterval(adsPD_checkMouse, 10);
	}
}

function cbsiPushup(){

	// get pushdown div as an object and as a node position to determine its position on the page	
	var expandObj = adsPD_getObj(adsPD_globalCurrentKey);
	var expandObjNode = adsPD_getObjNode(adsPD_globalCurrentKey);
	adsPD_gExpandObjNode = adsPD_getObjNode(adsPD_globalCurrentKey);
	
	// Set Global Vars for Timeout scope
	adsPD_gCollapseObjNode = expandObjNode;
	
	// IF MOVIE IS EXPANDED
	if(adsPD_gExpandState){
		// Stop the mouse detection routine
		if(expandObj.adsExpand_collapseType=="rolloff"){
			// Set the Boolean to mark that Auto-Collapse is not active
			adsPD_gRolloverActive = false;
			// Clear Auto-Collapse function
			window.clearInterval(adsPD_gTimerInterval);		
		}
		
		// Kill Auto Collapse if called beforehand
		clearTimeout(adsPD_CloseTimerHandler);
		
		// SEND PUSHUP SIGNAL TO MOVIE
		// Create a fast Interval to look for the Expand Movie, if movie is found the command will be sent
		adsPD_gCheckTimerInterval=window.setInterval("adsPD_checkExpand('callCBSIPushup')", 10);
		
		// adsPD_gBaseHeight and gVertDistance should already be set
		adsPD_gFullHeight = expandObj.adsExpand_pdFullHeight;
				
		// Find total distance to expand
		adsPD_gVertDistance = expandObj.adsExpand_pdFullHeight - expandObj.adsExpand_pdBaseHeight;	

		// Set the Pull Distance equal to the Full Height
		adsPD_gVPullDistance = 0;

		// Set Expand DIV z-index to 9999 for proper layering
		//document.getElementById(expandObj.adsExpand_pdDivName).style.zIndex=1000;
		//document.getElementById(adsPD_gVPFloatName).style.zIndex = 100;	
	
		// Find total distance to collapse
		adsPD_gVertDistance = expandObj.adsExpand_pdFullHeight - expandObj.adsExpand_pdBaseHeight;

		// Find Number of Pixels to move per second.  Multiply expand length by 1% to convert milliseconds to seconds
		adsPD_gVertDistancePerSecond = Math.round(adsPD_gVertDistance / (expandObj.adsExpand_collapseLength * 0.001));
			
		adsPD_gVertPixPerPull = Math.round(adsPD_gVertDistancePerSecond / expandObj.adsExpand_FPS);
		
		// Determine Interval for Vert Push function in milliseconds to equal out to FPS rate
		adsPD_gVertPullTimer =  Math.round(1000 / expandObj.adsExpand_FPS);
		
		//launch Pulldown Collapse by invoking setInterval on vertPull function
		adsPD_gCollapseTimerHandler = setInterval(vertPull, adsPD_gVertPullTimer);

		if(!adsPD_gAutoExpandBool){
			// Record Collapse impression
			document.getElementById(expandObj.impTrack_name).src=expandObj.collapseImpTrack;		
	
			// If 3rd Party Expand Trackign supported
			if(expandObj.collapseImpTrack3P){
				// reload tracking gif to track the expand action
				document.getElementById(expandObj.impTrack_name3P).src=expandObj.collapseImpTrack3P;
			} 		
		}
		
		// Always set Auto Expand Bool to false to enable roll-off collapses after auto expand
		adsPD_gAutoExpandBool	= false;
		
		// Take other expands off hold
		adsPD_gOnHold = false;
		
	}	
}

function cbsiTrackingLoaded(){
	ads_swfReady = true;
}

	//GLOBAL VARS
	// stores value for Auto Collapse timer
	var adsPD_CloseTimer;
	// Auto Collapse Handler
	var adsPD_CloseTimerHandler;
	// stores value for Base Height
	var adsPD_gBaseHeight;
	// stores value for Full Height
	var adsPD_gFullHeight;
	// Auto-Close Trigger for Full and Auto Expands
	var adsPD_gAutoExpandBool = false;
	// Boolean for all occurances of Expand. False is closed, True is open.
	var adsPD_gExpandState = false;
	// Global Var to put all expand actions on hold until the current Expand Animation is done animating
	var adsPD_gOnHold = false;
	// Boolean to put expand actions on hold while one expand is still active
	var adsPD_gRolloverActive = false;
	// Check to see if Expand Movie is available yet
	var adsPD_gExpandChecker = false;
	var adsPD_gExpandCheckerCount = 0;
	// VERT PUSH GLOBALS - Change with each call of Vert Push
	var adsPD_gVPDistance;
	var adsPD_gVPullDistance;
	var adsPD_gVPNext;
	var adsPD_gVPTempHeight;
	var adsPD_gVPName;
	var adsPD_gVPFloatName;	
	// Global to Hold the current expand's Vertical Distance of travel on expand
	var adsPD_gVertDistance;
	// Global to hold pixels per second of expand
	var adsPD_gVertDistancePerSecond;
	// Global to hold number of pixels to move per call of VertPush
	var adsPD_gVertPixPerPush;
	// Global to hold number of pixels to move per call of VertPull
	var adsPD_gVertPixPerPull;	
	// Global to hold millisecond intervals of FPS
	var adsPD_gVertPushTimer;
	// Global to hold millisecond intervals of FPS
	var adsPD_gVertPullTimer;
	// Global to check if SWF movie is ready to recieve signals
	var ads_swfReady = false;
	//Handlers to kill setInterval timer routines
	var adsPD_gExpandTimerHandler;
	var adsPD_gCollapseTimerHandler;
	var adsPD_gCheckTimerInterval;
	var adsPD_gDelayTimerInterval;
	// Boolean for all occurances of Expand. False is closed, True is open.
	var adsPD_gExpandState = false;
	// Boolean to put expand actions on hold while one expand is still active
	var adsPD_gRolloverActive = false;
	// Remember the Node position of most recent expand
	var adsPD_gExpandObjNode;
	// Remember the Node position for collapse
	var adsPD_gCollapseObjNode;
	// Global for Delay Mouse Cursor - Most Recent Position
	var adsPD_gDelayRecentX;
	var adsPD_gDelayRecentY;
	// Remember the key of most recent expand
	var adsPD_globalCurrentKey;
	// Remember the Rolloff key of most recent rollover expand
	var ads_globalRolloffKey;
	// Check to see if Expand Movie is available yet
	var adsPD_gExpandChecker = false;
	var adsPD_gExpandCheckerCount = 0;
	// Global Timer Vars
	var adsPD_gTimerInterval;
	var adsPD_gSWFCheckerInterval;
	var adsPD_gDivHeight;

// Launch Pushdown Initiation	
function ads_goPD(o){
	// Set the current expand Div Name globally for VertPush and Pull methods
	adsPD_gVPName = o.adsExpand_pdDivName;
	// Define Expand Method for this Instance
	// a. after page load event attach expand div as nearest possible child to Body element
	// b. on ad call attach expand div as direct child of base div
	// c. Friendly IFrame Solution
	
	// Current Default Expand Method is 'a'
	o.expandMethod = "a";
	
	// Create Array of domains that will automatically convert to Expand Method "b"
	// c13-chd-dev1.cnet.com - Urban Baby testing
	// urbanbaby.com - Urban Baby
	// uat.cnet.com - Redball Dev
	// cnet.com - CNET
	// theinsider.com - The Insider
	// lastfm.com - last.fm
	var domainList = ["c13-chd-dev1.cnet.com", "urbanbaby.com", "uat.cnet.com", "cnet.com", "theinsider.com", "last.fm"];	
	var adsDomain = document.domain;
	for(var x=0; x<domainList.length; x++){
		if(adsDomain.indexOf(domainList[x])!=-1){		
			o.expandMethod = "b";
		}
	}
		
	// Backdoor key to force an Expand Method
	if(o.expandMethodForce!=undefined)
			o.expandMethod = o.expandMethodForce;
	
	// Set Frequency Capping
	if(o.pdFreqCap==undefined)
		o.pdFreqCap = 1;		
	// Set Z-Index
	if(o.pdZindex==undefined)
		o.pdZindex = 100;		
	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {		
		// Get Flash Version
		var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		// Check for Flash and Cookie Support
		if(hasRightVersion && navigator.cookieEnabled ) {  // if we've detected an acceptable version and has cookies enabled
			// Retrieve expand Obj to access all data
			var expandObj = adsPD_getObj(o.objKey);
			// Set global current key equal to current expand
			adsPD_globalCurrentKey = o.objKey;
			// Boolean to see if user has been served a Pushdown
			o.ads_serveUser = true;
			// Create the cookie for Pushdown Limiter
			var cookie = new expandObj.Cookie("adsPDLimit"+o.objKey);
			// Find the Domain of the Current Site
			var dm = document.domain.split('.');
			var ckAttr = dm[dm.length-2]+'.'+dm[dm.length-1];
			// Check for existing cookie Timestamp
			if (!cookie.pdTimestamp) {
				// if not, set one
				var ads_pdNow = new Date(); // Create an object holding the current date and time
				cookie.pdTimestamp = ads_pdNow.getTime();												
				cookie.pdVisits = 1;
				// Store the cookie data
				cookie.store(1,"/",ckAttr);
				// Set Expand Var for Flash Movie to autopushdown
				o.adsExpand_flashVarsBase = o.adsExpand_flashVarsBase + "&expandType=autopushdown";
				// If this is an AutoPushdown, set the Takeover Variable for CBS Sports
				// Detect for cbsi global var
				if(typeof(cbsiadGlobal)=="undefined")
					cbsiadGlobal = {};
				// set Takeover var to 1
				cbsiadGlobal.has_takeover=1;				
			// If there is a Cookie set already
			} else {
				// Get the Current Timestamp
				var ads_pdNow = new Date(); // Create an object holding the current date and time				
				// See how much time has passed since original Timestamp and compare that number to the creative's cookie Timer
				//console.debug("Cookie Timestamp is: " + cookie.pdTimestamp);
				// Cancel Pushdown if less time has passed
				//console.debug("Time difference is: " + ((ads_pdNow.getTime() - cookie.pdTimestamp)/(1000*60*60)));				
					cookie.pdVisits++;
				if(((ads_pdNow.getTime() - cookie.pdTimestamp)/(1000*60*60))<o.pdAutoCookieTimer && cookie.pdVisits>o.pdFreqCap){
					o.ads_serveUser = false;					
					// Or reset Timestamp, run pushdown
				} else {
					//console.debug("time is more, serve again");
					//Get Current Time
					cookie.pdTimestamp = ads_pdNow.getTime();
					//console.debug("Cookie Timestamp is: " + cookie.pdTimestamp);
					// Set Expand Var for Flash Movie to autopushdown
					o.adsExpand_flashVarsBase = o.adsExpand_flashVarsBase + "&expandType=autopushdown";
					// If this is an AutoPushdown, set the Takeover Variable for CBS Sports
					// Detect for cbsi global var
					if(typeof(cbsiadGlobal)=="undefined")
						cbsiadGlobal = {};
					// set Takeover var to 1
					cbsiadGlobal.has_takeover=1;						
				}
				// Store the cookie data
				cookie.store(1,"/",ckAttr);
			}

			//If Full Auto Expand and cookie timer is true, make the DIV Height equal to the full height
			if(o.pdAutoExpand == "full" && o.ads_serveUser){
				// Set DIV element height
				adsPD_gDivHeight = o.adsExpand_pdFullHeight;
				// Set Global Base Height
				adsPD_gBaseHeight = o.adsExpand_pdBaseHeight;				
		
				// Otherwise make DIV Height equal to base height
			} else {
				adsPD_gDivHeight = o.adsExpand_pdBaseHeight;	
			}
			
			// Write the Content Onto Page
			document.write('<div style="width:100%; text-align:center;"><div id="'+o.adsExpand_pdDivName+'" style="width:'+o.pdFullWidth+'px; height:'+o.adsExpand_pdBaseHeight+'px; margin:0px auto; text-align:left; overflow:hidden">');	

			// embed the flash movie
			/*AC_FL_RunContent(
						'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
						'width', o.pdFullWidth,
						'height', o.adsExpand_pdFullHeight,
						'src', o.adsExpand_pdFlashPath,
						'quality', 'high',
						'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
						'align', 'middle',
						'play', 'true',
						'loop', 'true',
						'scale', 'showall',
						'wmode', 'transparent',
						'devicefont', 'false',
						'id', o.adsExpand_expandName,
						'bgcolor', o.adsExpand_bgcolorBase,
						'FlashVars', o.adsExpand_flashVarsBase,
						'name', o.adsExpand_expandName,
						'menu', 'true',
						'allowFullScreen', 'false',
						'allowScriptAccess','always',
						'movie', o.adsExpand_pdFlashPath,
						'salign', ''
			); //end AC code */
			
			if(o.adsExpand_pdFlashPath.indexOf(".swf") == -1){
				o.adsExpand_pdFlashPath = o.adsExpand_pdFlashPath+".swf";
			}			
			if(BrowserDetect.OS.indexOf("Mac") != -1 && BrowserDetect.browser.indexOf("Firefox") != -1){
				o.ads_expandHTML = '<embed scale="noscale" src="'+o.adsExpand_pdFlashPath+'" wmode="transparent" quality="high" bgcolor="'+o.adsExpand_bgcolorExpand+'" width="'+o.pdFullWidth+'" height="100%" salign="tl" allowFullScreen="false" name="'+o.adsExpand_expandName+'" align="top" allowScriptAccess="always" FlashVars="'+o.adsExpand_flashVarsBase+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
			} else if(BrowserDetect.browser == "Firefox"){
				o.ads_expandHTML = '<embed name="'+o.adsExpand_expandName+'" src="'+o.adsExpand_pdFlashPath+'" wmode="transparent" quality="high" bgcolor="'+o.adsExpand_bgcolorExpand+'" width="'+o.pdFullWidth+'" height="'+o.adsExpand_pdFullHeight+'" salign="tl" scale="noscale" allowFullScreen="false" align="top" allowScriptAccess="always" FlashVars="'+o.adsExpand_flashVarsBase+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
			} else {
				o.ads_expandHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+o.pdFullWidth+'" height="'+o.adsExpand_pdFullHeight+'" id="'+o.adsExpand_expandName+'" align="top"><param name="allowScriptAccess" value="always" /><param name="FlashVars" value="'+o.adsExpand_flashVarsBase+'" /><param name="movie" value="'+o.adsExpand_pdFlashPath+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><param name="scale" value="showall"><param name="allowFullScreen" value="false"></object>';			
			}
	
			// Add Tracking GIF
			document.write('<img src="'+ o.flashImpTrack +'" alt="" border="0" height="1" width="1" name="'+o.impTrack_name+'" id="'+o.impTrack_name+'" />');
			// If Third Party Tracking GIF
			if(o.ads3PflashImpTrack!=""){
				document.write('<img src="'+ o.flashImpTrack3P +'" alt="" border="0" height="1" width="1" name="'+o.impTrack_name3P+'" id="'+o.impTrack_name3P+'" />');
			}
			// Close Divs
			document.write('</div></div>');

			// For Expand Method A wait till page load event to write expand panel	
			if(expandObj.expandMethod=='a'){
				o.runOnLoad(cbsiRunPushdown);
			// For Expand Method B wite expand panel when ad call executes
			} else {
				cbsiRunPushdown();
			}
			
		// Default GIF
		} else {  // flash is too old or we can't detect the plugin
			document.write(o.defaultGIF);  // insert non-flash content
		}
	}
}

function cbsiAutoCloser(){
	adsPD_CloseTimerHandler = setTimeout(cbsiPushup, adsPD_CloseTimer);
}

function cbsiPushdownFullExpandAuto(){

	var key = ads_globalCurPDArray[0];

	// Retrieve expand Obj to access all data
	var expandObj = adsPD_getObj(key);	
	
	document.getElementById(adsPD_gVPFloatName).style.height=expandObj.adsExpand_pdFullHeight+'px';
	
	// Set DIV height equal to full expand on auto expand
	document.getElementById(adsPD_gVPName).style.height=expandObj.adsExpand_pdFullHeight+'px';
	
	// Set Auto Collapse Trigger
	adsPD_gAutoExpandBool = true;				
	// Set Expanded state as "true"
	adsPD_gExpandState = true;		
	// Set up loop to check for loading of SWF movie
	adsPD_gSWFCheckerInterval=window.setInterval(ads_PDCheckSWF, 100);
	
	// reload tracking gif to track the Auto Expand
	document.getElementById(expandObj.impTrack_name).src=expandObj.autoExpandImpTrack;

	// If 3rd Party Auto Expand Tracking supported
	if(expandObj.autoExpandImpTrack3P){
		// reload tracking gif to track the expand action
		document.getElementById(expandObj.impTrack_name3P).src=expandObj.autoExpandImpTrack3P;
	} 	

}

function cbsiRunPushdown(){

	var key = ads_globalCurPDArray.shift();
	ads_globalCurPDArray.push(key);	
	
	// tempObj is a placeholder for inserting the expand layer
	var tempObj;
	// find either the first available DIV object or the first node
	for(var x=0; x<document.body.childNodes.length; x++){	
		if((document.body.childNodes[x].nodeName)=='DIV'){
			tempObj=document.body.childNodes[x];
			break;
		} else {
			tempObj=document.body.firstChild;
		}	
	}	
	
	// Retrieve expand Obj to access all data
	var expandObj = adsPD_getObj(key);
	
	// Set the current expand Div Name globally for VertPush and Pull methods
	adsPD_gVPFloatName = expandObj.adsExpand_pdDivName + "Float";
	
	// Retrieve Base Panel DIV Element to control it
	var baseDiv = document.getElementById(expandObj.adsExpand_pdDivName);
	
	var attachObj = document.getElementById(expandObj.adsExpand_pdDivName);
	
	// find left mouse position
	var ads_baseXleft = adsPD_getX(baseDiv);	

	// find top mouse position by adding the top offset to the Base panel y position
	var ads_baseYtop = adsPD_getY(baseDiv);	
		
	// create a new div object to hold the expand	
	adPDSlide=document.createElement('div');

	if(expandObj.expandMethod == "a"){
		// Insert AdPDSlide expand panel before the first DIV or child node found
		document.body.insertBefore(adPDSlide,tempObj);	
		// assign its unique id
		adPDSlide.id = adsPD_gVPFloatName;
		// Set CSS Styles
		adPDSlide.style.position = "absolute";
		adPDSlide.style.padding = "0px";
		adPDSlide.style.margin = "0px";
		adPDSlide.style.overflow = "hidden";
		adPDSlide.style.top = ads_baseYtop + "px";
		adPDSlide.style.left = ads_baseXleft + "px";
	} else {
		// Insert AdPDSlide directly to the base div
		attachObj.appendChild(adPDSlide);
		// assign its unique id
		adPDSlide.id = adsPD_gVPFloatName;
		// Set CSS Styles
		adPDSlide.style.position = "absolute";
		adPDSlide.style.padding = "2px 0px 2px 0px";
		adPDSlide.style.margin = "0px";
		adPDSlide.style.overflow = "hidden";	
	}		

	adPDSlide.style.width = expandObj.pdFullWidth+"px";
	adPDSlide.style.height = expandObj.adsExpand_pdBaseHeight+"px";
	adPDSlide.style.zIndex = expandObj.pdZindex;
	// Fill the Div with the expand Flash object
	adPDSlide.innerHTML = expandObj.ads_expandHTML;

	// Lauch Auto-Expand & Auto-Collapse
	// Only auto-expands for "full" are supported currently	
	if(expandObj.pdAutoExpand=="full" && expandObj.ads_serveUser) {
		// Auto Expand
		expandObj.runOnLoad(cbsiPushdownFullExpandAuto);
		// Set the Global Close AutoTimer
		adsPD_CloseTimer=expandObj.pdAutoExpandTimer;
		// Auto Collapse
		expandObj.runOnLoad(cbsiAutoCloser);
	}			


}

function ads_pdRuntimeCode(){
	if(ads_globalCurPDArray.length!=0){
		// Get Expand Object from Global Array 'ads_expObjArray'
		for(x=0;x<ads_expObjArray.length;x++){
			if(ads_expObjArray[x].objKey==ads_globalCurPDArray[(ads_globalCurPDArray.length-1)]){
				ads_goPD(ads_expObjArray[x]);		
			}
		}
	}
}

// run the Runtime Code
ads_pdRuntimeCode();