//
var Try = {
  these: function() {
    var returnValue;
    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }
    return returnValue;
  }
};
//Create XMLHTTP and DOM
var Util = {
  getXMLHttp: function(){
	return Try.these(
		function() {return new ActiveXObject('Microsoft.XMLHTTP')},
		function() {return new ActiveXObject('Msxml2.XMLHTTP')},
		function() {return new XMLHttpRequest()}
	) || null;
  },
  getDom: function(){
	return Try.these(
		function() {return new ActiveXObject('Microsoft.XMLDOM')},
		function() {return new ActiveXObject("MSXML2.DOMDocument")},
		function() {return document.implementation.createDocument('','',null)}
	) || null;
  }
};
//Exec XMLHTTP Request
var XMLHttp = {
  //[d]send data；[t]request type；[u]request url；[a]if asynchronous
  run: function(d, t, u, a){
    var xmlhttp = Util.getXMLHttp();
    var reqType = t?t:'post';
    var reqUrl = u?u:'/XMLHttpServlet';
    var asyn = a?a:'false';
	if(!xmlhttp){
      alert('Create XMLHttpRequest Error');
      return null;
	}
    xmlhttp.open(reqType, reqUrl, asyn);
    if(reqType.toLowerCase() == 'post'){
    	xmlhttp.setRequestHeader('CONTENT-TYPE','application/x-www-form-urlencoded');
    }
    try{
  	    xmlhttp.send(d);
	  	return xmlhttp.responseXML;
    }catch(e){
	    alert('Run XMLHttpRequest Error: ' + e.message
	      + 'Send Data: ' + d + '\n'
	      + 'RequestType: ' + reqType + '\n'
	      + 'RequestUrl: ' + reqUrl + '\n'
	      + 'Asynchronous: ' + asyn);
	    return null;
    }
  }
};
//Add and Get Cookie
var CookieUtil = {
	//[n]name; [v]value; [h]hours; [p]path; [d]domain; [s]secure;
	save: function(n, v, h, p, d, s){
		var cs = n + '=' + escape(v);
		//because of time difference (h+8)
		if(h) cs += '; expires=' + (new Date(new Date().getTime() + (h+8)*3600000)).toGMTString();
		if(p) cs += '; path=' + p;
		if(d) cs += '; domain=' + d;
		if(s) cs += '; ' + s;
		//alert(cs);
		document.cookie = cs;
	},
	//get cookie value by name
	getValue: function(n){	
		var cookies = document.cookie;
		if(cookies == '')	return '';
		var start = cookies.indexOf(n + '=');
		if(start == -1)	return '';
		start += n.length + 1;
		var end = cookies.indexOf(';', start);
		if(end == -1)	end = document.cookie.length;
		var cv = cookies.substring(start, end);
		return unescape(cv);
	}
};

/////////////////////////////////////////////////////
//
function $(n){
	return document.getElementById(n);
}

function $i(i){
	try{
		return parseInt(i);
	}catch(ex){
		return 0;
	}
}

function MessageTip(id)
{
	//显示ID
	this.id = id;
	this.obj = $(id);
	//显示位置
	this.divTop = $i(this.obj.style.top);
	this.divLeft = $i(this.obj.style.left);
	this.divHeight = this.obj.offsetHeight;
	this.divWidth = this.obj.offsetWidth;
	this.docWidth = document.body.clientWidth;
	this.docHeight = document.body.clientHeight;
	//内部属性
	this.timeout= 150;
	this.speed = 30;
	this.step = 3;
	this.timer = 0;
	this.pause = false;
	this.close = false;
	this.autoClose = true;
	//记录提示
	this.add(this);
}

MessageTip.prototype.tips = new Array();

MessageTip.prototype.add = function(mt)
{
	this.tips.push(mt);
}

MessageTip.prototype.show = function()
{
	if(this.onload()){
  	var me = this;
  	var mess = this.obj;
  	mess.onmouseover = function(){me.pause=true;};
  	mess.onmouseout = function(){me.pause=false;};
  	mess.style.top = $i(document.body.scrollTop) + this.docHeight + 10;
  	mess.style.left = $i(document.body.scrollLeft) + this.docWidth - this.divWidth;	
  	mess.style.visibility = 'visible';
  	var moveUp = function()
  	{
  		var tHeight = me.divHeight;
  		var t = me.tips;
  		for(var i=0; i<t.length; i++){
  			var tt = t[i];
  			if(tt==me){
  				break;
  			}else{
  				tHeight += tt.divHeight;
  			}
  		}
  		if($i(mess.style.top) <= (me.docHeight - tHeight + $i(document.body.scrollTop))){
        me.timeout--; 
        if(me.timeout==0){
  			  window.clearInterval(me.timer);
  				if(me.autoClose){
  					me.hide();
  				}
  			}
  		} else {
  			mess.style.top = $i(mess.style.top) - me.step;
  	  }
  	}
  	this.timer = window.setInterval(moveUp,this.speed);
  }
}

MessageTip.prototype.hide = function()
{
	if(this.onunload()){
		var me = this;
		var mess = this.obj;
    if(this.timer>0){
      window.clearInterval(me.timer);
    }
		var moveDown = function()
		{
			if(me.pause==false || me.close){
				if($i(mess.style.top) >= ($i(document.body.scrollTop) + me.docHeight + 10)){
			  	window.clearInterval(me.timer);
		  		mess.style.visibility='hidden';
				} else {
	  			mess.style.top = $i(mess.style.top) + me.step;
	  		}
		  }
	  }
	  this.timer = window.setInterval(moveDown,this.speed);
  }
}

MessageTip.prototype.resize = function()
{
	this.divHeight = $(this.id).offsetHeight;
	this.divWidth = $(this.id).offsetWidth;
	this.docWidth = document.body.clientWidth;
	this.docHeight = document.body.clientHeight;
	$(this.id).style.top = this.docHeight - this.divHeight + document.body.scrollTop;
	$(this.id).style.left = this.docWidth - this.divWidth + document.body.scrollLeft;	
}

MessageTip.prototype.noTip = function()
{
	CookieUtil.save('liyunetbbstipshow','yes',24);
}

MessageTip.prototype.onload = function()
{
	var noTip = CookieUtil.getValue('liyunetbbstipshow');
	if(noTip == ''){
		return true;
	}else{
		return false;
	}
}

MessageTip.prototype.onunload = function()
{
	return true;
}
/////////////////////////////////////////////////////
