var tmr;
var t;
var obj;
var duration;
var expiryTime;
var approvalDate;

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();
		//var expires = "; expires=Mon, 15 Feb 2010 08:00:00 UTC";
	}
	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 sFa() {

    initDuration();
    initExpiryTime();
    initApprovalDate();
      
    var x = readCookie('EnergySense_HomepageAd');
    if (x) 
    {/*Cookie Exists*/
    
        /*DO NOT DISPLAY AD */
        
        /* Check if last approval date of the source page <> approval date stored in the cookie */
        var approvalDate_cookie = x;
        var approvalDate_db = approvalDate;
        
        //alert('approvalDate_cookie='+x);
        //alert('approvalDate_db='+approvalDate);
        
        if(approvalDate_cookie != approvalDate_db)
        {
            // Source page has been updated meanwhile ==> we need to display the new AD                                
	        setupCookieAndShowPopup();	        
        }
        else
        {
            //DO NOTHING
            //alert('DO NOTHING');            
        }    
    }
    else 
    {
        /*Cookie does not exist ==> first time logged OR cookie expired */ 
    	setupCookieAndShowPopup();
	 }
}

function setupCookieAndShowPopup()
{
    obj = gObj();
	sLft();
    shw(true);
    t = 0;
    sTmr();
  
    createCookie('EnergySense_HomepageAd', approvalDate ,expiryTime);   
    setTimeout('hFa()', duration*1000);
}

function initDuration()
{
    /*read display duration (time)*/      
     var litDuration = document.getElementById("litDuration");
     duration = litDuration.innerText;         
}

function initExpiryTime()
{
     /*read no of days until the user will see the popup again*/
     var litExpiryTime = document.getElementById("litExpiryTime");
     expiryTime = litExpiryTime.innerText;
}

function initApprovalDate()
{
     /*read approval date*/
     var litApprovalDate = document.getElementById("litApprovalDate");
     approvalDate = litApprovalDate.innerText;         
}

function hFa() {
	t = -100;
	sTmr();
	return false;
}

function sTmr() {
	tmr = setInterval("fd()",20);		
}

function fd() {
	var amt = Math.abs(t+=10);
	if(amt == 0 || amt == 100) clearInterval(tmr);
	amt = (amt == 100)?99.999:amt;
  	
	obj.style.filter = "alpha(opacity:"+amt+")";
	obj.style.KHTMLOpacity = amt/100;
	obj.style.MozOpacity = amt/100;
	obj.style.opacity = amt/100;
	
	if(amt == 0) shw(false);
}

function sLft() {
	var w = 160;	// set this to 1/2 the width of the fa div defined in the style sheet 
			// there's not a reliable way to retrieve an element's width via javascript!!
					
	var l = (document.body.innerWidth)? document.body.innerWidth / 2:document.body.offsetWidth / 2;

	obj.style.left = (l - w)+"px";
}

function gObj() {
	return document.getElementById("fa");	
}

function shw(b) {
	(b)? obj.className = 'show':obj.className = '';	
}
