// JavaScript Document
// @ author: john
// @ date: 2009-01-23
// @ Copyright http://www.ecomers.cn
// you can set it like this: var fAlert = new friendlyalert();  or  var fAlert = new friendlyalert({delaytime : 300, box_w : 1000});
// the key of the array arguments[0] must the same as  window_background, box_w, box_h, box_background ......
function friendlyalert() {
	this.window_background = '#777'; //提示时浏览器的大背景颜色
	this.box_w             = 400; //提示窗口的宽度;函数的第二个参数;为形参;默认为400px
	this.box_h             = 100; //提示窗口的高度;函数的第三个参数;为形参;默认为100px
	this.box_background    = 'white'; //提示窗口的背景颜色
	this.box_border        = 'yes'; //提示窗口是否有边框
	this.border_color      = '#369'; //提示窗口的边框颜色
	this.box_title         = 'yes'; //默认为有标题栏;如果没有设置自动关闭时间的话;始终都有标题栏
	this.title_background  = '#369'; //提示标题的背景颜色
	this.title_color       = 'white'; //提示窗口的标题(关闭符号)颜色
	this.title_border      = 'yes'; //提示窗口标题是否有边框
	this.delaytime         = 0; //如果需要定时关闭的话;请在调用函数时设置参数delaytime的值为相应时间;默认为不自动关闭
	this.box_font          = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
	this.title_font        = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";

	if((arguments[0] !== "undefined") && (typeof arguments[0] === "object")) {

		if((arguments[0].window_background !== "undefined") && (typeof arguments[0].window_background == "string")) this.window_background = arguments[0].window_background;
		if((arguments[0].box_w !== "undefined") && (typeof arguments[0].box_w == "number")) this.box_w = arguments[0].box_w;
		if((arguments[0].box_h !== "undefined") && (typeof arguments[0].box_h == "number")) this.box_h = arguments[0].box_h;
		if((arguments[0].box_background !== "undefined") && (typeof arguments[0].box_background == "string")) this.box_background = arguments[0].box_background;
		if((arguments[0].box_border !== "undefined") && (typeof arguments[0].box_border == "string")) this.box_border = arguments[0].box_border;
		if((arguments[0].border_color !== "undefined") && (typeof arguments[0].border_color == "string")) this.border_color = arguments[0].border_color;
		if((arguments[0].box_title !== "undefined") && (typeof arguments[0].box_title == "string")) this.box_title = arguments[0].box_title;
		if((arguments[0].title_background !== "undefined") && (typeof arguments[0].title_background == "string")) this.title_background = arguments[0].title_background;
		if((arguments[0].title_color !== "undefined") && (typeof arguments[0].title_color == "string")) this.title_color = arguments[0].title_color;
		if((arguments[0].title_border !== "undefined") && (typeof arguments[0].title_border == "string")) this.title_border = arguments[0].title_border;
		if((arguments[0].delaytime !== "undefined") && (typeof arguments[0].delaytime == "number")) this.delaytime = arguments[0].delaytime;
		if((arguments[0].box_font !== "undefined") && (typeof arguments[0].box_font == "string")) this.box_font = arguments[0].box_font;
		if((arguments[0].title_font !== "undefined") && (typeof arguments[0].title_font == "string")) this.title_font = arguments[0].title_font;
	}
	
	this.kill = function() {
		if (document.getElementById("bg][div")) {
			document.body.removeChild(document.getElementById("bg][div")); 
		}
		if (document.getElementById("box][title")) {
			document.getElementById("box][div").removeChild(document.getElementById("box][title")); 
		}
		if (document.getElementById("box][div")) {
			document.body.removeChild(document.getElementById("box][div")); 
		}
	}
	
	this.pagesize = function() {
		var x_scroll, y_scroll, page_h, page_w;
		if (window.innerHeight && window.scrollMaxY) {	
			x_scroll = document.body.scrollWidth;
			y_scroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			x_scroll = document.body.scrollWidth;
			y_scroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			x_scroll = document.body.offsetWidth;
			y_scroll = document.body.offsetHeight;
		}
		var window_w, window_h;
		if (self.innerHeight) {	// all except Explorer
			window_w = self.innerWidth;
			window_h = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			window_w = document.documentElement.clientWidth;
			window_h = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			window_w = document.body.clientWidth;
			window_h = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(y_scroll < window_h){
			page_h = window_h;
		} else { 
			page_h = y_scroll;
		}
		// for small pages with total width less then width of the viewport
		if(x_scroll < window_w){	
			page_w = window_w;
		} else {
			page_w = x_scroll;
		}
		var page_window_size = new Array(page_w, page_h, window_w, window_h);
		return page_window_size;
	}
	
	this.divalert = function(str) { 
		//alert (this.box_w);
		//alert(this.box_h)
		this.kill();
		page_size = new this.pagesize();
		var p_width, p_height, s_width, s_height;
		p_width  = page_size[0];
		p_height = page_size[1];
		s_width  = page_size[2];
		s_height = page_size[3];
		
		// if(typeof arguments[1] == "number" || typeof arguments[1] == "string")this.Direction = arguments[1];
		var box_w             = this.box_w;
		var box_h             = this.box_h; 
		var box_border        = this.box_border;
		var box_font          = this.box_font;
		var delaytime         = this.delaytime; 
		var box_title         = this.box_title; 
		var window_background = this.window_background; 
		var border_color      = this.border_color; 
		var box_background    = this.box_background; 
		var title_border      = this.title_border;
		var title_background  = this.title_background; 
		var title_color       = this.title_color; 
		var title_font        = this.title_font;
	
		var bg_obj = document.createElement("div"); 
		bg_obj.setAttribute("id","bg][div"); 
		bg_obj.style.position = "absolute"; 
		bg_obj.style.top = "0"; 
		bg_obj.style.background = window_background; 
		bg_obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style = 3, opacity = 25, finishOpacity = 75);"; 
		bg_obj.style.opacity = "0.6"; 
		bg_obj.style.left = "0"; 
		bg_obj.style.zIndex = 999; 		
		bg_obj.style.width = p_width + "px"; 
		bg_obj.style.height = p_height + "px"; 
		document.body.appendChild(bg_obj); 
		
		var box_obj = document.createElement("div") 
		box_obj.setAttribute("id", "box][div"); 
		box_obj.setAttribute("align", "left"); 
		box_obj.style.position = "absolute"; 
		box_obj.style.zIndex = 1000;
		box_obj.style.background = box_background; 
		box_obj.style.font = box_font; 
		if (box_border == 'yes')	box_obj.style.border = "1px solid " + border_color; 
		box_obj.style.width = box_w + "px"; 
		box_obj.style.height = box_h + "px"; 
		box_obj.style.top = (document.documentElement.scrollTop + (s_height-box_h)/2) + "px"; 
		//alert (document.documentElement.scrollTop);
		box_obj.style.left = (s_width-box_w)/2 + "px"; 
		
		document.body.appendChild(box_obj); 
	
		if ((delaytime == 0 && box_title != 'no') || box_title == "yes") {
			var title = document.createElement("h4"); 
			title.setAttribute("id","box][title"); 
			title.setAttribute("align","right"); 
			title.style.margin = "0"; 
			title.style.padding = "3px"; 
			title.style.background = title_background; 
			title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX = 20, startY = 20, finishX = 100, finishY = 100, style = 1, opacity = 75, finishOpacity = 100);"; 
			title.style.opacity = "0.75"; 
			if (title_border == 'yes') title.style.border = "1px solid " + border_color; 
			title.style.height = "18px"; 
			title.style.font = title_font; 
			title.style.color = title_color; 
			title.style.cursor = "pointer"; 
			title.innerHTML = "<b>×</b>"; 
			
			title.onclick = function(){ 
				document.body.removeChild(bg_obj); 
				document.getElementById("box][div").removeChild(title); 
				document.body.removeChild(box_obj); 
			} 
			
			document.getElementById("box][div").appendChild(title); 	
		} //end if
		
		var txt = document.createElement("p"); 
		txt.style.margin = "1em"; 
		txt.setAttribute("id","box][msg"); 
		txt.innerHTML = str; 
		
		document.getElementById("box][div").appendChild(txt); 
		
		if (delaytime !== 0) {
			setTimeout(this.kill, delaytime);
		}	
	} 

	this.loading = function(str) { 
		this.kill();
		page_size = new this.pagesize();
		var p_width, p_height, s_width, s_height;
		p_width  = page_size[0];
		p_height = page_size[1];
		s_width  = page_size[2];
		s_height = page_size[3];
		
		var div_w = 230;
		var div_h = 50;
		var div_bg = "yellow";
		var div_color = "green";
		var delaytime = 0;
		var div_font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
		
	if((arguments[1] !== "undefined") && (typeof arguments[1] === "object")) {

		if((arguments[1].div_w !== "undefined") && (typeof arguments[1].div_w == "number")) div_w = arguments[1].div_w;
		if((arguments[1].div_h !== "undefined") && (typeof arguments[1].div_h == "number")) div_h = arguments[1].div_h;
		if((arguments[1].div_padding !== "undefined") && (typeof arguments[1].div_padding == "number")) div_padding = arguments[1].div_padding;
		if((arguments[1].div_bg !== "undefined") && (typeof arguments[1].div_bg == "string")) div_bg = arguments[1].div_bg;
		if((arguments[1].div_color !== "undefined") && (typeof arguments[1].div_color == "string")) div_color = arguments[1].div_color;
		if((arguments[1].delaytime !== "undefined") && (typeof arguments[1].delaytime == "number")) delaytime = arguments[1].delaytime;
		if((arguments[1].div_font !== "undefined") && (typeof arguments[1].div_font == "string")) div_font = arguments[1].div_font;
	}
	
		var div_padding = parseInt(div_h / 3);

		var box_obj = document.createElement("div") 
		box_obj.setAttribute("id", "box][div"); 
		box_obj.setAttribute("align", "center");
		box_obj.style.position = "absolute"; 
		box_obj.style.background = div_bg; 
		box_obj.style.padding = div_padding;
		box_obj.style.color = div_color;
		box_obj.style.font = div_font; 
		box_obj.style.width = div_w + "px"; 
		box_obj.style.height = div_h + "px"; 
		box_obj.innerHTML = str;
		box_obj.style.top = (document.documentElement.scrollTop + (s_height-div_h)/2) + "px"; 
		box_obj.style.left = (s_width-div_w)/2 + "px"; 
		
		document.body.appendChild(box_obj); 
		
		if (delaytime !== 0) {
			setTimeout(this.kill, delaytime);
		}	
	} 
	
}
