if(!self.ScanScout)
{
	ScanScout = {};
}

if(!ScanScout.AdUtils)
{
	ScanScout.AdUtils = {};
    if (navigator.appName.indexOf("Microsoft") != -1) ScanScout.AdUtils.browser = "IE";
}


ScanScout.AdUtils.showCompanionBannerHTML = function(adId, html, errorUrl, parentId)
{
	if(!html || !adId)
	{
		return;
	}
	
	var refId = parentId || "ssCompanionBanner";
	
	var locationRef = document.getElementById(refId);
	if(!locationRef)
	{
        self.ss_companionAd = html;
        if (errorUrl) {
            var img = new Image(1,1);
            img.src = errorUrl;
            return;
        }

	}
	
	//TODO: see if needed to handle HTTP vs HTTPS replacements
	
	
	locationRef.innerHTML = html;
}

if (!self.JSON) {
    self.JSON = {};
}
if (!self.JSON.parse) {
    self.JSON.parse = function(expr) {
        eval ('(' + expr + ')'); //No native JSON parser.
    }
}

/**
* invoked from the ARE. Attempts to inject a full browser div into the
* DOM and loads our external ad unit into it.
* 
* @param adWrapperURL	the url of the ExternalAdWrapper to load into the div. The ARE will be responsible for appending necessary query params to the URL
*
* @returns	success (1|0)
*/	
ScanScout.AdUtils.showFullScreenUnit = function( adWrapperURL, config )
{
	ScanScout.AdUtils.isIECSSCompat = (ScanScout.AdUtils.doc.compatMode && ScanScout.AdUtils.doc.compatMode.indexOf("CSS1") >= 0) ? true : false;

    var configObj = JSON.parse(config);

    if (configObj && configObj.abtest) {
        ScanScout.AdUtils.fsMode = configObj.abtest;
    }

    //Create top level lightbox
	var el = ScanScout.AdUtils.doc.createElement("div");
	el.id = "ss_inPageARE";
    el.style.position = 'absolute'
    el.style.top = '0px';
    el.style.left = '0px';
    ScanScout.AdUtils.doc.body.appendChild(el);

    ScanScout.AdUtils.createIframeShim();
    el.appendChild(ScanScout.AdUtils.createLightBox());
    ScanScout.AdUtils.createFullScreenAd(adWrapperURL);
    ScanScout.AdUtils.addEventListener(ScanScout.AdUtils.window, "resize", "onresize", ScanScout.AdUtils.onWindowResize)
	
	return 1;
}

ScanScout.AdUtils.showExternalUnit = function(adWrapperURL, config) {
    //Just call showFullScreen for now. ExternalUnit = Expanded IN Page Banner
    var isFsCapable = ScanScout.AdUtils.isFullScreenCapable();
    if (isFsCapable == 1) {
        ScanScout.AdUtils.showFullScreenUnit(adWrapperURL, config);
    }

    return isFsCapable;
}

ScanScout.AdUtils.createFullScreenAd = function(adWrapperURL) {
    var adDiv = ScanScout.AdUtils.doc.createElement("div");
    adDiv.id = "ss_adDiv";
    adDiv.style.position = 'absolute'
    adDiv.style.zIndex = 1000001;

    ScanScout.AdUtils.setAdSize(adDiv);

    adDiv.innerHTML = ScanScout.AdUtils.embedSWF("ssFullScreenUnit", adWrapperURL ,'100%', '100%');
	ScanScout.AdUtils.doc.getElementById('ss_inPageARE').appendChild(adDiv);
}

ScanScout.AdUtils.setAdSize = function(divElement) {
    //Find page dimensions
    var top = ScanScout.AdUtils.window.window.pageYOffset || ScanScout.AdUtils.doc.body.scrollTop || 0;
    var left = ScanScout.AdUtils.window.window.pageXOffset || ScanScout.AdUtils.doc.body.scrollLeft || 0;
    
    divElement.style.top = top + 'px';
	divElement.style.left= left + 'px';
	divElement.style.width =  ScanScout.AdUtils.getVisibleWindowWidth() + 'px';
    divElement.style.height = ScanScout.AdUtils.getVisibleWindowHeight() + 'px';

}

