/* Everything below this line is old, don't use it anymore */


var BasePane = new Class({
    getOptions : function() {
        return {};
    },

    getEvents : function() {
        return {};
    },

    initialize : function(pane,trigger,events,options){
        this.pane = pane;
        this.trigger = trigger;
        this.setOptions(this.getOptions(),options);
        this.events = Object.extend(this.getEvents(), events);
        for(evt in this.events) {
            this.trigger.addEvent(evt,this.initEvent(this,evt).bindWithEvent(this.trigger));
        }
    },

    initEvent : function(parent,event) {
        return function(e) { parent.events[event].call(parent,e); }
    },


    toggle : function() {
        if(!this.options.state) {
            this.open();
        } else {
            this.close();
        }
    },

    force : function(state) {
        if(state) {
            this.show();
        } else {
            this.hide();
        }
    },

    hide : function() {
        this.options.state = false;
        this.pane.setStyle("visibility","hidden");
    },

    show : function() {
        this.options.state = true;
        this.pane.setStyle("visibility","visible");
    },

    close : function() {
        this.hide();
    },

    open : function() {
        this.show();
    }
});

BasePane.implement(new Events);
BasePane.implement(new Options);


var ClickHoverPane = new Class({
    testInBounds : function() {
        if(!this.pane.inBounds(pedro.event.mousePos,this.options.ppad) &&
           !this.trigger.inBounds(pedro.event.mousePos,this.options.tpad)) {
            this.close();
        } else {
            this.testInBounds.delay(this.options.bscan,this);
        }
    }
});

var BasePosPane = BasePane.extend({
    getOptions : function() {
        return {
            'pos' : {
                'x' : ['left','left',0],
                'y' : ['top','top',0]
            }
        }
    },

    initialize : function(pane,trigger,events,options) {
        this.parent(pane,trigger,events,options);
        this.oCoords = this.pane.getCoordinates();
        this.adoptPane();
        window.addEvent("resize",(function(){ this.adjustForResize() }).bind(this));
    },

    adoptPane : function() {
        $(document.body).adopt(this.pane);
        if (pedro.os.isLinux || pedro.browser.isIE6) {
            this.frame = iframe({
                'width' : "100%",
                'height': "100%",
                'frameBorder':'0',
                'scrolling':'no',
                'display':'block',
                'visibility':'visible'
            })
            this.pane.appendChild(this.frame);
        }
        this.positionPane();
        this.recordCoords();
    },

    positionPane : function() {
        var prm = {};
        if (!$chk(this.tCoords)) this.tCoords = this.trigger.getCoordinates();
        if (!$chk(this.pCoords)) this.pCoords = this.pane.getCoordinates();
        var oCoords = this.tCoords;

        this.tCoords = this.trigger.getCoordinates();

        prm[this.options.pos.x[0]] =
            this.tCoords[this.options.pos.x[1]] +
            this.options.pos.x[2] +
            (-1*(this.tCoords[this.options.pos.x[1]] - oCoords[this.options.pos.x[1]]))	+
            'px';

        prm[this.options.pos.y[0]] =
            this.tCoords[this.options.pos.y[1]] +
            this.options.pos.y[2] +
            (-1*(this.tCoords[this.options.pos.y[1]] - oCoords[this.options.pos.y[1]]))	+
            'px';

        this.pCoords[this.options.pos.x[0]] = prm[this.options.pos.x[0]].toInt();
        this.pCoords[this.options.pos.y[0]] = prm[this.options.pos.y[0]].toInt();

        this.pane.setStyles(prm);
    },

    recordCoords : function() {
        this.pCoords = this.pane.getCoordinates();
    },

    adjustForResize : function() {
        this.positionPane();
        this.recordCoords();
    }
});

var ClickHoverPosPane = BasePosPane.extend({

    getOptions : function() {
        return {
            'tpad'  : 20,	/* trigger padding */
            'ppad'  : 10,	/* pane padding */
            'odur' 	: 250, 	/* open fx duration */
            'cdur'	: 75,	/* close fx duration */
            'tdelay': 250,	/* trigger delay */
            'bscan' : 500,	/* boundry scan */
            'pos' : {
                'x' : ['left','right',0],
                'y' : ['top','bottom',0]
            }
        };
    },

    getEvents : function() {
        return {
            'click' : function(evt) { this.open(); }
        };
    },

    initialize : function(pane,trigger,events,options) {
        this.parent(pane,trigger,events,options);
        this.hide();
    },

    open : function() {
        this.show();
        this.tCoords = this.trigger.getCoordinates();
        this.positionPane();
        this.timer = this.testInBounds.delay(this.options.bscan,this);
    },

    close : function() {
        this.hide();
        $clear(this.timer);
    }

});

ClickHoverPosPane.implement(new ClickHoverPane);

