﻿//Copyright 2008 Yontoo technology, Inc.

function PreloadImage(imgSrc)
{
    var img = new Image();
    img.src = imgSrc;
}
function PreloadImages()
{
    PreloadImage("http://images.pagerage.com/Site/layerboxes/applybuttonro.jpg");
    PreloadImage("http://images.pagerage.com/Site/layerboxes/donebuttonro.jpg");
    PreloadImage("http://images.pagerage.com/Site/layerboxes/installbuttonro.jpg");
    PreloadImage("http://images.pagerage.com/Site/layerboxes/previewbuttonro.jpg");
    PreloadImage("http://images.pagerage.com/Site/layerboxes/selectbuttonro.jpg");
    PreloadImage("http://images.pagerage.com/Site/layerboxes/cancelbuttonro.jpg");
}
PreloadImages();

function getInstallId()
{
    try
    {
        if (typeof(yontooAPI) == "undefined")
        {
            try
            {
                if (window.ActiveXObject)
                {
                    yontooAPI=new ActiveXObject("YontooIEClient.Api");
                }
            }
            catch(ex)
            {
            }
        }

        if (typeof(yontooAPI) != "undefined")
        {
            var id = yontooAPI.getInstallId();
            return id;
        }
        else
        {
            var idElement = document.getElementById("YontooInstallID");
            return idElement.textContent;
        }
    }
    catch(ex)
    {
    }
    return "";
}

var currentApplyDiv = null;
var currentLayoutCompleteDiv = null;
var currentLayoutTable = null;
var currentBusyDiv = null;

var pendingMoniker = null;
var pendingColor = null;

var pushedDivs = new Array();

function pushDiv(div)
{
    pushedDivs.push(div);
}

function clearPushedDivs()
{
    while(pushedDivs.length > 0)
    {
        var div = pushedDivs.pop();
        div.style.display="none";
    }
}

function showLayoutCompleteMessage()
{
    if ((typeof (currentLayoutCompleteDiv) == "undefined") || (typeof (currentLayoutTable) == "undefined") || (currentLayoutCompleteDiv == null) || (currentLayoutTable == null)) {
        alert("Layout change complete.");
    }
    else
    {
        GlobalShowLayoutDiv(currentLayoutCompleteDiv, currentLayoutTable, null);
    }
}

function setCookie(name, value, expMinutes, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());

    if (expMinutes) {
        expMinutes = expMinutes * 1000 * 60;
    }
    var expires_date = new Date(today.getTime() + (expMinutes));

    document.cookie = name + "=" + escape(value) +
                ((expMinutes) ? ";expires=" + expires_date.toGMTString() : "") +
                ((path) ? ";path=" + path : "") +
                ((domain) ? ";domain=" + domain : "") +
                ((secure) ? ";secure" : "");
}

function HandleReadyStateChange()
{
    try
    {
        if (xhrSetFBLayoutBrowse.readyState == 4)
        {
            if (xhrSetFBLayoutBrowse.status == 200)
            {
                switch (xhrSetFBLayoutBrowse.responseText)
                {
                    case "Success":
                        var id = getInstallId();
                        if ((id == null) || (id.length == 0)){
                            alert("Please make sure you have Yontoo Layers Installed before selecting a Layout.");
                        }
                        else if ((navigator.appVersion.indexOf('MSIE 6.0') > -1) && (navigator.appVersion.indexOf('MSIE 7.0') == -1) && (navigator.appVersion.indexOf('MSIE 8.0') == -1)) {
                            alert("PageRage is not currently supported on IE 6 or earlier. ");
                        }
                        else {
                            showLayoutCompleteMessage();
                        }
                        break;
                    case "LayoutRemoved":
                        alert("Layout has been removed.");
                        break;
                    case "LayoutNotFound":
                        alert("Sorry, that layout could not be found.");
                        break;
                    case "AuthFail":
                        setCookie("prLayoutMoniker", pendingMoniker, 60, '/', 'pagerage.com', '');
                        setCookie("prLayoutColor", pendingColor, 60, '/', 'pagerage.com', '');
                        showLayoutCompleteMessage();
                        break;
                    default:
                        alert("Unexpected Error. Please contact PageRage support if this continues. [11]");
                        break;
                }
            }
            else
            {
                alert("Unexpected Error. Please contact PageRage support if this continues. [12][" + xhrSetFBLayoutBrowse.status + "]");
            }
            if (currentBusyDiv != null) {
                currentBusyDiv.style.display = "none";
            }
        }
    }
    catch(ex)
    {
        alert("An error occured while setting the layout [3].");
        //alert(ex);
    }
}

