﻿/*******************************************
Popup Window
Copyright: eDrive Services 2000 - 2009
*******************************************/
var se_mouse_x = 0;
var se_mouse_y = 0;
var se_distance_to_right_edge = 0;
var se_distance_to_bottom = 0;
var se_contextmenu = null;
var se_popWin = {
    imgFiles: ['/images/smarteditor/min.gif', '/images/smarteditor/close.gif', '/images/smarteditor/restore.gif', '/images/smarteditor/resize.gif', '/images/smarteditor/logo.gif', ],
    ajaxBustCache: true,
    ajaxLoading: '<b>Loading content. Please wait...</b>',
    minimiseOrder: 0,
    zIndexValue: 500,
    tObjects: [],
    lastActiveT: {},

    init: function(t) {
        var domWin = document.createElement("div");
        domWin.id = t;
        domWin.className = "se_popWin";
        var domWinHTML = '';
        domWinHTML = '<div class="pw-hnd">';
        domWinHTML += '<div class="pw-logo"><img src="' + this.imgFiles[4] + '" title="eDrive Smart Editor" /></div>';
        domWinHTML += '<div class="pw-titlebar">Popup Window</div>';
        domWinHTML += '<div class="pw-ctrls"><img src="' + this.imgFiles[0] + '" title="Minimize" /><img src="' + this.imgFiles[1] + '" title="Close" /></div>';
        domWinHTML += '</div>';
        domWinHTML += '<div class="pw-content"></div>';
        domWinHTML += '<div class="pw-status"><div class="pw-resize" style="background: transparent url(' + this.imgFiles[3] + ') top right no-repeat;">&nbsp;</div></div>';
        domWinHTML += '</div>';
        domWin.innerHTML = domWinHTML;
        document.getElementById("se_popWinContainer").appendChild(domWin);
        var t = document.getElementById(t);
        var divs = t.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            if (/pw-/.test(divs[i].className)) {
                t[divs[i].className.replace(/pw-/, "")] = divs[i];
            }
        }
        t.hnd._parent = t;
        t.logo._parent = t;
        t.titlebar._parent = t;
        t.resize._parent = t;
        t.ctrls._parent = t;
        t.onclose = function() { return true };
        t.onmousedown = function() { se_popWin.setFocus(this) };
        t.hnd.onmousedown = se_popWin.setupDrag;
        t.resize.onmousedown = se_popWin.setupDrag;
        t.ctrls.onclick = se_popWin.enableControls;
        t.show = function() { se_popWin.show(this) };
        t.hide = function() { se_popWin.hide(this) };
        t.close = function() { se_popWin.close(this) };
        t.setSize = function(w, h) { se_popWin.setSize(this, w, h) };
        t.moveTo = function(x, y) { se_popWin.moveTo(this, x, y) };
        t.isResize = function(bol) { se_popWin.isResize(this, bol) };
        t.isScrolling = function(bol) { se_popWin.isScrolling(this, bol) };
        t.load = function(contentType, contentSource, title) { se_popWin.load(this, contentType, contentSource, title) };
        this.tObjects[this.tObjects.length] = t;
        return t;
    },

    open: function(t, contentType, contentSource, title, attr, reCalcOnload) {
        var d = se_popWin;
        function getValue(Name) {
            var config = new RegExp(Name + "=([^,]+)", "i");
            return (config.test(attr)) ? parseInt(RegExp.$1) : 0;
        }
        if (document.getElementById(t) == null)
            t = this.init(t);
        else
            t = document.getElementById(t)
        this.setFocus(t)
        t.setSize(getValue(("width")), (getValue("height")));
        var xpos = getValue("center") ? "middle" : getValue("left");
        var ypos = getValue("center") ? "middle" : getValue("top");
        if (typeof reCalcOnload != "undefined" && reCalcOnload == "recal" && this.scroll_top == 0) {
            if (window.attachEvent && !window.opera)
                this.addEvent(window, function() { setTimeout(function() { t.moveTo(xpos, ypos) }, 400) }, "load");
            else
                this.addEvent(window, function() { t.moveTo(xpos, ypos) }, "load");
        }
        t.isResize(getValue("resize"));
        t.isScrolling(getValue("scrolling"));
        t.style.visibility = "visible";
        t.style.display = "block";
        t.content.style.display = "block";
        t.moveTo(xpos, ypos);
        t.load(contentType, contentSource, title);
        if (t.state == "minimized" && t.ctrls.firstChild.title == "Restore") {
            t.ctrls.firstChild.setAttribute("src", se_popWin.imgFiles[0]);
            t.ctrls.firstChild.setAttribute("title", "Minimize");
            t.state = "fullview";
        }
        return t;
    },

    setSize: function(t, w, h) {
        t.style.width = Math.max(parseInt(w), 150) + "px";
        t.content.style.height = Math.max(parseInt(h), 100) + "px";
    },

    moveTo: function(t, x, y) {
        this.getViewpoint();
        t.style.left = (x == "middle") ? this.scroll_left + (this.docwidth - t.offsetWidth) / 2 + "px" : this.scroll_left + parseInt(x) + "px";
        t.style.top = (y == "middle") ? this.scroll_top + (this.docheight - t.offsetHeight) / 2 + "px" : this.scroll_top + parseInt(y) + "px";
    },

    isResize: function(t, bol) {
        t.status.style.display = (bol) ? "block" : "none";
        t.resizeBool = (bol) ? 1 : 0;
    },

    isScrolling: function(t, bol) {
        t.content.style.overflow = (bol) ? "auto" : "hidden";
    },

    // loads content into window and set the title 
    // Content types: "inline", "iframe", or "ajax"
    load: function(t, contentType, contentSource, title) {
        if (t.isClosed) {
            alert("Popup Window has been closed, so no window to load contents into. Open/Create the window again.");
            return;
        }
        var contentType = contentType.toLowerCase();
        if (typeof title != "undefined")
            t.titlebar.firstChild.nodeValue = title;
        if (contentType == "inline")
            t.content.innerHTML = contentSource;
        else if (contentType == "div") {
            var inlineDivRef = document.getElementById(contentSource);
            t.content.innerHTML = (inlineDivRef.defaultHTML || inlineDivRef.innerHTML);
            if (!inlineDivRef.defaultHTML)
                inlineDivRef.defaultHTML = inlineDivRef.innerHTML;
            inlineDivRef.innerHTML = "";
            inlineDivRef.style.display = "none";
        }
        else if (contentType == "iframe") {
            t.content.style.overflow = "hidden";
            if (!t.content.firstChild || t.content.firstChild.tagName != "IFRAME")
                t.content.innerHTML = '<iframe src="" frameborder="0" style="margin:0; padding:0; width:100%; height: 100%" name="_iframe-' + t.id + '"></iframe>';
            window.frames["_iframe-" + t.id].location.replace(contentSource);
        }
        else if (contentType == "ajax") {
            this.ajax_connect(contentSource, t);
        }
        t.content.datatype = contentType;
    },

    setupDrag: function(e) {
        var d = se_popWin;
        var t = this._parent;
        d.etarget = this;
        var e = window.event || e;
        d.initmousex = e.clientX;
        d.initmousey = e.clientY;
        d.initx = parseInt(t.offsetLeft);
        d.inity = parseInt(t.offsetTop);
        d.width = parseInt(t.offsetWidth);
        d.contentheight = parseInt(t.content.offsetHeight);
        if (t.content.datatype == "iframe") {
            t.style.backgroundColor = "#FFF";
            t.content.style.visibility = "hidden";
        }
        document.onmousemove = d.getDistance;
        document.onmouseup = function() {
            if (t.content.datatype == "iframe") {
                t.content.style.backgroundColor = "#99B5D1";
                t.content.style.visibility = "visible";
            }
            d.stop();
        }
        return false;
    },

    getDistance: function(e) {
        var d = se_popWin;
        var etarget = d.etarget;
        var e = window.event || e;
        d.distancex = e.clientX - d.initmousex;
        d.distancey = e.clientY - d.initmousey;
        if (etarget.className == "pw-hnd")
            d.move(etarget._parent, e);
        else if (etarget.className == "pw-resize")
            d.resize(etarget._parent, e);
        return false;
    },

    getViewpoint: function() {
        var ie = document.all && !window.opera;
        var domclientWidth = document.documentElement && parseInt(document.documentElement.clientWidth) || 100000;
        this.standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
        this.scroll_top = (ie) ? this.standardbody.scrollTop : window.pageYOffset;
        this.scroll_left = (ie) ? this.standardbody.scrollLeft : window.pageXOffset;
        this.docwidth = (ie) ? this.standardbody.clientWidth : (/Safari/i.test(navigator.userAgent)) ? window.innerWidth : Math.min(domclientWidth, window.innerWidth - 16);
        this.docheight = (ie) ? this.standardbody.clientHeight : window.innerHeight;
    },

    rememberAttributes: function(t) {
        this.getViewpoint();
        t.lastx = parseInt((t.style.left || t.offsetLeft)) - se_popWin.scroll_left;
        t.lasty = parseInt((t.style.top || t.offsetTop)) - se_popWin.scroll_top;
        t.lastwidth = parseInt(t.style.width);
    },

    move: function(t, e) {
        t.style.left = se_popWin.distancex + se_popWin.initx + "px";
        t.style.top = se_popWin.distancey + se_popWin.inity + "px";
    },

    resize: function(t, e) {
        t.style.width = Math.max(se_popWin.width + se_popWin.distancex, 150) + "px";
        t.content.style.height = Math.max(se_popWin.contentheight + se_popWin.distancey, 100) + "px";
    },

    enableControls: function(e) {
        var d = se_popWin;
        var sourceobj = window.event ? window.event.srcElement : e.target;
        if (/Minimize/i.test(sourceobj.getAttribute("title")))
            d.minimise(sourceobj, this._parent);
        else if (/Restore/i.test(sourceobj.getAttribute("title")))
            d.restore(sourceobj, this._parent);
        else if (/Close/i.test(sourceobj.getAttribute("title")))
            d.close(this._parent);
        return false;
    },

    minimise: function(button, t) {
        se_popWin.rememberAttributes(t);
        button.setAttribute("src", se_popWin.imgFiles[2]);
        button.setAttribute("title", "Restore");
        t.state = "minimized";
        t.content.style.display = "none";
        t.status.style.display = "none";
        if (typeof t.minimiseOrder == "undefined") {
            se_popWin.minimiseOrder++;
            t.minimiseOrder = se_popWin.minimiseOrder;
        }
        t.style.left = "10px";
        t.style.width = "200px";
        var windowspacing = t.minimiseOrder * 10;
        t.style.top = se_popWin.scroll_top + se_popWin.docheight - (t.hnd.offsetHeight * t.minimiseOrder) - windowspacing + "px";
    },

    restore: function(button, t) {
        se_popWin.getViewpoint();
        button.setAttribute("src", se_popWin.imgFiles[0]);
        button.setAttribute("title", "Minimize");
        t.state = "fullview";
        t.style.display = "block";
        t.content.style.display = "block";
        if (t.resizeBool)
            t.status.style.display = "block";
        t.style.left = parseInt(t.lastx) + se_popWin.scroll_left + "px";
        t.style.top = parseInt(t.lasty) + se_popWin.scroll_top + "px";
        t.style.width = parseInt(t.lastwidth) + "px";
    },


    close: function(t) {
        try {
            var closePopWin = t.onclose();
        }
        catch (err) {
            var closePopWin = true;
        }
        finally {
            if (typeof closePopWin == "undefined") {
                alert("An error has occured somwhere inside your \"onclose\" event handler");
                var closePopWin = true;
            }
        }
        if (closePopWin) {
            if (t.state != "minimized")
                se_popWin.rememberAttributes(t);
            if (window.frames["_iframe-" + t.id])
                window.frames["_iframe-" + t.id].location.replace("about:blank");
            else
                t.content.innerHTML = "";
            t.style.display = "none";
            t.isClosed = true;
        }
        return closePopWin;
    },


    setOpacity: function(targetobject, value) {
        if (!targetobject)
            return
        if (targetobject.filters && targetobject.filters[0]) {
            ;
            if (typeof targetobject.filters[0].opacity == "number")
                targetobject.filters[0].opacity = value * 100;
            else
                targetobject.style.filter = "alpha(opacity=" + value * 100 + ")";
        }
        else if (typeof targetobject.style.MozOpacity != "undefined")
            targetobject.style.MozOpacity = value;
        else if (typeof targetobject.style.opacity != "undefined")
            targetobject.style.opacity = value;
    },

    setFocus: function(t) {
        this.zIndexValue++;
        t.style.zIndex = this.zIndexValue;
        t.isClosed = false;
        this.setOpacity(this.lastActiveT.hnd, 0.5);
        this.setOpacity(t.hnd, 1);
        this.lastActiveT = t;
    },


    show: function(t) {
        if (t.isClosed) {
            alert("Popup Window has been closed, so nothing to show. Open/Create the window again.");
            return;
        }
        if (t.lastx)
            se_popWin.restore(t.ctrls.firstChild, t);
        else
            t.style.display = "block";
        this.setFocus(t);
        t.state = "fullview";
    },

    hide: function(t) {
        t.style.display = "none";
    },

    ajax_connect: function(url, t) {
        var page_request = false;
        var bustcacheparameter = "";
        if (window.XMLHttpRequest)
            page_request = new XMLHttpRequest();
        else if (window.ActiveXObject) {
            try {
                page_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    page_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) { }
            }
        }
        else
            return false;
        t.content.innerHTML = this.ajaxLoading;
        page_request.onreadystatechange = function() { se_popWin.ajax_loadPage(page_request, t) };
        if (this.ajaxBustCache)
            bustcacheparameter = (url.indexOf("?") != -1) ? "&" + new Date().getTime() : "?" + new Date().getTime();
        page_request.open('GET', url + bustcacheparameter, true);
        page_request.send(null);
    },

    ajax_loadPage: function(page_request, t) {
        if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)) {
            t.content.innerHTML = page_request.responseText;
        }
    },


    stop: function() {
        se_popWin.etarget = null;
        document.onmousemove = null;
        document.onmouseup = null;
    },

    addEvent: function(target, functionref, tasktype) {
        var tasktype = (window.addEventListener) ? tasktype : "on" + tasktype;
        if (target.addEventListener)
            target.addEventListener(tasktype, functionref, false);
        else if (target.attachEvent)
            target.attachEvent(tasktype, functionref);
    },

    cleanup: function() {
        for (var i = 0; i < se_popWin.tObjects.length; i++) {
            se_popWin.tObjects[i].hnd._parent = se_popWin.tObjects[i].resize._parent = se_popWin.tObjects[i].ctrls._parent = null;
        }
        window.onload = null;
    }
}

