/*
 * something function in ajax
 * created 2008/12/05
 * created by thuyqt
 */

var isIE = isSupportedIE();
var OLns4 = (navigator.appName=='Netscape' && parseInt(navigator.appVersion)==4);
var ns = (navigator.appName.indexOf("Netscape") != -1);

function isSupportedIE() {
	var userAgent = navigator.userAgent.toLowerCase() ;

	// IE Check supports ActiveX controls
	if (userAgent.indexOf("msie") != -1 && userAgent.indexOf("mac") == -1 && userAgent.indexOf("opera") == -1) {
		var version = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
		if(version >= 5.5 ) {
			return true;
		}
		else {
			return false;
		}
	}
}

// find obj's position
function findElementPos(obj) {
	var x = 0;
	var y = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}//if offsetParent exists
	else if (obj.x && obj.y) {
		y += obj.y;
		x += obj.x;
	}
	return new coordinate(x, y);
}//findElementPos
		
function findElementPosX(el) {
	curleft = 0;
	if (el.offsetParent) {
		while (el.offsetParent) {
			curleft += el.offsetLeft;
			el = el.offsetParent;
		}
	}//if offsetParent exists
	else if (el.x)
		curleft += el.x;
		
	return curleft;
}

function findElementPosY(el) {
	curtop = 0;
	if (el.offsetParent) {
		while (el.offsetParent) {
			curtop += el.offsetTop;
			el = el.offsetParent;
		}
	}//if offsetParent exists
	else if (el.y)
		curtop += el.y;
		
	return curtop;
}

/**
 * coordinate class
 **/
function coordinate(_x, _y) {
  var x = _x;
  var y = _y;
  this.add = add;
  this.sub = sub;
  this.x = x;
  this.y = y;

  function add(rh) {
    return new position(this.x + rh.x, this.y + rh.y);
  }

  function sub(rh) {
    return new position(this.x + rh.x, this.y + rh.y);
  }
}
// define Lusi class
var LULO = function() {
	return {
		/**
		 * AJAX status class 
		 */ 
		ajaxStatusClass: {},
		/**
		 * General namespace for Lusi utils
		 */
		util: {}
	}
}();

/**
 * General Lusi Utils
 */ 
LULO.util = function () {
	return {
		getViewportWidth: function() {
			 var width = -1;
			 var mode = document.compatMode;

			 if (mode || isIE) { // (IE, Gecko, Opera)
					switch (mode) {
						case 'CSS1Compat': // Standards mode
							 width = document.documentElement.clientWidth;
							 break;
	
						default: // Quirks
							 width = document.body.clientWidth;
							 break;
					}
			 }
			 else { // Safari
					width = self.innerWidth;
			 }
			 return width;
		},
		
		getRegion:function(el) {
			var height 	= parseInt(OLns4 ? el.clip.height : el.offsetHeight);
			var width 	= parseInt(OLns4 ? el.clip.width : el.offsetWidth);
			
			var Region = {width:width, height:height};	
			return Region;
		},
		
		getXY:function(el) {
			var left 	= findElementPosX(el);
			var top 	= findElementPosY(el);
			
			var XY = {left:left, top:top};	
			return XY;
		}
	};
}();

// --- begin ajax status class
LULO.ajaxStatusClass = function() {};
LULO.ajaxStatusClass.prototype.statusDiv = null;
LULO.ajaxStatusClass.prototype.oldOnScroll = null;
LULO.ajaxStatusClass.prototype.shown = false; // state of the status window

// reposition the status div, top and centered
LULO.ajaxStatusClass.prototype.positionStatus = function() {
	var scrollTop = 0;
	if (document.documentElement && document.documentElement.scrollTop)
		scrollTop = ns ? pageYOffset : document.documentElement.scrollTop;
	else if (document.body)
		scrollTop = ns ? pageYOffset : document.body.scrollTop;
		
	this.statusDiv.style.top = (scrollTop + 50) + 'px';
	statusDivRegion = LULO.util.getRegion(this.statusDiv);
	statusDivWidth = statusDivRegion.width;
	
	this.statusDiv.style.left = LULO.util.getViewportWidth() / 2 - statusDivRegion.width / 2 + 'px';
}

// private func, create the status div
LULO.ajaxStatusClass.prototype.createStatus = function(text) {
	statusDiv = document.createElement('div');
	statusDiv.className = 'loading';
	statusDiv.style.position = 'absolute';
	
	statusDiv.style.opacity = .8;
	statusDiv.style.filter = 'alpha(opacity=80)';
	statusDiv.id = 'ajaxStatusDiv';
	document.body.appendChild(statusDiv);
	this.statusDiv = getObjectById('ajaxStatusDiv');
}

// public - show the status div with text
LULO.ajaxStatusClass.prototype.showStatus = function(text) {
	if (typeof text == 'undefined') {
		if (typeof strLoading == 'string') {
			text = strLoading;
		}
		else {
			text = 'Loading...';
		}
	}
	
	if(!this.statusDiv) {
		this.createStatus(text);	
	}
	else {
		this.statusDiv.style.display = '';
	}
	this.statusDiv.innerHTML = '&nbsp;<b>' + text + '</b>&nbsp;';
	this.positionStatus();
	
	if(!this.shown) {
		this.shown = true;
		this.statusDiv.style.display = '';
		
		if(window.onscroll)
			this.oldOnScroll = window.onscroll; // save onScroll
		window.onscroll = this.positionStatus;		
	}
}

// public - hide it
LULO.ajaxStatusClass.prototype.hideStatus = function(text) {
	if(!this.shown)
		return;
		
	this.shown = false;
	
	if(this.oldOnScroll)
		window.onscroll = this.oldOnScroll;
	else
		window.onscroll = '';
	this.statusDiv.style.display = 'none';
}

LULO.ajaxStatusClass.prototype.flashStatus = function(text, time) {
	this.showStatus(text);
	
	window.setTimeout('ajaxStatus.hideStatus();', time);
}

var ajaxStatus = new LULO.ajaxStatusClass();
