var kdtUI = (function(){
	function xWindow(id, hasLayer){
		this.id = id;
		this.isDisplay = true;
		this.$window = $('#' + id);
		if (!this.$window.length) {
			this.$window = $('<div></div>').attr('id', id).addClass('tabbox').appendTo(document.body);
		}
		this.layer = {};
		this.hasLayer = hasLayer == undefined? hasLayer: true;
		if (this.hasLayer) {
			this.layer = getLayer();
		}
		this.html = function (html) {
			this.$window.html(html)
		}
		this.onEvent = function(event, type) {
			// type == 'browser'表示该事件来自浏览器，type == window 表示该窗口自身的事件
			type = type == undefined? 'browser' : type;
			type = type == 'browser'? type : 'window';
/*
			if (type == 'browser') {
				switch(event.type) {
					case '':
						
						break;
				}
			} else {
				switch (event)
				{
					case '':

					break;
				
				}
			
			}
			*/
		},

		this.show = function() {
			//@todo 计算居中位置
			var tabBoxHeight = this.$window.height();
			this.$window.css({'display':'block' , 'left':'50%' , 'margin-left':'-175px' , 'margin-top':-tabBoxHeight/2});
			if($.browser.version=='6.0') {
				this.$window.css({'margin-top':0});
			}

			this.isDisplay = true;
			this.$window.show();
			if (this.hasLayer) {
				this.layer.show();
			}
		},

		this.hide = function() {
			this.isDisplay = false;
			var tabBoxHeight = this.$window.height();
			//this.$window.css({'opacity':1 , 'left':'50%' , 'top':'50%' , 'margin-left':'-175px' , 'margin-top':-tabBoxHeight/2});
			this.$window.hide();
			if (this.hasLayer) {
				this.layer.hide();
			}
		}
	}
	
	function Layer(id) {
		this.$layer = $('#' + id);
		if (!this.$layer.length) {
			this.$layer = $('<div></div>').attr('id', id).addClass('transparent').appendTo(document.body);
		}
		this.show = function() {
			var pageScreenHeight = $(document).height();
			this.$layer.height(pageScreenHeight);
			
			this.$layer.show();
		}

		this.hide = function() {
			this.$layer.hide();
		}
	}

	function getLayer() {
		if (!window.kdtLayer) {
			window.kdtLayer = new Layer('kdtLayer');
		}
		return window.kdtLayer;
	}

	function Tab(tab, page, index) {
		this.tabCurrentClassName = 'current';
		this.$tab = $(tab);
		this.$page = $(page);
		this.selectedIndex = index || 0;
		this.length = this.$tab.length;

		
		if (this.$tab.length != this.$page.length) {
			return false;
		}
		this.$tab.click((function(Tab){
				return function(){
					Tab.select(Tab.$tab.index(this));
				}
			})(this));
	
		this.select(this.selectedIndex);
	}

	Tab.prototype = {
		// 得到当前的页对象
		getSelectedTab: function () {
			return this.$tab.eq(this.selectedIndex);
		},
		getSelectedPage: function() {
				 	return this.$page.eq(this.selectedIndex);
				 },

		// 选中一个页
		select: function(index) {
			if (index < 0 || index >= this.length) {
				return false;
			}
			if (index == this.selectedIndex) {
				return;
			}
			
			this.$tab.eq(this.selectedIndex).children().removeClass(this.tabCurrentClassName);
			this.$tab.eq(index).children().addClass(this.tabCurrentClassName);
			this.$page.eq(this.selectedIndex).hide();
			this.$page.eq(index).show();
			this.selectedIndex = index;
		}
	}
	var ui = {
		windowManager: {
			windows: {},
			getWindow: function(window_id) {
				if (window_id == undefined) {
					return false;
				}
				if (this.windows[window_id]) {
					return this.window[window_id];
				}
				return false;
			},

			newWindow: function(window_id, usingLayer) {
				if (window_id == undefined) {
					return false;
				}
				if (this.windows[window_id]) {
					return this.window[window_id];
				}
				usingLayer = usingLayer == undefined ? true : usingLayer;
				return this.windows[window_id] = new xWindow(window_id, usingLayer);
			},
			
			unsetWindow: function(window_id) {
				if (window_id == undefined || !this.windows[window_id]) {
					return false;
				}
				
				this.windows[window_id] = null;
				return true;
			},

			/**
			 *
			 * 通知所有窗口浏览器事件
			 */
			telEvent: function(event) {
				for (var i in this.windows )
				{
					this.windows[i].onEvent(event); 
				}
			}
		},

		editWindow: { // 编辑、密码修改窗口
				    init: function(callback, reload) {
						  if (!this.window) {
						  	this.window = ui.windowManager.newWindow('mdfBox', true);
						  }
						  reload = reload == undefined? true: reload; 
						  if (!this.tab || reload) {
						  	$.get('?act=edit', function(text){
									ui.editWindow.window.html(text);
									ui.editWindow.window.$window.find('input[type="text"],input[type="password"]').click(function(){
												     ui.editWindow.window.$window.find('input[type="text"],input[type="password"]').removeClass('focus');
												     $(this).addClass('focus');
												     });
									if (callback != undefined) {
										callback();
									}
								  })
						  }
					  },
				    showAndSelectTab: function(index) {
							      /*
							     if (this.tab && this.window) {
								     this.tab.select(index);
								     this.window.show();
						  	     } else {
								     */
								     this.init((function(index) {return function(){
											     ui.editWindow.tab =  new Tab('#mdfBox > .tab > ul > li', '#mdfBox > .combox > .inner_combox > .i_inner_combox > div');
											     
											     
											     window.showErr_hideMsg = true;
											     ui.editWindow.tab.select(index);
											     ui.editWindow.window.show();
										     }})(index), true);
								/*	
						      		}
						      		*/
						     },
				    hide: function() {
					  	this.window.hide();
					  },
				    repasswdSucc: function() {
						this.tab.getSelectedPage().children('form').hide();
					      	this.tab.getSelectedPage().children('.success').show();
					      },
					editSucc: function() {
							this.tab.getSelectedPage().children('form').hide();
					      	this.tab.getSelectedPage().children('.success').show();
						  }
			    }, 
		forgetWindow: {
				show: function() {
					      if (!this.window) {
						  	this.window = ui.windowManager.newWindow('forgetPWD', true);
					      }
					      $.get('?act=forgotpwd', function(text){
									ui.forgetWindow.window.html(text);
									window.showErr_hideMsg = true;
									$ul = ui.forgetWindow.window.$window.find('form > ul');
									$input = $ul.find('input[type=text],input[type=password]');
									$input.addClass('font_gray').click((function($ul){
											return function() {
												$input.filter('.focus').removeClass('focus');
												$(this).addClass('focus').removeClass('font_gray');
											}
										})($input));
									
									ui.forgetWindow.window.show();
								  })
				      
				      },
				hide: function() {
					      this.window.hide();
				      },
				forgetSucc: function() {
					    $('#forgetForm').hide().next().show();
					    },
				showError: function() {
					   $('#forgetForm').hide().next().next().show();
					   },
				retry: function() {
				       $('#forgetForm').show().next().next().hide();
				       
				       }
			   
			   },
		chkLoginWindow: {
			   show: function() {
					      if (!this.window) {
						  	this.window = ui.windowManager.newWindow('chkLoginWindow', true);
					      }
						  ui.chkLoginWindow.window.show();
			   },
			   hide: function() {
					  this.window.hide();
				  }
			   }
	}

	window.resizeFunctions['windowManager'] = function(event) {
		ui.windowManager.telEvent(event);
	}

	return ui;
})();
