﻿

function PopupClose(f)
{
    parent.win.Hide(f);
}

//common use
//**********************************************
function PopupOpen(url, width, height)
{
    win.Show(url, width, height);
}
//**********************************************


//admin onpage popup object
//**********************************************
var admin={
    holderId:"admin",
    Show:function ()
    {
        var hol=$(this.holderId);
        var w=$W(this.holderId);
        var h=$H(this.holderId);
        
        if(hol)
        {
            var x=(document.documentElement.clientWidth-w)/2;
            var y=((document.documentElement.clientHeight-h)/2);

            hol.style.zIndex = 2;
            hol.style.position = "absolute";
            hol.style.left=x + "px";
            hol.style.top=y + "px";
            
            this.AddShadow(this.holderId)
        }
    },
    Hide:function ()
    {
        //var hol=$(this.holderId);
            
        //this.RemoveShadow(this.holderId)
    },
    AddShadow:function(id)
    {
        var holderShadowId=id + "_shadow";
        var holderShadowCenterId=id + "_shadow_center";
        var s=$(holderShadowId);
        if(s)
        {
            
        }
        else
        {
            var sHtml=''
                + '<table id="' + holderShadowId + '" class="shadow" cellpadding="0" cellspacing="0" border="0">'
                + '<tr><td class="TL">&nbsp;</td><td class="TM">&nbsp;</td><td class="TR">&nbsp;</td></tr>'
                + '<tr><td class="CL">&nbsp;</td><td class="CM" id="' + holderShadowCenterId + '">&nbsp;</td><td class="CR">&nbsp;</td></tr>'
                + '<tr><td class="BL">&nbsp;</td><td class="BM">&nbsp;</td><td class="BR">&nbsp;</td></tr>'
                + '</table>';
               
            var hol = document.createElement("span");
            //hol.innerHTML = sHtml;
            
            
            document.body.appendChild(hol);
            hol.innerHTML = sHtml;
            
            
            //return;
            s=$(holderShadowId);
            
        }
        
        var center=$(holderShadowCenterId);
        var host=$(id);
        if(host && s && center)
        {
        
            s.style.left=host.offsetLeft-10 + "px";
            s.style.top=host.offsetTop-9 + "px";
            
            s.style.width=host.offsetWidth + 26 + "px";
            s.style.height=host.offsetHeight + 26 + "px";
            
            center.style.height=host.offsetHeight - 25+ "px";
            
            
        }
    },
    RemoveShadow:function(id)
    {
        var holderShadowId=id + "_shadow";
        var s=$(holderShadowId);
        if(s)
        {
            document.removeChild(s);
        }
    },
    PromptDelete:function(msg)
    {
        return confirm(msg);
    }
}
//**********************************************








