﻿// Crank: Floating Window Javascript Library
// Version: 2.0
// Last Update: November 10, 2008
// Author: Ross Dickinson of Eek! LLC.
// Copyright: Ross Dickinson
// Contact: ross@zeropointpromotion.com
//
// This library is to be used in conjunction with the CrankOS Framework for ASP.Net
// and the Crank Javascript Library. Otherwise it's pretty useless.


//var StatusBar = function SB() {
//    var SB = CreateObj("StatusBar");
//    SB.className = "StatusBar";
//    SB.Parent = "";
//    SB.x = 0;
//    SB.y = 0;
//    SB.Width = null;
//    SB.Height = null;
//    SB.MarginLeft = 0;
//    SB.MarginRight = 0;
//        
//        
//    SB.ResizeRight = CreateObj("Resize");
//        NewChild(SB,SB.ResizeRight);
//        SB.ResizeRight.className = "ResizeRight";
//    
//    SB.ResizeRight.onmousedown = function() { 
//        SB.x = Doc.x;
//        SB.y = Doc.y;
//        
//        SB.Parent = SB.parentNode;
//        SB.Parent.CoverIFrame(true);
//        
//        SB.Width = parseFloat(getStyle(SB.Parent,"width"));
//        SB.MarginRight = parseFloat(SB.Parent.style.marginRight);
//        SB.Height = parseFloat(getStyle(SB.Parent,"height"));
//        SB.MarginTop = parseFloat(SB.Parent.style.marginTop);
//        
//        if (document.addEventListener) { document.addEventListener("mousemove",ResizeWinRight,false); }
//        else if (document.attachEvent) { document.attachEvent("onmousemove",ResizeWinRight); }
//        else { document.onmousemove = ResizeWinRight; }
//    };
//    
//    SB.ResizeRight.onmouseup = EndResizing;
//    
//    function EndResizing() {
//        SB.Parent.CoverIFrame(false);
//        if (document.addEventListener) { document.removeEventListener("mousemove",ResizeWinRight,false); }
//        else if (document.attachEvent) { document.detachEvent("onmousemove",ResizeWinRight); }
//        else { document.onmousemove = false; }
//    }
//    
//    function ResizeWinRight() {
//        if ((SB.Width + (Doc.x - SB.x)) > 200) {
//            SB.Parent.style.width = SB.Width + (Doc.x - SB.x) + "px"; 
//        }
//        if ((SB.Height + (Doc.y - SB.y)) > 200) {
//            SB.Parent.style.height = SB.Height + (Doc.y - SB.y) + "px"; 
//        }
//        SB.Parent.ReSizeContent();
//    }
//    
//    return SB;
//}





