if (!self.ScanScout) ScanScout = {};
if (!ScanScout.inPageUtils) {
ScanScout.inPageUtils = {};
ScanScout.adUnits = {};
    
/************************************************************************
** Standard In-Page Methods
**
*************************************************************************/
//Wrapper Method to auto-start in-page Photo Overlay if image URL data is defined
ScanScout.launchPhotoAds = function () {
	if (!self.ss_partnerId || !self.ss_photoUrls || ScanScout.isPhotoAdsRunning) return;

	for (var i=0; i < ss_photoUrls.length; i++) {
		var imgElement = ScanScout.inPageUtils.findImageTag(ss_photoUrls[i]);
		if (imgElement) {
            ScanScout.startAdOnImage(imgElement);
        }
	}

    ScanScout.isPhotoAdsRunning = true;
}

ScanScout.startAdOnImage = function (imageElement, ss_partnerId) {

	var params = {
		mediaElement: imageElement,        
		width: imageElement.width,
		height: imageElement.height,
		ss_mediaId: imageElement.src.toString(),
		ss_partnerId: ss_partnerId || self.ss_partnerId,
        ss_streamBlacklist : self.ss_streamBlacklist,
		ss_mediaUrl: imageElement.src.toString(),
        ss_mediaDescription: ''
	};
	
    	return new ScanScoutARE(params);

}

//In-Page Wrapper
ScanScout.startInpageAd = function (partnerId, adSize, adCode) {
    adSize = validateInpageDimensions( adSize );

    var size = adSize.split("x");

    var id = ScanScout.inPageUtils.getRandomNumber();

    document.write("<div id=\"ss_adPlacement_" + id + "\"></div>");

    var docTitle = '';
    try {
        docTitle = top.document.title;
    } catch (e) {
        docTitle = document.title;
    }

    var params = {
        mediaElement: document.getElementById("ss_adPlacement_" + id),
        width: size[0],
        height: size[1],
        ss_partnerId: partnerId,
        delayStart: true,
        ss_mediaId: ScanScout.inPageUtils.getFilteredPageUrl(),
        ss_mediaTitle: docTitle,
        id: id
    };

	if (self.ss_fireTPs) {
        params['ss_fireTPs'] = self.ss_fireTPs;
    }

    if (self.ss_adDataFile) {
        params['ss_adDataFile'] = self.ss_adDataFile;
    }

    if (self.ss_logURL) {
        params['ss_logURL'] = self.ss_logURL;
    }

    if (self.ss_noLog) {
        params['ss_noLog'] = self.ss_noLog;
    }

    var ad = new ScanScoutARE(params);
    ad.adMode = "banner";
    ad.baseAdHeight = size[1];
    ad.baseAdUrl = self.ss_baseAdUrl || "http://static.scanscout.com/ads/cip3.swf";
    if (adCode) {
        ad.fallbackAdCode = adCode;
    }
    ad.startAds();
}

/**
 * @param str	the string representing the width and height of the ad i.e) 300x250
 */
function validateInpageDimensions( str )
{
	var adSize = str;
	
	switch( adSize )
	{
		case "728x90" : 
			break;
		
		default:
			adSize = "300x250";
	}
	
	return adSize;
}


ScanScout.sendEvent = function (escapedEvent) {
    var event = unescape(escapedEvent);
	//This is a dispatcher that will send events to the right in-page object
    if (event.toString().indexOf("eventType") < 0) return;
    var eventObj = eval ('(' + event + ')');

    var ssARE = ScanScout.adUnits[eventObj.id];
    if (!ssARE) return; //Could not find the ARE instance to dispatch event to

    if (eventObj.eventType == "ss_hasAds") {
        if (ssARE.adShownCallback) ssARE.adShownCallback(eventObj.id);
        ssARE.expandAdContainer();
    } else if (eventObj.eventType == "ss_noAds") {
        if (ssARE.noAdsCallback) ssARE.noAdsCallback(eventObj.eventInfo);
        if (eventObj.defaultAd) ssARE.fallbackAdFromServer = eventObj.defaultAd; 
        ssARE.close();
    } else if (eventObj.eventType == "ss_bauClicked") {
        if (ssARE.adClickedCallback) ssARE.adClickedCallback();
    }

}

/**************************************************************************************************
** ScanScout In-Page ARE Object
**************************************************************************************************/
//Constructor
ScanScoutARE = function (params) {
	this.adMode = "overlay"; //future support for ticker

    this.mediaWidth = 0;
	this.mediaHeight = 0;
	this.topMargin = 0;
	this.rightMargin = 0;
	this.bottomMargin = 0;
	this.leftMargin = 0;

	//Intialize params if we have any
	if (typeof params == "string") this.init({mediaElementId: params});
    else if (typeof params == "object" && params.nodeType) this.init({mediaElement: params});
    else if (typeof params == "object") this.init(params);

	if (params && !params.delayStart) this.startAds();
}

ScanScoutARE.prototype.baseAdHeight = 65;
ScanScoutARE.prototype.baseAdUrl = "http://static.scanscout.com/ads/sip3.swf";

//Callbacks
ScanScoutARE.prototype.setNoAdsCallback = function (funcName) {this.noAdsCallback = funcName;}
ScanScoutARE.prototype.setAdShownCallback = function (funcName) {this.adShownCallback = funcName;}
ScanScoutARE.prototype.setAdClickedCallback = function (funcName) {this.adClickedCallback = funcName;}

ScanScoutARE.prototype.setAdDiv = function (divId) {this.adDivId = divId;}

//Media dimensions
ScanScoutARE.prototype.setMediaSize = function (width, height) {this.mediaWidth = width; this.mediaHeight = height;}

ScanScoutARE.prototype.setMargin = function (top, right, bottom, left) {
	//Follow CSS conventions on defining margins.
	this.topMargin = top;
	this.rightMargin = right || top;
	this.bottomMargin = bottom || rigth || top;
	this.leftMargin = left || right || top;
}

ScanScoutARE.prototype.init = function (params) {
    //Unique Ad Instance ID
    this.id = params.id ||params.ss_mediaId;
    
    //Media reference can be the html dom element or just the ID for the element
    this.mediaElement = params.mediaElement || this.mediaElement;
    this.mediaElementId = params.mediaElementId || this.mediaElementId;
    this.ss_pageUrl = ScanScout.inPageUtils.getFilteredPageUrl();

	//Display parameters
    this.adMode = params.adMode || this.adMode;
	this.mediaWidth = params.width || this.mediaWidth;
	this.mediaHeight = params.height || this.mediaHeight;
	this.topMargin = params.topMargin || params.margin || this.topMargin;
	this.rightMargin = params.rightMargin || params.margin || this.rightMargin;
	this.bottomMargin = params.bottomMargin || params.margin || this.bottomMargin;
	this.leftMargin = params.leftMargin || params.margin || this.leftMargin;
    this.adState = "collapsed";
	this.fireTPs = params.ss_fireTPs;
	
	//Callbacks
	this.noAdsCallback = params.noAdsCallback || this.noAdsCallback;
	this.adShownCallback = params.adShownCallback || this.adShownCallback;
	this.adClickedCallback = params.adClickedCallback || this.adClickedCallback;
	
	//ss_* Meta-data
	for (var prop in params) {
		if (prop.indexOf("ss_") <0) continue;
		this[prop] = params[prop];
	}

}

ScanScoutARE.prototype.startAds = function (params) {
    if (params) this.init(params);

    this.logInit();
    
	if (!this.verifyTags()) {
		if (this.noAdsCallback) this.noAdsCallback("Missing required Meta data tags");
		return;
	}

	if (ScanScout.adUnits[this.id]) {
		if (this.noAdsCallback) this.noAdsCallback("Ad unit already created for ID  " + this.id);
		return;
	}
	
	if (!this.mediaElement && !this.mediaElementId) {
		if (this.noAdsCallback) this.noAdsCallback("Could not find Media to overlay");
		return;
	}

    if (!this.mediaElement) this.mediaElement = document.getElementById(params.mediaElementId);

    if (this.mediaHeight == 0 || this.mediaWidth == 0) {
        //Try to auto detect media size
        this.mediaHeight = this.mediaElement.height;
        this.mediaWidth = this.mediaElement.width;
    }
    this.insertAdDiv();

	this.buildAdUnit();

    if (!ScanScout.inPageUtils.resizeHandlerLoaded) {
    ScanScout.inPageUtils.addEventListener(window, "resize", "onresize", ScanScout.inPageUtils.handleWindowResizeWrapper);
        ScanScout.inPageUtils.resizeHandlerLoaded = 1;
    }

    ScanScout.adUnits[this.id] = this;
}

ScanScoutARE.prototype.insertAdDiv = function () {
    var imgPos = ScanScout.inPageUtils.getElementPosition(this.mediaElement);    
    this.rndId = ScanScout.inPageUtils.getRandomNumber();
    var divId = "ss_ads_" + this.rndId;

    this.adDiv = document.createElement("div");
    this.adDiv.setAttribute ("id", divId);

    if (self.ss_useAbsolutePostioning || (this.mediaElement.style && this.mediaElement.style.position == "absolute") ||
        (this.mediaElement.getAttribute("align") && this.mediaElement.getAttribute("align") != "none") ||
        (this.mediaElement.style.cssFloat && this.mediaElement.style.cssFloat != "none") ||
        (this.mediaElement.style.styleFloat && this.mediaElement.style.styleFloat != "none")) {
	    this.adDiv.style.position = "absolute";
	    this.adDiv.style.top = (imgPos.top + this.mediaHeight) + 'px';
	    this.adDiv.style.left = imgPos.left + 'px';
    } else {
        this.adDiv.style.position = "relative";
	    this.adDiv.style.top = '0px';
	    this.adDiv.style.left = '0px';
    }

    this.adDiv.style.width = this.mediaWidth + 'px';
	this.adDiv.style.height = '1px'; // initially not visible
	this.adDiv.style.display = 'block';

    if (this.mediaElement.parentNode.tagName.toLowerCase() == "a") {
        this.mediaElement.parentNode.parentNode.insertBefore( this.adDiv, this.mediaElement.parentNode.nextSibling );
    } else {
        this.mediaElement.parentNode.insertBefore( this.adDiv, this.mediaElement.nextSibling );
    }
}

ScanScoutARE.prototype.buildAdUnit = function() {
	var baseXPos= (this.adMode == "overlay")?(-1 * (this.bottomMargin + this.baseAdHeight)) :0;
	var baseYPos = this.leftMargin;
	var adWidth = this.mediaWidth - this.leftMargin - this.rightMargin;

    var hasDefaultAd = (this.fallbackAdCode == null)?"0":"1";

    var isInIframe = true;
    try {if (top.location.href == window.location.href) {isInIframe = false;}}catch(e){};
	
	var flashVars = "ss_inPage=true" +
    "&id=" + encodeURIComponent(this.id) +
	"&ss_pageURL=" + encodeURIComponent(this.ss_pageUrl || "") +  //should be document.location.href
	"&ss_partnerId=" + encodeURIComponent((this.ss_partnerId != null) ? this.ss_partnerId : "") +
	"&ss_noLog="+ encodeURIComponent(this.ss_noLog || "") + 
	"&ss_mediaId=" + encodeURIComponent(this.ss_mediaId || "") +
	"&ss_mediaTitle=" + encodeURIComponent(ScanScout.inPageUtils.reduceNonvisibleChars(this.ss_mediaTitle || "")) +
	"&ss_mediaDescription=" + encodeURIComponent(ScanScout.inPageUtils.reduceNonvisibleChars(this.ss_mediaDescription || "")) +
	"&ss_mediaKeywords=" + encodeURIComponent(ScanScout.inPageUtils.reduceNonvisibleChars(this.ss_mediaKeywords || "")) +
	"&ss_mediaCategories=" + encodeURIComponent(ScanScout.inPageUtils.reduceNonvisibleChars(this.ss_mediaCategories || "")) +
	"&ss_mediaURL=" + encodeURIComponent(this.ss_mediaURL || "") +
	"&ss_adDataFile=" + encodeURIComponent(this.ss_adDataFile || this.ss_adDataXML || "") +
	"&ss_logURL=" + encodeURIComponent(this.ss_logURL || "") + 
	"&ss_width=" + adWidth +				// main video width (and width availble for banner)
	"&ss_height=" + this.baseAdHeight + 		// main BAU Height
	"&ss_adType=" + this.adMode +
    "&ss_mediaType=" + encodeURIComponent(ScanScout.inPageUtils.reduceNonvisibleChars(this.ss_mediaType || "")) +
    "&ss_streamBlacklist=" + encodeURIComponent(ScanScout.inPageUtils.reduceNonvisibleChars(this.ss_streamBlacklist || "")) +
    "&ss_hasDefaultAd=" +  hasDefaultAd +
    "&ss_isInIframe=" + isInIframe +
	"&ss_fireTPs=" + this.fireTPs || "";

    var baseAdUrl = ScanScout.inPageUtils.baseAdUrlOverride || this.baseAdUrlOverride || this.baseAdUrl;
	var baseAdUnitHTML = ScanScout.inPageUtils.embedSWF("ss_BAU_" + this.rndId, baseAdUrl, '100%', '100%', flashVars) ;
	
	var baseDiv = document.createElement('div');
	baseDiv.style.position = "absolute";
	baseDiv.style.left = '0px';
	baseDiv.style.top = '0px';
	baseDiv.style.height = '1px'
	baseDiv.style.border = '0px solid blue';
    this.adDiv.appendChild(baseDiv);

	var src =
		'<div id="ss_adsBase_' + this.rndId + '"' +
		'  style="position: relative; left: ' + baseYPos + 'px; top: ' + baseXPos + 'px;' +
		'    height: ' + this.baseAdHeight + 'px;' +
		'    width: ' + adWidth + 'px;' +
		'    backgroundImage: url(image/space.gif);' +
		'    background: transparent;' +
		'    border: 0px solid hotpink; display: block;">' +
			baseAdUnitHTML +
		'</div>';
		
	baseDiv.innerHTML = src;
    this.logEmbed();
}


ScanScoutARE.prototype.expandAdContainer= function() {
    if (this.adMode == "overlay") return; //overlays do not require the ad container to expand
    this.adDiv.style.height = this.baseAdHeight + 'px';
}

ScanScoutARE.prototype.close = function () {
    if (this.fallbackAdCode != null || this.fallbackAdFromServer != null) {
        this.showFallbackAd();
    } else {
        this.adDiv.parentNode.removeChild(this.adDiv);
        ScanScout.adUnits[this.id] = null;
    }
};

ScanScoutARE.prototype.showFallbackAd = function() {
    var adHtml = (this.fallbackAdCode != null)?this.fallbackAdCode:this.fallbackAdFromServer;
    if (adHtml.match(/^http/)) {
        var baseUrl = this.baseAdUrl.substring(0, this.baseAdUrl.lastIndexOf('/', this.baseAdUrl.indexOf('.swf')));
        adHtml = '<iframe src="' + baseUrl + '/dhtml/adServing/ssFallbackAd.html?scriptSrc=' + escape(adHtml) + '" scrolling="no" frameborder="no" height=250 width=300/>';
    }    
    this.adDiv.innerHTML = adHtml;
}

//Test for required meta data tags
ScanScoutARE.prototype.verifyTags = function () {
	if (!this.ss_mediaId)
		return false;
	else if (!this.ss_partnerId)
		return false;
	return true;
}

ScanScoutARE.prototype.updateLocation = function () {
	if (this.adDiv.style.position != "absolute") return;

    var imgPos = ScanScout.inPageUtils.getElementPosition(this.mediaElement);
	this.adDiv.style.top = (imgPos.top + this.mediaHeight) + 'px';
	this.adDiv.style.left = imgPos.left + 'px';
}

ScanScoutARE.prototype.expandBanner = function (left, top, width, height) {
    if (this.adMode !== "banner" || this.adState === "expanded") {
        return; //only supported for banner mode
    }

    var baseAd = document.getElementById("ss_adsBase_" + this.rndId );
    var adPos = ScanScout.inPageUtils.getElementPosition(baseAd);

    baseAd.style.left = adPos.left + left;
    baseAd.style.top = adPos.top + top;
    baseAd.style.width = width + 'px';
    baseAd.style.height = height + 'px';

    this.adState = "expanded";
}

ScanScoutARE.prototype.collapseExpandedBanner = function () {
    if (this.adMode !== "banner" || this.adState === "collapsed") {
        return; //only supported for banner mode
    }

    var baseAd = document.getElementById("ss_adsBase_" + this.rndId );

    baseAd.style.top = 0;
    baseAd.style.height = this.baseAdHeight;

    //Take margins into account
    baseAd.style.left = this.leftMargin;
    baseAd.style.width = this.mediaWidth - this.leftMargin - this.rightMargin;

    this.adState = "collapsed";
}

ScanScoutARE.prototype.logInit = function() {
    var targetUrl = 'http://dt.scanscout.com/ssframework/dt/pt.png?trackcd=1281112862090&pid=' + this.ss_partnerId;
    ScanScout.inPageUtils.logEvent(targetUrl);
}

ScanScoutARE.prototype.logEmbed = function() {
    var targetUrl = 'http://dt.scanscout.com/ssframework/dt/pt.png?trackcd=1281112955882&pid=' + this.ss_partnerId;
    ScanScout.inPageUtils.logEvent(targetUrl);
}

/************************************************************************
** Utility Methods
**
*************************************************************************/

ScanScout.inPageUtils.getFilteredPageUrl = function() {
    var url = "";
    try {
        if (top.location.href) {
            url = top.location.href;
        } else {
            url = ScanScout.inPageUtils.getPageDomain();
        }
    } catch(error) {
        url = ScanScout.inPageUtils.getPageDomain();
    }

    if (url.match(/doubleclick\.net/)) {
        url = "http://ad.doubleclick.net/dynamicpage.html";
    } else if (url.match(/yieldmanager\.com/)) {
        url = "http://ad.yieldmanager.com/dynamicpage.html";
    }

    return url;
}

ScanScout.inPageUtils.getPageDomain = function() {
    return "http://" + window.location.hostname;
}

ScanScout.inPageUtils.logEvent = function(targetUrl) {
    var el = new Image(1, 1);
    el.src = targetUrl;
    document.body.appendChild(el);
}

ScanScout.inPageUtils.findImageTag = function (imgUrl) {
	var imageElements = document.getElementsByTagName('img');
	var matches = new Array();
	
	for (i in imageElements) {
		var e = imageElements[i].src;
		if (e == undefined || e == null) {
			continue;
		}		
		if (ScanScout.inPageUtils.getFullUrl(e.toString()).toLowerCase() == ScanScout.inPageUtils.getFullUrl(imgUrl).toLowerCase()) {
			matches.push(imageElements[i]);
		}
	}
	
	if (matches.length == 1) {
		return matches[0];
	}
	
	return null;
}

//This will return the fully qualified url
ScanScout.inPageUtils.getFullUrl = function (url) {
	if (url.indexOf("http://") >= 0) return url;
	
	var pageUrl = ScanScout.inPageUtils.getPageUrl();
	
	if (url.indexOf("/") == 0) {
		//Just parse out the top level domain
		pageUrl = ScanScout.inPageUtils.getPageDomain();
	}

	return pageUrl + url;
}

ScanScout.inPageUtils.getPageUrl = function(testUrl) {
	var url = testUrl || window.location.toString();
	return url.substring(0, url.lastIndexOf('/')+1);
}

ScanScout.inPageUtils.getElementPosition = function (baseElem) {
	var offsetTrail = baseElem;
	var offsetLeft = 0;
	var offsetTop = 0;

	// account for IE 6 CSS compatibility mode
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
        if (offsetTrail && ScanScout.inPageUtils.getElementStyle(offsetTrail,"position", "position") == "relative") {
            offsetTrail = null;
        }
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	
	return ({top: offsetTop, left: offsetLeft});
}

ScanScout.inPageUtils.getElementStyle = function (elem, IEStyleProp, CSSStyleProp) {
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

// Reduce any consecutive characters outside of 32 - 126 to a single space
ScanScout.inPageUtils.reduceNonvisibleChars = function(sText)
{
	var sNewText = "";
	if(typeof sText == "string")
	{
		for (var i=0; i<sText.length; i+=1) 
		{
			var iCode = sText.charCodeAt(i);
			// to convert to entities, use "&#" + iCode + ";"
			sNewText += ((iCode < 32 || iCode > 126) ? ' ' : sText.charAt(i));
		}
		sNewText = sNewText.replace(/\s+/g, ' ');
	}
	return sNewText;
}

ScanScout.inPageUtils.embedSWF = function(id, url, width, height, flashvars)
{
	var obj = "";
	
	if (navigator.appName.indexOf("Microsoft") != -1) //IE
	{
		obj = '<object id="'+ id +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="border: 0px solid red; z-index: 1000001;" width="'+ width +'" height="'+ height +'">';
		obj += '<param name="movie" value="'+ url +'" />';
		obj += '<param name="allowScriptAccess" value="always" />';
		obj += '<param name="wmode" value="transparent" />';
		obj += '<param name="scale" value="noscale" />';
		obj += '<param name="salign" value="tl" />';
		if (flashvars) obj += '<param name="flashvars" value="'+ flashvars +'" />';
		obj += "</object>";
	}
	else
	{
		obj = '<embed id="'+id+'" name="' + id + '"';
		obj += ' src="' + url + '"';
		obj += ' type="application/x-shockwave-flash"';
		obj += ' pluginsPage="http://www.macromedia.com/go/getflashplayer"';
		obj += ' allowScriptAccess="always"';
		obj += ' wmode="transparent"';
		obj += ' scale="noscale" salign="tl"';
		obj += ' style="z-index: 10; border: 0px solid red"';
		obj += ' width="' + width + '"';
		obj += ' height="' + height + '"';
		if (flashvars) obj += ' flashVars="' + flashvars + '"';
		obj += '></embed>';
	}
	return obj;

}

ScanScout.inPageUtils.addEventListener = function (target, eventName, ieEventName, func) {
    if (window.addEventListener) target.addEventListener(eventName, func, false);
    else if (window.attachEvent) target.attachEvent(ieEventName, func);
}

ScanScout.inPageUtils.handleWindowResizeWrapper = function () {
    //This throttles the resize event
    clearTimeout(ScanScout.inPageUtils.resizeDelay);
    ScanScout.inPageUtils.resizeDelay = setTimeout("ScanScout.inPageUtils.handleWindowResize()", 10);
}

ScanScout.inPageUtils.handleWindowResize = function () {
    for (var ad in ScanScout.adUnits) {
        ScanScout.adUnits[ad].updateLocation();
    }    
}

ScanScout.inPageUtils.getRandomNumber = function(num) {
	num = num || 1000000000;
	return (Math.floor(Math.random() * num));
}

ScanScout.inPageUtils.getPageLocation = function (adId) {
     var ad = ScanScout.adUnits[adId];
     var adPosition = ScanScout.inPageUtils.getElementPosition(ad.adDiv);
     var browserDimensions = ScanScout.inPageUtils.getWindowDimensions();
     var fullPageDimensions = ScanScout.inPageUtils.getFullPageDimensions();

     var jsonString = '{' +
                 '"adLocation" : {' +
                     '"top": ' + adPosition.top + ', ' +
                     '"left": ' + adPosition.left + ', ' +
                     '"width": ' + ad.mediaWidth + ', ' +
                     '"height": ' + ad.mediaHeight +
                 '},' +
                 '"browserDimensions" : {' +
                     '"top": ' + browserDimensions.top + ', ' +
                     '"left": ' + browserDimensions.left + ', ' +
                     '"width": ' + browserDimensions.width + ', ' +
                     '"height": ' + browserDimensions.height +
                 '},' +
                 '"fullPageDimensions" : {' +
                     '"top": ' + fullPageDimensions.top + ', ' +
                     '"left": ' + fullPageDimensions.left + ', ' +
                     '"width": ' + fullPageDimensions.width + ', ' +
                     '"height": ' + fullPageDimensions.height +
                 '}}';	
     return jsonString;
}

ScanScout.inPageUtils.getWindowDimensions = function() {
    var wd = window.document;
    var height = 0;
    var width = 0;
    var top = 0;
    var left = 0;


    if (wd.body.clientHeight > 0) {
        height=wd.body.clientHeight;
    } else if (wd.documentElement.clientHeight>0) {
        height=wd.documentElement.clientHeight;
    } else if (wd.innerHeight>0) {
        height=wd.innerHeight;
    }

    if (wd.body.clientWidth>0) {
        width=wd.body.clientWidth;
    } else if (wd.documentElement.clientWidth>0) {
        width=wd.documentElement.clientWidth;
    } else if (wd.innerWidth>0) {
        width=wd.innerWidth;
    }

    var left = window.pageXOffset || document.body.scrollLeft;
	var top = window.pageYOffset || document.body.scrollTop;


    return {width: width, height: height, top: top, left: left};
}

ScanScout.inPageUtils.getFullPageDimensions = function () {
    var db = document.body;
    var de = document.documentElement;
    var pageHeight = Math.max(db.scrollHeight, de.scrollHeight, db.offsetHeight, de.offsetHeight, db.clientHeight, de.clientHeight);
    var pageWidth = Math.max( db.scrollWidth, de.scrollWidth, db.offsetWidth, de.offsetWidth, db.clientWidth, de.clientWidth);

    return {width: pageWidth, height: pageHeight, top: 0, left: 0};
}

ScanScout.inPageUtils.expandBanner  = function (adId, left, top, width, height) {
    var ad = ScanScout.adUnits[adId];
    if (ad) {
        ad.expandBanner(left, top, width, height);
        return 1;
    }

    return 0;


}

ScanScout.inPageUtils.collapseExpandedBanner = function(adId) {
    var ad = ScanScout.adUnits[adId];
    if (ad) {
        ad.collapseExpandedBanner();
        return 1;
    }
    return 0;

}

//Try to Auto-start photo ads
if (!self.ss_supressPhotoAdsAutoStart) ScanScout.inPageUtils.addEventListener(window, "load", "onload", ScanScout.launchPhotoAds);
}

if (self.ss_partnerId && self.ss_size && self.ss_mode == "inpage") ScanScout.startInpageAd(self.ss_partnerId, self.ss_size);

