/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
// Very simple plugin for adding a close context menu to tabs
Ext.ux.TabCloseMenu = function(){
	var tabs, menu, ctxItem;
	this.init = function(tabpanel){
		tabs = tabpanel;
		tabs.on('contextmenu', onContextMenu);
	}
	function onContextMenu(ts, item, e){
		//タブの右クリックメニューの禁止
		if(item.contextmenucancel)
			return ;

		if(!menu){ // create context menu on first right click
			menu = new Ext.menu.Menu([{
				id: tabs.id + '-close',
				text: 'タブ情報表示',
				iconCls:'tx-show-node-icon',
				handler : function(){
debug.log(ctxItem);

				}
			},{
				id: tabs.id + '-close',
				text: 'このタブを削除',
				iconCls:'tx-tab-delete-icon',
				handler : function(){
					tabs.remove(ctxItem);
				}
			},{
				id: tabs.id + '-close-others',
				text: 'これ以外のタブを削除',
				iconCls:'tx-tab-delete-all-icon',
				handler : function(){
					tabs.items.each(function(item){
						if(item.closable && item != ctxItem){
							tabs.remove(item);
						}
					});
				}
			},{
				id: tabs.id + '-close-all',
				text: 'すべてのタブを削除',
				iconCls:'tx-tab-delete-all-icon',
				handler : function(){
					tabs.items.each(function(item){
						if(item.closable){
							tabs.remove(item);
						}
					});
				}
			},{
				id: tabs.id + '-close-right',
				text: '右のタブを削除',
				iconCls:'tx-tab-delete-right-icon',
				handler : function(){
					var pos=tabs.items.indexOf(ctxItem);
					tabs.items.each(function(item,i){
						if(item.closable){
							if(i>pos)
								tabs.remove(item);
						}
					});
				}
			},{
				id: tabs.id + '-close-left',
				text: '左のタブを削除',
				iconCls:'tx-tab-delete-left-icon',
				handler : function(){
					var pos=tabs.items.indexOf(ctxItem);
					tabs.items.each(function(item,i){
						if(item.closable){
							if(i<pos)
								tabs.remove(item);
						}
					});
				}
			},{
				id: tabs.id + '-fix',
				text: 'このタブを固定する',
				iconCls:'tx-tab-fix-icon',
				handler : function(){

					tabs.getTabMgr().fix(ctxItem.node);
					ctxItem.closable=false;

					//アイコンを固定タブにする
					Ext.fly(tabs.getTabEl(ctxItem).id).first().replaceClass("x-tab-strip-close","tx-tab-strip-fix");
				}
			},{
				id: tabs.id + '-unfix',
				text: 'このタブの固定を解除する',
				iconCls:'tx-tab-unfix-icon',
				handler : function(){
					ctxItem.closable=true;

					//アイコンを戻す
					if(ctxItem == tabs.getActiveTab()){
						Ext.fly(tabs.getTabEl(ctxItem).id).first().replaceClass("tx-tab-strip-fix","x-tab-strip-close");
					}else{
						Ext.fly(tabs.getTabEl(ctxItem).id).first().removeClass("tx-tab-strip-fix");

					}
				}
			}]);
		}
		ctxItem = item;
		var items = menu.items;
		items.get(tabs.id + '-close').setDisabled(!item.closable);
		var disableOthers = true;
		tabs.items.each(function(){
			if(this != item && this.closable){
				disableOthers = false;
				return false;
			}
		});
		items.get(tabs.id + '-close-others').setDisabled(disableOthers);
		menu.showAt(e.getPoint());
	}
};

