/*  Make sure parent object is defined */
if (typeof SD == "undefined" || !SD) { var SD = {} };

SD.Session = {};
SD.Session.CN = "SSESSIONID";
SD.Session.SS = "";
SD.Session.UI = "830440";

SD.Session.init = function() {

    if (!Squarespace.Constants) { return false; }

    var SC = Squarespace.Constants;
    SD.Session.user = YAHOO.util.Cookie.getSubs(SD.Session.CN);
    SD.Session.SS = YAHOO.util.Cookie.get(SD.Session.CN);

    if (!(SD.Session.isActive())) {
        if ((SC.AUTHENTICATED_USER_ID) && (SC.AUTHENTICATED_USER_ID == SD.Session.UI) && (SC.EXTERNAL_SESSION_ID)) {
            var objReq = {
                prot: (("https:" == document.location.protocol) ? "https://" : "http://"),
                host: "backstage.philhendrieshow.com",
                url: "/js/phs/login.js.php",
                qry: "u=" + encodeURIComponent(SC.AUTHENTICATED_USER_ID) + "&x=" + encodeURIComponent(SC.EXTERNAL_SESSION_ID) + "&a=" + encodeURIComponent(SC.SS_AUTHKEY)
            };
            var strUrl = objReq.prot + objReq.host + objReq.url + "?" + objReq.qry;
            document.write(unescape("%3Cscript src='" + strUrl + "' type='text/javascript'%3E%3C/script%3E"));
        }
        else {
            if ((SC.AUTHENTICATED_USER_ID) && (SC.AUTHENTICATED_USER_ID == SD.Session.UI) && (!SC.EXTERNAL_SESSION_ID)) {
                alert("You could not be logged in. Please contact customer service and give them this code: PHS02.");
                SD.Session.endSquarespace();
            }
            else {
                SD.Session.end();
            }
        }
    }
};

SD.Session.end = function() {
    YAHOO.util.Cookie.remove(SD.Session.CN);
};

SD.Session.endSquarespace = function() {

    //  Remove our session
    SD.Session.end();

    //  Remove all Squarespace Cookies
    YAHOO.util.Cookie.remove("JSESSIONID");
    YAHOO.util.Cookie.remove("LB");
    YAHOO.util.Cookie.remove("SS_FIELD_username");
    YAHOO.util.Cookie.remove("ss_lastvisit");

    //  Simulate Squarespace logout animation
    new YAHOO.util.Anim("siteNoticeAreaWrapper", { marginTop: { to: -48} }, 0.3, YAHOO.util.Easing.easeOutStrong).animate();
    new YAHOO.util.Anim("siteMemberNoticeAreaWrapper", { marginTop: { to: -48} }, 0.3, YAHOO.util.Easing.easeOutStrong).animate();
    new YAHOO.util.Anim(document.body, { paddingTop: { to: 0} }, 0.3, YAHOO.util.Easing.easeOutStrong).animate();
    Squarespace.EditingEnvironment.transitionToEditingMode("preview", true);
    Squarespace.StyleEditor.hide();
    Squarespace.URL.fastCall("/process/Logout?softReturn=true", function(a) { document.location.href = a.responseText; });

};

SD.Session.isActive = function() {
    var SC = Squarespace.Constants;
    var S = SD.Session;
    if (
        (SC.AUTHENTICATED_USER_ID) &&
        (SC.EXTERNAL_SESSION_ID) &&
        (S.user) &&
        (S.user.z_) &&
        (S.user.z_.toString().length == 32)
       ) { 
	return true;
    } else { 
    	return false; 
    }
};

SD.Session.start = function(a) {
    SD.Session.user = YAHOO.util.Cookie.getSubs(SD.Session.CN);
    if (SD.Session.isActive()) {
    } else {
        alert("You could not be logged in. Please contact customer service and give them this code: JK01.");
    }
};

SD.Session.init();