//function popupTimer()
function popupTimer(obj, top, left, height, width, textFont, textColor, backColor, borderColor, borderStyle, borderWidth, text, floatPopup, closeButton, guid, time, alertTime, backImage)
{
        //predefined variables
	this.myGlide = null;
	this.glideIdName = '_jh_pop_float_box';
	this.timerLabel = '_jh_pop_time_label';
//	this.cookieTimeLabel = 'jh_pop_start_time_';
	this.cookieShowLabel = 'jh_pop_show_label_';

	//user defined variables
	this.objName = obj;
	this.top = top;
	this.left = left;
	this.height = height;
	this.width = width;
	this.textFont = textFont;
	this.textColor = textColor;
	this.backColor = backColor;
	this.borderColor = borderColor;
	this.borderStyle = borderStyle;
	this.borderWidth = borderWidth;
	this.text = text;
	this.floatPopup = floatPopup;
	this.closeButton = closeButton;
	this.guid = guid;
	this.timeLimit = time * 60000;
	this.alertTime = alertTime * 60000;
	this.backgroundImage = backImage;
	
	//class variables
	this.lastSec = null;
	this.currentTime = null;
	this.intervalUpdateTime = null;
	this.intervalIFrame = null;
	this.isIE6 = false;
	this.alertShowTime = true;
	this.alertTimeHolder = 0;
	var arVersion = navigator.appVersion.split("MSIE")
  	var version = parseFloat(arVersion[1])
	if (navigator.appVersion.indexOf("MSIE") != -1 && (version >= 5.5) && (version < 7) && (document.body.filters)) 
	{
		this.isIE6 = true;
	}
	//initialization functions
	if(this.checkShowPopup())
	{
		this.startTime = this.getTime();
		this.updateTime();
		this.makeGliderHolder();
		if(this.floatPopup)
		{
			if(this.isIE6)
			{
				this.intervalIFrame = setInterval(this.objName + ".updateIFrame()",10);
			}
			this.setGliding();
		}
		this.intervalUpdateTime = setInterval(this.objName + ".updateTime()",100);
	}
}

popupTimer.prototype.updateIFrame = function()
{
	var iframe = document.getElementById(this.glideIdName+"_iFrame");
	var div = document.getElementById(this.glideIdName);
	if( iframe && div )
	{
  		iframe.style.top = div.style.top;
  		iframe.style.left = div.style.left;
	}
}

popupTimer.prototype.makeCloseButton = function(parent)
{
	var newdiv = document.createElement('div');

	newdiv.setAttribute('id',this.glideIdName + "_close");
	newdiv.innerHTML = "<a style='color:" + this.closeButton + ";' href='javascript:" + this.objName + ".destroyGliderHolder();'>[close]</a>";
	newdiv.style.position = 'absolute';
	newdiv.style.zIndex = 5001;
	newdiv.style.right = '2px';
	newdiv.style.top = '2px';
	newdiv.style.fontFamily = "arial";
	newdiv.style.fontSize = "10px";
	newdiv.style.borderStyle = "none";
	parent.appendChild(newdiv);
}

popupTimer.prototype.checkShowPopup = function()
{
	if(readCookie(this.cookieShowLabel + this.guid) == null)
	{
		createCookie(this.cookieShowLabel + this.guid, "jh", 1825) 
		return true;
	}
	return false;
}

popupTimer.prototype.updateTime = function()
{
	var curTime = this.getTime();
	var deltaTime = curTime - this.startTime;
	var timeLeft = this.timeLimit - deltaTime;
	var count = 0;
	
	if(deltaTime < 0)
	{
		timeLeft = 0;
	}
        if(timeLeft > 0)
        {
        	var hr = getHrFromTime(timeLeft);
        	var min = getMinFromTime(timeLeft);
        	var sec = getSecFromTime(timeLeft);
        	this.currentTime = this.makeTimeString(hr,min,sec);
        	var labelArray = document.getElementsByTagName("label");
        	var count = 0;
    		var updateLabels = false;
    		    	
        	if(timeLeft < this.alertTime)
		{
			var modTime = timeLeft % 250;
			if(modTime > this.alertTimeHolder)
			{
				this.alertTimeHolder = modTime;
			}
			else
			{
				this.alertTimeHolder = 0;
				this.alertShowTime = !this.alertShowTime;
				updateLabels = true;
			}
		}
		else if(this.lastSec != sec )
        	{
        		updateLabels = true;
        	}
		if (updateLabels)
		{
        		this.lastSec = sec;
			while(count < labelArray.length)
			{
				if(labelArray[count].id == this.timerLabel)
				{
					if (this.alertShowTime)
					{
						labelArray[count].innerHTML = this.currentTime;
					}
					else
					{
						labelArray[count].innerHTML = "";
					}
				}
				count++;
			}
		}
	}
	else
	{
		this.destroyGliderHolder();
	}
}

popupTimer.prototype.makeTimeString = function(hr, min, sec)
{
	var outHr = hr.toString();
	var outMin = min.toString();
	var outSec = "";

	if (sec < 10)
	{
		outSec = "0" + sec.toString();
	}
	else
	{
		outSec = sec.toString();
	}
	if(hr > 0)
	{
		if (min < 10)
		{
			outMin = "0" + min.toString();
		}
		return outHr + ":" + outMin + ":" + outSec;
	}
	return outMin + ":" + outSec;
}