//Effects funcs
//**********************************************
var fx = {
    version: '1.0.1',
    interval: 5,
    percent: 50,
    slideX: function(name, x, f)
    {
        this.slide(name, x, true, f);
    },
    slideY: function(name, y, f)
    {
        this.slide(name, y, false, f);
    },
    slide: function(name, n, isX, f)
    {
        var el = $(name);
        if (!$Null(el))
        {
            var curN = (isX) ? $X(name) : $Y(name);
            el.style.position = "absolute";

            var stepN = Math.round(n * this.percent / 100);
            if (Math.abs(stepN) < 1) { stepN = (n > 0) ? 1 : -1; };
            if (isX) el.style.left = (curN + stepN) + "px";
            else el.style.top = (curN + stepN) + "px";
            n = n - stepN;

            if (Math.abs(n) > 0)
            { setTimeout("fx.slide('" + name + "'," + n + "," + isX + ",\"" + f + "\")", this.interval); }
            else if (!$Null(f)) eval(f);
        }
    },

    dimm: function(name, s, e, f)
    {
        var el = $(name);
        if (!$Null(el))
        {
            var n = e - s; //n>0 means fade in
            var stepN = (n > 0) ? 10 : -20;
            var o = s + stepN;
            //normalize opacity 0<0<100
            if (o > 100) o = 100; if (o < 0) o = 0;
            if (o > 100) o = 100; if (o < 0) o = 0;
            this.alfa(name, o);

            if (n > 0) el.style.display = "block"; //show if fade in

            //$("out").innerHTML+="name=" + name + ", op=" + o + ", step=" + stepN + ", s=" + s + ", e=" + e + "<br>";

            if ((n > 0 && o < e) || (n < 0 && o > e))
            {
                setTimeout("fx.dimm(\"" + name + "\"," + o + "," + e + ",\"" + f + "\")", this.interval);
            }
            else
            {
                if (n < 0 && o <= 0) el.style.display = "none";
                if (!$Null(f)) eval(f);
            }
        }
        else if (!$Null(f)) eval(f);
    },
    alfa: function(name, val)
    {
        var el = $(name);
        if (!$Null(el))
        {
            if (document.all)
                el.style.filter = 'alpha(opacity=' + val + ')';
            else
                el.style.opacity = val / 100;
        }
    },
    hide: function(name, f)
    {
        var el = $(name);
        if (!$Null(el))
        {
            var curN = $H(name);
            el.style.overflow = "hidden";
            el.style.paddingTop = "0px";
            el.style.paddingBottom = "0px";

            var stepN = Math.round(curN * this.percent / 100);
            if (stepN < 1) { stepN = 1; };
            if (curN - stepN > 0)
                el.style.height = (curN - stepN) + "px";
            else
                el.style.height = "0px";

            //$("out").innerHTML+="H=" + curN + ", step=" + stepN + "<br>";
            //alert("curN=" + curN + ", stepN=" + stepN)
            if (curN > 0)
            { setTimeout("fx.hide(\"" + name + "\",\"" + f + "\")", this.interval); }
            else
            {
                el.style.display = "none";
                if (!$Null(f)) eval(f);
            }
        }
    },
    show: function(name, f, n)
    {
        var el = $(name);
        if (!$Null(el))
        {
            if ($Null(n))
            {//get original height
                var pos = el.style.position, vis = el.style.visibility;
                el.style.position = "absolute"; el.style.visibility = "hidden"; el.style.display = "block"; el.style.height = "auto";
                n = $H(name);
                el.style.display = "block"; el.style.visibility = vis; el.style.position = pos;
                el.style.height = "0";
            }
            var curN = $H(name);
            el.style.display = "block";
            el.style.overflow = "hidden";

            var stepN = Math.round(n * this.percent / 100);
            if (stepN < 1) { stepN = 1; };
            el.style.height = (curN + stepN) + "px";
            n = n - stepN;
            //alert(curN+stepN)
            if (n > 0)
            { setTimeout("fx.show(\"" + name + "\",\"" + f + "\"," + n + ")", this.interval); }
            else
                if (!$Null(f)) eval(f);
        }
    }
};
//**********************************************





//Popup object
//**********************************************
var win = {
    version: '1.0',
    url: '',
    w: 0, h: 0,
    bg: null,   //main window shade
    pop: null,  //popup object
    head: null, //popup header object
    body: null, //popup body object
    frame: null, //popup inner frame object
    msg: null,  //popup loading message
    Show: function(url, w, h)
    {
        this.url = url;
        this.w = w;
        this.h = h;

        this.showBg();
    },
    Hide: function(f)
    {
        fx.dimm(win.pop.id, 50, 0, "fx.dimm(win.bg.id,50,0,'" + f + "');win.destroyWin();");
        return false;
    },
    showWin: function()
    {
        if (!this.pop)
        {
            this.pop = document.createElement("div");
            this.pop.id = "myPopUp";
            this.pop.className = "myPopUp";
            this.pop.innerHTML = ''
            //+ '<div id="myPopUpHead" class="myPopUpHead" onmousedown="drag.Drag(\'myPopUp\',event)"><a href="#close" class="myPopUpClose" onclick="return win.Hide()">Close</a></div>'
                + '<div id="myPopUpHead" class="myPopUpHead" onmousedown="drag.Drag(\'myPopUp\',event)">&nbsp;</div>'
                + '<div id="myPopUpBody" class="myPopUpBody">'
                + '     <div id="myPopUpMsg" class="myPopUpMsg">&nbsp;</div>'
                + '     <iframe src="' + this.url + '" id="myPopUpFrame" class="myPopUpFrame" allowTransparency="true" frameborder="0" scrolling="no" onload="win.loadUrlComplete()"></iframe>'
                + '</div>';

            document.body.appendChild(this.pop);
            this.body = $("myPopUpBody");
            this.frame = $("myPopUpFrame");
            this.msg = $("myPopUpMsg");
            this.head = $("myPopUpHead");
        }


        //set popup position
        this.pop.style.top = (($B().clientHeight - this.h - 50) / 2 + $B().scrollTop) + "px";
        this.pop.style.left = ($B().clientWidth - this.w) / 2 + "px";

        //set popup dimentions
        this.body.style.height = this.h + "px";
        this.msg.style.height = this.h + "px";
        this.frame.style.height = this.h + "px";

        this.body.style.width = this.w + "px";
        this.msg.style.width = this.w + "px";
        this.frame.style.width = this.w + "px";

        this.showWaitMsg();

        //this.pop.style.display = "block";
        fx.dimm(this.pop.id, 60, 100, "");

        this.head.style.width = this.pop.offsetWidth + "px";

        this.loadUrl();

        return false;
    },
    destroyWin: function()
    {
        document.body.removeChild(this.pop);
        this.pop = null;
    },
    showFrame: function()
    {
        this.frame.style.display = "block";
    },
    showWaitMsg: function()
    {
        this.msg.style.display = "block";
    },
    hideWaitMsg: function()
    {
        this.msg.style.display = "none";
    },
    showBg: function()
    {
        if (!this.bg)
        {
            this.bg = document.createElement("div");
            this.bg.id = "myPopUpBg";
            this.bg.className = "myPopUpBg";
            document.body.appendChild(this.bg);
        }
        this.bg.style.width = document.body.offsetWidth + "px";
        this.bg.style.height = ((document.body.offsetHeight > $B().clientHeight) ? document.body.offsetHeight : $B().clientHeight) + "px";

        fx.dimm(this.bg.id, 0, 50, "win.showWin()");
    },
    loadUrl: function()
    {
        this.frame.src = this.url;
    },
    loadUrlComplete: function()
    {
        this.hideWaitMsg();
        this.showFrame();
    }
};
//**********************************************



