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;
}

/**
* 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.isNN4 = (document.layers) ? true : false;
	ScanScout.AdUtils.isIECSSCompat = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;

    var configObj = eval ('(' + config + ')');

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

    //Set all IFrames, Objects, and Embeds to no visibility
    ScanScout.AdUtils.hideInteractivePageElements();

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

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

ScanScout.AdUtils.createFullScreenAd = function(adWrapperURL) {
    var adDiv = document.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%');
	document.getElementById('ss_inPageARE').appendChild(adDiv);
}

ScanScout.AdUtils.setAdSize = function(divElement) {
    //Find page dimensions
    var top = window.window.pageYOffset || document.body.scrollTop || 0;
    var left = window.window.pageXOffset || document.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.createLightBox = function () {
    //Create the lightbox area next
	var el = document.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 (document.getElementById(id)) document.getElementById(id).focus() }, 10 );
	
	return obj;
}

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

    //Restore visibility to any IFrame, Embed, or Object on the page
    ScanScout.AdUtils.restoreHiddenPageElements();
}

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 = document.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 = document.getElementById("ss_adDiv");
    if (adDiv) {
        ScanScout.AdUtils.setAdSize(adDiv);
    }

}

ScanScout.AdUtils.isFullScreenCapable = function ()
{
	if(top.location.href!= window.location.href) {return 0;}

    //Don't need to test Object/embeds unless user is IE
    if (ScanScout.AdUtils.browser != "IE") {return 1;}

	//Look for all embeds and test for wmode
	var embeds = document.getElementsByTagName('embed');
	for (var i=0; i< embeds.length; i++) {
		if (ScanScout.AdUtils.isInWindowMode(embeds[i])) {
			return 0;
		}
	}

	var objectDivs = document.getElementsByTagName('object');
	for (var i=0; i< objectDivs.length; i++) {
		if (ScanScout.AdUtils.isInWindowMode(objectDivs[i])) {
			return 0;
		}
	}


	return 1;
}

ScanScout.AdUtils.isInWindowMode = function (el) {
	var wmode = "";

	if (el.tagName.toLowerCase() === "embed") {
		wmode = el.getAttribute('wmode').toLowerCase();
	}

	if (el.tagName.toLowerCase() === "object") {
		var wmode = el['wmode'];
		if (!wmode) {
            var childNodes = el.childNodes || el.children;
            wmode = ScanScout.AdUtils.getWmodeFromObjectChildren(childNodes)
		}
	}

	return (!wmode || wmode.toLowerCase() === "window")?1:0;
}

ScanScout.AdUtils.getWmodeFromObjectChildren = function (childElements) {
    var wmode = "";

    if (!childElements) {
    	return wmode;
    }

    for (var i=0; i < childElements.length; i++) {
        var param = childElements[i];
        if (param.name.toLowerCase() === "wmode") {
            wmode = param.value;
        }
    }
    return wmode;
}

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

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

    return document.body.clientHeight;

}

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

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

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

ScanScout.AdUtils.hideInteractivePageElements = function () {
    ScanScout.AdUtils.hideElements("iframe");
    ScanScout.AdUtils.hideOverflowDivs();

    //Don't hide object/embeds for IE
    if (ScanScout.AdUtils.browser == "IE") {return;}

    ScanScout.AdUtils.hideElements("embed");
    ScanScout.AdUtils.hideElements("object");    
}

ScanScout.AdUtils.hideElements = function(tagName) {
    var elements = document.getElementsByTagName(tagName);
    if (!ScanScout.AdUtils.hiddenElements) {
        ScanScout.AdUtils.hiddenElements = [];
    }
    if (elements) {
        var length = elements.length;
        for (var i = 0; i < length; i++) {
            var el = elements[i];
            var visibility = ScanScout.AdUtils.getCssStyleForElement(el, "visibility", "visibility") || "visible";
            ScanScout.AdUtils.hiddenElements.push({element: el, visibility: visibility});
        }

        for (var i = 0; i < length; i++) {
            var el = elements[i];
            el.style.visibility = "hidden";
        }
    }
}

ScanScout.AdUtils.hideOverflowDivs = function() {
    if (!ScanScout.AdUtils.overflowDivs) {
        ScanScout.AdUtils.overflowDivs = [];
    }
    var elements = document.getElementsByTagName('div');

    if (elements) {
        var length = elements.length;
        for (var i = 0; i < length; i++) {
            var el = elements[i];
            if (ScanScout.AdUtils.getCssStyleForElement(el, "overflow", "overflow") != "visible") {
                var overflow = ScanScout.AdUtils.getCssStyleForElement(el, "overflow", "overflow") || "visible"; 
                ScanScout.AdUtils.overflowDivs.push({element: el, overflow: overflow});
            }            
        }

        length = ScanScout.AdUtils.overflowDivs.length;
        for (var i = 0; i < length; i++) {
            var el = ScanScout.AdUtils.overflowDivs[i]['element'];
            el.style.overflow = "hidden";
        }
    }

}

ScanScout.AdUtils.restoreHiddenPageElements = function () {
    if (ScanScout.AdUtils.hiddenElements) {
        var length = ScanScout.AdUtils.hiddenElements.length;
        for(var i=0; i< length; i++) {
            ScanScout.AdUtils.hiddenElements[i].element.style.visibility = ScanScout.AdUtils.hiddenElements[i].visibility;
        }

        ScanScout.AdUtils.hiddenElements=[];

    }

    if (ScanScout.AdUtils.overflowDivs) {
        var length = ScanScout.AdUtils.overflowDivs.length;
        for(var i=0; i< length; i++) {
            ScanScout.AdUtils.overflowDivs[i].element.style.overflow= ScanScout.AdUtils.overflowDivs[i].overflow;
        }
        
        ScanScout.AdUtils.overflowDivs =[]
    }
}

ScanScout.AdUtils.getCssStyleForElement = function (elem, wc3Style, ieStyle) {
    if (elem.currentStyle) {
        return elem.currentStyle[wc3Style];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(ieStyle);
    }
    return "";
}