var _CRNKWINDOWBUILDER = function() {
    var priv_This = this;
    var priv_Window = CreateObj("FW");
    
    this.Window = priv_Window;
    this.IsOnDesktop = false;
    this.IsActive = false;
    
     
    this.Methods = {
        ResizeContent : function() {
            var Me = priv_This.Content.Me;
            var WinHeight = GetHeight(priv_Window);
            var WinWidth = GetWidth(priv_Window);
            var TitleHeight = GetHeight(priv_This.Titlebar.Me);
            
            Me.height = GetHeight(priv_Window) - TitleHeight - GetHeight(priv_This.StatusBar.Me) + "px";
            Me.width = WinWidth + "px";
            Me.style.marginTop = TitleHeight + "px";
            priv_This.Titlebar.Me.style.width = WinWidth + "px";
//            try { priv_This.Content.contentWindow.ResizeMyContents(); }
//            catch(error) { return false; }
        },
        
        AddEvents : function() {
            Crank.Events.Add(priv_Window,"mousedown",priv_This.Methods.BringToFront,false);
            Crank.Events.Add(priv_This.Titlebar.Me,"mousedown",priv_This.Methods.AllowMove,false);
            Crank.Events.Add(priv_This.Titlebar.Me,"mouseup",priv_This.Methods.DisallowMove,false);
            Crank.Events.Add($Window,"mouseup",priv_This.Methods.DisallowMove,false);
            Crank.Events.Add(priv_This.Titlebar.Controls.Me,"mouseout",priv_This.Methods.ReallowMove,true);
            Crank.Events.Add(priv_This.Titlebar.Controls.Close.Me,"click",priv_This.Methods.KillThis,true);
            
            if (Crank.User.HasIE) { 
                Crank.Events.Add(priv_This.Titlebar.Controls.Me,"mouseover",priv_This.Methods.DisallowMoveFromControls,false);
                Crank.Events.Add($Window,"onmouseout",priv_This.Methods.DisallowMove,false);
                Crank.Events.Add($Window,"onclick",priv_This.Methods.DisallowMove,false);
            }
            else { Crank.Events.Add(priv_This.Titlebar.Controls.Me,"mousedown",priv_This.Methods.DisallowMoveFromControls,false); }
        },
        
        Anchor : function() {
            if (priv_This.Settings.Window.AnchorPosition === "Left") {
                priv_Window.style.marginLeft = (GetBrowserWidth() - parseFloat(getStyle(priv_Window,"width"))) + "px";      
            }
            if (priv_This.Settings.Window.AnchorPosition === "Right") {
                priv_Window.style.marginRight = "0px";      
            }
            if (priv_This.Settings.Window.AnchorPosition === "Top") {
                priv_Window.style.marginTop = "0px";      
            }
            if (priv_This.Settings.Window.AnchorPosition === "Bottom") {
                priv_Window.style.marginTop = (GetBrowserHeight() - parseFloat(getStyle(priv_Window,"height"))) + "px";        
            }
        },
        
        BringToFront : function() {
            if (!priv_This.IsActive) {
                var MySiblings = Crank.Objects.Window.GetOpenWindows();
                var TempZ = parseFloat(priv_Window.style.zIndex) + 1;
                for (var i=0; i<MySiblings.length; i++) {
                    MySiblings[i].Parent.IsActive = false;
                    if (Crank.Debugger.IsOn) { MySiblings[i].Parent.Settings.Titlebar.SetTitle(MySiblings[i].style.zIndex); }
                    if (MySiblings[i].style.zIndex >= TempZ) { TempZ = (parseFloat(MySiblings[i].style.zIndex) + 1); }
                    try {
                        if (MySiblings[i].Parent.Settings.Titlebar.ChangeColorWhenInactive) {
                            MySiblings[i].Parent.Titlebar.Me.style.backgroundColor = MySiblings[i].Parent.Settings.Titlebar.InactiveBackColor;
                            MySiblings[i].Parent.Titlebar.TextObject.Me.style.color = MySiblings[i].Parent.Settings.Titlebar.InactiveForeColor;
                        }
                    }
                    catch(Error) {  }
                }
                priv_This.Titlebar.Me.style.backgroundColor = priv_This.Settings.Titlebar.BackColor;
                priv_This.Titlebar.TextObject.Me.style.color = priv_This.Settings.Titlebar.ForeColor;
                priv_Window.style.zIndex = TempZ;
                priv_Window.style.display = "block";
                if (Crank.Debugger.IsOn) { priv_This.Settings.Titlebar.SetTitle(TempZ); }
                priv_This.IsActive = true;
                if (priv_This.TaskbarItem) { priv_This.TaskbarItem.ActivateItem(); }
            }
        },
        
        CascadeOnStart : function() {
            var Siblings = Crank.Objects.Window.GetOpenWindows().length + 1;
            while(Siblings>10) { Siblings = Siblings - 10; }
            priv_Window.style.marginLeft = (Siblings * GetHeight(priv_This.Titlebar.Me)) + "px";
            priv_Window.style.marginTop = (Siblings * GetHeight(priv_This.Titlebar.Me)) + "px";
        },
        
        CenterOnStart : function() {
            var MyWidth = GetWidth(priv_Window);
            var BrowserWidth = Crank.User.BrowserWidth();
            priv_Window.style.marginLeft = (BrowserWidth/2) - (MyWidth/2) + "px";       
                    
            var MyHeight = GetHeight(priv_Window);
            var BrowserHeight = Crank.User.BrowserHeight();
            priv_Window.style.marginTop = (BrowserHeight/2) - (MyHeight/2) + "px";     
        },
        
        CoverIFrame : function CIF(bool) {
            if (bool) {
                $Desktop.onselectstart = function() { return false; };
                $Desktop.onmousedown = function() { return false; };
                var MySiblings = Crank.Objects.Window.GetOpenWindows();
                for (var i=0; i<MySiblings.length; i++) {
                    MySiblings[i].ContentCover.style.display = "block";
                }
            }
            else if (!bool) {
                $Desktop.onselectstart = function() { return true; };
                $Desktop.onmousedown = function() { return true; };
                priv_Window.ContentCover.style.display = "none";        
            }
        },
        
        ReloadMeNow : function() { priv_This.Content.src = priv_This.Content.src; },
        
        MoveMe : function() {
            if (Crank.User.HasIE) {
                if (priv_This.Settings.AllowMovement) {
                    priv_Window.style.marginLeft = Doc.x - priv_This.Settings._MouseX + "px";
                    priv_Window.style.marginTop = Doc.y - priv_This.Settings._MouseY + "px";
                }
            }
            else {
                priv_Window.style.marginLeft = Doc.x - priv_This.Settings._MouseX + "px";
                priv_Window.style.marginTop = Doc.y - priv_This.Settings._MouseY + "px";
            }
        },
        
        AllowMove : function() {  
            priv_This.Settings.AllowMovement = true;
            priv_This.Methods.CoverIFrame(true);
            if (Crank.User.HasIE) { Crank.Events.Add(document,"mousemove",priv_This.Methods.MoveMe,true); }
            else { Crank.Events.Add(Crank.Desktop.Window,"mousemove",priv_This.Methods.MoveMe,true); }
            
            priv_This.Settings._MouseX = Doc.layerX;
            priv_This.Settings._MouseY = Doc.layerY;
        },
        
        DisallowMove : function() {
            priv_This.Settings.AllowMovement = false;   
            priv_This.Methods.CoverIFrame(false); 
            if (Crank.User.HasIE) { Crank.Events.Remove(Crank.Desktop.Document,"mousemove",priv_This.Methods.MoveMe,true); }
            else { Crank.Events.Remove(Crank.Desktop.Window,"mousemove",priv_This.Methods.MoveMe,true); }
        },
                  
        DisallowMoveFromControls : function() {
            priv_This.Methods.DisallowMove();
            Crank.Events.Remove(priv_This.Titlebar.Me,"mousedown",priv_This.Methods.AllowMove,false);
        }, 
        
        ReallowMove : function() { Crank.Events.Add(priv_This.Titlebar.Me,"mousedown",priv_This.Methods.AllowMove,false); },
        
        KillThis : function() { 
            Crank.Events.Remove(priv_This.Titlebar.Me,"mousedown",priv_This.Methods.AllowMove,false);
            Crank.Events.Remove(priv_This.Titlebar.Me,"mouseup",priv_This.Methods.DisallowMove,false);
            Crank.Events.Remove(priv_This.Titlebar.Controls.Me,"mousedown",priv_This.Methods.DisallowMoveFromControls,false);
            Crank.Events.Remove(priv_This.Titlebar.Controls.Me,"mouseout",priv_This.Methods.ReallowMove,false);
            Crank.Events.Remove(priv_This.Titlebar.Controls.Close.Me,"click",priv_This.Methods.KillThis,false);
            //
            try { priv_This.Content.contentWindow.UnloadMe();  }
            catch(oops) { return false; }
            finally {
                priv_Window.style.display = "none";
              //  priv_This.TaskbarItem.Me.style.display = "none";
                if (Crank.User.HasIE) { return false; }
                else {
                  // delete priv_Window.TaskBarItem;
                   delete priv_Window.Content;
                }            
                delete priv_Window;
            }
        }            
    };   
    
    this.Settings = {
        _ImportsCrank : Crank,
        ImageDirectoryPath : "Images/",
        _MouseX : 0,
        _MouseY : 0,
        AllowMovement : false,
        Window : {
            AllowResizing : true, 
            AllowScrolling : false,
            AnchorPosition : "false",
            CascadeOnStart : true,
            CenterOnStart : false,
            ClassName : false,
            Height : "400px",
            ID : ("FWin" + Math.round(Math.random()*10000000)),
            URL : "Wrong.aspx",
            Width : "400px"
        },
        Titlebar : {
            BackImage : "Titlebar Background.png",
            BackColor : "#7eaae8",
            ChangeColorWhenInactive : true, 
            ForeColor : "#ffffff",
            InactiveBackColor : Crank.Colors.CrankDeepBlue,
            InactiveForeColor : "#f3f3f3",
            Text : "Some Chicken Made an Oopsy",
            SetTitle : function(TitleText) { 
                priv_This.Settings.Titlebar.Text = TitleText;
                priv_This.Titlebar.TextObject.Me.innerHTML = TitleText;
               // priv_This.TaskbarItem.UpdateTitle();
            }
        },
        Controls : {
            CloseImgName : "Titlebar Control Close.png",
            MinimizeImgName : "Titlebar Control Min.png",
            ShowClose : true,
            ShowMinimize : true 
        },
        StatusBar : {
            BackImgName : "Statusbar Background.png",
            ResizeRightImgName : "Resize Left.png"
        }       
    };
    
    this.SetFormat = {
        Default : function() {
            priv_Window.style.display = "block";
            priv_Window.style.position = "absolute";
            priv_Window.style.top = "0px";
            priv_Window.style.left = "0px";
            priv_Window.style.width = "400px";
            priv_Window.style.height = "400px";
            priv_Window.style.backgroundColor = "#ffffff";
            priv_Window.style.border = "ridge 1px silver";
            priv_Window.style.zIndex = 10;
        }
    };

    this.Content = {
        Me : CreateObj("iframe"),
        Build : function() {    
            var Me = priv_This.Content.Me;
            Me.src = priv_This.Settings.Window.URL;
            Me.frameBorder = false;   
            Me.style.display = "block";
            Me.style.position = "absolute";
            Me.style.backgroundColor = "white";
            Me.style.marginTop = "3px";
            Me.style.border = "none 0px"; 
            if (priv_This.Settings.Window.AllowScrolling) { Me.scrolling = "yes"; }
            else { Me.scrolling = "no"; }
            return Me;
        }
    };

    this.ContentCover = {
        Me : CreateObj("ContentCover"),
        Build : function() {   
            var Me = priv_This.ContentCover.Me;     
            NewChild(priv_Window, Me);
            Me.style.display = "none";
            Me.style.height = "100%";
            Me.style.width = "100%";
            Me.style.position = "absolute";
            Me.style.left = "0px";
            Me.style.top = "0px";
            Me.style.backgroundColor = "transparent";
            Me.style.zIndex = 1;
            if (Crank.User.HasIE) {
                Me.style.backgroundColor = "white";
                Me.style.filter = "alpha(opacity=1)";
            }
            priv_This.ContentCover.Me = Me;
            return Me;
        }
    }

    this.Titlebar = {
        Me : CreateObj("Titlebar"),
        Build : function() {
            var Me = priv_This.Titlebar.Me;
            NewChild(Me,priv_This.Titlebar.TextObject.Build());
            NewChild(Me,priv_This.Titlebar.Controls.Build());
            Me.style.position = "absolute";
            Me.style.display = "block";
            Me.style.width = "100%";
            Me.style.height = "30px";
            Me.style.top = "0px";
            Me.style.left = "0px";
            Me.style.margin = "0px";
            Me.style.padding = "0px";
            Me.style.backgroundColor = priv_This.Settings.Titlebar.BackColor;
            Me.style.backgroundImage = 'url("' + priv_This.Settings.ImageDirectoryPath + priv_This.Settings.Titlebar.BackImage + '")';
            Me.style.zIndex = "10";
            Me.style.overflow = "hidden";
            return Me;
        },
        TextObject : {
            Me : CreateObj("TitleText"),
            Build : function() {                
                var TO = priv_This.Titlebar.TextObject.Me;
                TO.style.zIndex = 1;
                TO.style.top = "0px";
                TO.style.left = "0px";
                TO.style.display = "block";
                TO.style.height = "100%";
                TO.style.width = "90%";
                TO.style.paddingLeft = "5px";
                TO.style.color = priv_This.Settings.Titlebar.ForeColor;
                TO.style.fontFamily = "Calibri,Verdana,Arial";
                TO.style.fontSize = "14px";
                TO.style.lineHeight = "30px";
                TO.style.fontWeight = "bold";
                
                
                return TO;
            }
        },
        Controls : {
            Me : CreateObj("TitleControls"),
            Build : function() {
                var C = priv_This.Titlebar.Controls.Me;
                C.style.zIndex = "2";
                C.style.display = "block";
                C.style.position = "absolute";
                C.style.top = "0px";
                C.style.right = "0px";
                C.style.width = "60px";
                C.style.height = "25px";
                C.style.margin = "3px 2px 0px 0px";
                C.style.padding = "0px";
                C.style.direction = "rtl";
                if (priv_This.Settings.Controls.ShowClose) { NewChild(C,priv_This.Titlebar.Controls.Close.Build()); }
                if (priv_This.Settings.Controls.ShowMinimize) { NewChild(C,priv_This.Titlebar.Controls.Minimize.Build()); }
                return C;
            },
            BaseControl : function() {
                var Control = CreateObj("Control");
                Control.style.display = "block";
                Control.style.position = "relative";
                Control.style.top = "0px";
                Control.style.left = "0px";
                Control.style.width = "25px";
                Control.style.height = "100%";
                Control.style.styleFloat = "right";
                Control.style.cssFloat = "right";
                priv_This.Titlebar.Controls.BaseEvents.Add(Control); 
                return Control;
            },
            BaseEvents : {
                Add : function(Control) {
                    Crank.Events.Add(Control,"mouseout",priv_This.Titlebar.Controls.BaseEvents.Mouseout,true);
                    if (Crank.User.HasIE) { Crank.Events.Add(Control,"mousemove",priv_This.Titlebar.Controls.BaseEvents.Mouseover,true); }
                    else { Crank.Events.Add(Control,"mouseover",priv_This.Titlebar.Controls.BaseEvents.Mouseover,true); }
                },
                Mouseover : function(e) { 
                    priv_Window.style.cursor = "pointer";
                    Crank.Events.Target(e).style.backgroundPosition = "-26px 0px";
                },
                Mouseout : function(e) { 
                    priv_Window.style.cursor = "default";
                    Crank.Events.Target(e).style.backgroundPosition = "0px 0px";
                }
            },
            Close : { 
                Me : false,
                Build : function() {
                    var Close = new priv_This.Titlebar.Controls.BaseControl();
                    Close.style.backgroundImage = 'url("' + priv_This.Settings.ImageDirectoryPath + priv_This.Settings.Controls.CloseImgName + '")';
                    priv_This.Titlebar.Controls.Close.Me = Close;
                    return Close;
                }               
            },
            Minimize : { 
                Me : false,
                Build : function() {
                    var Minimize = new priv_This.Titlebar.Controls.BaseControl();
                    Minimize.style.backgroundImage = 'url("' + priv_This.Settings.ImageDirectoryPath + priv_This.Settings.Controls.MinimizeImgName + '")';
                    priv_This.Titlebar.Controls.Minimize.Me = Minimize;
                    Crank.Events.Add(Minimize,"click",priv_This.Titlebar.Controls.Minimize.Minimize,false);
                    return Minimize;
                },
                Minimize : function() { priv_Window.style.display = "none"; }               
            }
        }
    };
    
    this.StatusBar = {
        Me : CreateObj("StatusBar"),
        Build : function() { 
            var SB = priv_This.StatusBar.Me;
            SB.Parent = priv_Window;
            SB.x = 0;
            SB.y = 0;
            SB.Width = null;
            SB.Height = null;
            SB.MarginLeft = 0;
            SB.MarginRight = 0;
            SB.style.display = "block";
            SB.style.position = "absolute";
            SB.style.backgroundColor = "silver";
            SB.style.backgroundImage = 'url("' + priv_This.Settings.ImageDirectoryPath + priv_This.Settings.StatusBar.BackImgName + '")';
            SB.style.bottom = "0px";
            SB.style.left = "0px";
            SB.style.width = "100%";
            SB.style.height = "20px";   
            
            if (priv_This.Settings.Window.AllowResizing) { NewChild(SB,priv_This.StatusBar.ResizeRight.Build()); }
                   
            return SB; 
        },
        Parent : priv_Window,
        ResizeRight : {
            Me : CreateObj("Resize"),
            Build : function() {
                var Resize = priv_This.StatusBar.ResizeRight.Me;
                Resize.style.display = "block";
                Resize.style.position = "absolute";
                Resize.style.backgroundColor = "transparent";
                Resize.style.backgroundRepeat = "no-repeat";
                Resize.style.backgroundPosition = "bottom right";            
                Resize.style.backgroundImage = 'url("' + priv_This.Settings.ImageDirectoryPath + priv_This.Settings.StatusBar.ResizeRightImgName + '")';
                Resize.style.right = "0px";
                Resize.style.top = "0px";
                Resize.style.height = "100%";
                Resize.style.width = "20px";
                
                return Resize;
            }
        }
    };
            
    if (Crank.Objects.TaskbarItem) {
        this.TaskbarItem = $Crank.Objects.TaskbarItem.New(priv_This);
        $Crank.Objects.Taskbar.ResizeTaskItems();     
    }
    
    this.Run = function() {  
        NewChild($Crank.Site.Desktop(),priv_Window); 
        priv_This.SetFormat.Default();
        priv_Window.id = priv_This.Settings.Window.ID;
        priv_Window.Parent = priv_This;
        priv_Window.ContentCover = priv_This.ContentCover.Me;
        
        priv_Window.style.width = priv_This.Settings.Window.Width;
        priv_Window.style.height = priv_This.Settings.Window.Height;
        
        NewChild(priv_Window,priv_This.Titlebar.Build()); 
        NewChild(priv_Window,priv_This.Content.Build()); 
        NewChild(priv_Window,priv_This.ContentCover.Build());
        NewChild(priv_Window,priv_This.StatusBar.Build());  
        priv_This.Methods.AddEvents();
        priv_This.Methods.ResizeContent();
        priv_This.Methods.BringToFront();
        
            if (priv_This.Settings.Window.CascadeOnStart) { priv_This.Methods.CascadeOnStart(); }
            if (priv_This.Settings.Window.CenterOnStart) { priv_This.Methods.CenterOnStart(); }
       
        
    };
}


