var _DependWindow_Array = new Array();
function destroyAllDependWindow(obj){
	for(var i=0;i<_DependWindow_Array.length;i++){
		try{
			if(typeof(obj) == "undefined" || _DependWindow_Array[i].thisObj.id != obj.thisObj.id)
			_DependWindow_Array[i].thisObj.remove();
		}catch(e){
			
		}
	}
}
function destroyAllDependWindowExceptObj(obj){
	destroyAllDependWindow(obj);
}
function deleteDependWindowClickFunc(evt){
	evt.cancelBubble = true;
	SearchEvent().cancelBubble = true;
	destroyAllDependWindow();
}

/**
 * 依赖DIV
 * 参数：id：依赖的DOM ID , isAutoX:是否根据浏览器窗口自动调整显示X轴位置 , isAutoY：同isAutoX
 */
function DependWindow(id,isAutoX,isAutoY){
	
	if(!id){
		throw new Error("DependWindow must be have a id(String) or a jquery object(jqueryObject)!");
	}
	
	isAutoX = typeof isAutoX == 'undefined' ? true : false;
	isAutoY = typeof isAutoY == 'undefined' ? true : false;
	
	//继承OusheID类
	ousheExtend(OusheID , this);
	
	_DependWindow_Array.push(this);
	
	try{
		SearchEvent().cancelBubble = true;
	}catch(SearchEventException){
		
	}
	
	if((typeof(id)).toUpperCase() == "STRING"){
		this.dependObj = $("#"+id);
	}else{
		this.dependObj = $(id);
	}
	
	this.jqueryObj = new SuspendDiv("dependWindow_"+this.id);
	//this.jqueryObj.click(function(evt){alert(1);});
	
	var jqueryObj = this.jqueryObj;
	
	$.extend(this,this.jqueryObj);
	
	
	
	this.show = function(n){
		destroyAllDependWindowExceptObj(this);
		
		//var top = this.dependObj.offset().top + this.dependObj.height();
		//var left = this.dependObj.offset().left;
		//this.jqueryObj.css({top:top,left:left});
		
		
		
		this.jqueryObj.show(n);
		
		this.adjustPosition();
	}
	
	//调整位置adjust position
	this.adjustPosition = function(){
	  var document_width = $(document).width();
	  var window_width = $(window).width()-25;
	  
	  var document_height = $(document).height();
	  var window_height = $(window).height()-25;
	  
	  var width = this.jqueryObj.width();
	  var height = this.jqueryObj.height();
    
	  var top = this.dependObj.offset().top + this.dependObj.outerHeight() + this.dependObj.scrollTop();
		var left = this.dependObj.offset().left + this.dependObj.scrollLeft();
		
		if(isAutoX && (left + width) > (window_width+document.documentElement.scrollLeft)){
		    left = document_width - width - (document_width-window_width) + document.documentElement.scrollLeft;
		}
		if(isAutoY && (top + height) > (window_height+document.documentElement.scrollTop)){
		    top = document_height - height - (document_height-window_height) + document.documentElement.scrollTop;
		}
		//alert(top + ":" +left)
		this.jqueryObj.css({top:top,left:left});
	}
	
	//var objStr = this.getThisObjStr();
	this.remove = function(){
		//alert(1);
		jqueryObj.remove();
	}
	var remove = this.remove;
	
	//为网页注册click事件，删除动态创建的DIV
	$(document).click(deleteDependWindowClickFunc);
}