﻿/**
 * @author mark
 * date 2008/12/19
 * Import: DropthingsUI, Sys, jQuery
 * usage: new Widgets.Behaviors.UI.Max({ widget: [object], targetUrl: [string], icon: [string] });
 */

Type.registerNamespace('Widgets.Behaviors.UI');
Widgets.Behaviors.UI.Max = function() { this.initialize.apply(this, arguments); };
Widgets.Behaviors.UI.Max.prototype = {
    initialize: function(args) {
    },

    onInit: function() {
        var thisObj = this;
        var elt = this.getWidget().element;
        this.getState().processState = "minimized";

        var activeEvent = function() {
            if (thisObj.getState().processState == "minimized")
                thisObj.maximize();
            else if (thisObj.getState().processState == "maximized")
                thisObj.minimize();
        }

        if (this.getState().handle == "title-bar")
            $(elt).find("div.widget_header").dblclick(activeEvent);
        else if (this.getState().handle == "widget-context")
            $(elt).find("div.widget_body").dblclick(activeEvent);
        else if (this.getState().handle == "max-icon") {
            $(elt).find(".widget_size").click(activeEvent);
            $(elt).find(".widget_size").click(function() {
                if (thisObj.getState().processState == "maximized")
                    $(".widget_box", elt).removeClass("icon_size_min").addClass("icon_size_max");
                else if (thisObj.getState().processState == "minimized")
                    $(".widget_box", elt).removeClass("icon_size_max").addClass("icon_size_min");
            });
        }

        if (this.getState().view == "max")
            thisObj.maximize();
    },

    maximize: function() {
        if (!this.getState().enabled)
            return;

        if (this.onMaximizing() == false)
            return;

        if (this.getState().processState == "minimized") {
            this.getState().processState = "maximizing";
            // begin update DOM
            if (this.getState().fillin == "widget-container") {
                var eltId = this.getWidget().element.id;
                $("table.table_fixed > * > tr > td").filter(function(index) {
                    if ($(this).find("#" + eltId).length == 0) {
                        return true;
                    } else {
                        $(this).find("div.widget").not("#" + eltId).hide(); //.css("display", "none");
                        return false;
                    }
                }).hide(); //.css("display", "none");
                $(document).scrollTop($("#contents").position().top);
                $(this.getWidget().element).addClass("maximized");
            }

            this.getState().processState = "maximized";

            this.onMaximize();
        }
    },

    minimize: function() {
        if (!this.getState().enabled)
            return;

        if (this.onMinimizing() == false)
            return;

        if (this.getState().processState == "maximized") {
            this.getState().processState = "minimizing";

            // begin update DOM
            if (this.getState().fillin == "widget-container") {
                var eltId = this.getWidget().element.id;
                $("table.table_fixed > * > tr > td").filter(function(index) {
                    if ($(this).find("#" + eltId).length == 0) {
                        return true;
                    } else {
                        $(this).find("div.widget").not("#" + eltId).show(); //.css("display", "block");
                    }
                }).show(); //.css("display", "block");
                $(document).scrollTop(0);
                $(this.getWidget().element).removeClass("maximized");
            }

            this.getState().processState = "minimized";

            this.onMinimize();
        }
    },

    onMaximize: function() { return true },

    onMinimize: function() { return true },

    onMaximizing: function() { return true },

    onMinimizing: function() { return true },

    getWidget: function() { throw new Error("No widget instance"); },

    getState: function() { throw new Error("No state instance"); }
};