/**
 * 依赖js:jQuery.js,com.mapbar.object.js,com.mapbar.aop.js
 */
if(typeof window.com == "undefined"){
	window.com = {};
}
if(typeof window.com.mapbar == "undefined"){
	window.com.mapbar = {};
}
if(typeof window.com.mapbar.window == "undefined"){
	window.com.mapbar.window = {};
}
/**
 * 窗口对象
 */
(function(package){
	// Window begin ****************************
	package.conf = {
		//窗口默认的样式名称
		WINDOW_DEFAULT_CLASSNAME : "mapbar_window"
	};
	/**
	 * 窗口对象
	 * 参数 : {
		 dom : dom对象或jQuery支持的选择器字符串,默认在body下动态创建div
		 className : 附加到dom的样式表
		 css : 附加到dom的css属性
		}
	 */
	var Window = com.mapbar.object.createClass(null, {
		group : null,
		dom : null,
		init : function(arg0){
			var _this = this;
			this.dom = typeof arg0.dom == "undefined" ? this.getNewDom() : jQuery(arg0.dom);
			this.dom.addClass(package.conf.WINDOW_DEFAULT_CLASSNAME);
			if(typeof arg0.className == "string"){
				this.dom.addClass(arg0.className);
			}
			if(typeof arg0.css == "object"){
				this.dom.css(arg0.css);
			}
			
			//取消事件传递
			//this.dom.click(function(){com.mapbar.event..cancelEvent();});
			this.dom.get(0).onclick = function(){com.mapbar.event.cancelEvent();};
			
			//如果有窗口组，添加到组中
			this.group = arg0.group || this.group;
			if(this.group){
				if(this.group instanceof Array){
					jQuery.each(this.group, function(){
						this.add(_this);
					});
				}else{
					this.group.add(this);
				}
			}
		},
		getNewDom : function(){
			return jQuery("<div/>").hide().appendTo(jQuery(document.body));
		},
		show : function(){
			this.dom.show();
		},
		hide : function(){
			this.dom.hide();
		},
		toggle : function(){
			this.dom.toggle();
		},
		destroy : function(){
			this.hide();
			this.dom.remove();
			delete this;
		},
		toString : function(){
			return "com.mapbar.window.Window";
		}
	});
	
	//依赖窗口对象
	var DependWindow = com.mapbar.object.createClass(Window, {
		dependDom : null,
		align : "right",
		valign : "bottom",
		offset : {top : 0, left : 0},
		init : function(arg0){
			DependWindow._super.init.call(this, arg0);
			this.dependDom = typeof arg0.dependDom == "undefined" ? jQuery(document.body) : jQuery(arg0.dependDom);
			
			jQuery.extend(this, {
				align : (arg0.align=="left"||arg0.align=="right"||arg0.align=="center") ? arg0.align : this.align,
				valign : (arg0.valign=="top"||arg0.valign=="bottom"||arg0.valign=="middle") ? arg0.valign : this.valign
			});
			this.offset = jQuery.extend({}, this.offset, arg0.offset);
		},
		show : function(){
			this.adjust();
			DependWindow._super.show.call(this);
		},
		toggle : function(){
			this.adjust();
			DependWindow._super.toggle.call(this);
		},
		//调整位置
		adjust : function(){
			var dependDomOffset = this.dependDom.offset();
			var top = 0;
			var left = 0;
			
			if(this.align == "right"){
				left = dependDomOffset.left;
			}else if(this.align == "left"){
				left = dependDomOffset.left + this.dependDom.width() - this.dom.width();
			}else if(this.align == "center"){
				left = dependDomOffset.left - Math.abs(this.dependDom.width()-this.dom.width())/2;
			}
			
			if(this.valign == "top"){
				top = dependDomOffset.top;
			}else if(this.valign == "bottom"){
				top = dependDomOffset.top + this.dependDom.height();
			}else if(this.valign == "middle"){
				top = dependDomOffset.top - Math.abs(this.dependDom.height()-this.dom.height())/2;
			}
			
			this.dom.css({
				top : top + this.offset.top,
				left : left + this.offset.left
			});
		},
		toString : function(){
			return "com.mapbar.window.DependWindow";
		}
	});
	
	var TipWindow = com.mapbar.object.createClass(DependWindow, {
		contentDom : null,
		closeDom : null,
		arrow : true,
		arrowDom : null,
		init : function(arg0){
			var _this = this;
			if(typeof arguments[0] == "undefined"){
				arguments[0] = {};
			}
			if(typeof arguments[0].offset == "undefined"){
				arguments[0].offset = {top : 0, left : 0};
			}
			if(typeof arguments[0].offset.top == "undefined"){
				arguments[0].offset.top = 0;
			}
			arguments[0].offset.top += 6;
			
			this.arrow = typeof arguments[0].arrow != "boolean" ? this.arrow : arguments[0].arrow;
			
			TipWindow._super.init.apply(this,arguments);
			
			this.dom.addClass("POPNav");
			this.dom.empty();
			
			var div0 = jQuery("<div/>").addClass("br");
			this.dom.append(div0);
			
			//为计算出实际宽度暂时添加到body
			var tipImg = jQuery('<img src="images/POP_bg_arrow.gif" />').hide().appendTo(document.body);
			this.arrowDom = tipImg;
			//计算箭头图片的位置
			this.adjustArrow();
			
			if(this.arrow){
				this.arrowDom.show();
			}else{
				this.arrowDom.hide();
			}
			div0.append(
				jQuery('<div class="tl"/>')
				.append(
					jQuery('<div class="tr"/>')
					.append(
						jQuery('<div/>')
						.append(this.arrowDom)
					)
				)
			);
			
			//close
			this.closeDom = jQuery('<span title="关闭" class="closeThis" />').click(function(){
				_this.close();
			}).hide();
			div0.append(this.closeDom);
			
			if(typeof arg0.close == "undefined" || arg0.close){
				this.showClose();
			}else{
				this.hideClose();
			}
			
			var mainDiv = jQuery('<div class="theMain"/>');
			div0.append(mainDiv);
			
			div0.append('<div class="bLine"/>');
			
			div0.append();
			
			
			//添加内容
			var content = arg0.content;
			if(typeof content != "undefined"){
				if(content instanceof Array){
					for(var i=0; i<content.length; i++)
					mainDiv.append(content[i]);
				}else{
					mainDiv.append(content);
				}
			}
			//将内容dom对象公开
			this.contentDom = mainDiv;
			
		},
		close : function(){
			this.hide();
		},
		showClose : function(){
			this.closeDom.show();
		},
		hideClose : function(){
			this.closeDom.hide();
		},
		clean : function(){
			this.contentDom.empty();
		},
		adjust : function(){
			TipWindow._super.adjust.apply(this,arguments);
		},
		adjustArrow : function(){
			if(this.align == "left"){
				this.arrowDom.css({right:this.dependDom.width()/2 - this.arrowDom.width()/2});
			}else if(this.align == "right"){
				this.arrowDom.css({left:this.dependDom.width()/2 - this.arrowDom.width()/2});
			}else if(this.align == "center"){
				this.arrowDom.css({left:this.dom.width()/2 - this.arrowDom.width()/2});
			}
		},
		toString : function(){
			return "com.mapbar.window.TipWindow";
		}
	});
	// Window end ****************************
	
	// Group begin ****************************
	var Group = com.mapbar.object.createClass(null, {
		name : null,
		member : null,
		init : function(arg0){
			arg0 = arg0 || {};
			this.member = arg0.member || [];
			this.name = arg0.name;
		},
		add : function(win){
			if(win){
				var exist = false;
				for(var i=0;i<this.member.length;i++){
					exist = win == this.member[i];
					if(exist){
						break;
					}
				}
				if(!exist){
					this.member.push(win);
					return true;
				}
			}
			return false;
		},
		del : function(win){
			this.member = jQuery.grep(this.member, function(obj){
				return win == obj;
			}, true);
		},
		toString : function(){
			return "com.mapbar.window.Group";
		}
	});
	
	var SingleOpenGroup = com.mapbar.object.createClass(Group, {
		add : function(win){
			var _this = this;
			if(win){
				var isAdd = SingleOpenGroup._super.add.call(this, win);
				if(isAdd){
					com.mapbar.aop.addBefore(win, "show", function(){
						jQuery.each(_this.member, function(){
							this.hide();
						});
					});
					com.mapbar.aop.addBefore(win, "toggle", function(){
						jQuery.each(_this.member, function(){
							if(win != this){
								this.hide();
							}
						});
					});
				}
			}
		}
	});
	
	var ClickCloseGroup = com.mapbar.object.createClass(Group, {
		init : function(){
			var _this = this;
			ClickCloseGroup._super.init.apply(this, arguments);
			jQuery(document).click(function(event){
				//对火狐特殊处理，不是点击鼠标左键，返回。
				if(jQuery.browser.mozilla && event.button != 0){
					return;
				}
				_this.documentClickFunc();
			});
		},
		documentClickFunc : function(){
			jQuery.each(this.member, function(){
				this.hide();
			});
		}
	});
	
	var AutoAdjustGroup = com.mapbar.object.createClass(Group, {
		init : function(){
			var _this = this;
			AutoAdjustGroup._super.init.apply(this, arguments);
			jQuery(window).resize(function(){
				_this.documentResizeFunc();
			});
		},
		documentResizeFunc : function(){
			jQuery.each(this.member, function(){
				if(this.adjust)
				this.adjust();
			});
		}
	});
	// Group end ****************************
	
	//将需要公开的类公开
	jQuery.extend(package,{
		Window : Window,
		DependWindow : DependWindow,
		TipWindow : TipWindow,
		
		Group : Group,
		SingleOpenGroup : SingleOpenGroup,
		ClickCloseGroup : ClickCloseGroup,
		AutoAdjustGroup : AutoAdjustGroup
	});
})(com.mapbar.window);