~
function($)
{
	$.Tabs = function(a)
	{
		this._init(a)
	};
	$.Tabs.INDEX={};
	var f = (function()
	{
		var t = $('<div>')[0];
		t.id = "recycler";
		try
		{
			return  t
		}
		finally
		{
			t = null
		}
	})();
	var g = function(a)
	{
		a = typeof(a) === 'string' ? $(a)[0] : a;
		if (a.hasChildNodes())
		{
			g(a.firstChild);
			g(a)
		}
		if (a.tagName === 'IFRAME')
		{
			$(a).unbind();
			a.src = 'about:blank';
			if (typeof a.contentWindow.document === 'object')
			{
				a.contentWindow.document.open();
				a.contentWindow.document.write('');
				a.contentWindow.document.clear();
				a.contentWindow.document.close()
			}
		};
		if (a && f)
		{
			$(a).unbind();
			f.appendChild(a);
			f.innerHTML = ''
		}
	};
	var h = function(D)
	{
		var d = D;
		var e = [];
		return  		{
			addEvents: function()
			{
				for (var i = 0; i < arguments.length; i++)
				{
					if (this.indexOfEvent(arguments[i]) < 0)
					{
						e.push(
						{
							name: arguments[i],
							handlers: null
						})
					}
				}
			},
			addEventHandler: function(a, b)
			{
				var c = this.indexOfEvent(a);
				if (c > -1)
				{
					if (!e[c].handlers)
					{
						e[c].handlers = []
					}
					e[c].handlers.push(b)
				}
			},
			trigger: function(a)
			{
				var b = this.indexOfEvent(a);
				if (b > -1)
				{
					if (!e[b].handlers)
					{
						return  					}
					for (var i = 0; i < e[b].handlers.length; i++)
					{
						e[b].handlers[i].apply(d, Array.prototype.slice.call(arguments, 1))
					}
				}
			},
			removeEvents: function()
			{
				for (var i = 0; i < arguments.length; i++)
				{
					var a = this.indexOfEvent(arguments[i]);
					if (a > -1)
					{
						e[a] = null;
						e.splice(a, 1)
					}
				}
			},
			indexOfEvent: function(a)
			{
				for (var j = 0; j < e.length; j++)
				{
					if (e[j].name === a)
					{
						return  j
					}
				}
				return  - 1
			}
		}
	};
	$.extend($.Tabs.prototype,
	{
		_init: function(a)
		{
			this.options = $.extend(
			{
				el: null,
				closable: false,
				width: '100%',
				height: "100%",
				headerWidth: 'auto',
				handlers: null,
				selectedIndex: 0,
				headerTemplate: '<a href="#{href}" title="#{text}"><span>#{text}</span></a><a class="em-tab-close">'+(IS_VISITOR?'&nbsp;':'<span class="em-tab-closable-#{closable}"></span>')+'</a>',
				frameTemplate: '<div id="#{id}"><iframe url="#{url}" id="iframe-#{id}" name="iframe-#{id}" class="iframe-#{tid}"  scrolling="no" src="" frameborder="0" allowTransparency="true" style="width: 100%;height: 100%; visibility: inherit;display: inherit;"></iframe></div>'
			},
			a);
			if (typeof this.options.el != 'object' && typeof this.options.el != 'string')
			{
				alert("初始化对象为空或不存在");
				return  			}
			this.events = new h(this);
			this.events.addEvents('tabadd', 'tabremove', 'tabchange', 'tabbeforeload', 'tabshow', 'tabcontextmenu', 'tabload');
			if (this.options.handlers)
			{
				for (var i = 0,
				l = this.options.handlers.length; i < l; i++)
				{
					this.events.addEventHandler(this.options.handlers[i].name, this.options.handlers[i].fn)
				}
			}
			this.items = k();
			this.stack = m();
			this.frameCount = 0;
			
			this.body = $(this.options.el).width(this.options.width).height(this.options.height);
			this.strip = this.body.find('ul:first').addClass('menuBox').wrap('<div class="nav"></div>').wrap('<div class="menu"></div>');
			this.stripWrap = this.strip.parent();
			this.header = this.stripWrap.parent();

			var b = this.strip.find('li');
			for (var i = 0, L = b.length; i < L; i++)
			{
				var c = $(b[i]).find('a');
				$(b[i]).html(this.options.headerTemplate.replace(/#\{text\}/g, c.text()).replace(/#\{closable\}/g, $(b[i]).attr('closable') || this.options.closable)).attr("self",$.url.get("self",c.attr('href'))).children('a:first').attr('href', c.attr('href'));
				g(c.get(0));
				this._addTab(b[i])
			};
			b = null;
			var o=this.options.selectedIndex;
			isNaN(o)?o=this.options.selectedIndex=0:0;
			this.select(o);
			o=null;
		},

		add: function(H, C, A, b)
		{
			if(this.getTabCount()>7)return !alert("你最多可以建立8个页签");
			var rid=getRID(5);
			C = typeof C === 'string' ? C+(IS_GUEST?"&demo=1":"")+"&id="+rid: '#' + $(C).attr('id');
			var a = $('<li class="tabClass-'+this.getTabCount()+'">' + this.options.headerTemplate.replace(/#\{text\}/g, H).replace(/#\{closable\}/g, A ? A: this.options.closable) + '</li>').insertBefore(this.strip.children("span"));
			a.find('a:first').attr('href', C);
			this._addTab(a.get(0));
			/**
			 *选中新建的页签
			 */
			CD_doAction("newTab",{title:H,id:rid,colsize:"33%,34%,33%"});

			this.select(this.getTabCount() - 1);
			Tabs.bindTabEvent();
			/**
			 *设置编辑状态
			 */

			a.find('a:first').click();
			a = null;
			this.events.trigger('tabadd', this.getSelectedTab())
		},
		select: function(n)
		{
			
			if (n < 0 || n >= this.getTabCount()) return;
			if (n != this.getSelectedIndex())
			{
				this.stack.add(this.getSelectedTab())
			}
			$("#tabs_container").children("li").removeClass("em-tab-selected");	
			var a = this.getSelectedTab()||this.getTab(0);
			a ? this._hideTab(a) : null;
			$(a.header).find(".em-tab-close span").attr("class","em-tab-closable-false");
			var d = this.increate;
			this.increate = this.options.selectedIndex < n;
			this.options.selectedIndex = n;
			var b = this.getSelectedTab();
			this._showTab(b);
			
			IS_VISITOR?0:$(b.header).attr("closable","true").find(".em-tab-close span").attr("class","em-tab-closable-true");
			var c = $(b.content).find('iframe').css(
			IS_FIRST_LOAD?{}:{
				marginLeft: (this.increate ? 1 : -1) * document.body.clientWidth
			});
			if (b.type === 'url' && c.attr('src') === '')
			{
				this.events.trigger('tabbeforeload', b);
				c.bind('load',
				{
					obj: this.events,
					args: b
				},
				function(e)
				{
					e.data.obj.trigger('tabload', e.data.args)
				});
				c.attr('src', c.attr('url'))
			}
			
			this.id=$.url.get("id",c.attr('url'));
			IS_VISITOR?0:(window.Tabs?CD_doAction("switchTab",{index:n}):0);
			$.Tabs.INDEX[this.id]=n;
			IS_FIRST_LOAD?0:c.animate(
			{
				marginLeft: 0
			},
			"slow");
			//IS_FIRST_LOAD=false;
			this.events.trigger('tabchange', b, a);
			a = c = null;
			try
			{
				return b
			}
			finally
			{
				
				b=null;
			}
		},

		remove: function(n)
		{
			var t = this.getTab(n);
			if (!t)
			{
				return  			}
			this._removeTab(t);

			var a = false;
			if (n < this.getSelectedIndex())
			{
				this.options.selectedIndex--
			}
			else
			if (n === this.getSelectedIndex())
			{
				a = this.getIndex(this.stack.next())
			}
			if (typeof a === 'number')
			{
				this.select(a < 0 ? this.getTabCount() - 2 : a)
			}
			
			this.id=$.url.get("id",t.a.attr("href"));
			$.Tabs.INDEX[this.id]=null;
			t = a = null;
			this.events.trigger('tabremove')
		},
		contextmenu: function(n)
		{
			this.events.trigger('contextmenu', this.getTab(n))
		},
		getTab: function(n)
		{
			return  this.items.get(n)
		},
		getSelectedTab: function()
		{
			return  this.getSelectedIndex() < this.getTabCount() && this.getSelectedIndex() >= 0 ? this.getTab(this.getSelectedIndex()) : null
		},
		getTabCount: function()
		{
			return  this.items.getCount()
		},
		getBody: function()
		{
			return  this.body.get(0)
		},
		getSelectedIndex: function()
		{
			return  this.options.selectedIndex
		},
		getIndex: function(a)
		{
			for (var C = 0,
			B = this.getTabCount(); C < B; C++)
			{
				if (this.getTab(C) === a)
				{
					return  C
				}
			}
			return  - 1
		},
		getContentHeight: function()
		{
			var a = 0;
			var b = this.body.children('div:visible:not(.em-tab-content-selected)');
			for (var i = 0,
			L = b.length; i < L; i++)
			{
				a += $(b[i]).height()
			}
			b = null;
			return  this.body.height() - a
		},
		getHeadersWidth: function(n)
		{
			n = typeof n === 'number' ? n: this.items.getCount() - 1;
			var a = parseInt(this.strip.css('padding-left'));
			for (var i = 0; i <= n; i++)
			{
				a += $(this.items.get(i).header).outerWidth(true)
			}
			return  a
		},
		_addTab: function(b)
		{
			$(b).find('a:first').width(this.options.headerWidth);
			var a = $(b).find('a:first');
			var t = this._getTabType(a);
			this.items.add(this._createItem(b, this._getTabContent(a), t,a));
			$(b).find('a:first').unbind('click').bind('click',
			{
				'obj': this,
				'action': 'select'
			},
			this._headerEventHander);
			$(b).unbind('contextmenu').bind('contextmenu',
			{
				'obj': this,
				'action': 'contextmenu'
			},
			this._headerEventHander);
			var id=$.url.get("id",a.attr("href"));
			
			IS_VISITOR||id==1||id==2?$(b).find('.em-tab-close').hide():$(b).find('.em-tab-close').find("span").unbind('click').bind('click',
			{
				'obj': this,
				'action': 'remove'
			},
			this._headerEventHander);

			$.Tabs.INDEX[id]=this.getTabCount()-1;
			a = b = t = null
		},
		_removeTab: function(t)
		{
			var a = this;
			g(t.header);
			$(t.content).animate(
			{
				opacity: 0,
				width: 0,
				height: 0
			},
			"fast",
			function()
			{
				g(t.content);
				if (t.type === 'url') a.frameCount--;
				a.stack.remove(t);
				a.items.remove(t)
			})
		},
		_createItem: function(H, C, T,A)
		{
			return  			{
				'a':A,
				'header': H,
				'content': C,
				'type': (T != 'url' ? 'div': T)
			}
		},
		_hideTab: function(T)
		{
			$(T.header).removeClass('em-tab-selected');
			$(T.content).removeClass('em-tab-content-selected').addClass('em-tab-content');
			T = null
		},
		_showTab: function(T)
		{
			$(T.header).addClass('em-tab-selected');
			$(T.content).addClass('em-tab-content-selected').removeClass('em-tab-content');
			this.events.trigger('tabshow', T);
			T = null
		},
		_getTabContent: function(A)
		{
			var a = null,
			preV = null;
			if (this._getTabType(A) === 'div')
			{
				a = $(A.attr('href')).addClass('em-tab-content')
			}
			else
			{
				var id=$.url.get("id",A.attr('href'));
				a = $(this.options.frameTemplate.replace(/#\{url\}/g, A.attr('href')).replace(/#\{id\}/g, id).replace(/#\{tid\}/g, id)).appendTo(this.body).addClass('em-tab-content');
				this.id=id;
				$.Tabs.INDEX[this.id]=this.options.selectedIndex;
				A.attr('href', '#id=' + $.url.get("id",A.attr('href')))
			}
			A = preHeight = preV = null;
			a.height(this.getContentHeight());
			return  a.get(0)
		},
		_getTabType: function(D)
		{
			if (D.attr('href').substring(0, 1) === '#') return  'div';
			return  'url'
		},
		_headerEventHander: function(e)
		{
			//e.preventDefault();
			var b = e.data.obj;
			if(e.data.action=="remove")
			{
				if(b.getTabCount()==1)return alert("你不能删除最后一个页签");
				if(!confirm("确定删除该页签么？"))
				{
					return false;
				}
			}
			var a = $(this).parents('li:eq(0)').get(0);
			
			for (var i = 0,
			L = b.getTabCount(); i < L; i++)
			{
				if (b.getTab(i).header === a)
				{
					if (e.data.action === 'select')
					{
						if(i === b.getSelectedIndex())
						{
							var rid=b.id;
							var _before = $.trim($(this).text());
							if($("#INP_"+rid).length||$(a).attr("closable")=="false"||rid==1||rid==2)return;
							var ot=$.trim($(this).text().replace(/\"/g,"&quot;"));
							var this_a=$(this).clone(true);
							//var __ = $(this).html().replace($(this).text),pageTitle;
							//alert(__);
							//$(this).outerHTML($("<input maxlength='16' value='"+ot+"' id='INP_"+rid+"'>"));
							$(this).outerHTML($('<input maxlength="16" value="'+ot+'" id="INP_'+rid+'">'));
							$("#INP_"+rid).blur(function()
							{	
      						   var _that = $(this);
							   var pageTitle = $j.trim($(this).val()).replace(/\"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); 
							   if(pageTitle == _before){
							   		_that.replaceWith(this_a.html(pageTitle));
									return;
							   }
							   if(pageTitle == ''){
							   	  alert('页签标题不能为空！');
								  $("#INP_" + rid ).val("").focus();
								  return false;
							    }
								else
								if(pageTitle.length > 16){
							   	  alert('页签标题不要超过16个字符');
								  $("#INP_" + rid ).val(pageTitle).select();
								  return false;
							    }
								$.post("index.php?r=//exist",{id:rid,title:pageTitle},function(o){
									if( /EXIST/.test(o) ){
										 alert("页签不能有重名!");
										 return $("#INP_" + rid ).val(pageTitle).focus() & false;
										  
								 		 //return false;
									}
									else{
										ot!=$(this).val()?CD_doAction("renameTab",{title:pageTitle,id:rid}):0;
										_that.replaceWith(this_a.html(pageTitle));
									}
									
								});
							}).keydown(function (e)
							{
								if(e.keyCode==13){this.blur()}
							});
							setTimeout('$("#INP_'+rid+'").select();',10);

						}
						else
						{
							b[e.data.action](i);
						}
						this.blur()
					}
					else b[e.data.action](i);
					break
				}
			}
			a = b = null
		}
	});
	var k = function()
	{
		var A = [];
		return  		{
			get: function(i)
			{
				return  A[i]
			},
			add: function(B)
			{
				A.push(B)
			},
			remove: function(E)
			{
				if (typeof E === 'number')
				{
					A.splice(E, 1);
					return  				}
				var D = [];
				for (var C = 0,
				B = A.length; C < B; C++)
				{
					if (A[C] != E)
					{
						D.push(A[C])
					}
				}
				A = D;
				D = null
			},
			getCount: function()
			{
				return  A.length
			}
		}
	};
	var m = function()
	{
		var A = [];
		return  		{
			add: function(B)
			{
				A.push(B);
				if (A.length > 10)
				{
					A.shift()
				}
			},
			remove: function(E)
			{
				var D = [];
				for (var C = 0,
				B = A.length; C < B; C++)
				{
					if (A[C] != E)
					{
						D.push(A[C])
					}
				}
				A = D
			},
			next: function()
			{
				return  A.pop()
			}
		}
	}
} (jQuery);