var ExpanderPane = BasePane.extend({
    getEvents : function() {
        return {
            'click' : function(evt) { this.toggle(); }
        };
    },

    initialize : function(pane,trigger,events,options) {
        this.parent(pane,trigger,events,options);
        var tmp = this;
        this.ofx = new Fx.Style(this.pane, "height", {
            'duration' : 500,
            'onComplete' : function() {
                tmp.onComplete();
            }
        });
        this.pCoords = this.pane.getCoordinates();
        this.pane.setStyle("overflow","hidden");
        this.hide();
    },

    onComplete : function() {
        this.inprocess = false;
    },

    toggle : function() {
        if(this.inprocess) return;
        if(!this.options.state) {
            this.open();
        } else {
            this.close();
        }
    },

    _open: function() {
        this.inprocess = true;
        this.options.state = true;
        this.ofx.start(0,this.pCoords.height);
        this.trigger.setHTML("Less");
        this.trigger.removeClass("expand");
        this.trigger.addClass("contract");
    },

    open : function() {
        this.fireEvent('onOpen', this, 10);
    },

    close : function() {
        this.inprocess = true;
        this.options.state = false;
        this.ofx.start(0);
        this.trigger.setHTML("More");
        this.trigger.removeClass("contract");
        this.trigger.addClass("expand");
    },

    hide : function() {
        this.options.state = false;
        this.ofx.set(0);
    }

});


var SchedulePane = BasePane.extend({
    getEvents : function() {
        return {
            'click' : function(evt) { this.open(); }
        };
    },

    initialize : function(pane,trigger,options) {
        this.parent(pane,trigger,{},options);
        this.hide();
    },

    hide : function() {
        this.options.state = false;
        this.pane.setStyle("display","none");
        this.trigger.removeClass("enabled");
    },

    show : function() {
        this.options.state = true;
        this.pane.setStyle("display","block");
        this.trigger.addClass("enabled");
        this.trigger.blur();
    },

    _open : function() {
        this.show();
    },

    open : function() {
        this.fireEvent('onOpen', this, 10);
    },

    close : function() {
        this.hide();
    }

});

var EdgeAwareBasePosPane = BasePosPane.extend({
    positionPane : function() {
        var prm = {};
        if (!$chk(this.tCoords)) this.tCoords = this.trigger.getCoordinates();
        if (!$chk(this.pCoords)) this.pCoords = this.pane.getCoordinates();
        this.tCoords = this.trigger.getCoordinates();

        var ch = this.tCoords.top - window.getScrollTop() + this.oCoords.height;
        var yoffset = 0; var wh = window.getHeight();
        if (ch > wh) { yoffset = wh - ch - 115; } // changing the position on the page height //

        var cw = this.tCoords[this.options.pos.x[1]] - window.getScrollLeft() + this.oCoords.width;
        var xoffset = 0; var ww = window.getWidth();
        if (cw > ww) { xoffset = ww - cw - 250; } // changing the position on the page width //

        prm[this.options.pos.x[0]] =
            this.tCoords[this.options.pos.x[1]] +
            this.options.pos.x[2] + xoffset + 'px';

        prm[this.options.pos.y[0]] =
            this.tCoords[this.options.pos.y[1]] +
            this.options.pos.y[2] + yoffset + 'px';

        this.pCoords[this.options.pos.x[0]] = prm[this.options.pos.x[0]].toInt();
        this.pCoords[this.options.pos.y[0]] = prm[this.options.pos.y[0]].toInt();

        this.pane.setStyles(prm);
    }

});

var MoreFlyOutPant = EdgeAwareBasePosPane.extend({
    getOptions : function() {
        return {
            'tpad'  : 20,	/* trigger padding */
            'ppad'  : 10,	/* pane padding */
            'odur' 	: 250, 	/* open fx duration */
            'cdur'	: 75,	/* close fx duration */
            'tdelay': 250,	/* trigger delay */
            'bscan' : 500,	/* boundry scan */
            'pos' : {
                'x' : ['left','right',0],
                'y' : ['top','top',0]
            }
        };
    },

    getEvents : function() {
        return {
            'click' : function(evt) { this.open(); }
        };
    },

    initialize : function(pane,trigger,events,options) {
        this.parent(pane,trigger,events,options);
        this.addEvent("onOpen",(function(){
            this.timer = this.testInBounds.delay(this.options.bscan,this);
        }));
        this.ofx = new Fx.Styles(this.pane, {
            'duration' : this.options.odur,
            //'transition' : Fx.Transitions.backOut,
            'onComplete' : (function() {
                this.onComplete();
            }).bind(this)
        });
        this.hide();
    },

    onComplete : function() {
        this.inprocess = false;
        if (!this.options.state) {
            this.hide();
        } else {
            this.pane.addClass("enabled-fly-out");
            this.timer = this.testInBounds.delay(this.options.bscan,this);
        }
    },

    open : function() {
        this.inprocess = true;
        this.options.state = true;
        this.positionPane();
        this.show();
        this.fireEvent('onOpen', this, this.options.odur);
        this.ofx.start({
            'height' : [0,this.oCoords.height],
            'width'  : [0,this.oCoords.width],
            'left'   : [this.tCoords.left,this.pCoords.left],
            'top'    : [this.tCoords.top,this.pCoords.top]
        });
    },

    close : function() {
        this.inprocess = true;
        this.options.state = false;
        this.pane.removeClass("enabled-fly-out");
        this.fireEvent('onClose', this, this.options.odur);
        this.ofx.start({
            'height' : 0,
            'width'  : 0,
            'left'   : [this.pCoords.left,this.tCoords.left],
            'top'    : [this.pCoords.top,this.tCoords.top]
        });

    },

    show : function() {
        this.pane.setStyle("visibility","visible");
    },

    hide : function() {
        this.pane.setStyle("visibility","hidden");
    }

});