popupTimer.prototype.getTime = function()
{	
	var dateObj = new Date();
	return dateObj.getTime();
}

popupTimer.prototype.setGliding = function()
{
	this.myGlide = new Glider(this.glideIdName,this.left, this.top, null, null, 700,-1);
	this.myGlide.show();
}

popupTimer.prototype.makeGliderHolder = function()
{
        //make new div
	var newdiv = document.createElement('div');
	
	//set div attributes
	newdiv.setAttribute('id',this.glideIdName);
	newdiv.innerHTML = this.text;
	newdiv.style.height = this.height + "px";
	newdiv.style.width = this.width + "px";
	newdiv.style.position = 'absolute';
	newdiv.style.zIndex = 5000;
	newdiv.style.backgroundColor = this.backColor;
	newdiv.style.fontFamily = this.textFont;
	newdiv.style.color = this.textColor;
	newdiv.style.borderColor = this.borderColor;
	newdiv.style.borderStyle = this.borderStyle;
	newdiv.style.borderWidth = this.borderWidth + "px";
	if(!this.floatPopup)
	{
		newdiv.style.top = this.top + "px";
		newdiv.style.left = this.left + "px";
	}
	if(this.backgroundImage.length > 0)
	{
		newdiv.style.backgroundImage = "url('" + this.backgroundImage + "')";
	}
	
	if (this.isIE6) 
  	{
//  		cover.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + this.backdropImage + "\', sizingMethod='scale');";
  		var iframe = document.createElement('IFRAME');
  		var myversionInfo=detectDoctype(); 
  		iframe.setAttribute('id',this.glideIdName+'_iFrame');
  		if(this.borderStyle == "none" || myversionInfo == null)
  		{
  			iframe.style.width = this.width + "px";
  			iframe.style.height = this.height + "px";
  		}
  		else
  		{
  			iframe.style.width = (this.width + (2*this.borderWidth)) + "px";
  			iframe.style.height = (this.height + (2*this.borderWidth)) + "px";
  		}
  		iframe.style.top = this.top + "px";
  		iframe.style.left = this.left + "px";
  		iframe.style.position = 'absolute';
  		iframe.style.display = 'inline';
  		iframe.style.zIndex=1000;
  		iframe.frameBorder="0";
  		iframe.scrolling="no";
  		iframe.marginwidth="0";
  		iframe.marginheight="0";
  		iframe.allowtransparency="true"
  		iframe.style.filter='chroma(color="#FFFFFF")';
  		document.body.appendChild(iframe);
	}
	
	//update clocks
	var labels = newdiv.getElementsByTagName("label");
	var count = 0;
	while(count < labels.length)
	{
		if(labels[count].id == this.timerLabel)
		{
			labels[count].innerHTML = this.currentTime;
		}
		count++;
	}
	//write to html
	if(this.closeButton != false)
	{
		this.makeCloseButton(newdiv);
	}
	document.body.appendChild(newdiv);
}

popupTimer.prototype.destroyGliderHolder = function()
{
	var regEx = /^\[object/;
	var glideHolder = document.getElementById(this.glideIdName);
	if(regEx.test(glideHolder))
	{
		closeBtn=document.getElementById(this.glideIdName + "_close");
		if(regEx.test(closeBtn))
		{
			closeBtn.parentNode.removeChild(closeBtn);
		}
		glideHolder.parentNode.removeChild(glideHolder);
	}
	iFrameHolder = document.getElementById(this.glideIdName+"_iFrame");
	if(regEx.test(iFrameHolder))
	{
		iFrameHolder.parentNode.removeChild(iFrameHolder);
		if (this.intervalIFrame != null)
		{
			clearInterval(this.intervalIFrame);
		}
	}
	if(this.myGlide != null)
	{
		this.myGlide.id = "";
		delete this.myGlide;
		this.myGlide = null;
	}
	if(this.intervalUpdateTime != null)
	{
		clearInterval(this.intervalUpdateTime)
		this.intervalUpdateTime = null;
	}
}

//==============================================================
//==============================================================
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;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}

function getHrFromTime(time)
{
	return parseInt(time / 3600000);
}
function getMinFromTime(time)
{
	time %= 3600000;
	return parseInt(time / 60000);
}
function getSecFromTime(time)
{
	time %= 1440000;
	time %= 60000;
	return parseInt(time / 1000);
}

function versionInfo() 
{ 
	this.xhtml=""; 
	this.version=""; 
	this.importance=""; 
} 

function detectDoctype()
{
	var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi; 
	var myversionInfo=new versionInfo(); 
	
	/********************************************* 
	Just check for internet explorer. 
	**********************************************/ 
	if(typeof document.namespaces != "undefined")
	{
		if(document.all[0].nodeType==8) 
			re.exec(document.all[0].nodeValue); 
		else 
			return null; 
	}
	else
	{
		if(document.doctype != null) 
			re.exec(document.doctype.publicId); 
		else 
			return null; 
	} 
	myversionInfo.xhtml=RegExp.$1; 
	myversionInfo.version=RegExp.$2; 
	myversionInfo.importance=RegExp.$3; 
	return myversionInfo; 
}