ScanScout.AdUtils.createIframeShim = function () {
    var shim = ScanScout.AdUtils.doc.createElement("iframe");
    shim.id="ssIframeShim";
    shim.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
    shim.style.position="absolute";
    shim.style.zIndex=1000000-1;
    shim.style.width = ScanScout.AdUtils.getFullPageWidth() + 'px';
    shim.style.height = ScanScout.AdUtils.getFullPageHeight() + 'px';
    shim.style.top=0;
    shim.style.left=0;
    shim.setAttribute("frameborder", 0);
    shim.setAttribute("src", "javascript:void(0);");

    var el = ScanScout.AdUtils.doc.getElementById('ss_inPageARE');
    if (el) {
     el.appendChild(shim);
    }
}


ScanScout.AdUtils.createLightBox = function () {
    //Create the lightbox area next
	var el = ScanScout.AdUtils.doc.createElement("div");
	el.id = "ss_lightBox";
    el.style.opacity = .75;
    el.style.filter = 'alpha(opacity="75")';
	el.style.backgroundColor = "#606060";
    el.style.position = 'relative'
    el.style.zIndex = 1000000;
    el.style.display = 'block';
    el.style.top = '0px';
    el.style.left = '0px';
    el.style.width = ScanScout.AdUtils.getFullPageWidth() + 'px';
    el.style.height = ScanScout.AdUtils.getFullPageHeight() + 'px';

    return el;
}

ScanScout.AdUtils.embedSWF = function(id, url, width, height) 
{
	var obj = "";

    var wmode = "transparent";
	
	if (ScanScout.AdUtils.browser == "IE")
	{
		obj = '<object id="'+ id +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="border: 0px solid red; z-index: 1000001;" width="100%" height="100%">';
		obj += '<param name="movie" value="'+ url +'" />';
		obj += '<param name="allowScriptAccess" value="always" />';
		obj += '<param name="wmode" value="' + wmode + '" />';
		obj += '<param name="scale" value="noscale" />';
		obj += '<param name="salign" value="tl" />';
		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="' + wmode + '"';
		obj += ' scale="noscale" salign="tl"';
		obj += ' style="height: 100%; width: 100%; z-index: 1000001;"';
		obj += '></embed>';
	}
	
	setTimeout( function(){ if (ScanScout.AdUtils.doc.getElementById(id)) ScanScout.AdUtils.doc.getElementById(id).focus() }, 10 );
	
	return obj;
}

ScanScout.AdUtils.closeFullScreenView = function () 
{
	var el = ScanScout.AdUtils.doc.getElementById("ssFullScreenUnit");
	if (el) el.parentNode.removeChild(el);
	
	el = ScanScout.AdUtils.doc.getElementById('ss_inPageARE');
	if (el) el.parentNode.removeChild(el);

    el = ScanScout.AdUtils.doc.getElementById("ssIframeShim");
    if (el) el.parentNode.removeChild(el);
}

ScanScout.AdUtils.closeExternalUnit = function() {
    ScanScout.AdUtils.closeFullScreenView();
}

ScanScout.AdUtils.onWindowResize = function (event) {
    clearTimeout(ScanScout.AdUtils.resizeDelay);
    ScanScout.AdUtils.resizeDelay=setTimeout("ScanScout.AdUtils.handleResize()", 10);
}

ScanScout.AdUtils.handleResize = function () {
    //Resize the lightbox
    var lbDiv = ScanScout.AdUtils.doc.getElementById("ss_lightBox");
    if (lbDiv) {
        lbDiv.style.width = ScanScout.AdUtils.getFullPageWidth() + 'px';
        lbDiv.style.height = ScanScout.AdUtils.getFullPageHeight() + 'px';
    }

    //Resize the player div
    var adDiv = ScanScout.AdUtils.doc.getElementById("ss_adDiv");
    if (adDiv) {
        ScanScout.AdUtils.setAdSize(adDiv);
    }

}