MoreFlyOutPant.implement(new ClickHoverPane);


var FlyOutPane = EdgeAwareBasePosPane.extend({
    getOptions : function() {
        return {
            'tpad'  : 10,	/* trigger padding */
            'ppad'  : 10,	/* pane padding */
            'odur' 	: 250, 	/* open fx duration */
            'cdur'	: 75,	/* close fx duration */
            'tdelay': 500,	/* trigger delay */
            'bscan' : 500,	/* boundry scan */
            'pos' : {
                'x' : ['left','right',0],
                'y' : ['top','bottom',0]
            }
        };
    },

    getEvents : function() {
        return {
            'mouseover' : function(evt) { this.hoverOpen(); },
            'click' : function(evt) { this.open(); }
        };
    },

    initialize : function(pane,trigger,events,options) {
        this.parent(pane,trigger,events,options);
        var tmp = this;
        this.ofx = new Fx.Styles(this.pane, {
            'duration' : this.options.odur,
            'transition' : Fx.Transitions.backOut,
            'onComplete' : function() {
                tmp.onComplete();
            }
        });

        this.cfx = new Fx.Styles(this.pane, {
            'duration' : this.options.cdur,
            'onComplete' : function() {
                tmp.onComplete();
            }
        });
        this.hide();
    },

    onComplete : function() {
        this.inprocess = false;
        if (!this.options.state) {
            this.hide();
        } else {
            this.pane.addClass("enabled-fly-out");
            this.timer = this.testInBounds.delay(this.options.bscan,this);
        }
    },


    open : function() {
        this.inprocess = true;
        this.options.state = true;
        this.positionPane();
        this.show();
        this.ofx.start({
            'height' : [0,this.pCoords.height],
            'width'  : [0,this.pCoords.width],
            'left'   : [this.tCoords.left,this.pCoords.left]
        });
    },

    close : function() {
        this.inprocess = true;
        this.options.state = false;
        this.pane.removeClass("enabled-fly-out");
        this.cfx.start({
            'height' : 0,
            'width'  : 0,
            'left'   : [this.pCoords.left,this.tCoords.left]
        });
    },

    show : function() {
        this.pane.setStyle("visibility","visible");
    },

    hide : function() {
        this.pane.setStyle("visibility","hidden");
    },

    hoverOpen : function() {
        if (!this.options.state && !this.inprocess) {
            (function(){
                if(!this.options.state && this.trigger.inBounds(pedro.event.mousePos,this.options.tpad)) {
                    this.open();
                }

            }).delay(this.options.tdelay,this);
        }
    }

});

FlyOutPane.implement(new ClickHoverPane);

var BasePaneManager = new Class({
    getOptions : function() {
        return {};
    },

    initialize : function(panes,paneClass,options){
        this.setOptions(this.getOptions(),options);
        this.panes = [];
        var parent = this;
        for (var pane in panes) {
            if ($chk($(pane)) && $chk($(panes[pane]))) {
                var p = new paneClass($(pane),$(panes[pane]),{},{});
                p.addEvent("onOpen",(function(){
                    parent.open(this);
                }).bind(p));
                if (this.options.defaultPane == pane) {
                    parent.open(p);
                }
            }
        }
    },

    open : function(pane) {
        if ($chk(this.current) && this.current != pane) {
            this.current.close();
        }
        this.current = pane;
        this.current._open();
    }

});

BasePaneManager.implement(new Events);
BasePaneManager.implement(new Options);

var SelectorPaneManager = BasePaneManager.extend({
    getOptions : function() {
        return {};
    },

    initialize : function(panes,options,selector){
        this.setOptions(this.getOptions(),options);
        this.panes = [];
        this.selector = selector;
        var parent = this;

        var i = 0;
        for (var pane in panes) {
            if ($chk($(pane)) && $chk($(panes[pane]))) {
                var p = new SchedulePane($(pane),$(panes[pane]),{'paneSelectorIndex' : i});
                p.addEvent("onOpen",(function(){
                    parent.open(this);
                }).bind(p));
                if (this.options.defaultPane == pane) {
                    parent.open(p);
                }
                this.panes[i] = p;
            }
            i = i + 1;
        }

        this.selector.addEvent('change',
            (function() {
                this.open(this.panes[this.selector.options.selectedIndex]);
            }).bind(this)
        );

    },

    open : function(pane) {
        if ($chk(this.current) && this.current != pane) {
            this.current.close();
        }
        this.selector.options[pane.options.paneSelectorIndex].selected = true;
        this.current = pane;
        this.current._open();
    }
});