// Displaying a catfish popup

var popupzone = 1;
var popupid = 64;
var popupintervaldays = 7;
var popupemail = '';

// "The Catfish is a Plenty Good Enough Fish For Anyone"
// Mark Twain


//logging info
var catfishnow = new Date(), catfishuid = catfishnow.getTime() + '-' + catfishnow.getMilliseconds() + '-' + Math.floor(Math.random() * 10000000);


//alert(window.location.pathname);


function createcatfish()
{
	// create catfish here
	catfish = document.createElement('div');
	catfish.id = 'catfish';
	if (catfishpopulate) catfishpopulate(catfish);
	return catfish;
}

function deploycatfish(catfish)
{
	cookiearray = document.cookie.split(/;\s*/);
	var slideme = true;

	for (entry in cookiearray)
	{
		if (cookiearray[entry] == 'SPsub=1') return;
		if (cookiearray[entry] == 'SPcookie=1') return;
		if (cookiearray[entry] == 'SPcatfish=1') return;
		if (cookiearray[entry] == 'SPcatfish=2') slideme = false;
	}

	if (!document.defaultView)
	{               // IE position:fixed hacking
		scrollheight = document.body.parentNode.scrollTop;
		var subelements = [];
		for (var i = 0; i < document.body.childNodes.length; i++) {
	 		subelements[i] = document.body.childNodes[i];
		}

		var zip = document.createElement('div');    // Create the outer-most div (zip)
		zip.className = 'zip';                      // call it zip

		for (var i = 0; i < subelements.length; i++) {
			zip.appendChild(subelements[i]);
		}
		document.body.appendChild(zip); // add the major div
		catfish.style.position = 'absolute';
		document.body.className = 'zipped';
		document.body.parentNode.className = 'zipped';
		zip.scrollTop = scrollheight;
		document.body.parentNode.scrollTop = 0;
	}
	catfish.style.marginBottom = '-500px';

	document.body.appendChild(catfish);

	catfishheight = catfish.offsetHeight ? catfish.offsetHeight : 20;
	catfish.style.marginBottom = (0 - catfishheight) + 'px';
	catfishposition = 0;

	if (slideme) catfishtimeout = setTimeout(startcatfish, 1600);
		else finishcatfish();
}

function startcatfish()
// starts the catfish sliding up
{
	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 - getcatfishoverlap()) +'px';
	// set cookie so it won't 'slide' up for the rest of the session
	document.cookie = 'SPcatfish=2; path=/';
	// logging via remote scripting
	var logu = document.createElement('img');
	logu.src = '/popup/popuplog.php?zoneid='+popupzone+'&popupid='+popupid+'&type=catfish&path=' + encodeURI(document.location.pathname) + '&uid=' + catfishuid;
}

function destroycatfish()
{
	if (!catfish) return false;
	document.body.removeChild(catfish);
	document.body.parentNode.style.paddingBottom = '0';
	// set cookie so it won't appear for the rest of the day
	var expire = new Date();
	expire.setTime(expire.getTime() + (43200000)); // 12 hours
	document.cookie = 'SPcatfish=1; expires=' + expire.toGMTString() + '; path=/';
	return false;
}

function destroycatfishnoreturn()
{
	destroycatfish();
	var expire = new Date();
	expire.setTime(expire.getTime() + (86400000 * 365));
	document.cookie = 'SPsub=1; expires=' + expire.toGMTString() + '; path=/';
	return false;
}

function catfishlaunchpopup()
{
	//open the window
	pp = window.open(this.href, 'catfishpopup', 'scrollbars,width=560,height=420,resizable');
	//if it opened successfully, log conversion and don't follow the link
	if(pp && !pp.closed)
	{
		var logu = document.createElement('img');
		logu.src = '/popup/popuplog.php?converted=1&uid=' + catfishuid;
		if(typeof event != 'undefined') { event.cancelBubble = true; }//for IE
		return false;
	}
	//otherwise just follow the link
	else
	{
		return true;
	}
}

