// interstitial.js
// Austin Hill (austinh@x10.com)
// ---------------------------------------------------------------
// This javascript class allows interstitial ads to be shown on pages, only on the first view,
//   not from ad clicks, and not on order pages
//  REQUIRES:
//  Jquery 1.4+
//  Jquery UI 1.8+
//----------------------------------------------------------------


var jQuery14 = jQuery.noConflict(); 
function closeAd(){
  jQuery14("#x10InterstitialAd").hide();
  jQuery14("#curtain").hide();
}
function x10InterstitialAd ( linkUrl, adImage, width, height ){
	
	this.contentUrl = linkUrl;
	this.adImage = adImage;
	this.currentPage = location.href;
	this.referer = document.referer;
	this.height = height + 30;
	this.imgHeight = height;
	this.width = width;
	this.imgWidth = width;
	//safety switch
	this.showAds = true;
	this.cookieFirstVisit = "XIS";
	this.daysToExpireCookie = 7;
	this.urlRegexes = [];
	
	//Fill out url exemption regular expressions
	this.urlRegexes[0] = "^.*order\.cgi.*$";
    if(navigator.userAgent.indexOf('bot') > -1){ this.showAds=false; }
    if(navigator.userAgent.indexOf('Google Web Preview') > -1){ this.showAds=false; }
	
	
	
	this.renderAd = function(){
		
		if( this.showAds == true ){
			var isFirstTimeVisit = this.isFirstTimeVisit();
			var isExemptPage = this.isExemptPage();
			var isReferrerNonpaid = this.isReferrerExempt();
			if( ( isFirstTimeVisit == true ) && 
				( isExemptPage == false ) && 
				( isReferrerNonpaid == false) ){
				
				jQuery14("body").append("<div id='x10InterstitialAd' align=center style='width:"+this.width+"px;height:"+this.height+"px;'></div>");
				jQuery14("#x10InterstitialAd").append("<a href='"+this.contentUrl+"' style='margin:0;padding:0;'><img border='0' src='"+this.adImage+"' height="+this.imgHeight+" width="+this.imgWidth+" /></a><br/><a href='javascript:closeAd()'><b>Close</b></a>");
                jQuery14("#x10InterstitialAd").css("top","10px");
                jQuery14("#x10InterstitialAd").css("left","15%");
                jQuery14("#x10InterstitialAd").css("z-index","1000");
                jQuery14("#x10InterstitialAd").css("position","absolute");
                jQuery14("#x10InterstitialAd").css("background-color","white");
                jQuery14("#x10InterstitialAd").css("border","1px solid black");
                jQuery14("body").append("<div id='curtain'></div>");
                jQuery14("#curtain").css("width","100%");
                jQuery14("#curtain").css("height","3000px");
                jQuery14("#curtain").css("background-color","black");
                jQuery14("#curtain").css("z-index","999");
                jQuery14("#curtain").css("position","absolute");
                jQuery14("#curtain").css("top","0px");
                jQuery14("#curtain").css("left","0px");
                jQuery14("#curtain").css("opacity",".8");
                jQuery14("#curtain").css("filter","Alpha(Opacity=80)");

			}
		}
	}
	
	this.isExemptPage = function(){
		var currentPageUrl = this.currentPage;
		for ( regexString in this.urlRegexes ){
			var currentRegex = new RegExp( this.urlRegexes[regexString], "i");
			if( currentPageUrl.match( currentRegex ) ){
				return true;
			}
		}
		return false;
	}
	/* If the page view is from an ad, don't show the interstitial */
	this.isReferrerExempt = function(){
		
		var googleRegex = new RegExp("google","i");
		var bingRegex = new RegExp("bing","i");
		var yahooRegex = new RegExp("search.yahoo","i");
		var documentReferrer = document.referrer;
		var queryCookie = this.getCookie("Query");
		if( documentReferrer.match(googleRegex)){
			var beginString = "REFERER";
			var beginningIndex = queryCookie.indexOf("REFERER=") + beginString.length;
			var endIndex = queryCookie.indexOf("&gs_rfai");
			var matchingPart = queryCookie.substr(beginningIndex, endIndex);
			if( matchingPart == documentReferrer){
				return true;
			}
		}		
		else if( (documentReferrer.match(bingRegex) ) || (documentReferrer.match(yahooRegex)) ){
			var beginString = "REFERER";
			var beginningIndex = queryCookie.indexOf("REFERER=") + beginString.length;
			var endIndex = queryCookie.indexOf("&CAMPAIGN");
			var matchingPart = queryCookie.substr(beginningIndex, endIndex);
			if( matchingPart == documentReferrer){
				return true;
			}
		}
		return false;
	}
	
	this.isFirstTimeVisit = function(){
		if ( this.getCookie(this.cookieFirstVisit) == "" ){
			//Set cookie
			this.setCookie( this.cookieFirstVisit, "1", this.daysToExpireCookie );
			return true;
		}
		return false;
	}
	
	this.setCookie = function( cookieName, value, expireDays){
		var exDate = new Date();
		exDate.setDate( exDate.getDate() + expireDays );
		document.cookie=cookieName+ "=" + escape(value) + ((expireDays==null) ? "" : ";expires="+exDate.toUTCString());
	}
	
	this.getCookie = function ( cookieName ){
		var cookieStart;
		if (document.cookie.length > 0){
			cookieStart = document.cookie.indexOf(cookieName + "=");
			if (cookieStart != -1){
				cookieStart = cookieStart + cookieName.length+1;
				cookieEnd = document.cookie.indexOf(";",cookieStart);
				if (cookieEnd == -1) cookieEnd = document.cookie.length;
				return unescape(document.cookie.substring( cookieStart,cookieEnd ) );
			}
		}
		return "";
	}
}


