
// -------------------------- // -------------------------- //
/*
 * something function in ajax
 * created 2009/05/01
 * 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

/**
 * 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: {},
		/**
		 * Tab selector utils
		 */ 
		tabChooser: {}
	}
}();

/**
 * General Lusi Utils
 */ 
LULO.util = function () {
	return {
		evalScript:function(text) {
			objRegex = /<\s*script[^>]*>((.|\s|\v|\0)*?)<\s*\/script\s*>/igm;
			result = objRegex.exec(text)
				
			while(result) {
				try{
					eval(result[1]);
				} 
				catch(e) {}
				result = objRegex.exec(text);
			}
		},

		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;
		},

		getViewportHeight: function() {
			 var height = -1;
			 var mode = document.compatMode;

			 if (mode || isIE) { // (IE, Gecko, Opera)
					switch (mode) {
						case 'CSS1Compat': // Standards mode
							 height = document.documentElement.clientHeight;
							 break;
	
						default: // Quirks
							 height = document.body.clientHeight;
							 break;
					}
			 }
			 else { // Safari
					height = self.innerHeight;
			 }
			 return height;
		},
		
		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
LULO.ajaxStatusClass.prototype.statusText = 'Loading...';

// 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 = this.statusText;
	}
	
	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() {
	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();
// ----------------- // ----------------- //

// --- begin ajax login form class
LULO.ajaxLoginClass = function() {};
LULO.ajaxLoginClass.prototype.loginDiv = null;
LULO.ajaxLoginClass.prototype.oldOnScroll = null;
LULO.ajaxLoginClass.prototype.shown = false; // state of the status window

// private func, create the login div
LULO.ajaxLoginClass.prototype.createLogin = function( width, height ) {
	var loginDiv = document.createElement('div');
	
	loginDiv.id = 'ajaxLoginDiv';
	loginDiv.className = 'login_form';
	loginDiv.style.position = 'absolute';	
//	loginDiv.style.opacity = .8;
//	loginDiv.style.filter = 'alpha(opacity=80)';
	loginDiv.style.width = width +'px';
	loginDiv.style.height = height +'px';
	
	document.body.appendChild(loginDiv);
	this.loginDiv = getObjectById('ajaxLoginDiv');
}

// public - show the login div with text
LULO.ajaxLoginClass.prototype.showLogin = function( sLink, width, height ) {
	if( typeof(sLink) == 'undefined' ) {
		sLink = 'index2.php?option=com_login';
	/*	alert( 'Invalid request Url' );
		return;*/
	}
	if( typeof(width) == 'undefined' ) {
		width = 350;
	}
	if( typeof(height) == 'undefined' ) {
		height = 250;
	}
	
	if( !this.loginDiv ) {
		this.createLogin( width, height );	
	}
	else {
		this.loginDiv.style.display = '';
	}
	
	this.loginDiv.innerHTML = '<table width="100%" height="100%"><tr><td style="text-align:center;"><img border="0" src="images/loading_128x128.gif"></td></tr></table>';
	
	if( !this.shown ) {
		disableBody( true );
		
		this.shown = true;
		this.loginDiv.style.display = 'block';
		
		AjaxRequest.get(
			{
				'url': sLink,
				'onSuccess': function(req) {
					if( req.responseText ) {
						ajaxLogin.loginDiv.innerHTML = req.responseText;
					}
				},
				'onError': cancelLoginRequest
			}
		);
		
		if( window.onscroll )
			this.oldOnScroll = window.onscroll; // save onScroll
		window.onscroll = this.positionLogin;
	}
	this.positionLogin();
	
	return false;
}

// public - hide it
LULO.ajaxLoginClass.prototype.hideLogin = function() {
	if( !this.shown )
		return;
		
	this.shown = false;
	
	if( this.oldOnScroll )
		window.onscroll = this.oldOnScroll;
	else
		window.onscroll = '';
	
	this.loginDiv.style.display = 'none';
}

// reposition the login div, top and centered
LULO.ajaxLoginClass.prototype.positionLogin = function() {
	if( typeof(this.loginDiv) == 'undefined' ) {
		this.loginDiv = getObjectById('ajaxLoginDiv');
	}
	
	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;
	
	var loginDivRegion = LULO.util.getRegion(this.loginDiv);
	
	this.loginDiv.style.top = ((LULO.util.getViewportHeight() / 2 - loginDivRegion.height / 2) + scrollTop) + 'px';	
	this.loginDiv.style.left = (LULO.util.getViewportWidth() / 2 - loginDivRegion.width / 2) + 'px';
}

function cancelLoginRequest() {
	disableBody( false );
	ajaxLogin.hideLogin();
}

// --- end ajax login form class
var ajaxLogin = new LULO.ajaxLoginClass();
// ----------------- // ----------------- //

// --- begin ajax display form class
LULO.ajaxDisplayClass = function() {};
LULO.ajaxDisplayClass.prototype.displayDiv = null;
LULO.ajaxDisplayClass.prototype.oldOnScroll = null;
LULO.ajaxDisplayClass.prototype.shown = false; // state of the status window

// private func, create the display div
LULO.ajaxDisplayClass.prototype.createDisplay = function( width, height ) {
	var displayDiv = document.createElement('div');
	
	displayDiv.id 						= 'ajaxDisplayDiv';
	displayDiv.className 			= 'display_form';
	displayDiv.style.position = 'absolute';
	displayDiv.style.width 		= width +'px';
	displayDiv.style.height 	= height +'px';
	
	document.body.appendChild(displayDiv);
	this.displayDiv = getObjectById('ajaxDisplayDiv');
}

// public - show the display div with text
LULO.ajaxDisplayClass.prototype.showDisplay = function( sLink, width, height ) {
	if( typeof(sLink) == 'undefined' ) {
		alert( 'Invalid request Url' );
		return;
	}
	if( typeof(width) == 'undefined' )
		width = 450;
	if( typeof(height) == 'undefined' )
		height = 350;
	
	if( !this.displayDiv )
		this.createDisplay( width, height );	
	else {
		this.displayDiv.style.display 	= 'none';
		this.displayDiv.style.width 		= width +'px';
		this.displayDiv.style.height 		= height +'px';
	}
	
	var btnClose = '<a style="position:absolute; top:0px; left:'+ (width-18) +'px; z-index:1000; background-color:#FFFFFF; padding:3px; filter:alpha(opacity=80); -moz-opacity:0.8; opacity: 0.8;"'
	+ ' href="javascript:cancelDisplayRequest()" title="close">'
	+ '<img border="0" src="images/icon_close.gif" />'
	+ '</a>'
	;
	this.displayDiv.innerHTML = btnClose
	+ '<table width="100%" height="100%">'
	+ '<tr><td style="text-align:center;">'
	+ '<img border="0" src="images/loading_128x128.gif">'
	+ '</td></tr>'
	+ '</table>'
	;
	
	if( !this.shown ) {
		disableBody( true );
		
		this.shown = true;
		this.displayDiv.style.display = 'block';
		
		AjaxRequest.get(
			{
				'url': correctUrlAjax( sLink ),
				'onSuccess': function(req) {
					if( req.responseText ) {
						ajaxDisplay.displayDiv.innerHTML = btnClose + req.responseText;
					}
				},
				'onError': cancelDisplayRequest
			}
		);
		
		if( window.onscroll )
			this.oldOnScroll = window.onscroll; // save onScroll
		window.onscroll = this.positionDisplay;
	}
	this.positionDisplay();
	
	return false;
}

// public - hide it
LULO.ajaxDisplayClass.prototype.hideDisplay = function() {
	if( !this.shown )
		return;
	
	this.shown = false;
	
	if( this.oldOnScroll )
		window.onscroll = this.oldOnScroll;
	else
		window.onscroll = '';
	
	this.displayDiv.style.display 	= 'none';
	this.displayDiv.style.width 		= '1px';
	this.displayDiv.style.height 		= '1px';
	this.displayDiv.style.top 			= '-1000px';
	this.displayDiv.style.left 			= '-1000px';
}

// reposition the display div, top and centered
LULO.ajaxDisplayClass.prototype.positionDisplay = function() {
	if( typeof(this.displayDiv) == 'undefined' ) {
		this.displayDiv = getObjectById('ajaxDisplayDiv');
	}
	
	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;
	
	var displayDivRegion = LULO.util.getRegion(this.displayDiv);
	
	this.displayDiv.style.top = ((LULO.util.getViewportHeight() / 2 - displayDivRegion.height / 2) + scrollTop) + 'px';	
	this.displayDiv.style.left = (LULO.util.getViewportWidth() / 2 - displayDivRegion.width / 2) + 'px';
}

function cancelDisplayRequest() {
	disableBody( false );
	ajaxDisplay.hideDisplay();
}

// --- end ajax display form class
var ajaxDisplay = new LULO.ajaxDisplayClass();
// ----------------- // ----------------- //