var se_popWinModal = {
    overlayStack: 0,
    open: function(t, contentType, contentSource, title, attr, reCalcOnload) {
        var d = se_popWin;
        this.popWinOverlay = document.getElementById("se_popWinOverlay");
        this.overlayStack++;
        this.loadPopWinOverlay();
        if (reCalcOnload == "recal" && d.scroll_top == 0)
            d.addEvent(window, function() { se_popWinModal.adjustPopWinOverlay() }, "load");
        var t = d.open(t, contentType, contentSource, title, attr, reCalcOnload);
        t.ctrls.firstChild.style.display = "none";
        t.ctrls.onclick = function() { se_popWinModal.close(this._parent, true) };
        t.show = function() { se_popWinModal.show(this) };
        t.hide = function() { se_popWinModal.close(this) };
        return t
    },


    loadPopWinOverlay: function() {
        try {
            var d = se_popWin;
            d.getViewpoint();
            this.docheightcomplete = (d.standardbody.offsetHeight > d.standardbody.scrollHeight) ? d.standardbody.offsetHeight : d.standardbody.scrollHeight;
            this.popWinOverlay.style.width = d.docwidth + "px";
            this.popWinOverlay.style.height = this.docheightcomplete + "px";
            this.popWinOverlay.style.left = 0;
            this.popWinOverlay.style.top = 0;
            this.popWinOverlay.style.visibility = "visible";
            this.popWinOverlay.style.display = "block";
        } catch (e) { }
    },

    adjustPopWinOverlay: function() {
        if (this.popWinOverlay != null && this.popWinOverlay.style.display == "block")
            this.loadPopWinOverlay();
    },

    closePopWinOverlay: function() {
        this.overlayStack--;
        if (this.overlayStack == 0)
            this.popWinOverlay.style.display = "none";
    },


    close: function(t, forceclose) {
        t.contentDoc = (t.content.datatype == "iframe") ? window.frames["_iframe-" + t.id].document : t.content;
        if (typeof forceclose != "undefined")
            t.onclose = function() { return true };
        if (se_popWin.close(t))
            this.closePopWinOverlay();
    },


    show: function(t) {
        se_popWinModal.overlayStack++;
        se_popWinModal.loadPopWinOverlay();
        se_popWin.show(t);
    }
}
se_popWin.addEvent(window, function() { if (typeof se_popWinModal != "undefined") se_popWinModal.adjustPopWinOverlay() }, "resize");
window.onunload = se_popWin.cleanup;
se_addLoadEvent(se_onstart);

