var WBEdit = 
{
    Element_BottomBar   : null,
    Element_BottomDock  : null,
    Element_Mask    : null,
    Element_Window  : null,
    Element_IFrame  : null,
    Button_LiveEdit : null,
    Button_Edit     : null,
    Button_Return   : null,
    Button_LogOut   : null,
    RelativePath    : "",
    LastBlockEdited : null,
    FNWindowSubmit  : null,
    BottomBarHolder : null,
    Initialize      : function (_RelativePath, LiveEditActive)
    {
        this.RelativePath = _RelativePath;
     
        // **** CONSTRUCT THE DOCK ***   
        this.BottomBarHolder = document.createElement("div");
        this.BottomBarHolder.className = "webBlocksHolder";
        document.body.appendChild(this.BottomBarHolder);
        
        this.Element_BottomBar = document.createElement("div");
        this.Element_BottomBar.className = "webBlocksControlPanel";
        this.BottomBarHolder.appendChild(this.Element_BottomBar);
        
        var innerTable = document.createElement("table");
        innerTable.cellPadding = 0;
        innerTable.cellSpacing = 0;
        innerTable.align = "center"; // very important, causes the centering
        this.Element_BottomBar.appendChild(innerTable);
        
        var tBody = document.createElement("tbody");
        innerTable.appendChild(tBody);
        
        var innerTR = document.createElement("tr");
        tBody.appendChild(innerTR);
        
        var LeftBar = document.createElement("td");
        LeftBar.className = "bar-left";
        innerTR.appendChild(LeftBar);
        
        this.Element_BottomDock = document.createElement("td");
        this.Element_BottomDock.className = "bar-middle";
        innerTR.appendChild(this.Element_BottomDock);
        
        var RightBar = document.createElement("td");
        RightBar.className = "bar-right";
        innerTR.appendChild(RightBar);
        
        // **** DONE ****
        
        // now we can fill the dock with some stuff.
        var WALogo = document.createElement("div");
        WALogo.className = "waLogo";
        this.Element_BottomDock.appendChild(WALogo);
        
        // now the "Live Edit" button
        this.Button_LiveEdit = document.createElement("a");
        this.Button_LiveEdit.href = this.RelativePath + "/Admin/WebBlocks/StartLiveEdit.aspx?return=true";
        this.Button_LiveEdit.className = "standardButton";
        this.Button_LiveEdit.innerHTML = "Turn Live Edit <b>" + (LiveEditActive ? "OFF" : "ON") + "</b>";
        this.Element_BottomDock.appendChild(this.Button_LiveEdit);
        
        this.Button_Return = document.createElement("a");
        this.Button_Return.href = this.RelativePath + "/Admin/Default.aspx";
        this.Button_Return.className = "standardButton";
        this.Button_Return.innerHTML = "Return To Admin";
        this.Element_BottomDock.appendChild(this.Button_Return);
        
        this.Button_Edit = document.createElement("a");
        this.Button_Edit.href = "javascript:WBEdit.StopPreview();";
        this.Button_Edit.className = "standardButton";
        this.Button_Edit.innerHTML = "Return To Editor";
        this.Button_Edit.style.display = "none"; // hidden by default
        this.Element_BottomDock.appendChild(this.Button_Edit);
        
        this.Button_LogOut = document.createElement("a");
        this.Button_LogOut.href = this.RelativePath + "/Admin/Login/LogOut.aspx";
        this.Button_LogOut.className = "standardButton";
        this.Button_LogOut.innerHTML = "Log Out";
        this.Element_BottomDock.appendChild(this.Button_LogOut);
        
        this.StartScrollingDetector();
    },
    EnsureElements  : function ()
    {
        if (this.Element_Mask == null)
        {
            // the div that gives us the background-fade effect
            this.Element_Mask = document.createElement("div");
            this.Element_Mask.id = "Element_Mask";
            this.Element_Mask.className = "wbBlackoutDiv";
            this.Element_Mask.style.display = "none";
            document.body.appendChild(this.Element_Mask);
            
            // the wrapper for the iframe
            this.Element_Window = document.createElement("div");
            this.Element_Window.id = "Element_Window";
            this.Element_Window.className = "wbEditDiv";
            this.Element_Window.style.display = "none";
            document.body.appendChild(this.Element_Window);
            
            // create the pop up div
            this.Element_IFrame = document.createElement('iframe');
            this.Element_IFrame.className = "wbEditFrame";
            this.Element_IFrame.frameBorder = 0;
            this.Element_Window.appendChild(this.Element_IFrame);
        }
    },
    EditWrapperOver : function(div)
    {
        div.style.background='#DFE6EB'; 
        div.childNodes[0].style.display = "block";
    },
    EditWrapperOut  : function(div)
    {
        div.style.background='transparent';
        div.childNodes[0].style.display = "none";
    },
    ShowEditWindow  : function()
    {
        $(this.Element_Mask).show();
        $(this.Element_Window).show();
    },
    HideEditWindow  : function()
    {
        $(this.Element_Mask).hide();
        $(this.Element_Window).hide();
    },
    EditTextBlock   : function(blockTitle, el)
    {
        this.LastBlockEdited = document.getElementById(el);
        this.EnsureElements();
        
        //var scrollTop = $(document).scrollTop() + 200;
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + 200;
        var docHeight = $(document).height();
        
        $(this.Element_Window).css("top", scrollTop + "px");
        $(this.Element_Mask).css("height", docHeight + "px");
        
        var rand = Math.floor(Math.random()*100001)

        this.Element_IFrame.src = this.RelativePath + "/Admin/WebBlocks/EditTextContent_Frame.aspx?ContentTitle=" + blockTitle + "&rand=" + rand;

        this.ShowEditWindow();
    },
    PreviewTextBlock         : function(content, _FNWindowSubmit)
    {
        this.FNWindowSubmit = _FNWindowSubmit;
        this.HideEditWindow();
        
        // kill all of the contents of the block in question, and drop in the new content
        $(this.LastBlockEdited).empty().append(content);
        
        // remove all of the "edit" borders
        $(".EditWrapper").removeAttr("class").removeAttr("onmouseover").removeAttr("onmouseout");
        
        $(this.Button_LiveEdit).hide();
        $(this.Button_Return).hide();
        $(this.Button_LogOut).hide();
        $(this.Button_Edit).show();
        
        // unhide the preview button on the toolbar
        //$(Button_Preview).show();
    },
    StopPreview             : function()
    {
        this.ShowEditWindow();
    },
    StartScrollingDetector  : function()
    {
        //window.onscroll = this.DetectScroll;
        window.setInterval("WBEdit.DetectScroll()", 500);
    },
    DetectScroll            : function()
    {
        var position = 0;
        var isMSIE = /*@cc_on!@*/!1;
        if (isMSIE) {position = Math.max(document.body.scrollTop, document.documentElement.scrollTop);}
        else {position = window.pageYOffset;}
       
        
        WBEdit.BottomBarHolder.style.bottom = "-" + position + "px";
    }
}