// client side code based on catfish ad tutorial from sitepoint
// http://www.sitepoint.com/blogs/2005/10/18/the-catfish-part-1/
// http://www.sitepoint.com/blogs/2005/10/21/catfish-ads-part-2/

// Deploy the footer

// The footer optin form should be located in an element (DIV) of id 'footer' and should be hidden
// out of view (hidden by initial CSS)

// Stack up window.onload events using this function from Simon Willison -
// http://www.sitepoint.com/blog-post-view.php?id=171578
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// function to remove optin and reset page padding
function destroyfooter() {
	// make the footer invisible
	toggleLayer('optincrusher');

	// reset the padding at the bottom
	document.getElementsByTagName('html')[0].style.padding= '0';
	return false;
}

function registerCloseme() {
	var closelink = document.getElementById('closeme');
	closelink.onclick = destroyfooter;
}

function registerDonoshow() {
	var donotshowlink = document.getElementById('donotshow');
	donotshowlink.onclick = donotshowfooter;
}

addLoadEvent(function() {
	registerCloseme();
	registerDonoshow();
});

var footer;

// initializing
function deployfooter() {
	// identify the footer div for script access
	footer = document.getElementById('optincrusher');

	// start footer rollout after displayDelay
	if (getCookie('donotshow')!='true' && donotshowLoggedin == false) {
		footertimeout = setTimeout(startfooter, displayDelay);
	}
}

// starts the footer sliding up
function startfooter() {
	footerposition = 0; // footerposition is expressed in percentage points (out of 100)
	footertimeout = setInterval(positionfooter, slidespeed);
}

// position the footer (see startfooter which initializes the timeout for this function call)
function positionfooter() {
	// the magic 100s below indicate percentage with the footer approaching 100% deployed
	footerposition += slidestep;
	footer.style.marginBottom = '-' + (((100 - footerposition) / 100) * footerheight) + 'px';
	
	// if I'm 100% deployed then finish footer
	if (footerposition >= 100) {
		clearTimeout(footertimeout);
		footertimeout = setTimeout(finishfooter, 1);
	}
}

function finishfooter() {
	// just in case we overdeployed in positionfooter(), ensure marginBottom is zero (0)
	footer.style.marginBottom = '0px';	
	
	// increase padding at bottom of document so that page content isn't hidden under 
	// the footer when the page is scrolled all the way to the bottom
	document.body.parentNode.style.paddingBottom = (footerheight - footeroverlap) +'px';
	
	// here you could use AJAX (or similar) to log the popup hit for tracking purposes	
}

addLoadEvent(deployfooter);

// cookie management functions below 

function setCookie(c_name,value,path,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) + (path ? '; path=' + path : '') + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

// this deletes the cookie when called
function delCookie(c_name) {
	var tmp = getCookie(c_name);
	if(tmp) { setCookie(c_name,tmp,'/',-1); }
}

// visibility management function

function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}