function se_onstart() {
    se_contextmenu = document.getElementById('se_cmsMenu');
    se_addGenericEvent(document, 'mousemove', se_mousemove);
    se_addGenericEvent(document, 'click', se_hide_contextmenu);
}

function se_context_menuitem_highlight(element, color) {
    element.className = 'highlight';
}

function se_context_menuitem_unhighlight(element) {
    element.className = '';
}

function se_show_contextmenu(event) {
    se_get_page_boundaries();
    se_contextmenu.style.left = se_mouse_x + "px";
    se_contextmenu.style.top = se_mouse_y + "px";
    se_contextmenu.style.visibility = "visible";
    // adjust menu if near window edge
    if (se_distance_to_right_edge < se_contextmenu.offsetWidth)
        se_contextmenu.style.left = 2 + se_mouse_x - se_contextmenu.offsetWidth + "px";  // The 2+ is not some dumb kludge - 
    if (se_distance_to_bottom < se_contextmenu.offsetHeight)                    // it places the menu just under the pointer,
        se_contextmenu.style.top = 2 + se_mouse_y - se_contextmenu.offsetHeight + "px";  // instead of just outside
    try {
        window.getSelection().collapseToStart();  // try to compensate for tendency to treat right-clicking as text selection
    } catch (e) { } // do nothing
    // prevent the event from bubbling up and causing the regular browser context menu to appear.
    event.cancelBubble = true;
    if (event.stopPropagation) event.stopPropagation();
    if (event.preventDefault) event.preventDefault();
    return false;
}

