
	/*  LightAlert, version 1.1.4
	 *  (c) 2008 Jason, Liu Cha Shian (json.liu@gmail.com)
	 *  
	 *  LightAlert is freely distributable under the terms of an MIT-style license.
	 *  Please refrain from removing this header
	 *
	/*--------------------------------------------------------------------------*/
	var LightAlert = function (obj){
		this.fadetimerLA = null;
		this.browserLA = this.getBrowser();
		this.settingLA = this.setup(obj);
		this.body = parent.document.getElementsByTagName("body")[0];
		this.lightalert = this.createBox();
		this.overlayLA = {};
		this.init(obj);
		
	}
	
	LightAlert.prototype = {
		init: function(obj){
			THIS = this;
			this.overlayLA = this.displayOverlay(); //overlay
			this.displayMessage(obj); //box
			this.addEvent(window, "resize", function(){ THIS.getScroll() });
		},
	
		getBrowser: function(){
			b = navigator.userAgent.toLowerCase();
			if (b.indexOf('opera')!=-1) return 'opera';
			else if (b.indexOf('msie 7')!=-1) return 'ie7';
			else if (b.indexOf('msie')!=-1)  return 'ie6';
			else if (b.indexOf('safari')!=-1) return 'safari';
			else if (b.indexOf('gecko')!=-1)  return 'gecko';
		},
	
		setup: function(pars){
			var obj = {};
			obj.interval = 50;
			obj.show = pars.show || "0";
			obj.fade = pars.fade || "1"; 
			obj.fadein = 50; // 100 = n
			obj.fadetime = 500; // half seconds
			obj.fadestep = 25;
			obj.pause = 5000; //pause 5 seconds before fading out
			obj.overlayOpac = 50;
			obj.font =  pars.font || "bold 12px Arial";
			obj.position = this.browserLA == "ie6" ? "absolute" : "fixed";		
			obj.bgcolor = pars.bgcolor || "#FFF";
			obj.border = pars.border || "1px solid #003";
			obj.icon = pars.icon || "css/lightbox/loading.gif";
			obj.w = pars.w || "300";
			obj.padding = "15px 15px 15px 59px";
			obj.bwidth = this.getBrowserWidth();
			return obj;
		},
		
		createBox: function(){
			box = document.createElement("div");
			box.style.font = this.settingLA.font;	
			box.style.padding = this.settingLA.padding;
			box.style.width = this.settingLA.w+"px";
			box.style.left = (this.settingLA.bwidth - this.settingLA.w) /2+"px";
			box.style.top = 0;
			box.style.position = this.settingLA.position;
			box.style.background = this.settingLA.bgcolor+" url("+this.settingLA.icon+") no-repeat 1em";
			box.style.border = this.settingLA.border;
			box.onclick = function(){THIS .destroy();}
			if (this.settingLA.fadein < 100){
				if (this.isIE())
					box.style.filter = "alpha(opacity="+this.settingLA.fadein+")";
				else
					box.style.opacity= this.settingLA.fadein / 100;
			}
			return box;
		},
	
		displayOverlay: function(){
			THIS = this;
			overlayLA = document.createElement("div");
			overlayLA.style.position = this.settingLA.position;
			overlayLA.style.backgroundColor = "#003";
	
			if (this.isIE())
				overlayLA.style.filter = "alpha(opacity="+this.settingLA.overlayOpac+")";
			else
				overlayLA.style.opacity= this.settingLA.overlayOpac/100;
	
			overlayLA.style.width = this.settingLA.bwidth+"px";
			overlayLA.style.height = this.getBrowserHeight()+"px";
			if (this.browserLA == "ie6"){
				overlayLA.style.height = Math.max(document.documentElement.clientHeight,document.body.scrollHeight) + 30;
			}
			overlayLA.style.left = 0;
			overlayLA.style.top = 0;
			overlayLA.onclick = function(){
				THIS.destroy();
			}
			this.body.appendChild(overlayLA);
			return overlayLA;
		},
	
		displayMessage: function(obj){
			var msg;
			if (typeof(obj) == "string")
				msg = obj;
			if (typeof(obj) == "object")
				msg = obj.msg;
	
			msg = typeof(msg) == "undefined" ? "//Syntax Error: msg is not defined" : msg;
			
			this.lightalert.innerHTML = msg;
			this.body.appendChild(this.lightalert);
			//realign to middle
			box.style.top = (this.getBrowserHeight() - this.lightalert.clientHeight) / 2 +"px";
			this.fadeIn();
		},
		
		fadeIn: function(){
			THIS = this;
			if (this.settingLA.fadein < 100 && this.settingLA.fade=="1"){
				this.settingLA.fadein += this.settingLA.fadestep;
				if (this.isIE()) {
					this.lightalert.style.filter = "alpha(opacity="+this.settingLA.fadein+")";
				}else{
					this.lightalert.style.opacity=this.settingLA.fadein/100;
				}
				this.fadetimerLA = setTimeout(function(){THIS.fadeIn()},this.settingLA.interval);
			}else{
				if (this.settingLA.fade=="0"){
					if (this.isIE())
						this.lightalert.style.filter = 100;
					else
						this.lightalert.style.opacity=1;
				}
				clearTimeout(this.fadetimerLA);
				if (this.settingLA.show == "0")
					setTimeout(function(){THIS.fadeOut()},this.settingLA.pause);
			}	
		},
	
		fadeOut: function(){
			THIS = this;
			if (this.settingLA.fadein > 0 && this.settingLA.fade=="1"){
				this.settingLA.fadein -= this.settingLA.fadestep;
				if (this.isIE()) {
					this.lightalert.style.filter = "alpha(opacity="+this.settingLA.fadein+")";
				}else{
					this.lightalert.style.opacity=this.settingLA.fadein/100;
				}
				this.fadetimerLA = setTimeout(function(){THIS.fadeOut()},this.settingLA.interval);
			}else{
				clearTimeout(this.fadetimerLA);
				this.destroy();
			}
		},
	
		destroy: function(){
			try{
				THIS = this;
				clearTimeout(this.fadetimerLA);
				this.body.removeChild(this.lightalert);
				this.body.removeChild(this.overlayLA);
				this.removeEvent(window, "resize", function(){THIS.getScroll()});
			}catch(e){}
		},
		
		isIE: function(){
			if (this.browserLA == "ie6" || this.browserLA == "ie7")
				return true;
			return false;
		},
	
		getScroll: function(){
			if (document.documentElement && document.documentElement.scrollTop) {
				t = document.documentElement.scrollTop;
				l = document.documentElement.scrollLeft;
				w = document.documentElement.scrollWidth;
				h = document.documentElement.scrollHeight;
			} else if (document.body) {
				t = document.body.scrollTop;
				l = document.body.scrollLeft;
				w = document.body.scrollWidth;
				h = document.body.scrollHeight;
			}
			this.lightalert.style.left = (w - this.settingLA.w) /2+"px";
			this.lightalert.style.top = (this.getBrowserHeight() - this.settingLA.h) /2+"px";
	
			this.overlayLA.style.width = this.getBrowserWidth()+"px";
			this.overlayLA.style.height = this.getBrowserHeight()+"px";
			if (this.browserLA == "ie6"){
				this.overlayLA.style.height = Math.max(document.documentElement.clientHeight,document.body.scrollHeight) + 30;
			}
			return t;
		},
		
		getBrowserWidth: function(){
			if (window.innerWidth){
				return window.innerWidth;
			}else if (document.documentElement && document.documentElement.clientWidth != 0){
				return document.documentElement.clientWidth;}
			else if (document.body){
				return document.body.clientWidth;
			}
			return 0;
		},
	
		getBrowserHeight: function(){
			if (window.innerHeight){
				return window.innerHeight;
			}else if (document.documentElement && document.documentElement.clientHeight != 0){
				return document.documentElement.clientHeight;}
			else if (document.body){
				return document.body.clientHeight;
			}
			return 0;
		},
	
		addEvent: function( obj, type, fn ) { 
			if ( obj.attachEvent ) { 
				obj['e'+type+fn] = fn; 
				obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
				obj.attachEvent( 'on'+type, obj[type+fn] ); 
			} else 
				obj.addEventListener( type, fn, false ); 
		},
	
		removeEvent: function ( obj, type, fn, funcObj ) { 
			THIS = this;
			if ( obj.detachEvent ) { 
				obj.detachEvent( 'on'+type, obj[type+fn] ); 
				obj[type+fn] = null; 
			} else {
				obj.removeEventListener( type, fn, false ); 
				
			}
		}
	}
	
	function alert(obj){
		new LightAlert(obj);
	}
	

