﻿/// <reference path="jQ/jquery-latest-vsdoc.js" />
/// <reference path="../../jquery/ui/ui.draggable.js" />
/// <reference path="AMS.js" />


// this is the new widget manager
(function () {
    var $AMS = window.$AMS;

    var WidgetMan = function () {
        this.Init();

    };

    WidgetMan.prototype = {
        wl: null // list of widgets on page
        , Init: function () {
            this.wl = new Array();
        }
        , Load: function () {

        }
        , FILByID: function (zwcid) {
            // finds the widget in the managers list based on the wid returns null if not found
            // returns widget obj if is found
            var _rtn = null;
            for (ind in this.wl) {
                if (this.wl[ind].zwcid === zwcid) {
                    _rtn = this.wl[ind];
                    break;
                }
            }
            return _rtn;
        }
        , FIPByID: function (elid) {
            // finds a widget on the page based on the widget ID... must find in list 1st...
            // returns null if not found.. returns the jquery obj if found
            var _rtn = null;
            var _obj = this.FILByID(elid);
            if (_obj !== null) {
                // obj was found.. now see if we can get the element on the page
                _rtn = $("#" + _obj.elid);
                if (_rtn.length == 0) {
                    _rtn = null;
                }
            }
            return _rtn;
        }
        , FILByEl: function (elid) {
            // find a widget in the list based on the elemnet id returns null if not found
            // returns widget obj if is found
            var _rtn = null;
            for (ind in this.wl) {
                if (this.wl[ind].elid === elid) {
                    _rtn = this.wl[ind];
                    break;
                }
            }
            return _rtn;
        }
        , FIPByEl: function (elid) {
            // finds a widget on the page based on the elementid
            // returns null if not found.. returns the jquery obj if found
            var _rtn = null;
            var _obj = this.FILByEl(elid);
            if (_obj !== null) {
                // obj was found.. now see if we can get the element on the page
                _rtn = $("#" + _obj.elid);
                if (_rtn.length == 0) {
                    _rtn = null;
                }
            }
            return _rtn;
        }
        , RegWidget: function (obj) {
            // need to check ID if this widget is registered ...
            // if it is and this has been call then it should be from a post back ... need to call updated on widget not add
            // remember we are going to store the actual object in the list
            var _tmpOb;
            if (this.wl.length > 0) {
                // check if this widget exists
                var _obj = this.FILByID(obj.zwcid);
                if (_obj == null) {
                    // create obj.
                    _tmpOb = new this.WidgetClass(obj, this.WidgetClass); // $.extend(true, this.WidgetClass.prototype, this.WidgetClass, obj);
                    this.wl[this.wl.length] = _tmpOb;
                }
                else {
                    // update obj
                    _obj.Update(obj);
                }
            }
            else {
                // create obj
                _tmpOb = new this.WidgetClass(obj, this.WidgetClass); // $.extend(true, this.WidgetClass.prototype, this.WidgetClass, obj);
                this.wl[this.wl.length] = _tmpOb;
            }
        }

    };

    // remove for live
    $AMS.WidgetMan = $AMS.$RO("AMS.WidgetMan", WidgetMan);

    if ($AMS.WidgetMan === undefined) {
        $AMS.WidgetMan = $AMS.$RO("AMS.WidgetMan", WidgetMan);
    }



    var TitleBarClass = function (s) {
        // note if shown is false then only build if the current member is the owner of the page or has permissions to this widget
        return (function () {

            var _elm = null;
            var t = this;
            var Obj = function () {
                this.Init();
            };

            Obj.prototype = {
                title: ""
                , visible: false
                , Init: function () {
                    this.title = s.title;
                    _elm = "<div class=\"ui-title\">test</div>";
                }
                , OMO: function () {
                    var _p = s;
                    var _e = _elm;
                }
            };

            return new Obj();
        })();
    };

    var Widget = function (obj, a) {
        $.extend(true, this, a, obj);
        this.Init();
    };


    Widget.prototype = {
        /* variables */


        /* element info */
        elid: null /* this is the widgets elementid */
        , pelid: null /* this is the elementid of this widget */
        , upDatePanel: null /* this is the id of the update panel */
        , felid: null /* this is the element id of the footer.. once we have this build this object we'll use an object torack it. */
        , telid: null /* this is the id of the title bar... once we have this build this object we'll use an object torack it. */

        /* properties */
        , pid: null  /* this is the id of the parent... will probably encrypt // later will use zone manager to set this so dont have to pass */
        , tKey: null /* this is the widgets key */
        , zwcid: null /* this is the zonewidgetcrossid encrypted for post back use */
        , level: null /* this is the page level of the widget.  if it is app=1, root = 2, and so on */
        , name: "" /* this is the widgets name */
        , title: "" /* widgets title if different then name or title */
        , isA: null /* if widget is active on page.  only important when in editing mode */
        , isExpanded: null /* this is if the widget is currently expanded */
        , isColapsable: null /* this is only important */
        , perm: null /* =false if current person is allowed to see it.  not realy import for none editing more widget will not show up at all */
        , style: null /* this will be used if member has added custom styling */
        , showTitle: false /* if title is to be shown */
        , showFooter: false /* if footer is to be shown */
        , footerText: "" /* text to be added to the footer */
        , resizeable: false /* tells weather this widget can be resized */
        , cpos: null /* this is the current posistion of the widget in the list */
        , zt: null /* this is the zone type.. this will be used to tell when type of direction resize can be done */
        , isDmode: false /* set to true if the current widget is in dragdrop mode */
        , isInCMode: false /* if true this widget is in content editing mode */
        , ce: false /* this is true if the member can edit anything. if false do not build any menus */
        , cec: false /* this is true if the current member can edit the content */
        , ces: false /* this is true if the current member can edit the settings */
        , crz: false /* this is true if the current member can resize the widget */

        /* objects */
        , cmds: null /* this will be the object that stores the command tree */
        , cMenu: null /* this is the content editing menu */
        , dMenu: null /* this is the menu used for dragdrop */
        , appObj: null /* this is any data obj that the inner app needs to store */

        /* event listeners */
        , omoFun: null /* this is an array of function that need to fire on the mouse over event */
        , omlFun: null /* this is an array of functions that need to fire on the mouse leave event */

        /* functions */
        , Init: function () {
            //            this.elid = obj.elid;
            //            this.pelid = obj.pelid;

            this.omoFun = new Array();
            this.omlFun = new Array();
            $("#" + this.elid).mouseenter($.proxy(this.OME, this));
            $("#" + this.elid).mouseleave($.proxy(this.OML, this));
            if (this.ce) { this.BuildCMenu(); };
            this.telid = TitleBarClass(this);
        }

        /* command icon objects */
        , cmdsClass: function () {

            var _cmds = function (s) {

                // private variables
                this.custom = new Array();

                var _editIconClass = function (s) {
                    /* this is the settings construct */
                    // private variabels
                    this.ome = new Array();        /* events to fire when mouse enters this */
                    this.oml = new Array();        /* fires when leaves obj */
                    this.clickStart = new Array(); /* stores the events to fire when the click event starts before the postback is called to the event */
                    this.clickEnd = new Array();   /* stores the events to fire after the post back */
                    this.p = s;
                    this.visible = true;
                    // private functions
                    var OME = function (e) {
                        /* this fires when the settings icon is fired this will be mostly used for styling */
                        this.element.toggleClass(this.p.$gv.css.cmdIconHover);
                    };

                    var OML = function (e) {
                        /* this fires when the settings icon is fired this will be mostly used for styling */
                        this.element.toggleClass(this.p.$gv.css.cmdIconHover);
                    };

                    var Click = function (e) {

                        if (!this.p.isInCMode) {
                            this.element.toggleClass(this.p.$gv.css.cmdIconHover);
                            var _w = $("#" + this.p.elid); // $(e.currentTarget);
                            var _m = $("#wcm_" + _w.attr("id"));
                            _m.hide();
                            _m.appendTo("body");

                            //                            PageManager.Mouse.x = this.p.$gv.mloc.x;
                            //                            PageManager.Mouse.y = this.p.$gv.mloc.y;

                            var ih = this.p.elid + "_EditingMe";
                            $("#" + this.p.elid + "_ClientHeight").val($("#" + this.p.elid).height());
                            $("#" + this.p.elid + "_ClientWidth").val($("#" + this.p.elid).width());

                            if (this.clickStart.length > 0) {
                                for (ind in this.clickStart) {
                                    this.clickStart[ind](this.p, e);
                                }
                            };

                            // old function... will be replaced

                            Widgets.StartEditingContent(this.p.elid, ih);
                        }
                    };

                    // public variables
                    this.visible = true;           /* if this icon is visible  */

                    // public functions
                    this.element = null;
                    this.MouseEnter = function (fun) { };
                    this.MouseLeave = function (fun) { };
                    this.OnClickStart = function (fun) { };
                    this.OnClickEnd = function (fun) { };

                    this.element = $(s.$gv.elm.widgetCommands.editIcon);
                    this.element.attr("id", this.p.elid + "_edit_icon");
                    this.element.click($.proxy(Click, this));
                    this.element.mouseenter($.proxy(OME, this));
                    this.element.mouseleave($.proxy(OML, this));

                };

                var _settingsIconClass = function (s) {
                    /* this is the settings construct */
                    // private variabels
                    this.ome = new Array();        /* events to fire when mouse enters this */
                    this.oml = new Array();        /* fires when leaves obj */
                    this.clickStart = new Array(); /* stores the events to fire when the click event starts before the postback is called to the event */
                    this.clickEnd = new Array();   /* stores the events to fire after the post back */
                    this.p = s;
                    this.visible = true;
                    // private functions
                    var OME = function (e) {
                        /* this fires when the settings icon is fired this will be mostly used for styling */
                        this.element.toggleClass(this.p.$gv.css.cmdIconHover);
                    };

                    var OML = function (e) {
                        /* this fires when the settings icon is fired this will be mostly used for styling */
                        this.element.toggleClass(this.p.$gv.css.cmdIconHover);
                    };

                    var Click = function (e) {

                        if (!this.p.isInCMode) {
                            var _w = $("#" + this.p.elid); // $(e.currentTarget);
                            var _m = $("#wcm_" + _w.attr("id"));
                            _m.hide();
                            _m.appendTo("body");
                            this.element.toggleClass(this.p.$gv.css.cmdIconHover);

                            var newwidgeturl = "/default.cmd?cmd=2.2&wid=" + this.p.zwcid;
                            var oManager = GetRadWindowManager();
                            var oWnd = oManager.open(newwidgeturl, "DialogWindow");
                            oWnd.setSize($(window).width() * .75, $(window).height() * .80);
                            oWnd.center();


                            if (this.clickStart.length > 0) {
                                for (ind in this.clickStart) {
                                    this.clickStart[ind](this.p, e);
                                }
                            };

                        }
                    };

                    // public variables
                    this.visible = true;           /* if this icon is visible  */

                    // public functions
                    this.element = null;
                    this.MouseEnter = function (fun) { };
                    this.MouseLeave = function (fun) { };
                    this.OnClickStart = function (fun) { };
                    this.OnClickEnd = function (fun) { };

                    this.element = $(s.$gv.elm.widgetCommands.settingsIcon);
                    this.element.click($.proxy(Click, this));
                    this.element.mouseenter($.proxy(OME, this));
                    this.element.mouseleave($.proxy(OML, this));

                };



                // public variables
                this.visible = true;

                // create setting icon
                this.settings = new _settingsIconClass(s);


                // create edit icon
                this.edit = new _editIconClass(s);

                // public functions


            };

            var _cmd = new _cmds(this);

            var _elm = $(this.$gv.elm.normalDiv);
            _elm.addClass(this.$gv.css.widgetCmdBar);
            _elm.append(_cmd.edit.element);
            _elm.append(_cmd.settings.element);


            // add clear object
            _elm.append(this.$gv.elm.clearDiv);

            _cmd.element = _elm;

            this.cmds = _cmd;

            // build command list

        }

        , GetCommandList: function () {
            if (this.cmds == null) {
                // build command list
                this.cmdsClass();
            }

            return this.cmds.element;
        }

        , RegContainer: function () {
            /* this register the update panel information */
        }
        , BuildCMenu: function () {

            var _cMenu = function (s) {

                this._p_ome = function (s, e) {
                    if (!s.isDmode && !s.isInCMode) {
                        var _w = $("#" + s.elid); // $(e.currentTarget);
                        var _m = $("#wcm_" + _w.attr("id"));
                        _m.appendTo(_w);
                        var _t = _w.position().top - _m.height();
                        if (_w.position().top < _m.height()) {
                            // put at bottom of widget.
                            _t = _w.position().top + _w.height();
                        }

                        if ($.browser.msie && $.browser.version < 8) {
                            // need to set width
                            _m.css("width", _w.width() * .7);
                            // also need to move down just a little
                            _t = _w.position().top - (_m.height() * .25);
                        }

                        _m.css("top", _t).css("left", _w.position().left).show();

                    }

                };

                this._p_oml = function (s, e) {
                    if (!s.isDmode && !s.isInCMode) {

                        var _w = $("#" + s.elid);
                        var _m = $("#wcm_" + _w.attr("id"));
                        _m.hide();
                        _m.appendTo("body");
                        var _t = "";

                    }

                };

                var _item = $("#" + s.elid);
                if (_item != null) {
                    if (_item.length != 0) {
                        // var _w = _item; // $("#" + _item);
                        var _m = $(s.$gv.elm.normalDiv); // this.$gf.BuildWidgetCMenu(_data.ZoneWidgetCrossID);
                        _m.hide();
                        _m.attr("id", "wcm_" + _item.attr("id")).addClass($AMS.$gv.css.widgetCmdMenu).css('visibility', 'visible');
                        _m.css("z-index", 500);

                        var _title = $(s.$gv.elm.normalDiv);
                        var _t = $(s.$gv.elm.normalDiv).appendTo(_title);

                        _t.html("<span>Menu: </span><span title='Click to edit title!'>" + s.title + " </span>");
                        _t.css("padding-left", "3px").css("padding-top", "3px")
                        .css("padding-right", "10px").css("float", "left");


                        _title.addClass(s.$gv.css.widgetCmdTitle);
                        _title.appendTo(_m);

                        var __cmdmenu = s.GetCommandList();
                        __cmdmenu.css("float", "right");
                        __cmdmenu.appendTo(_title);

                        _title.append(s.$gv.elm.clearDiv);
                        _m.append(s.$gv.elm.clearDiv);
                        _m.appendTo("body");
                        // $("body").append(_m);

                    }
                }
            }

            this.cMenu = new _cMenu(this);
            this.MouseEnter(this.cMenu._p_ome);
            this.MouseLeave(this.cMenu._p_oml);
        }
        , Update: function (obj) {
            // the obj are the new settings being passed in
            // this updates an obj that already exists
            $.extend(true, this, obj);
            // i think i need to rebind mouse over objects... 
            $("#" + this.elid).mouseenter($.proxy(this.OME, this));
            $("#" + this.elid).mouseleave($.proxy(this.OML, this));

            var _t = this;
        }
        , MouseEnter: function (fun) {
            this.omoFun[this.omoFun.length] = fun;
        }
        , MouseLeave: function (fun) {
            this.omlFun[this.omlFun.length] = fun;
        }
        , OME: function (e) {
            if (this.omoFun.length > 0) {
                for (ind in this.omoFun) {
                    this.omoFun[ind](this, e);
                }
            }
            this.telid.OMO();
        }
        , OML: function (e) {
            if (this.omlFun.length > 0) {
                for (ind in this.omlFun) {
                    this.omlFun[ind](this, e);
                }
            }
        }
        , SettingClicked: function (e) {

            PageManager.Mouse.x = this.$gv.mloc.x;
            PageManager.Mouse.y = this.$gv.mloc.y;

            var ih = this.elid + "_EditingMe";
            $("#" + this.elid + "_ClientHeight").val($("#" + this.elid).height());
            $("#" + this.elid + "_ClientWidth").val($("#" + this.elid).width());

            Widgets.StartEditingContent(this.elid, ih);
            return false;
        }
    };

    // czidDom:'2_12_zone_content', widgetid:18, pid:12, zt:0, cpos:0, name:'head logo', ised:false
    // remove for live
    $AMS.WidgetMan.WidgetClass = $AMS.$RC("AMS.WidgetMan.Widget", Widget);

    if ($AMS.WidgetMan.WidgetClass === undefined) {
        $AMS.WidgetMan.WidgetClass = $AMS.$RC("AMS.WidgetMan.Widget", Widget);
    }

})();