// Deploy the Catfish

// The Catfish should be located in an element of id 'catfish' and should be hidden
// out of view

function addLoadListener(fn) {
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = fn;
		} else {
			window.onload = function() {
				oldfn();
				fn();
			}
		}
	}
}

function createCookie(name,value,hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*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);
}

var catfish;

function deploycatfish()
// initializing
{
	var openBannerCookie = readCookie('openBanner');
	if (openBannerCookie != "false") {
		
		catfish = document.getElementById('popupBanner');
		
		catfishheight = 165; // total height of catfish in pixels
		catfishoverlap = 25; // height of the 'overlap' portion only (semi-transparent)
		catfishtimeout = setTimeout(startcatfish, 2000);
		
	}
}

function startcatfish()
// starts the catfish sliding up
{
	catfishposition = 33; // catfishposition is expressed in percentage points (out of 100)
	catfishtimeout = setInterval(positioncatfish, 25);
}

function positioncatfish()
{
	catfishposition += 10;
	catfish.style.marginBottom = '-' + (((100 - catfishposition) / 100) * catfishheight) + 'px';
	if (catfishposition >= 100)
	{
		clearTimeout(catfishtimeout);
		catfishtimeout = setTimeout(finishcatfish, 1);
	}
}

function finishcatfish()
{
	catfish.style.marginBottom = '0';	
	// jump the bottom of the document to give room for the catfish when scrolled right down
	document.body.parentNode.style.paddingBottom = (catfishheight) +'px';
	
	// here you could use AJAX (or similar) to log the popup hit for tracking purposes	
}

addLoadListener(deploycatfish);

function destroycatfish() {
	catfish = document.getElementById('popupBanner');
	
	catfish.style.marginBottom = '-140px';
	
	var button = document.getElementById('openme');
	button.firstChild.style.width = '20px';
	button.firstChild.style.height = '20px';
	
	createCookie('openBanner','false',1);
}

function closeme() {
	var closelink = document.getElementById('closeme');
	closelink.onclick = destroycatfish;
}

function reopencatfish() {
	
	catfish = document.getElementById('popupBanner');
	
	catfishheight = 165; // total height of catfish in pixels
	catfishoverlap = 25; // height of the 'overlap' portion only (semi-transparent)
	catfishtimeout = setTimeout(startcatfish, 1);

	createCookie('openBanner','true',1);
}

function openme() {
	var openlink = document.getElementById('openme');
	openlink.onclick = reopencatfish;
}

addLoadListener(closeme);
addLoadListener(openme);
