// JavaScript Document
// Deploy the Catfish

// The Catfish should be located in an element of id 'catfish' and should be hidden
// out of view

var catfish;

/* 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 deploycatfish()
    // initializing
{
    catfish = document.getElementById('catfish');

    catfishheight = 155; // total height of catfish in pixels
    catfishoverlap = 85; // height of the 'overlap' portion only (semi-transparent)
    catfishtimeout = setTimeout(startcatfish, 1000);
}

function startcatfish()
    // starts the catfish sliding up
{
    catfishposition = 0; // 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 - catfishoverlap) +'px';

    // here you could use AJAX (or similar) to log the popup hit for tracking purposes	
}


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 catfish_lock() {
    //alert('locking');
    var date = new Date();
    date.setTime(date.getTime()+(60*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = "subscribed=yes"+expires+"; path=/";
}

if(readCookie('subscribed') != 'yes') { 
    addLoadEvent(deploycatfish);
}