function se_hide_contextmenu() {
    se_contextmenu.style.visibility = "hidden";
}

function se_addLoadEvent(func) {
    if (window.addEventListener)
        window.addEventListener("load", func, false);
    else if (document.addEventListener)
        document.addEventListener("load", func, false);
    else if (window.attachEvent)
        window.attachEvent("onload", func);
    else if (document.attachEvent)
        document.attachEvent("onload", func);
}

function se_addGenericEvent(source, trigger, func) {
    if (source.addEventListener)
        source.addEventListener(trigger, func, false);
    else if (source.attachEvent)
        source.attachEvent("on" + trigger, func);
}

function se_window_x() {
    if (window.screenX)
        return window.screenX
    else if (window.screenLeft)
        return window.screenLeft;
}

function se_window_y() {
    if (window.screenY)
        return window.screenY
    else if (window.screenTop)
        return window.screenTop;
}

function se_mousemove(e) {

    if (e && e.clientX && typeof (window.scrollY) == 'number') { // Moz
        se_mouse_x = e.clientX + window.scrollX;
        se_mouse_y = e.clientY + window.scrollY;
        event_target = e.target;
    }
    else if (window.event) { // IE
        if (document.documentElement)   // Explorer 6 Strict
        {
            se_mouse_x = window.event.clientX + document.documentElement.scrollLeft - 4;
            se_mouse_y = window.event.clientY + document.documentElement.scrollTop - 4;
        }
        else if (document.body) // all other Explorers
        {
            se_mouse_x = window.event.clientX + document.body.scrollLeft - 4;
            se_mouse_y = window.event.clientY + document.body.scrollTop - 4;
        }
        mouse_window_x = window.event.clientX;
        mouse_window_y = window.event.clientY;
    }
}

