// file name : AdHoverAd.js
// Usage : used for countdown display in hover Ad application

adTime=1;  					// stores seconds ad reminder 
chanceAd=1; 					// ad will be shown 1 in X times (put 1 for everytime)
var displaytime 				// to display time in seconds for countdown

var ns=(document.layers);			// stores browser type, ns for netscape navigator browser
var ie=(document.all);				// stores browser type, ie for internet explorer browser
var w3=(document.getElementById && !ie);	// stores browser type, w3 for mozilla browser
var calunit=ns? "" : "px"			//calculate units in pixels 
var ns6=document.getElementById&&!document.all	// stores type of browser
var g_bodyText;
var f_part;					// first part i.e. the text above countdown timer
var s_part;					// second part i.e. the text below countdown timer
var tm;						// variable for timer

adCount=0;					//// seconds ad count is shown
var expDays = 999; // number of days the cookie should last
var	chkshow=false;

//function for delay before the timer starts
// parameter : x -> delay seconds
function delay(x)				
{
	var d=new Date()			//object d for date 
	d=d.getSeconds()+x			
	if (d>=60) d=d-60
	// loop to run for the d seconds
	while (true)
	{
		var d1=new Date()		//object d1 for date 
		if(d==d1.getSeconds())
			break;
	}	
}

// funciton to set the browser cookie

function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}

function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name)
{  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function amt(name)
{
var count = GetCookie(name)
	if(count == null)
	{
		SetCookie(name,'1')
		return 1
	}
	else
	{
		var newcount = parseInt(count) + 1;
		DeleteCookie(name)
		SetCookie(name,newcount,exp)
		return count
	}
}

function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function checkCount(name) {
var count = GetCookie(name);
if (count == null) {
count=1;
SetCookie(name, count, exp);
}
else {
count++;
SetCookie(name, count, exp);
chkshow=true;
   }
}

//function to set timer clock
function setClock()			
{
	if (tm >= 0)
	{
		bodyText = f_part+tm+s_part;
		document.getElementById("dragtext").innerHTML=bodyText;
		tm--;
		timer = setTimeout("setClock()",1000);
	}
}

//function used to display popup with the parameters defined by the user, this function is called from html 
//Parameters:pleft -> left position of the window
//Parameters:ptop -> top position of the window
//Parameters:pheight -> text font style
//Parameters:pwidth -> text font style
//Parameters:bordercolor -> border color of the window
//Parameters:borderstyle -> border style of the window
//Parameters:borderwidth -> border width of the window
//Parameters:backgroundcolor -> background color of the window
//Parameters:sec -> delay seconds after which window appears
//Parameters:showeverytime -> true/false once for each user or not
//Parameters:bodytext -> text font style
//Parameters:dtime -> display time

function AdPopUp(pleft,ptop,pheight,pwidth,bordercolor,borderstyle,borderwidth,backgroundcolor,sec,showeverytime,bodytext,dtime)
{
	displaytime = dtime;
	
	chkshow=false;
	if (!showeverytime)
	{
		checkCount("popad")
	}
	else
	{
		DeleteCookie("popad")
	}

	if(!chkshow)
	{ 		

	setTimeout("Delaynew()",(sec*1000));
	
	if(!ns && !ie && !w3) return;
	if(ie)		adDiv=eval('document.all.sponsorAdDiv.style');
	else if(ns)	adDiv=eval('document.layers["sponsorAdDiv"]');
	else if(w3)	adDiv=eval('document.getElementById("sponsorAdDiv").style');
	if(ie){ 
		bodytext=document.getElementById("dragtext").innerHTML
			
		document.all.border2.style.backgroundColor=backgroundcolor
		document.all.border1.style.border = borderwidth+'px '+borderstyle+' '+bordercolor
		document.all.border1.width=pwidth + calunit
		document.all.border2.width=(pwidth-(borderwidth * 2)) + calunit
		document.all.border1.height=pheight + calunit
		if(((pheight-(borderwidth * 2)) + calunit)>0)
			document.all.border2.height=(pheight-(borderwidth * 2)) + calunit
		var tempText=bodytext.toLowerCase();
		i_o = tempText.indexOf("&lt;timer&gt;");
		f_part = bodytext.substr(0,i_o);
		s_part = bodytext.substr(i_o+13,bodytext.length);
		tm = dtime;
	}
	else if(ns6) {
			if(borderstyle=="double" && borderwidth==1)
			{
				borderwidth=2
			}
		bodytext=document.getElementById("dragtext").innerHTML
		var tempText=bodytext.toLowerCase();
		i_o = tempText.indexOf("&lt;timer&gt;");
		f_part = bodytext.substr(0,i_o);
		s_part = bodytext.substr(i_o+13,bodytext.length);
		document.getElementById("border1").style.border = borderwidth+'px '+borderstyle+' '+bordercolor
		document.getElementById("border2").style.backgroundColor=backgroundcolor
		document.getElementById("border1").style.width=pwidth + calunit
		document.getElementById("border2").style.width=pwidth + calunit
		document.getElementById("border1").style.height=pheight + calunit
		document.getElementById("border2").style.height=pheight + calunit

		tm = dtime;
	}	

	showAd(pleft,ptop,pheight,pwidth);
	}
	if(chkshow)
	{ 
		document.getElementById("sponsorAdDiv").innerHTML=''
	}
}

//function used to show ad with the parameters defined by the user, this function is called from AdPopUp function
//Parameters : pleft -> left position of the window
//Parameters : ptop -> top position of the window
//Parameters : pheight -> height of the window
//Parameters : pwidth -> width of the window


function showAd(pleft,ptop,pheight,pwidth)
{
	// places the window where user desires to place the popup window
	if(adCount<adTime*1)
	{
		adCount+=1;
		if (ie)
		{
			documentWidth  =truebody().offsetWidth/2+truebody().scrollLeft-20;
			documentHeight =truebody().offsetHeight/2+truebody().scrollTop-20;
		}	
		else if (ns)
		{
			documentWidth=window.innerWidth/2+window.pageXOffset-20;
			documentHeight=window.innerHeight/2+window.pageYOffset-20;
		} 
		else if (w3)
		{
			documentWidth=self.innerWidth/2+window.pageXOffset-20;
			documentHeight=self.innerHeight/2+window.pageYOffset-20;
		} 

		if(pleft>0)	{adDiv.left=pleft+calunit }
		if(ptop>0)	{adDiv.top=ptop+calunit }
		if(pheight>0)	{adDiv.height=pheight+calunit }
		if(pwidth>0)	{adDiv.width=pwidth+calunit }
	
		setTimeout("showAd(" + pleft + "," + ptop + "," + pheight + "," + pwidth + ")",(displaytime * 1000));
	}
	else closeAd();
}

//function used to close ad pop up window

function closeAd()
{
	if (ie||w3)
	{
		adDiv.display="none";
		document.getElementById("dragtext").innerHTML=''
	}else
	{
		adDiv.visibility ="hide";
		document.getElementById("dragtext").innerHTML=''
	}
}

//this function returns the document body

function truebody(){
return (document.compatMode!="BackCompat")? document.documentElement : document.body
}

//this function is being called from AdPopUp for the timeout of delay

function Delaynew()
{
	if (ie||w3)
        adDiv.visibility="visible";
        else
        adDiv.visibility ="show";
	return;
}