function crhDefaultInterstitialAd(inOptions)
{
	var self = this;
	var itsCookiesInfo = { name: "crhInterstitial", path: "/" };

	var itsOptions =  {
        theUserKeyCodeCookie: {value: "EXTKEY=I070IM0", name: "userKeycodes"},
        theInterstitialUrl: "https://oascentral.consumerreports.org/RealMedia/ads/adstream_jx.ads/",
        theReferrerIndex: ".org/cro/",
        theInterVeilId: "#interVeil",
        theInterstitialContainer: "interstitialContainer",
        theCRHButtonId: ".crhbutton",
        theCloseButtonContainerId: ".closeicon"
   	};

	itsOptions = jQuery.extend({}, itsOptions, inOptions);

    self.createAd = function() {
        if (document.referrer.indexOf(itsOptions.theReferrerIndex) >= 0)
		{
            self.launchInterstitialAd();
        }
    };

	self.launchInterstitialAd = function()
	{
        var adContent = "";
		document.write = function(eval){adContent += eval;} //OVERRIDE TO SUPPORT EXTRACTING CONTENT FROM THE AD.. ajax WONT WORK ACCROSS DIFFERENT SUBDOMAIN AND HENCE THIS IS NECESSARY

        var theSrc = itsOptions.theInterstitialUrl + self.getVirtualPath() + "/1"+self.randomString(9, "0123456789")+"@x17" +
                     "?" + "article=112345&XE&status=active&if_nt_CookieAccept=Y&XE&" + self.readCookie("userAds");
                     // OAS_rdl and OAS_CA are set by https://oascentral.consumerreports.org/Scripts/oas_analytics.js

        jQuery.getScript(theSrc, function() {
            jQuery("body").append(adContent);
              if(adContent.indexOf("interVeil")!= -1){
                var theHost = "." + window.location.hostname.split(".").slice(-2).join(".");
                var itsKeyCodeCookieParam = new Cookie(DocumentCookieManager,itsOptions.theUserKeyCodeCookie.name, theHost ,itsCookiesInfo.path);
                var userKeyCodeValue = itsKeyCodeCookieParam.getValue();
                var theProducts = self.readCookie("userAds"); if(theProducts == null)theProducts = "";
                if(theProducts.indexOf("product=mg") == -1){
                    if (userKeyCodeValue == null)itsKeyCodeCookieParam.setValue(itsOptions.theUserKeyCodeCookie.value);
                    else if(userKeyCodeValue.indexOf("EXTKEY") == -1)itsKeyCodeCookieParam.setValue(itsOptions.theUserKeyCodeCookie.value+"&"+userKeyCodeValue);
                }
                jQuery(document).ready(function (){
                    jQuery(itsOptions.theInterVeilId).hide();
                    jQuery("#"+itsOptions.theInterstitialContainer).css("position","absolute");
                    if(jQuery("#"+itsOptions.theInterstitialContainer).length == 0) return;

                    self.showInterstitialContainer();

                    jQuery(itsOptions.theCloseButtonContainerId).click(function() {
                        jQuery("#"+itsOptions.theInterstitialContainer).hide();
                        jQuery(itsOptions.theInterVeilId).hide();
                    });

                    jQuery(itsOptions.theCRHButtonId).click(function() {
                        jQuery("#"+itsOptions.theInterstitialContainer).hide();
                        jQuery(itsOptions.theInterVeilId).hide();
                    });

                    jQuery(window).resize(function() {
                        self.showInterstitialContainer();
                    });

                    jQuery(window).scroll(function() {
                        self.showInterstitialContainer();
                    });
                });
            }
        });
	};

	self.showInterstitialContainer = function()
	{
		if (jQuery("#"+itsOptions.theInterstitialContainer).css("display") == "none")return;

		var scroll_top = jQuery(window).scrollTop();
		var scroll_left = jQuery(window).scrollLeft();
		var docwidth = jQuery(window).width();
		var docheight = jQuery(window).height();
		var docheightcomplete = (jQuery("body").get(0).offsetHeight>jQuery("body").get(0).scrollHeight)? jQuery("body").get(0).offsetHeight : jQuery("body").get(0).scrollHeight;

		if(jQuery(itsOptions.theInterVeilId).css("display") == "none"){
			jQuery(itsOptions.theInterVeilId).width(docwidth);
			jQuery(itsOptions.theInterVeilId).height(docheightcomplete);
			jQuery(itsOptions.theInterVeilId).css("left",0 + "px");
			jQuery(itsOptions.theInterVeilId).css("top",0 + "px");
			jQuery(itsOptions.theInterVeilId).css("visibility","visible");
			jQuery(itsOptions.theInterVeilId).show();
		}

		var objwidth = jQuery("#"+itsOptions.theInterstitialContainer).width();
        var objheight = jQuery("#"+itsOptions.theInterstitialContainer).height();
		var topposition = Math.floor(parseInt((docheight>objheight)? scroll_top+docheight/2-objheight/2 : scroll_top+5));
		var leftposition = docwidth/2-objwidth/2;

		jQuery("#"+itsOptions.theInterstitialContainer).css("left", leftposition+"px");
		jQuery("#"+itsOptions.theInterstitialContainer).css("top", topposition+"px");
		jQuery("#"+itsOptions.theInterstitialContainer).css("visibility","visible");
	};



    self.getVirtualPath = function(){
        var theVirtualPath = "";
        var theAdPathPrefix = "test.cu";
        if (window.location.host.toLowerCase().indexOf("consumerreports.org") != -1){
            theAdPathPrefix = "cu";
        }
        var thePWIPath = window.location.href.substring(window.location.href.substring(10).indexOf("/") + 10);
        if (thePWIPath.indexOf(".") != -1){
            theVirtualPath = theAdPathPrefix + thePWIPath.substring(0, thePWIPath.indexOf("."));
        }
        else{
            theVirtualPath = theAdPathPrefix + thePWIPath;
        }
        if (theVirtualPath.match("/$") == "/"){
            theVirtualPath = theVirtualPath.substring(0, theVirtualPath.length() - 1);
        }
        return theVirtualPath;
    };


    self.randomString = function(inStringLength, inSource) {
        var theText = "";
        for (var i = 0; i < inStringLength; i++){
            var thePosition = Math.floor(Math.random() * inSource.length);
            theText += inSource.substring(thePosition, thePosition + 1);
        }
        return theText;
    };

    self.readCookie = function(inName){
        var theNameWithEquals = inName + "=";
        var theCookieArray = document.cookie.split(';');
        for (var i = 0; i < theCookieArray.length; i++){
            var c = theCookieArray[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(theNameWithEquals) == 0) return c.substring(theNameWithEquals.length, c.length);
        }
        return "";
    };
}
