﻿/// <reference path="authentication.js"/>
/// <reference path="layout.js"/>
/// <reference path="tools.js"/>
(function () {
    var url = window.location.href.lastIndexOf("?") > 0 ?
    window.location.href.substring(0, window.location.href.lastIndexOf("?")) :
    (window.location.href.lastIndexOf("#") > 0 ? window.location.href.substring(0, window.location.href.lastIndexOf("#")) : window.location.href);
    var loginSpySrc = (tools.getUrlVars().authTest ? (tools.getUrlVars().ssl ? "https://test.iteacher.tw/gs4/loginSpy.htm?" : "http://test.iteacher.tw/gs4/loginSpy.htm?") : "http://web.ischool.com.tw/loginSpy.htm?") + ((tools.getUrlVars().dn && tools.getUrlVars().dn.replace(/^"|\"$/g, '') != "") ? "dn=" + tools.getUrlVars().dn + "&" : "") + "next=" + (url.substring(0, url.lastIndexOf("/")) + "/ischoolWebAuthentication.htm");
    var loginFrame = $("<iframe style='border:0px none;' src='" + loginSpySrc + "' ></iframe>");

    var loginLayer = layout.createNewLayer({ targetEle: loginFrame, visible: auth.getStatus() !== "authorized", fullScreen: true, zindex: 500 });
    loginLayer.onSizeChanged(function (newSize) {
        loginFrame.css({
            height: newSize.height + "px",
            width: newSize.width + "px"
        });
    });
    auth.onStatusChanged(function (newStatus) {
        if (newStatus == "authorized") {
            loginLayer.set({ visible: false });
        }
        else {
            if (newStatus == "unauthorized") {
                loginFrame = $("<iframe style='border:0px none;' src='" + loginSpySrc + "&action=logout' ></iframe>");
                loginLayer.set({ targetEle: loginFrame });
            }
            loginLayer.set({ visible: true });
        }
    });
})();
