var alertTimerId = 0;
var alertBox = {
	size : [330, 237],
	bodycolor : "fff",
	alert_head : "",
	link_point_down : "",
	link_point_up : "",
	point_color : "",
	font_color : "",
	charset : "utf-8",
	position : ["right", "bottom"],
	content_type : "useralert",
	topic_author : "",
	bg_overlay : "show",
	createAlert : function (opt) {
		var oldAlert = document.getElementById('mtalertwrapper');
		if(oldAlert != undefined) {
			this.closealert();
		}
		
		var headID = document.getElementsByTagName("head")[0];       
		var cssNode = document.createElement('link');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('href', 'http://www.mthai.com/m-point/assets/css/alert.css');
		cssNode.setAttribute('media', 'screen');
		cssNode.setAttribute('charset', this.charset);
		headID.appendChild(cssNode);
		
		var windimention = this.getWindowDimention();
		var box = this.getAlertPosition(windimention);
		var alertNode = document.createElement('div');
		alertNode.setAttribute('id', 'mtalertwrapper');
		alertNode.style.top = box.top+'px';
		alertNode.style.left = box.left+'px';
		
		var alertClose = document.createElement('div');
		alertClose.setAttribute('id', 'mtalertclose');
		alertClose.setAttribute('onClick', 'javascript:closealert()');
		alertClose.innerHTML = '<a id="closebox" href="#" onclick="javascript:closealert();return false;">x</a>';
		
		var param = "";
		if(typeof opt === 'object') {
			param = this.param(opt);
		}
		
		var alert_src = 'http://www.mthai.com/m-point/'+this.content_type+'.html?'+param+'&bodycolor='+this.bodycolor.replace("#", "");
		if(this.topic_author != "") {
			alert_src += '&topic_author=' + this.topic_author;
		}
		if(this.alert_head != "") {
			alert_src += '&alert_head=' + this.alert_head.replace("#", "");
		}
		if(this.font_color != "") {
			alert_src += '&font_color=' + this.font_color.replace("#", "");
		}
		if(this.point_color != "") {
			alert_src += '&point_color=' + this.point_color.replace("#", "");
		}
		if(this.link_point_up != "") {
			alert_src += '&link_point_up=' + this.link_point_up.replace("#", "");
		}
		if(this.link_point_down != "") {
			alert_src += '&link_point_down=' + this.link_point_down.replace("#", "");
		}
		
		var alertBody = document.createElement('iframe');
		alertBody.setAttribute('id', 'useralert');
		alertBody.setAttribute('src', alert_src);
		alertBody.setAttribute('allowTransparency', 'true');
		alertBody.setAttribute('marginwidth', '0');
		alertBody.setAttribute('marginheight', '0');
		alertBody.setAttribute('height', this.size[1]);
		alertBody.setAttribute('width', this.size[0]);
		alertBody.setAttribute('name', 'useralert');
		alertBody.setAttribute('scrolling', 'no');
		alertBody.setAttribute('border', '0');
		alertBody.setAttribute('frameborder', '0');
		
		alertNode.appendChild(alertClose);
		alertNode.appendChild(alertBody);
		
		if(this.bg_overlay != "none") {
			var box_overlay = document.createElement('div');
			
			box_overlay.setAttribute('id', 'box_overlay');
			if (parseInt(navigator.appVersion)>3) {
				if (navigator.appName=="Netscape") {
					box_overlay.style.width = (windimention.width)+'px';
					box_overlay.style.height = (windimention.height + 16)+'px';
				}
				if (navigator.appName.indexOf("Microsoft")!=-1) {
					box_overlay.style.width = (windimention.width + 20)+'px';
					box_overlay.style.height = (windimention.height + 20)+'px';
				}
			}
			box_overlay.style.top = '0px';
			box_overlay.style.left = '0px';
			box_overlay.setAttribute('onClick', 'javascript:closealert()');
			
			document.body.appendChild(box_overlay);
		}
		
		document.body.appendChild(alertNode);
		alertTimerId = setTimeout("closealert()", 5000);
	},
	getWindowDimention : function () {
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
				win_w = window.innerWidth-16;
				win_h = window.innerHeight-16;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				win_w = document.documentElement.clientWidth-20;
				win_h = document.documentElement.clientHeight-20;
			}
			
			return { width: win_w, height: win_h };
		}
		return null;
	},
	getAlertPosition : function (win) {
		if(win == undefined) {
			return null;
		}
		
		var alert_left = 0;
		var alert_top = 0;
		//if(this.is_numeric(this.position[0])) {
		if(typeof this.position[0] === 'number') {
			alert_left = this.position[0];
		}else {
			switch(this.position[0]) {
				case "left" :
					alert_left = 0;
				break;
				case "right" :
					alert_left = win.width - this.size[0];
				break;
				default :
					alert_left = (win.width / 2) - (this.size[0]/ 2 ) ;
				break;
			}
		}
		
		//if(this.is_numeric(this.position[1])) {
		if(typeof this.position[1] === 'number') {
			alert_top = this.position[1];
		}else {
			switch(this.position[1]) {
				case "top" :
					alert_top = 0;
				break;
				case "bottom" :
					alert_top = win.height - this.size[1];
				break;
				default :
					alert_top = (win.height / 2) - (this.size[1] / 2);
				break;
			}
		}
		
		return { left: alert_left, top: alert_top };
	},
	is_numeric : function (str) {
		var validChars = "0123456789"; 
		var isNumber = true;
		var char;
		
		for (i = 0; i < str.length && isNumber == true; i++) { 
			char = str.charAt(i); 
			if (validChars.indexOf(char) == -1) {
				isNumber = false;
			}
		}
		return isNumber;
	},
	closealert : function () {
		document.body.removeChild(document.getElementById('mtalertwrapper'));
	},
	param : function (opt) {
		var param = [];
		for (var k in opt) {
			try {
				v = opt[k];
			} catch (e) {
				v = "";
			}
			param.push(encodeURIComponent(k) + "=" + encodeURIComponent(v));
		}
		
		return param.join("&");
	}
}

function closealert() {
	if(document.getElementById('box_overlay') != null) {
		document.body.removeChild(document.getElementById('box_overlay'));
	}
	document.body.removeChild(document.getElementById('mtalertwrapper'));
	
	clearTimeout(alertTimerId);
}