ScanScout.AdUtils.isFullScreenCapable = function ()
{
    try {
        if(top.location.href) {
            
            return 1;
        }
        return 0;
    } catch (ex) {
        return 0;
    }
}

ScanScout.AdUtils.getVisibleWindowWidth = function ( ) {
	if (ScanScout.AdUtils.window.innerWidth)
	{
		return ScanScout.AdUtils.window.innerWidth;
	} 
	else if (ScanScout.AdUtils.isIECSSCompat) 
	{
		return ScanScout.AdUtils.doc.body.parentElement.clientWidth;
	} 
	else if (ScanScout.AdUtils.doc.body && ScanScout.AdUtils.doc.body.clientWidth)
	{
		return ScanScout.AdUtils.doc.body.clientWidth;
	}
	
	return null;
}

ScanScout.AdUtils.getVisibleWindowHeight = function ( ) {
	if (ScanScout.AdUtils.window.innerHeight)
	{
		return ScanScout.AdUtils.window.innerHeight;
	} 
    else if ((ScanScout.AdUtils.doc.body.parentNode.clientHeight > 0) && (ScanScout.AdUtils.doc.body.clientHeight != ScanScout.AdUtils.doc.body.parentNode.clientHeight))
    {
        return ScanScout.AdUtils.doc.body.parentNode.clientHeight;
    }

    return ScanScout.AdUtils.doc.body.clientHeight;

}

ScanScout.AdUtils.getFullPageHeight = function () {
    var db = ScanScout.AdUtils.doc.body;
    var de = ScanScout.AdUtils.doc.documentElement;
    return Math.max(db.scrollHeight, de.scrollHeight, db.offsetHeight, de.offsetHeight, db.clientHeight, de.clientHeight);
}

ScanScout.AdUtils.getFullPageWidth = function () {
    var db = ScanScout.AdUtils.doc.body;
    var de = ScanScout.AdUtils.doc.documentElement;
    return Math.max( db.scrollWidth, de.scrollWidth, db.offsetWidth, de.offsetWidth, db.clientWidth, de.clientWidth);
}

ScanScout.AdUtils.getWindowDimensions = function() {
    var wd = ScanScout.AdUtils.doc;

    var height = ScanScout.AdUtils.getVisibleWindowHeight();
    var width = ScanScout.AdUtils.getVisibleWindowWidth();

    var left = ScanScout.AdUtils.window.pageXOffset || wd.body.scrollLeft;
    var top = ScanScout.AdUtils.window.pageYOffset || wd.body.scrollTop;


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

ScanScout.AdUtils.getFullPageDimensions = function () {
    var pageHeight = ScanScout.AdUtils.getFullPageHeight();
    var pageWidth = ScanScout.AdUtils.getFullPageWidth();

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

ScanScout.AdUtils.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.AdUtils.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;
    }

    var width = ScanScout.AdUtils.getElementStyle(baseElem, "width", "width");
    var height = ScanScout.AdUtils.getElementStyle(baseElem, "height", "height");

    return ({top: offsetTop, left: offsetLeft, width: width, height: height});
}

ScanScout.AdUtils.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 "";
}

ScanScout.AdUtils.toJSONForFlash = function (obj) {
    var jsonString = "{";
    for (var key in obj) {
        jsonString += "'" + key + "': '" + obj[key] + "'";
    }
    jsonString += "}";

    return jsonString;
}

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

try{
    if (top.location.href != window.location.href) {
        ScanScout.AdUtils.doc = top.document;
        ScanScout.AdUtils.window = top;
    } else {
        ScanScout.AdUtils.doc = window.document;
        ScanScout.AdUtils.window = window;
    }
} catch(ex) {
    ScanScout.AdUtils.doc = window.document;
    ScanScout.AdUtils.window = window;
}