function isIE() {
    try {
        if (window.ActiveXObject)
            return true;
        else
            return false;
    }
    catch (ex) {
    }
    return false;
};

function isFF() {
    try {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("firefox") != -1)
            return true;
        else
            return false;
    }
    catch (ex) {
    }
    return false;
};

function isWin() {
    try {
        if (navigator.appVersion.toLowerCase().indexOf("windows") >= 0) {
            return true;
        }
    }
    catch (ex) {
    }
    return false;
};

function startInstall(){
    if (isWin()) {
        document.location.href = "http://download.yontoo.com/YontooClientSetup.exe", "InstallYontooLayers";
        if (isFF()) {
            setTimeout("document.location.href = 'http://www.pagerage.com/InstallYontooPluginFF.aspx', 'InstallYontooLayers';", 500);
        }
        else {
            setTimeout("document.location.href = 'http://www.pagerage.com/InstallYontooPluginIE.aspx', 'InstallYontooLayers';", 500);
        }
    }
    else {
        document.location.href = "http://download.yontoo.com/YontooFFClient.xpi", "InstallYontooLayers";
    }
};

function AjaxApplyLayout(strLayoutMoniker, strLayoutColor, layoutCompleteDivId, layoutTableId, busyDiv, bOld, bNew, bRemove, layoutDescription, thumbFileName) {

    try {


        var id = getInstallId();
        if ((id == null) || (id.length == 0)) {
            setCookie("prLayoutMoniker", strLayoutMoniker, 60, '/', 'pagerage.com', '');
            setCookie("prLayoutColor", strLayoutColor, 60, '/', 'pagerage.com', '');
            startInstall();
            return;
        }

        pendingMoniker = strLayoutMoniker;
        pendingColor = strLayoutColor;

        if (bRemove) {
            pendingMoniker = "remove";
            pendingColor = "remove";
        }

        currentLayoutCompleteDiv = document.getElementById(layoutCompleteDivId);
        currentLayoutTable = document.getElementById(layoutTableId);
        currentBusyDiv = document.getElementById(busyDiv);
        if (window.ActiveXObject) {
            try {
                xhrSetFBLayoutBrowse = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (ex) {
                try {
                    xhrSetFBLayoutBrowse = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (ex2) {
                }
            }
        }
        else {
            xhrSetFBLayoutBrowse = new XMLHttpRequest();
        }
        if (xhrSetFBLayoutBrowse != null) {
            var params = new Array();

            params.push("LayoutMoniker=");
            params.push(escape(encodeURI(strLayoutMoniker)));
            params.push("&LayoutColor=");
            params.push(escape(encodeURI(strLayoutColor)));
            params.push("&Remove=");
            params.push(bRemove);
            params.push("&Old=");
            params.push(bOld);
            params.push("&New=");
            params.push(bNew);
            params.push("&LayoutDescription=");
            params.push(layoutDescription);
            params.push("&ThumbFileName=");
            params.push(thumbFileName);

            var strParams = params.join('');

            xhrSetFBLayoutBrowse.onreadystatechange = HandleReadyStateChange;
            xhrSetFBLayoutBrowse.open("POST", "http://www.pagerage.com/SetFacebookLayout.ashx", true);
            xhrSetFBLayoutBrowse.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhrSetFBLayoutBrowse.setRequestHeader("Content-length", strParams.length);
            xhrSetFBLayoutBrowse.setRequestHeader("Connection", "close");
            xhrSetFBLayoutBrowse.send(strParams);
            if ((currentBusyDiv != null) && (currentLayoutTable != null))
                GlobalShowLayoutDiv(currentBusyDiv, currentLayoutTable, null);
        }
    }
    catch (ex) {
    }
}

function GetFullTop(node)
{
    var offset = 0;
    while (node.offsetParent != null)
    {
        offset += node.offsetTop;
        node = node.offsetParent;
    }
    return offset;
}

function GetFullLeft(node)
{
    var offset = 0;
    while (node.offsetParent != null)
    {
        offset += node.offsetLeft;
        node = node.offsetParent;
    }
    return offset;
}

var bRemove = false;

function MakeRemove()
{
    bRemove = true;
}

function GlobalShowLayoutDiv(div, table)
{
    div.style.position="absolute";
    clearPushedDivs();

    div.style.display="inline";
    pushDiv(div);
}