//Drag object
//**********************************************
var drag = {
    DragObj: null,
    mouseX: 0,
    mouseY: 0,
    Init:function()
    {
        $AddEvt(document, "mousemove", function(e) { return drag.DoDrag(e); });
        $AddEvt(document, "mouseup", function(e) { return drag.Drop(e); });
    },
    Drag: function(id, e)
    {
        this.DragObj = $(id);
        if (this.DragObj)
        {
            if (!e) e = window.event;
            this.mouseX = document.all ? e.clientX : e.pageX;
            this.mouseY = document.all ? e.clientY : e.pageY;

            this.DragObj.style.position = "absolute";
            $Alfa(this.DragObj.id, 50);

            if (typeof (Custom_OnDrag) == "function")
                Custom_OnDrag(this.DragObj);
        }
    },
    Drop: function(e)
    {
        if (this.DragObj)
        {
            $Alfa(this.DragObj.id, 100);

            if (typeof (Custom_OnDrop) == "function")
                Custom_OnDrop(this.DragObj);
            this.DragObj = null;
        }
    },
    DoDrag: function(e)
    {
        if (this.DragObj)
        {
            $NoSelect();
            if (!e) e = window.event;
            var mX = document.all ? e.clientX : e.pageX;
            var mY = document.all ? e.clientY : e.pageY;
            if (this.ValDiff(mX, this.mouseX) || this.ValDiff(mY, this.mouseY))
            {
                this.MoveBy(mX - this.mouseX, mY - this.mouseY);
                this.mouseX = mX; this.mouseY = mY;
            }
        }
    },
    MoveBy: function(X, Y)
    {
        this.DragObj.style.left = ($X(this.DragObj.id) + X) + "px";
        this.DragObj.style.top = ($Y(this.DragObj.id) + Y) + "px";
    },
    ValDiff: function(val1, val2)
    {
        return (Math.abs(val1 - val2) > 10)
    },
    OffsetLeft: function(obj)
    {
        if (obj)
        {
            var off = obj.offsetLeft;
            return off + this.OffsetLeft(obj.offsetParent);
        }
        else return 0;
    },
    OffsetTop: function(obj)
    {
        if (obj)
        {
            var off = obj.offsetTop;
            return off + this.OffsetTop(obj.offsetParent);
        }
        else return 0;
    }
}


drag.Init();

function Custom_OnDrag(obj)
{
    if (obj && obj.className == "myPopUp")
    {
        win.frame.style.display = "none";
    }
}
function Custom_OnDrop(obj)
{
    if (obj && obj.className == "myPopUp")
    {
        win.frame.style.display = "block";
    }
}
//**********************************************
