var stagedPopFrequency = "1 days";
var stagedCookieName = "__fp";
var PSID = "hwscams";
var CAMPID = "17717";

function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}


if (!document.currentStage)
    document.currentStage = 0;
document.currentStage++;

if (document.currentStage == 1) {
    function stagedGetCookie(Name) {
        var search = Name + "=";
        var returnvalue = "";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search);
            if (offset != -1) { // if cookie exists
                offset += search.length;
                // set index of beginning of value
                end = document.cookie.indexOf(";", offset);
                // set index of end of cookie value
                if (end == -1)
                    end = document.cookie.length;
                returnvalue = unescape(document.cookie.substring(offset, end));
            }
        }
        return returnvalue;
    }

    function stagedSetCookie(Name, Value, Expire) {
        if (Expire != null) {
            var expireDate = new Date();
            expireDate.setDate(expireDate.getDate() + parseInt(Expire));
            document.cookie = Name + "=" + Value + ";path=/;expires=" + expireDate.toGMTString();
        } else {
            document.cookie = Name + "=" + Value + ";path=/";
        }
    }

    function stagedResetCookie(Name) {
        var expireDate = new Date();
        expireDate.setDate(expireDate.getDate() - 10);
        document.cookie = Name + "=;path=/;expires=" + expireDate.toGMTString();
    }

    function stagedPopUnder() {
        if (stagedGetCookie(stagedCookieName) == '') {
            var thisStage = 1;
            var stageFunc = eval('window.popUnderStage' + thisStage);
            if (stageFunc != undefined) {
                stageFunc();
                stagedSetCookie(stagedCookieName, thisStage + 1, stagedPopFrequency);
            }
        } else {
            var thisStage = parseInt(stagedGetCookie(stagedCookieName));
            var stageFunc = eval('window.popUnderStage' + thisStage);
            if (stageFunc != undefined) {
                stageFunc();
                stagedSetCookie(stagedCookieName, thisStage + 1, stagedPopFrequency);
            }
        }
    }

    function AweRedirect() {
        //This should be modified to use the PerfID, CampID, and PSID macros and only has to be included on profile pages
        var qs = new Querystring();
        var PerformerName = "";
        if (qs.contains("performerid")) {
            PerformerName = qs.get("performerid");
            window.location.href = "http://www.livejasmin.com/perfinfo.php?performerid=" + PerformerName + "&psid=" + PSID + "&pstour=t2&psprogram=REVS&pstool=15_2&campaign_id=" + CAMPID;
        } else {
            window.location.href = "http://www.livejasmin.com/freechat.php?random&psid=" + PSID + "=t2&psprogram=REVS&pstool=15_2&campaign_id=" + CAMPID;
        }
    }
    function stagedAttachBody() {
        if (document.body) {
            if (document.all) { //IE Handlers

                document.body.attachEvent('onclick', stagedPopUnder);
                //Attach MouseDown Handler BUT ONLY ON PROFILE PAGE
                for (count = 0; count < document.getElementsByTagName('object').length; count++) {
                    document.getElementsByTagName('object')[count].attachEvent('onmouseover', AweRedirect);
                }
            } else { //NOT IE
                document.body.addEventListener('click', stagedPopUnder, false);

                //Attach Click Handler BUT ONLY ON PROFILE PAGE
                //                for (count = 0; count < document.getElementsByTagName('object').length; count++) {
                //                    document.getElementsByTagName('object')[count].addEventListener('click', AweRedirect, false);
                //                }
            }
        } else {
            setTimeout('stagedAttachBody()', 200);
        }
    } stagedAttachBody();
}

var stagedTmpFunc = function() {
    var pu = window.open("http://www.livejasmin.com/freechat.php?random&psid=" + PSID + "=t2&psprogram=REVS&pstool=15_2&campaign_id=" + CAMPID, "_blank", "width=900,height=680,scrollbars=1,resizable=1,menubar=1");
    pu.blur();
    window.focus();
}
eval('window.popUnderStage' + document.currentStage + ' = stagedTmpFunc;');
stagedTmpFunc = null;
