﻿try {
    if (typeof (PageRageApplyLayout) == "undefined") {
        // ================ PageRageApplyLayout Class ================
        PageRageApplyLayout =
        {
            layoutId: null,
            fbID: null,
            fbUserName: null,
            xhrApplyLayout: null,

            debugMsg: function (msg) {
                //alert(msg);
            },
            doWhenTrue: function (pred, continuation) {
                if (pred()) { continuation(); }
                else { setTimeout(function () { PageRageApplyLayout.doWhenTrue(pred, continuation); }, 100); }
            },

            installId: "",
            getInstallId: function () { //LATER maybe move this, plus enabledisable, setglobalpagerage to a shared js file.
                try {
                    if (PageRageApplyLayout.installId != "") {
                        return PageRageApplyLayout.installId;
                    }

                    if (typeof (yontooAPI) == "undefined") {
                        try {
                            if (window.ActiveXObject) {
                                yontooAPI = new ActiveXObject("YontooIEClient.Api");
                            }
                        }
                        catch (ex) {
                        }
                    }
                    if (typeof (yontooAPI) != "undefined") {
                        PageRageApplyLayout.installId = yontooAPI.getInstallId();
                    }
                    else {
                        PageRageApplyLayout.setInstallIdFF();
                    }
                    return PageRageApplyLayout.installId;
                }
                catch (ex2) {
                    PageRageApplyLayout.debugMsg("ApplyLayout.js [1]:" + ex2);
                }
                return "";
            },
            setInstallIdFF: function () {
                var idElement = document.getElementById("YontooInstallID");
                if (idElement !== null) {
                    PageRageApplyLayout.installId = idElement.textContent;
                }
            },
            isInstallIdReady: function () {
                return PageRageApplyLayout.getInstallId() != "";
            },

            setCookie: function (name, value, expMinutes, path, domain, secure) {
                try {
                    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" : "");
                }
                catch (ex) {
                    PageRageApplyLayout.debugMsg("ApplyLayout.js [2]:" + ex);
                }

            },

            getCookie: function (check_name) {
                try {
                    var a_all_cookies = document.cookie.split(';');
                    var a_temp_cookie = '';
                    var cookie_name = '';
                    var cookie_value = '';
                    var b_cookie_found = false;

                    for (i = 0; i < a_all_cookies.length; i++) {
                        a_temp_cookie = a_all_cookies[i].split('=');
                        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
                        if (cookie_name == check_name) {
                            b_cookie_found = true;
                            if (a_temp_cookie.length > 1) {
                                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
                            }
                            return cookie_value;
                        }
                        a_temp_cookie = null;
                        cookie_name = '';
                    }
                    if (!b_cookie_found) {
                        return null;
                    }
                }
                catch (ex) {
                    PageRageApplyLayout.debugMsg("ApplyLayout.js [2]:" + ex);
                }
            },

            isIE: function () {
                if ((navigator.appVersion.indexOf('MSIE 7.0') != -1) || (navigator.appVersion.indexOf('MSIE 8.0') != -1)) {
                    return true;
                }
                else {
                    return false;
                }
            },

            getLayoutCookie: function () {
                try {
                    PageRageApplyLayout.layoutId = PageRageApplyLayout.getCookie("prLayoutId", '/', 'pagerage.com');
                    PageRageApplyLayout.setCookie("prLayoutId", "", 1, '/', 'pagerage.com', '');
                    var extension = PageRageApplyLayout.getCookie("prExtension", '/', 'pagerage.com');
                    if (extension) {
                        PageRageApplyLayout.setCookie("prExtension", extension, 1, '/', 'pagerage.com', '');
                    }
                }
                catch (ex) {
                    PageRageApplyLayout.debugMsg("ApplyLayout.js [3]:" + ex);
                }
            },

            sendLayoutUpdate: function (nRetry, channelSource) {
                try {
                    if (PageRageApplyLayout.layoutId == "remove") {
                        return;
                    }
                    var frames = window.parent.frames;
                    for (var i = 0; i < frames.length; i++) {
                        try {
                            if (frames[i].document.location.href.split("#")[0] == "http://www.pagerage.com/FrameComm.aspx") {
                                var url;

                                url = "http://www.facebook.com/facebook?fcn=LayoutSelected&layoutId=" + PageRageApplyLayout.layoutId;

                                if (channelSource != "") {
                                    url = url + "&Channel=" + escape(channelSource);
                                }
                                frames[i].document.location.href = url;
                                return;
                            }
                        }
                        catch (ex) {
                        }
                    }
                    if (nRetry > 0) {
                        setTimeout("PageRageApplyLayout.sendLayoutUpdate(" + (nRetry - 1) + ",'" + channelSource + "');", 500);
                    }
                }
                catch (ex3) {
                    PageRageApplyLayout.debugMsg("sendLayoutUpdate exception outer: " + ex3);
                }
            },

            redirectToProfile: function () {
                if (PageRageApplyLayout.fbId === null) {
                    top.location.href = "http://www.facebook.com/profile.php";
                }
                else {
                    top.location.href = "http://www.facebook.com/profile.php?id=" + PageRageApplyLayout.fbId;
                }
            },

            applyPendingLayout: function () {
                try {
                    PageRageApplyLayout.getLayoutCookie();

                    if ((PageRageApplyLayout.layoutId === null) || (PageRageApplyLayout.layoutId.length === 0)) {
                        return false;
                    }

                    PageRageApplyLayout.doWhenTrue(
                        PageRageApplyLayout.isInstallIdReady,
                        PageRageApplyLayout.applyAndUpdate);
                }
                catch (ex4) {
                    PageRageApplyLayout.debugMsg("ApplyLayout.js [4]:" + ex4);
                }
                return false;
            },
            recordChannel: function () {
                PageRageApplyLayout.y2channel = "";
                if (PageRageApplyLayout.xhrApplyLayout.responseText != "#") {
                    PageRageApplyLayout.y2channel = PageRageApplyLayout.xhrApplyLayout.responseText; //LATER here's where we need to ship back the layout title (and anything else we need for the share). Maybe just construct the whole url server-side.
                }
            },
            applyAndUpdate: function () {
                PageRageApplyLayout.ajaxApplyLayout(PageRageApplyLayout.layoutId, false, PageRageApplyLayout.recordChannel, true);
                PageRageApplyLayout.sendLayoutUpdate(20, PageRageApplyLayout.y2channel);
            },
            createXhr: function () {
                if (PageRageApplyLayout.xhrApplyLayout === null) {
                    if (window.ActiveXObject) {
                        try {
                            PageRageApplyLayout.xhrApplyLayout = new ActiveXObject("Msxml2.XMLHTTP");
                        }
                        catch (ex) {
                            try {
                                PageRageApplyLayout.xhrApplyLayout = new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            catch (ex2) {
                            }
                        }
                    } else {
                        PageRageApplyLayout.xhrApplyLayout = new XMLHttpRequest();
                    }
                }
            },

            y2Channel: "",
            ajaxApplyLayout: function (layoutId, async, changeHandler, getSharer) {
                try {
                    PageRageApplyLayout.createXhr();

                    var params = [];
                    var clientId = PageRageApplyLayout.getInstallId();
                    params.push("LayoutId=");
                    params.push(escape(encodeURI(layoutId)));
                    params.push("&clientId=");
                    params.push(clientId);
                    params.push("&share=");
                    params.push(getSharer);

                    var strParams = params.join('');
                    if ((typeof (changeHandler) === "undefined") || (changeHandler === null)) {
                        changeHandler = function () { };
                    }

                    PageRageApplyLayout.xhrApplyLayout.open("POST", "http://www.pagerage.com/SetFacebookLayout.ashx", async);
                    PageRageApplyLayout.xhrApplyLayout.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    PageRageApplyLayout.xhrApplyLayout.setRequestHeader("Content-length", strParams.length);
                    PageRageApplyLayout.xhrApplyLayout.setRequestHeader("Connection", "close");
                    if (async) {
                        PageRageApplyLayout.xhrApplyLayout.onreadystatechange = changeHandler;
                    } else {
                        PageRageApplyLayout.xhrApplyLayout.onreadystatechange = function () { };
                    }
                    PageRageApplyLayout.xhrApplyLayout.send(strParams);
                    if (!async) {
                        changeHandler();
                    }
                } catch (ex) { //alert("aal: " + ex); 
                }
            }
        };
    }
}
catch (ex) {
    
}