popupcookiesenabled = false;
var expire = new Date();
expire.setTime(expire.getTime() + (10000));
document.cookie = 'SPtestcookie=1; expires=' + expire.toGMTString() + '; path=/';
cookiearray = document.cookie.split(/;\s*/);
for (entry in cookiearray)
{
	if (cookiearray[entry] == 'SPtestcookie=1') popupcookiesenabled = true;
}
if (popupcookiesenabled)
{
	catfish = createcatfish();
	catfisholdonload = window.onload;
	window.onload = function ()
	{
		deploycatfish(catfish);
		if (catfisholdonload) catfisholdonload();
	}
}

// Specific to this catfish

function catfishpopulate(catfish)
{
	catfish.style.backgroundImage = 'url(http://www.easyatm.com.tw/templates/easyatm/skins/default/images/catfish_bg.gif)';

	catfish.style.height = '65px';
	catfish.style.padding = '0';

	var btitle = 'EastATM';
	var imgsrc = 'http://www.easyatm.com.tw/templates/easyatm/skins/default/images/catfish_logo.gif';
	var linkurl = 'http://www.easyatm.com.tw/announce/index.php?keyid=0&announceid=32';
	var tagtext = 'Killer ideas to increase your freelance revenue!';
	var thisname = window.location.pathname;
	if( thisname == '/portal.html' )
		var calltoaction = '本公司聯絡電話改成 (02) 2700-1943';
	else
		var calltoaction = '小技巧：轉帳記得要去撿好康！所有銀行、郵局的晶片金融卡，可以去任何一家有優惠活動的網路ATM銀行使用喔~~例：郵局的金融卡到台灣銀行的網路ATM成功轉帳，就可以參加台銀的10萬元現金抽獎！';

	// create goaway
	ul = document.createElement('ul');
	l1 = document.createElement('li');
	l2 = document.createElement('li');
	closelink = document.createElement('a');
	closelink.appendChild(document.createTextNode('Close'));
	closelink.href = '?';
	closelink.onclick = destroycatfish;
	closelink.style.color = '#C7F1FE';
	//nomorelink = document.createElement('a');
	//nomorelink.appendChild(document.createTextNode('Don\'t show this again'));
	//nomorelink.href = '?';
	//nomorelink.onclick = destroycatfishnoreturn;
	//nomorelink.style.color = '#C7F1FE';
	l1.appendChild(closelink);
	//l2.appendChild(nomorelink);
	ul.appendChild(l1);
	//ul.appendChild(l2);
	ul.style.styleFloat = ul.style.cssFloat = 'right';
	ul.style.margin = '31px 16px 0 0';
	ul.style.fontSize = 'x-small';
	ul.style.listStyleType = 'none';
	ul.style.padding = '0';
	catfish.appendChild(ul);

	a = document.createElement('a');
	img = document.createElement('img');
	img.src = imgsrc;
	img.alt = btitle;
	a.href = linkurl;
	a.title = calltoaction;
	a.onclick = catfishlaunchpopup;
	a.appendChild(img);
	a.style.styleFloat = a.style.cssFloat = 'left';
	a.style.display = 'block';
	//a.style.padding = '27px 10px 0 184px';
	catfish.appendChild(a);

	var p = document.createElement('p');
	var a = document.createElement('a');
	a.href = linkurl;
	a.title = btitle;
	a.onclick = catfishlaunchpopup;
	a.appendChild(document.createTextNode(calltoaction));
	a.style.color = '#ffffcc';
	a.style.fontSize = '15px';
	a.style.textAlign = 'left';
	if (screen.width >= 1024)
	{
		//p.appendChild(document.createTextNode(tagtext));
		p.style.color = '#DAEEF3';
		//p.appendChild(document.createElement('br'));
	}
	//var img2 = document.createElement('img');
	//img2.src = '/images/icons/pdf5.gif';
	//img2.alt = 'PDF';
	//img2.style.verticalAlign = 'middle';
	//p.appendChild(img2);
	p.appendChild(document.createTextNode(' '));
	p.appendChild(a);
	p.style.margin = '0';
	p.style.padding = '20px 0 0 0';
	p.style.lineHeight = '20px';
	p.style.width = '900px';
	catfish.appendChild(p);
}

function getcatfishoverlap()
// returns how many pixels this catfish overlaps the content by
{
	return 22;
}