﻿var divfix = document.getElementById("theAd");
var sLocation = document.referrer.toLocaleLowerCase();
var rDomains = ["www.searchandoptimization.com","google.com"];
var fire = 0;
var cookiedays = 7;
var rnd_Range = 5;
var ieleft = 475;
var ietop = 161;
var lightDivHTML = '<a href="javascript:void(0)" onclick="closeDiv();">Close,  take me to content!</a><br /><br />This is your first visit. Hope you will enjoy it.<br />';

//Loop
for (i=0;i<rDomains.length;i++) {
	if (sLocation.indexOf(rDomains[i], 0) > -1) 
	{
		fire = 1;
		//alert(rDomains[i] + "__fire");
		break;		
	}
}

initDivs();

//IE
if (document.all)
{
	rnd_Range = Math.ceil(rnd_Range*Math.random())
	//Random-factor
	if (rnd_Range == 1) {
		if (fire == 1) {
			popUP();
		}
	}
}

function popUP() 
{
	divfix.style.cssText = "position:absolute;filter:alpha(opacity=0);opacity:0;z-index:999";
	divfix.style.left = ieleft + "px";
	divfix.style.top = ietop + "px";
	var light = document.getElementById('light');
	var fader = document.getElementById('fade');
	var arrayPageSize = getPageSize();
	light.style.display='block';
	fader.style.height = (arrayPageSize[1] + 'px');
	fader.style.display = 'block';
	createCookie("fader","true",cookiedays);
}

//Init divs
function initDivs() {
	//Lightbox
	var divLight = document.createElement("div");
	divLight.id = "light";
	divLight.style.cssText = "display: none;position: absolute;top: 150px;left: 350px;width: 400px;height: 250px;padding: 16px;border: none;background-color: white;overflow: auto;z-index:2;";
	divLight.innerHTML = lightDivHTML;
	document.body.appendChild(divLight);
	
	//Shadow
	var divFade = document.createElement("div");
	divFade.id = "fade";
	divFade.style.cssText = "display: none;position: absolute;top: 0px;left: 0px;width: 100%;background-color: black;-moz-opacity: 0.8;opacity:.80;filter: alpha(opacity=80);";
	document.body.appendChild(divFade);
}


function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//Cookie
var testx = readCookie('fader');

if (fire == 1) {
	//If there a cookie?
	if (testx) {
		//testx = true i IE
		//alert("'" + testx + "' no - fire");
	}
	else {
		//alert("yes - fire");
		popUP();
	}
}
	
//Close function
function closeDiv() {
	light.style.display='none';
	fader.style.display = 'none';
	
	//Write cookie
	createCookie("fader","true",cookiedays);
}