var _CRANKFLOATINGWINDOW = {
    Namespace : "Window",
    New : function() { return new $Window.Crank.Objects.Window._WindowBuilder(); },
    _WindowBuilder : _CRNKWINDOWBUILDER,
    GetOpenWindows : function() { return Crank.Desktop.Document.getElementsByTagName("FW"); },
    GetWindowByURL : function(WindowURL) {        
        var Windows = Crank.Desktop.Document.getElementsByTagName("iframe");
        var ResultWindow = false;
        for (var i=0; i<Windows.length; i++) {
            var WinDoc = Windows[i].contentWindow.location;
            if (HasTerm(WindowURL,WinDoc)) { ResultWindow = Windows[i].parentNode; }
        }
        return ResultWindow.Parent;
    },
    ChangeTitle : function(WindowByURL, Title) { 
        if (Crank.System.IsDesktop) {
            var W = Crank.Objects.Window.GetWindowByURL(WindowByURL);
            if (W) { W.Settings.Titlebar.SetTitle(Title); }
            else { alert("Whatever window it is you're trying to find, it ain't there. Error URL: " + WindowByURL); }
        }
        else { window.parent.Crank.Objects.Window.ChangeTitle(WindowByURL, Title); }
    },
    ExitApp : function(URL) { 
        var WindowToClose = Crank.Objects.Window.GetWindowByURL(URL);
        if (WindowToClose) { WindowToClose.Methods.KillThis(); } 
    },    
    ReloadAllWindows : function() { 
        var Floaters = Crank.Objects.Window.GetOpenWindows()
        for (var i=0; i<Floaters.length; i++) {
            Floaters[i].Parent.Methods.ReloadMeNow();
        }
    }
};
Crank.System.Register.Object(_CRANKFLOATINGWINDOW);