function se_get_page_boundaries() {
    if (window.innerWidth) {
        se_distance_to_right_edge = window.innerWidth - (se_mouse_x - window.scrollX)
        se_distance_to_bottom = window.innerHeight - (se_mouse_y - window.scrollY);
    } else if (document.body.clientWidth) {
        se_distance_to_right_edge = document.body.clientWidth - se_mouse_x;
        se_distance_to_bottom = document.body.clientHeight - se_mouse_y;
    }
}

/* The following lines should be added within the body tag of the page
<div id="popWinOverlay"></div>
<div id="popWinContainer"><span style="display:none">.</span></div>
*/

/* SmartEditor window operners */
function se_Login() { var loginWin = se_popWinModal.open("se_login", "iframe", "/se_login.aspx", "eDrive SmartEditor Login", "width=420px,height=405px,resize=0,scrolling=1,center=1", "recal"); }
function se_Help() { var helpWin = se_popWinModal.open("se_help", "iframe", "/admin/smarteditor/help.aspx", "eDrive SmartEditor Help", "width=840px,height=550px,resize=0,scrolling=1,center=1", "recal"); }
function se_Page(id) { var pageWin = se_popWinModal.open("se_page", "iframe", "/admin/smarteditor/page.aspx?pid=" + id, "eDrive Page SmartEditor", "width=840px,height=550px,resize=0,scrolling=1,center=1", "recal"); }
function se_PageSection(id) { var sectionWin = se_popWinModal.open("se_section", "iframe", "/admin/smarteditor/pagesection.aspx?sid=" + id, "eDrive Content Section SmartEditor", "width=840px,height=550px,resize=0,scrolling=1,center=1", "recal"); }
function se_PageGroup(id, group) { var groupWin = se_popWinModal.open("se_group", "iframe", "/admin/smarteditor/pagegroup.aspx?pid=" + id + "&grp=" + group, "eDrive Content Group SmartEditor", "width=420px,height=405px,resize=0,scrolling=1,center=1", "recal"); }
function se_PageMenu(id) { var menuWin = se_popWinModal.open("se_menu", "iframe", "/admin/smarteditor/pagemenu.aspx?mid=" + id, "eDrive Page Menu SmartEditor", "width=840px,height=550px,resize=0,scrolling=1,center=1", "recal"); }
