/*
	这个类只负责显示和定位，不负责详细的内容；
*/

function floatWindow()
{
	var floatWin = this;
	this.hfloatWin = null;
	
	//显示的模式 光标处显示or元素中央显示or元素左右上下角显示or自己设定位置显示or标准显示 method=CURSOR OR CENTER OR LEFT-TOP OR LEFT-BOTTOM OR RIGHT-TOP OR RIGHT-BOTTOM OR OR SETTING OR NORMAL;
	//默认的显示模式
	this.method = 'NORMAL';
	
	//当前事件
	this.event = '';
	
	//浮动窗口边框
	this.border = 0;
	
	//padding
	this.padding = 5;
	
	//浮动css窗口类名
	this.classname = '';
	
	//浮动窗口id
	this.id = 'zz-float-window';
	
	//浮动窗口宽和高 需要带单位（px）
	this.width = '';
	this.height = '';
	
	//偏移量
	this.offset_src_left = 0;
	this.offset_src_top = 0;
	
	//浮动窗口距离光标的偏移量
	this.offset_cursor_left = 10;
	this.offset_cursor_top = 10;
	
	//浮动窗口内容
	this.innerhtml = '';
	
	//初始化
	this.init_setting = function (classname,width,height) {
		
		this.set_classname(classname);
		this.set_rect(width,height);
	}
	
	//设置css类名
	this.set_classname = function (classname) {
		this.classname = classname;
	}
	
	//设置id
	this.set_id = function (id) {
		this.id = id;
	}
	
	//
	
	//设置高和宽
	this.set_rect = function (width,height) {
		
		this.width = width;
		this.height = height;
	}
	
	//设置模式
	this.set_method = function (method) {
		
		this.method = method;
	}
	
	//设置偏移量
	this.set_offsetsrc = function (left,top) {
	
		this.offset_src_left = left;
		this.offset_src_top = top;
	}
	
	//设置浮动窗口偏移光标的值
	this.set_offsetcursor = function (left,top) {
	
		this.offset_cursor_left = left;
		this.offset_cursor_top = top;
	}
	
	//重新设置边框
	this.set_border = function (border) {
		this.border = border;
	}
	this.set_padding = function (padding) {
		this.padding = padding;
	}
	this.set_innerhtml = function (innerhtml) {
		
		this.innerhtml = innerhtml;
	}

	
	//创建浮动框
	this.create_window = function() {
		//可以使用css来初始化高宽等
		if(this.hfloatWin == null) {
			var hfloatWin = document.createElement("DIV");
			if(this.classname =='') {
				hfloatWin.style.background = "#FFFFE1";
				hfloatWin.style.border = 'solid 1px #000';
				this.set_border(1);
				hfloatWin.style.padding = this.padding+'px';
				hfloatWin.style.color = '#909090';
			}
			else {
				
				hfloatWin.className = this.classname;
			}
			hfloatWin.style.width = this.width;
			hfloatWin.style.height = this.height;
			hfloatWin.id = this.id;
			hfloatWin.style.zindex= 1000;
			hfloatWin.style.position= "absolute";
			hfloatWin.style.visibility = "hidden";
			hfloatWin.style.cursor = "pointer";
			document.body.appendChild(hfloatWin);
			this.hfloatWin = hfloatWin;
		}
	}
	
	//检查高和宽
	this.check_width = function (htrigger) {
		var tmpwidth = (htrigger.offsetWidth-2*(this.border+this.padding))+'px';
		this.hfloatWin.style.width = this.hfloatWin.style.width == ''? tmpwidth : this.hfloatWin.style.width;	
	}
	
	
	//移动浮动窗口
	this.moveto = function(htrigger) {
		
		if(this.hfloatWin == null) return; 
  		var htrigger_height  = htrigger.offsetHeight; 
		var htrigger_width = htrigger.offsetWidth; 
		var htrigger_top  = htrigger.offsetTop;   
 		var htrigger_left = htrigger.offsetLeft; 
		
		//计算当前float窗口高和宽
		
		var float_obj_width = this.hfloatWin.offsetWidth;
		var float_obj_height = this.hfloatWin.offsetHeight;

  		while (htrigger = htrigger.offsetParent){htrigger_top+=htrigger.offsetTop; htrigger_left+=htrigger.offsetLeft;}

		switch(this.method) {
		
			case 'NORMAL':
				var tmptop = htrigger_top + htrigger_height + this.offset_src_top;
				var tmpleft = htrigger_left + this.offset_src_left;
				
				//计算当float windows是否超出边界
				var poor_width = tmpleft + float_obj_width - document.documentElement.scrollWidth;
				if(poor_width>0) {
					tmpleft = htrigger_width + htrigger_left - float_obj_width - this.offset_src_left;
				}
				//对个别浏览器的处理(这里是针对gg浏览器的处理)
				var tmpscrollTop = document.documentElement.scrollTop || document.body.scrollTop;
				var poor_height = tmptop + float_obj_height - (document.documentElement.clientHeight+tmpscrollTop);
				if(poor_height>0) {
					tmptop = htrigger_top - float_obj_height;
				}
				
				//定位
				this.hfloatWin.style.left = tmpleft + 'px';
				this.hfloatWin.style.top = tmptop + 'px';
				break;
			case 'CENTER':
				var tmptop= htrigger_top + (htrigger_height-float_obj_height)/2;
				this.hfloatWin.style.left = htrigger_left + (htrigger_width-float_obj_width)/2 + 'px';
				this.hfloatWin.style.top = tmptop + 'px';
				break;
			case 'LEFT-TOP':
				
				var tmptop= htrigger_top;
				this.hfloatWin.style.left = htrigger_left + 'px';
		
				this.hfloatWin.style.top = tmptop + 'px';
				break;
			case 'LEFT-BOTTOM':
				
				var tmptop= htrigger_top + htrigger_height - float_obj_height;
				this.hfloatWin.style.left = htrigger_left + 'px';
		
				this.hfloatWin.style.top = tmptop + 'px';
				break;
			case 'RIGHT-TOP':
				
				var tmptop= htrigger_top;
				this.hfloatWin.style.left = htrigger_left + (htrigger_width - float_obj_width) + 'px';
		
				this.hfloatWin.style.top = tmptop + 'px';
				break;	
			case 'RIGHT-BOTTOM':
				
				var tmptop= htrigger_top + htrigger_height - float_obj_height;
				this.hfloatWin.style.left = htrigger_left + (htrigger_width - float_obj_width) + 'px';
		
				this.hfloatWin.style.top = tmptop + 'px';
				break;
			case 'SETTING':
				
				this.hfloatWin.style.left = this.offset_src_left + 'px';
				this.hfloatWin.style.top = this.offset_src_top + 'px';
				break;
			case 'CURSOR':
				var tmpscrollTop = document.documentElement.scrollTop || document.body.scrollTop;
				var tmpscrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
				var tmpleft = this.event.clientX + this.offset_cursor_left + tmpscrollLeft;
				var tmptop = this.event.clientY + this.offset_cursor_top + tmpscrollTop;
				if((tmpleft+float_obj_width)>(document.documentElement.clientWidth+tmpscrollLeft)) {
					
					tmpleft = this.event.clientX + tmpscrollLeft - this.offset_cursor_left -float_obj_width;
				}
				if((tmptop+float_obj_height)>(document.documentElement.clientHeight+tmpscrollTop)) {
					
					tmptop = this.event.clientY + tmpscrollTop - this.offset_cursor_top -float_obj_height;
				}
				this.hfloatWin.style.left = tmpleft + 'px';
				this.hfloatWin.style.top = tmptop + 'px';
				break;		
			default:
				break;
		}
		


	}
	
	
	//显示hfloatWin
	this.show_window = function(htrigger) {
		
		//htrigger=document.getElementById(id);
		this.create_window();
		if(this.innerhtml!='') {
			this.hfloatWin.innerHTML=this.innerhtml;
		}else {
			this.hfloatWin.innerHTML=htrigger.innerHTML;
		}
		if(this.method == 'CURSOR') {
			
			this.hfloatWin.style.visibility = "visible";
			htrigger.onmousemove = function(event) {
				event = (event == null)? window.event:event;
				floatWin.event = event;
				floatWin.moveto(htrigger);

			}
			htrigger.onmouseout = function(event) {
				floatWin.hfloatWin.style.visibility = "hidden";
			}
			
			
		} else {
		
			this.check_width(htrigger);
			this.moveto(htrigger);
			document.onmouseover = function(event) {
				
				var trag;
				event = (event == null)? window.event:event;
				trag = event.srcElement? event.srcElement : event.target;
				if(trag.id == floatWin.hfloatWin.id || trag.id == htrigger.id) {
					if(floatWin.hfloatWin.style.visibility == "hidden")
						floatWin.hfloatWin.style.visibility = "visible";
				}
				else {
					if(floatWin.hfloatWin.style.visibility == "visible")
						floatWin.hfloatWin.style.visibility = "hidden";
				}
			}
		}
		
	}

}
