﻿function CMSFadeBackGround(ObjID, BackColor, Opacity)
{
    var docDymension = getDocDymension();
    var WinDymension = getWinDymension();
    var ScrlXY = getScrollXY();
    if(BackColor && BackColor=="")BackColor="#ccc"
    if(Opacity && Opacity=="")Opacity="80"
    FadeElement = document.createElement('div');
    FadeElement.id = "divFadeBackGround_" + ObjID;
    FadeElement.setAttribute("name", "divFadeBackGround");
    FadeElement.style.position="absolute";
    FadeElement.className="C_LayerOpacity";
    FadeElement.style.border="none";
    FadeElement.style.left="0px";
    FadeElement.style.top="0px";
    FadeElement.style.zIndex="49999";
    FadeElement.style.width=Math.max(docDymension[0], WinDymension[0]) + "px";
    FadeElement.style.height=Math.max(docDymension[1], WinDymension[1]) + "px";
    FadeElement.style.backgroundColor=BackColor;
    if(!document.all)
    {	FadeElement.style.opacity=String.format(".{0}", Opacity);
        FadeElement.style.moz_opacity=String.format(".{0}", Opacity);
    }
    else FadeElement.style.filter = String.format("alpha(opacity={0})", Opacity);
    document.forms[0].appendChild(FadeElement)
}
function RemoveCMSWindow(ObjID)
{
    var FadeElement = document.getElementById("divFadeBackGround_" + ObjID);
    var WindowLayer = document.getElementById("CMSWindowLayer_" + ObjID);
    if(WindowLayer)document.forms[0].removeChild(WindowLayer);
    if(FadeElement)document.forms[0].removeChild(FadeElement);
}
function CMSOpen(Obj)
{
    RemoveCMSWindow(Obj.id);
    var ObjPosition = getObjectPos(Obj)
    var CustomCorners = Obj.getAttribute("CustomCorners");
    var Title = Obj.getAttribute("CMSTitle");
    var TargetURL = Obj.getAttribute("TargetURL");
    var RoundCorner = Obj.getAttribute("RoundCorner");
    var PinLayer = Obj.getAttribute("PinLayer");
    var LayerWidth = Obj.getAttribute("LayerWidth");
    var LayerHeight = Obj.getAttribute("LayerHeight");
    var PosRelative = Obj.getAttribute("PosRelative");
    var LayerTop = Obj.getAttribute("LayerTop");
    var LayerLeft = Obj.getAttribute("LayerLeft");
    var FadeBackGround = Obj.getAttribute("FBG");
    var FBColor = Obj.getAttribute("FBColor");
    var FBOpacity = Obj.getAttribute("FBOpacity");
    var CloseImageURL = Obj.getAttribute("CloseImageURL");
    var ScroolBars = (Obj.getAttribute("ScroolBars") != "true")?"no":"auto";
    if(!Title) Title="";
    if(FadeBackGround == "true") CMSFadeBackGround(Obj.id, FBColor, FBOpacity);
    if(TargetURL != "" && TargetURL.substr(0,4).toLowerCase()!= "http")TargetURL = document.location.protocol + "//" + document.location.hostname + (document.location.port !=""?":"+document.location.port:"") + "/" + TargetURL;
    if(!LayerWidth || LayerWidth == "" || LayerWidth == "px") LayerWidth = String.format("{0}px", 600);
    if(!LayerHeight || LayerHeight == "" || LayerHeight == "px") LayerHeight = String.format("{0}px", 400);
    if(CloseImageURL && CloseImageURL != "")
    {
        CloseImageURL = CloseImageURL.replace("../" , "")
        CloseImageURL = CloseImageURL.toLowerCase().replace("UserControls/Handler.ashx?path=", "")
        CloseImageURL = document.location.protocol + "//" + document.location.hostname + (document.location.port !=""?":"+document.location.port:"") + "/" + CloseImageURL;
    }
    
    if(LayerLeft && LayerLeft != "" && LayerLeft != "px" && PosRelative == "true") LayerLeft = String.format("{0}px", (ObjPosition[0] + parseFloat(LayerLeft.replace("px", ""))));
    else if(!LayerLeft || LayerLeft == "" || LayerLeft == "px") LayerLeft = String.format("{0}px", ObjPosition[0]);
    if(LayerTop && LayerTop != "" && LayerTop != "px" && PosRelative == "true") LayerTop = String.format("{0}px", (ObjPosition[1] + parseFloat(LayerTop.replace("px", ""))));
    else if(!LayerTop || LayerTop == "" || LayerTop == "px") LayerTop = String.format("{0}px", ObjPosition[3]);
        
    var WindowLayer = document.createElement('div');
    WindowLayer.id = "CMSWindowLayer_" + Obj.id;
    WindowLayer.className = "CMSWindow C_Layer_" + Obj.id;
    WindowLayer.style.top=LayerTop;
    WindowLayer.style.left=LayerLeft;
    WindowLayer.style.zIndex="50000";
    WindowLayer.style.backgroundColor="#fff";
    var WinBody = document.createElement('div');
    WinBody.className = "bd-inner C_LayerBody";
    var Winframe = document.createElement('iframe');
    Winframe.id = "MyCMSWindow_" + Obj.id;
    Winframe.setAttribute("name", Obj.name);
    Winframe.setAttribute("frameBorder", "no");
    Winframe.setAttribute("scrolling", ScroolBars);
    Winframe.className = "C_LayerContent";
    Winframe.style.width=LayerWidth;
    Winframe.style.height=LayerHeight;
    Winframe.style.backgroundColor="#fff";
    Winframe.style.zIndex="50001";
    WinBody.appendChild(Winframe);
    //==============Creating Window Top Bar==============
    var WinCorners = document.createElement('span');
    WinCorners.style.backgroundColor = FBColor;
    WinCorners.className = ((CustomCorners == "true")?"c_corners":"corners") + " tb C_TopBorder";
    if(PinLayer == "false")WinCorners.onmousedown = function(ev)
    {   DrageCMSWindow(ev, document.getElementById("CMSWindowLayer_" + Obj.id));    };
    var WinTitle = document.createElement('label');
    WinTitle.innerHTML = Title;
    WinTitle.className = "MyTitle C_Title";
    WinCorners.appendChild(WinTitle);
    WinBody.appendChild(WinCorners);
    //============Creating Window Bottom Bar=============
    WinCorners = document.createElement('span');
    WinCorners.style.backgroundColor = FBColor;
    WinCorners.className = ((CustomCorners == "true")?"c_corners":"corners") + " bb C_BottomBorder";
    //WinCorners.innerHTML = ("<img id=\"LoadStatusImg_" + Obj.id + "\" src=\"WebResource.axd?d=c9_VFyyv_5L-c2zhI5SeMscU-fOQHtxMP8JbkIa8JZO8RD_eygGC_wqYrhLhO_q20&t=634037136263281250\" hspace=\"15\" vspace=\"2\">");
    WinBody.appendChild(WinCorners);
    //==============Creating Window Corners==============
    if(RoundCorner == "true")
    {
        WinCorners = document.createElement('span');
        WinCorners.className = ((CustomCorners == "true")?"c_corners":"corners") + " tl C_TopLeft";
        WinCorners.style.backgroundColor = FBColor;
        if(PinLayer == "false")WinCorners.onmousedown = function(ev)
        {   DrageCMSWindow(ev, document.getElementById("CMSWindowLayer_" + Obj.id))    };
        WinBody.appendChild(WinCorners);
        WinCorners = document.createElement('span');
        WinCorners.style.backgroundColor = FBColor;
        WinCorners.className = ((CustomCorners == "true")?"c_corners":"corners") + " tr C_TopRight";
        if(PinLayer == "false")WinCorners.onmousedown = function(ev)
        {   DrageCMSWindow(ev, document.getElementById("CMSWindowLayer_" + Obj.id))    };
        WinBody.appendChild(WinCorners);
        WinCorners = document.createElement('span');
        WinCorners.style.backgroundColor = FBColor;
        WinCorners.className = ((CustomCorners == "true")?"c_corners":"corners") + " bl C_BottomLeft";
        WinBody.appendChild(WinCorners);
        WinCorners = document.createElement('span');
        WinCorners.style.backgroundColor = FBColor;
        WinCorners.className = ((CustomCorners == "true")?"c_corners":"corners") + " br C_BottomRight";
        WinBody.appendChild(WinCorners);
    }
    //==================================================
    var WinClose = document.createElement('span');
    WinClose.className = "close C_Close";
    if(CloseImageURL && CloseImageURL != "")WinClose.style.backgroundImage = String.format("URL({0})", CloseImageURL);
    WinClose.onclick = function(){window.setTimeout("RemoveCMSWindow('" + Obj.id + "')", 100)};
    WinBody.appendChild(WinClose);
    WindowLayer.appendChild(WinBody);
    document.forms[0].appendChild(WindowLayer);
    
    FadeElement = document.getElementById("divFadeBackGround_" + Obj.id); 
    if(FadeElement){
        var docDymension = getDocDymension();
        var WinDymension = getWinDymension();
        FadeElement.style.width=(Math.max(docDymension[0], WinDymension[0])-10) + "px";
        FadeElement.style.height=Math.max(docDymension[1], WinDymension[1]) + "px";
    }
    LoadWinContent(Winframe, TargetURL, Obj.id)
}
//======================Accessing Document Width and Height========================
function LoadWinContent(iframe, TargetURL, WinID)
{
    try
    {
        //iframe.onload=function(){document.getElementById("LoadStatusImg_" + WinID).style.display = 'none';};
        iframe.doc = null;
        if(iframe.contentDocument)   iframe.doc = iframe.contentDocument;
        else if(iframe.contentWindow)iframe.doc = iframe.contentWindow.document;
        else if(iframe.document)     iframe.doc = iframe.document;
        if(iframe.doc)
        {
            var LoadingHTML="<table align=\"center\" height=\"100%\" width=\"100%\"><tr><td valign=\"middle\" align=\"center\"><img src=\"WebResource.axd?d=c9_VFyyv_5L-c2zhI5SeMqdDGaCX_aY4aBCz05rvCk0xetRAJaf3a7-0zTAZvwhX0&t=634037136263281250\"></td></tr></table>"
            iframe.doc.open();
            iframe.doc.write(LoadingHTML);
            iframe.doc.close();
        }
    }
    catch(ex){  }
    iframe.setAttribute("src", TargetURL);
}
function getWinDymension()
{
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' )       //Non-IE
    {
        
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }                                   //IE 6+ in 'standards compliant mode'
    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }                                   //IE 4 compatible
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
    {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    //window.alert( 'Width = ' + myWidth + ' and height = ' + myHeight );
    return [myWidth, myHeight];
}
function getScrollXY()
{
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' )      //Netscape compliant
    {
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    }                                   //DOM compliant
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
    {
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    }                                   //IE6 standards compliant mode
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
    {
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    //window.alert( 'Horizontal scrolling = ' + scrOfX + '\nVertical scrolling = ' + scrOfY );
    return [scrOfX, scrOfY];
}
function getDocDymension(doc)
{
    var docHt = 0, sh, oh;
    var docWt = 0, sw, ow;
    if(!doc)doc = document;
    if (doc.height)
    {
        docHt = doc.height;
        docWt = doc.width;
    }
    else if (doc.body)
    {
        if (doc.body.scrollHeight && doc.body.scrollWidth)
        {
            docHt = sh = doc.body.scrollHeight;
            docWt = sw = doc.body.scrollWidth;
        }
        if (doc.body.offsetHeight && doc.body.offsetWidth)
        {
            docHt = oh = doc.body.offsetHeight;
            docHw = ow = doc.body.offsetWidth;
        }
        if (sh && oh) docHt = Math.max(sh, oh);
        if (sw && ow) docwt = Math.max(sw, ow);
    }
    //window.alert( 'Document Width = ' + scrOfX + '\nDocument Height = ' + scrOfY );
    return [docWt, docHt];
}
function getObjectPos(obj)
{
    var objleft = 0, objtop = 0, objrit = 0, objbot = 0;
    objrit = objleft + obj.offsetWidth;
    objbot = objtop + obj.offsetHeight;
    if(obj.offsetParent)
        while(1) 
        {
          objleft += obj.offsetLeft;
          objtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x && obj.y)
    {
        objleft += obj.x;
        objtop += obj.y;
    }
    objrit += objleft;
    objbot += objtop;
    return [objleft, objtop, objrit, objbot];
}
//==============================Drag Window Icon===================================
var dragobj, mousex = 0, mousey = 0, grabx, graby, orix = 0, oriy = 0, elex = 0, eley = 0   
function falsefunc() { return false; }   
function getMouseXY(e)   
{    
    if (!e) e = document.all?event:window.event;  
    if (e)  
    {   
        if (e.pageX || e.pageY)  
        {  
            mousex = e.pageX;  
            mousey = e.pageY;  
            algor = '[e.pageX]';  
            if (e.clientX || e.clientY) algor += ' [e.clientX] '  
        }  
        else if (e.clientX || e.clientY)  
        {  
            mousex = e.clientX + document.body.scrollLeft;  
            mousey = e.clientY + document.body.scrollTop;  
            algor = '[e.clientX]';  
            if (e.pageX || e.pageY) algor += ' [e.pageX] '  
        }    
    }  
}  
function MoveImage(e)  
{  
    getMouseXY(e);  
    if (dragobj)  
    {  
        elex = orix + (mousex-grabx);  
        eley = oriy + (mousey-graby);  
        dragobj.style.position = 'absolute';  
        dragobj.style.left = (elex).toString(10) + 'px';  
        dragobj.style.top  = (eley).toString(10) + 'px';  
    }  
    return false;  
}  
function DrageCMSWindow(e, context)  
{  
    document.onmousedown = falsefunc; 
    if(context)
    {
        dragobj = context;
        dragobj.style.zIndex = 50000;  
        document.onmousemove = MoveImage;  
        document.onmouseup = Release; 
        ValidateIframes("none") 
        getMouseXY(e);  
        grabx = mousex;  
        graby = mousey;  
        elex = orix = dragobj.offsetLeft;  
        eley = oriy = dragobj.offsetTop;  
    }
}  
function Release()  
{  
    getMouseXY();  
    dragobj = null;
    ValidateIframes("inline")
    document.onmousemove = null;  
    document.onmouseup = null;  
    document.onmousedown = null;  
}
function ValidateIframes(action)
{
    /*var MyFrames = document.getElementsByName("iframe")
    for(i=0; i<MyFrames.length; i++)
        MyFrames[i].style.display = action;*/
}