/**
 * @fileOverview window窗口
 * @import jQuery.js
 * @import mapbar.object.js
 * @import mapbar.aop.js
 * @import mapbar.event.js
 * @author pengkun 
 * @revision 1.0
 */

if(typeof window.mapbar == "undefined"){
	window.mapbar = {};
}
if(typeof window.mapbar.window == "undefined"){
	window.mapbar.window = {};
}
/**
 * 窗口对象
 */
(function(package){
	// Window begin ****************************
	package.conf = {
		//窗口默认的样式名称
		WINDOW_DEFAULT_CLASSNAME : "mapbar_window"
	};
	
	/** 
	 * @description 窗口对象基类
	 * @constructor 构造器
	 * @param {Map} [arg0={}] 实例化参数
	 *              .[dom=default] {Element|jQuery|selector} dom对象或jQuery支持的选择器字符串,默认在body下动态创建div
	 *              .[className=null] {String} 附加到dom的样式表
	 *              .[css=null] {Map} 附加到dom的css属性
	 *              .[group=null] {mapbar.window.Group|Array} 窗口组对象或对象数组
	 */
	var Window = mapbar.object.createClass(null, {
		group : null,
		dom : null,
		/** @constructs */
		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(){mapbar.event..cancelEvent();});
			this.dom.get(0).onclick = function(){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();
		},
		css : function (css) {
			this.dom.css(css);
		},
		addChild : function (jq) {
			this.dom.append(jq);
		},
		removeChild : function () {
			this.dom.empty();
		},
		destroy : function () {
			this.hide();
			this.dom.remove();
			delete this;
		},
		toString : function () {
			return "mapbar.window.Window";
		}
	});
	
	/** 
	 * @description 依赖窗口对象
	 * @constructor 构造器
	 * @augments Window
	 * @param {Map} [arg0={}] 实例化参数
	 *              .[dependDom=document.body] {Element|jQuery|selector} 窗口依赖的对象
	 *              .[align=right] {String} 水平对齐方式(可选值:right,center,left)
	 *              .[valign=bottom] {String} 垂直对齐方式(可选值:top,middle,bottom)
	 *              .[offset={top : 0, left : 0}] {Object} 偏移设置
	 *              	.[top=0] {Number} 上边距
	 *              	.[left=0] {Number} 左边距
	 */
	var DependWindow = mapbar.object.createClass(Window, {
		dependDom : null,
		align : "right",
		valign : "bottom",
		offset : {top : 0, left : 0},
		/** @constructs */
		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);
		},
		setDependDom : function (dependDom) {
			if (dependDom) {
				this.dependDom = jQuery(dependDom);
			}
		},
		/** 
		 * @description 调整位置
		 * @function
		 */
		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 "mapbar.window.DependWindow";
		}
	});
	
	/** 
	 * @description 指针窗口对象
	 * @constructor 构造器
	 * @augments DependWindow
	 * @param {Map} [arg0={}] 实例化参数
	 *              .[arrow=true] {Boolean} 是否有指针
	 *              .[close=true] {Boolean} 是否显示关闭按钮
	 *              .[content=null] {Element|jQuery|Array} 信息栏的内容（dom或jQuery对象，或者是两者的数组）
	 */
	var TipWindow = mapbar.object.createClass(DependWindow, {
		contentDom : null,
		closeDom : null,
		arrow : true,
		arrowDom : null,
		/** @constructs */
		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('<span class="arrow" />').hide();//.appendTo(document.body);
			this.arrowDom = tipImg;
			
			div0.append(
				jQuery('<div class="tl"/>')
				.append(
					jQuery('<div class="tr"/>')
					.append(
						jQuery('<div/>')
						.append(this.arrowDom)
					)
				)
			);
			//计算箭头图片的位置
			this.adjustArrow();
			
			if (this.arrow) {
				this.arrowDom.show();
			} else {
				this.arrowDom.hide();
			}
			
			//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();
		},
		/** 
		 * @description 调整位置
		 * @function
		 */
		adjust : function () {
			TipWindow._super.adjust.apply(this,arguments);
			this.adjustArrow();
		},
		/** 
		 * @description 调整指针位置
		 * @function
		 */
		adjustArrow : function () {
			var arrowDomWidth = parseInt(this.arrowDom.css("width")) || 0;
			
			if (this.align == "left") {
				this.arrowDom.css({right:this.dependDom.width()/2 - (this.arrowDom.width() || arrowDomWidth)/2});
			} else if (this.align == "right") {
				this.arrowDom.css({left:this.dependDom.width()/2 - (this.arrowDom.width() || arrowDomWidth)/2});
			} else if (this.align == "center") {
				this.arrowDom.css({left:this.dom.width()/2 - (this.arrowDom.width() || arrowDomWidth)/2});
			}
		},
		addChild : function (jq) {
			this.contentDom.append(jq);
		},
		removeChild : function () {
			this.contentDom.empty();
		},
		toString : function () {
			return "mapbar.window.TipWindow";
		}
	});
	// Window end ****************************
	
	// Group begin ****************************
	/** 
	 * @description 窗口组对象
	 * @constructor 构造器
	 * @param {Map} [arg0={}] 实例化参数
	 *              .[name=null] {String} 组的名字
	 *              .[member=[]] {mapbar.window.Window[]} 组内成员数组
	 */
	var Group = mapbar.object.createClass(null, {
		name : null,
		member : null,
		/** @constructs */
		init : function (arg0) {
			arg0 = arg0 || {};
			this.member = [];
			var _member = arg0.member || [];
			for (var i=0; i<_member.length; i++) {
				this.add(_member[i]);
			}
			this.name = arg0.name;
		},
		/** 
		 * @description 将窗口对象增加到组中
		 * @function
		 * @param {mapbar.window.Window} [win] 要增加的窗口
		 * @return {Boolean} 是否添加（如果已经存在返回false）
		 */
		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;
		},
		/** 
		 * @description 将窗口对象从组中删除
		 * @function
		 * @param {mapbar.window.Window} [win] 要删除的窗口
		 */
		del : function (win) {
			this.member = jQuery.grep(this.member, function(obj){
				return win == obj;
			}, true);
		},
		toString : function () {
			return "mapbar.window.Group";
		}
	});
	
	/** 
	 * @description 单一打开窗口组对象
	 * @constructor 构造器
	 * @augments Group
	 */
	var SingleOpenGroup = mapbar.object.createClass(Group, {
		/** 
		 * @description 将窗口对象增加到组中,对窗口的show和toggle方法增加拦截器实现该功能
		 * @function
		 * @param {mapbar.window.Window} [win] 要增加的窗口
		 * @return {Boolean} 是否添加（如果已经存在返回false）
		 */
		add : function (win) {
			var _this = this;
			var isAdd = false;
			if (win) {
				isAdd = SingleOpenGroup._super.add.call(this, win);
				if (isAdd) {
					mapbar.aop.addBefore(win, "show", function(){
						jQuery.each(_this.member, function(){
							this.hide();
						});
					});
					mapbar.aop.addBefore(win, "toggle", function(){
						jQuery.each(_this.member, function(){
							if(win != this){
								this.hide();
							}
						});
					});
				}
			}
			return isAdd;
		}
	});
	
	/** 
	 * @description 空白处单击关闭窗口组对象
	 * @constructor 构造器
	 * @augments Group
	 */
	var ClickCloseGroup = mapbar.object.createClass(Group, {
		/** @constructs */
		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();
			});
		}
	});
	
	/** 
	 * @description 自动调整窗口组对象
	 * @constructor 构造器
	 * @augments Group
	 */
	var AutoAdjustGroup = mapbar.object.createClass(Group, {
		/** @constructs */
		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
	});
})(mapbar.window);