﻿var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
};
HintDialog = Class.create();

HintDialog.id = "__widget_hint_dialog";

HintDialog.register = function(obj, msg, options){
	if(!obj)
		return;
	new HintDialog(obj, msg, options);
}

HintDialog.prototype = {
	_msg : "",
	_obj : null,
	initialize : function(obj, msg, options){
		if(!obj)
			return;
	    var tempList = document.getElementsByTagName("DIV");
	    for(var i=0;i<tempList.length;i++){
	        if(tempList[i].className=="accurate" || tempList[i].className=="error"){
	           tempList[i].style.display = "none";
	        }
	    }
	    try{
	        SetTabPageHW(top.window.loadingIframeId);
	    }
	    catch(e){}
		this._msg = (msg) ? msg : "";
		this._options = {
				width : "",							//对话框宽度
				topOffset : 10,								//对话框相对表单控件的顶部偏远量
				leftOffset : -10,							//对话框相对表单控件的左侧偏远量
				showEvent : null,							//显示对话框的触发事件，默认为无，即创建了提示对话框后立即显示
				hideEvent : "blur"							//隐藏对话框的触发事件，默认为控件失去焦点
				
			};
		this._path="images/";
		if(options)
		{
			if(typeof(options.hideEvent)!="undefined") this._options.hideEvent=options.hideEvent;
			if(typeof(options.path)!="undefined")this._path=options.path;
		}
		this._obj = obj;
		this._showHint();
		var my=this;
		switch(this._options.hideEvent)
		{
			case "mouseout":this._obj.onmouseout=function(){eval(my._hideHint());};break;
			default:this._obj.onblur=function(){eval(my._hideHint());};break;
		}
	},
	_showHint : function(){
	
		if (document.getElementById(HintDialog.id))
			return;
		
		var objPosition = this._getAbsolutePos(this._obj);
		    objPosition.y -=  54;
		var box = document.createElement("DIV");
			box.id = HintDialog.id;
		with(box.style){
			position = "absolute";
			top = (objPosition.y+this._options.topOffset) + "px";
			left = (objPosition.x+this._options.leftOffset) + "px";
			width = this._options.width;
		}
		box.appendChild(this._generateContent());
		document.body.appendChild(box);
		//if (Browser.IE)
		//	Utils.hideShowCovered(box);
	},
	_generateContent : function(){
		var width = (this._getComputedStyle(this._options.width)-2) + "px";
		var content = document.createElement("DIV");
		var contentPadder = document.createElement("DIV");
		var msg = document.createElement("DIV");
			this._msg = "<div style=\"margin-top:15px; height:30px;\">"
			          + "<div style=\"float:left;\"><img src=\""+this._path+"text_content-left.gif\" /></div>"
			          + "<div style=\"float:left; height:24px; background:url("+this._path+"text_content-bg.gif); line-height:24px; color:#265972;\">"
			          + this._msg
			          + "</div>"
			          + "<div style=\"float:left;\"><img src=\""+this._path+"text_content-right.gif\" /></div>"
			          + "</div>"
			          + "<div style=\"clear:both;\"></div>"
			          + "<div style=\"margin-left:40px; width:80px; margin-top:-9px; position:absolute;\"><img src=\""+this._path+"buttom_angle.gif\" /></div>";
	    msg.innerHTML = this._msg;
			
		with(msg.style){
			overflow = "hidden";
		}
		
		contentPadder.appendChild(msg);
		content.appendChild(contentPadder);
		
		return content;
	},
	_hideHint : function(){
		var obj = document.getElementById(HintDialog.id);
		if (!obj)
			return;
		
		document.body.removeChild(obj);
		
		//if (Utils.isIE)	
		//	Utils.hideShowCovered(obj);
		
	},
	_getAbsolutePos : function(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = this._getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
	},
	_getComputedStyle : function(str){
		var reg = /[0-9]+/i;
		var value = str.match(reg);
		return (value == null) ? 0 : parseInt(value);
	}
};
