/* 
* $Id: plugins.js 380 2008-06-22 16:21:32Z lukejee $
*
*/
Ext.namespace('NGMP.plugins');

NGMP.plugins.PageSizePlugin = function() {
    NGMP.plugins.PageSizePlugin.superclass.constructor.call(this,{
        store:new Ext.data.SimpleStore({
            fields:["text","value"],
			data:[["10",10],
			["15",15],
			["20",20],
			["25",25],
			["30",30],
			["50",50],
			["100",100]]
        }),
		mode:"local",
		displayField:"text",
		valueField:"value",
		editable:false,
		allowBlank:false,
		triggerAction:"all",
		width:50,
		listWidth:50
    });
};

Ext.extend(NGMP.plugins.PageSizePlugin,Ext.form.ComboBox,{
    init:function(A){
        A.on("render",this.onInitView,this);
    },
    onInitView:function(A){
        A.add("-", LANG_messages_per_page + ":"," "," "," ",this);
        this.setValue(A.pageSize);
        this.on("select",this.onPageSizeChanged,A);
    },
    onPageSizeChanged:function(A){
        this.pageSize=parseInt(A.getValue(),0);
        this.doLoad(0);
    }
    
});


NGMP.plugins.IconMenu=function(A){
	Ext.apply(this,A);
    NGMP.plugins.IconMenu.superclass.constructor.apply(this,arguments);
};

Ext.extend(NGMP.plugins.IconMenu,Ext.util.Observable,{
    closeText:LANG_close,
	createDefault:true,
	dblClickClose:true,
	defaultItems:[
		"restore",
		"minimize",
		"maximize",
		"separator",
		"close"],
	maximizeText:LANG_maximize,
	minimizeText:LANG_minimize,
	restoreText:LANG_restore,
	style:"width:16px;height:16px;left:0;top:4px;position:absolute;cursor:pointer",
	closeHandler:function(){
        this.hideMenu();
        if(this.panel.closable){
            this.panel[this.panel.closeAction]();
        }
        
    }
    ,getItemByCmd:function(B){
        if(!this.menu||!B){
            return null;
        }
        var A=this.menu.items.find(function(C){
            return B===C.cmd;
        }
        );
        return A;
    }
    ,hideMenu:function(){
        if(this.menu){
            this.menu.hide();
        }
        
    }
    ,init:function(B){
        this.panel=B;
        this.iconCls=this.iconCls||B.iconCls;
        B.on({
            scope:this,render:this.onRender,hide:this.hideMenu,destroy:this.onDestroy
        });
        var C,A,D;
        var E="im-"+B.id;
        if(this.createDefault&&Ext.isArray(this.defaultItems)&&this.defaultItems.length){
            if(this.menu){
                this.menu.add("-");
            }
            else{
                this.menu=new Ext.menu.Menu({
                    id:E
                });
            }
            for(C=0;C<this.defaultItems.length;C++){
                this.addItem(this.defaultItems[C]);
            }
            
        }
        if(Ext.isArray(this.customItems)){
            if(!this.menu){
                this.menu=new Ext.menu.Menu({
                    id:E
                });
            }
            for(C=0;C<this.customItems.length;C++){
                this.addItem(this.customItems[C]);
            }
            
        }
        if(this.menu){
            this.menu.id=this.menu.id||E;
            this.menu=Ext.menu.MenuMgr.get(this.menu);
            this.menu.on("show",this.onMenuShow,this);
            this.menu.on("hide",this.onMenuHide,this);
            this.menu.parentPanel=this.panel;
        }
        
    }
    ,addItem:function(A){
        if("separator"===A||"-"===A){
            this.menu.add("-");
            return;
        }
        if("string"===typeof A){
            cfg={
                text:this[A+"Text"],cmd:A,iconCls:"x-im-icon x-tool x-tool-"+A,scope:this,handler:this[A+"Handler"]
            };
            
        }
        else{
            cfg=A;
        }
        this.menu.add(cfg);
    }
    ,maximizeHandler:function(){
        this.hideMenu();
        if(this.panel.maximizable){
            this.panel.maximize();
        }
        
    }
    ,minimizeHandler:function(){
        this.hideMenu();
        if(this.panel.minimizable){
            this.panel.minimize();
        }
        
    }
    ,onDestroy:function(){
        if(this.menu){
            this.menu.hide();
            this.menu.destroy();
        }
        
    }
    ,onMenuHide:function(A){
        this.ignoreNextClick=this.restoreClick.defer(350,this);
    }
    ,onMenuShow:function(A){
        this.ignoreNextClick=0;
    }
    ,onRender:function(){
        var B=this.panel.header;
        if(!B){
            return;
        }
        Ext.util.CSS.createStyleSheet(".x-im-icon{float:none!important;margin-left:0!important}");
        B.addClass("x-panel-icon");
        B.applyStyles({
            position:"relative"
        }
        );
        this.icon=B.insertFirst({
            tag:"div",id:Ext.id(),style:this.style,cls:this.iconCls,qtip:this.qtip||this.tooltip||""
        }
        ,"first");
        var A=B.down("img");
        if(A){
            this.icon.alignTo(A,"tl-tl");
            A.removeClass(this.panel.iconCls||this.iconCls);
            A.set({
                src:Ext.BLANK_IMAGE_URL
            });
        }
        this.icon.on({
            scope:this,
			dblclick:function(){
                if(this.dblClickClose){
                    this.closeHandler();
                }
            }
            ,click:{
                scope:this,delay:200,fn:function(D,C){
                    if(this.menu&&!this.menu.isVisible()&&!this.ignoreNextClick){
                        this.showMenu();
                    }
                    
                }
                
            }
            
        }
        );
        this.panel.setIconClass=this.setIconClass.createDelegate(this);
    }
    ,restoreClick:function(){
        this.ignoreNextClick=0;
    }
    ,restoreHandler:function(){
        this.hideMenu();
        this.panel.restore();
    }
    ,setIconClass:function(A){
        this.icon.replaceClass(this.iconCls,A);
    }
    ,showMenu:function(){
        var A;
        if(this.menu){
            try{
                A=this.getItemByCmd("close");
                if(A){
                    A.setDisabled(!this.panel.closable);
                }
                A=this.getItemByCmd("maximize");
                if(A){
                    A.setDisabled(!this.panel.maximizable||this.panel.maximized);
                }
                A=this.getItemByCmd("minimize");
                if(A){
                    A.setDisabled(!this.panel.minimizable||this.panel.minimized);
                }
                A=this.getItemByCmd("restore");
                if(A){
                    A.setDisabled(!(this.panel.minimized||this.panel.maximized));
                }
                this.menu.show(this.icon,"tl-bl?");
            }
            catch(B){
                
            }
            
        }
        
    }
    
});
Ext.reg("iconmenu",NGMP.plugins.IconMenu);
