/********************************************************************************/
/*                                                                              */
/*  Copyright (c) 2005 GENIESYS, Inc. All rights reserved                       */
/*                                                                              */
/*  Site    : http://www.geniesys.net                                           */
/*  Feedback: support@geniesys.net                                              */
/*                                                                              */
/*  File    : busy.js                                                           */
/*  Version : 1.0                                                               */
/*  Purpose : Enables hover and cursor effects on one or more tables.           */
/*  Requires: browser_version.js                                                */
/*                                                                              */
/*  Notes   : Distributed as part of software developed by GENIESYS Inc.        */
/*                                                                              */
/*  Maintenance History:                                                        */
/*  10/25/2006	A.D. Original Implementation                                    */
/*  11/03/2006  A.D. Added definition of "undefined"                            */
/********************************************************************************/

var busyDialogs = [];		// global collection of busy objects

if(navigator.appVersion.indexOf("MSIE 5.01")) {
	// "x = undefined" causes JS error in IE 5.01 . It doesn't know what undefined is. Isn't Microsoft a wonderful company?
	var undefined;		// Declare variable called "undefined", but don't assign any value.
}

function busy( type ) {	// definition of the cursor object
	var objID   = "busy" + busyDialogs.length;	  	// assign predefined name + numeric ID (index) to this object 

	switch( type ) {	// build one of the variants using standard DIV HTML element and output it to the browser
	case 1 :
		document.write('<div nowrap align=center class="busyDialog" name="' + objID + '" id="' + objID + '" ');
		document.write('style="position:absolute; border-width:none; border-style:outset; border-color: #ff0000; display:none; background-image:url(/global/img/4x4_st_50pc.png); padding:30px 30px 30px 30px; top:1px; left:1px; width:100pc; height:100pc; z-index:100" ');
		document.write('title="">');
		document.write('objID.innerHTML = < your HTML goes here >');
		document.write('</div>');
		break;
	case 2 :
		break;
	default:
		// Use common CSS class for all instances ---------v      . To use separate classes replace class="busyDialog" with class="' + objID + '" and define a number of classes (.busy0, .busy1. etc.) in you .css
		document.write('<div nowrap align=center class="busyDialog" name="' + objID + '" id="' + objID + '" ');
		document.write('style="position:absolute; border-width:thin; border-style:outset; display:none; background-image:url(/global/img/4x4_st.png); padding:50px 50px 50px 50px; top:1px; left:1px; width:250px; height:60px; z-index:100" ');
		document.write('title="Please wait while your request is being processed...">');
		document.write('<img src="/global/img/wait1.gif" border=0/>');
		// <b style="font-size:18pt;color:#333">Please wait...</b>
		document.write('</div>');
	}

	// obtained reference (poiner) to DIV object we just created above
	if( document.getElementById ) {
		var myObj = document.getElementById(objID);
	} else if( document.all ) {
		var myObj = document.all[objID];
	} else {
		var myObj = undefined;	// something is wrong
	}

	if( ! myObj ) return undefined;		// terminate if reference to that DIV object is not valid

	// === register in global collections ===
	busyDialogs[busyDialogs.length] = objID;

	// === Add additional methods and events ===
	myObj.show = _busy_method_show;
	myObj.hide = _busy_method_hide;

	// === Add additional properties ===

	// return reference to this DIV object
	return myObj;
}

function _busy_method_show() {
	var height=0, width=0, left=0, top=0; v_scroll_bar_size = 0; h_scroll_bar_size = 0;
	if(document.all){
		top   = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		left  = document.body.scrollLeft;
		height= document.body.clientHeight;
		width = document.body.clientWidth;
	} else {
/*		// all of these are undefined in IE
		alert("window.innerHeight = " + window.innerHeight	+
			  "\nwindow.ninnerWidth = " + window.innerWidth +
			  "\nwindow.pageXOffset = " + window.pageXOffset +
			  "\nwindow.pageYOffset = " + window.pageYOffset +
			  "\nwindow.screenX = " + window.screenX +
			  "\nwindow.screenY = " + window.screenY);
*/
		height= window.innerHeight;
		width = window.innerWidth;
		//v_scroll_bar_size = window.outerHeight - window.innerHeight;
		//h_scroll_bar_size = window.outerWidth  - window.innerWidth ;
	}

//	alert("document.all = " + document.all + "\nheight = " + height + "\nwidth = " + width + "\ntop = " + top + "\nleft = " + left);
//	alert("document.body = " + width + "x" + height + " (width x height)");

	// .offsetWidth and .offsetHeight properties are "0" until out DIV object is actualy made visible. Also, these properties are read-only.
	this.style.cursor  = "wait";
	this.style.display = "";

	// This will convert things like "thin" to "1px".
	this.style.paddingLeft       = parseStyleProperty( this.style.paddingLeft      );
	this.style.paddingRight      = parseStyleProperty( this.style.paddingRight     );
	this.style.paddingTop        = parseStyleProperty( this.style.paddingTop       );
	this.style.paddingBottom     = parseStyleProperty( this.style.paddingBottom    );
	this.style.borderWidth       = parseStyleProperty( this.style.borderWidth      );
	this.style.borderLeftWidth   = parseStyleProperty( this.style.borderLeftWidth  );
	this.style.borderRightWidth  = parseStyleProperty( this.style.borderRightWidth );
	this.style.borderTopWidth    = parseStyleProperty( this.style.borderTopWidth   );
	this.style.borderBottomWidth = parseStyleProperty( this.style.borderBottomWidth);
	this.style.marginLeft        = parseStyleProperty( this.style.marginLeft       );
	this.style.marginRight       = parseStyleProperty( this.style.marginRight      );
	this.style.marginTop         = parseStyleProperty( this.style.marginTop        );
	this.style.marginBottom      = parseStyleProperty( this.style.marginBottom     );
/*
	document.writeln("clientWidth | innerWidth : " + width + " , clientHeight | innerHeight : " + height + "<br>\n");
	document.writeln("1. b0 : " + this.style.top + " " + this.style.left + " " + this.style.width + " " + this.style.height + " ; .offsetWidth:" + this.offsetWidth + " this.offsetHeight:" + this.offsetHeight + "<br>\n");
	document.writeln("this.style.paddingLeft		= " + this.style.paddingLeft		+ " int(" + parseInt(this.style.paddingLeft     )	+ ")<br>\n");
	document.writeln("this.style.paddingRight		= " + this.style.paddingRight		+ " int(" + parseInt(this.style.paddingRight    )	+ ")<br>\n");
	document.writeln("this.style.paddingTop	        = " + this.style.paddingTop 		+ " int(" + parseInt(this.style.paddingTop      )	+ ")<br>\n");
	document.writeln("this.style.paddingBottom		= " + this.style.paddingBottom		+ " int(" + parseInt(this.style.paddingBottom   )	+ ")<br>\n");
	document.writeln("this.style.marginLeft	    	= " + this.style.marginLeft  		+ " int(" + parseInt(this.style.marginLeft      )	+ ")<br>\n");
	document.writeln("this.style.marginRight		= " + this.style.marginRight		+ " int(" + parseInt(this.style.marginRight     )	+ ")<br>\n");
	document.writeln("this.style.marginTop	    	= " + this.style.marginTop  		+ " int(" + parseInt(this.style.marginTop       )	+ ")<br>\n");
	document.writeln("this.style.marginBottom		= " + this.style.marginBottom		+ " int(" + parseInt(this.style.marginBottom    )	+ ")<br>\n");
	document.writeln("this.style.borderLeftWidth	= " + this.style.borderLeftWidth	+ " int(" + parseInt(this.style.borderLeftWidth )	+ ")<br>\n");
	document.writeln("this.style.borderRightWidth	= " + this.style.borderRightWidth	+ " int(" + parseInt(this.style.borderRightWidth)	+ ")<br>\n");
	document.writeln("this.style.borderTopWidth	    = " + this.style.borderTopWidth    	+ " int(" + parseInt(this.style.borderTopWidth  )	+ ")<br>\n");
	document.writeln("this.style.borderBottomWidth	= " + this.style.borderBottomWidth	+ " int(" + parseInt(this.style.borderBottomWidth)	+ ")<br>\n");
*/
	if( this.offsetWidth > width ) {
		var x = parseInt(width) - parseInt(this.style.paddingLeft) - parseInt(this.style.paddingRight) - parseInt(this.style.borderLeftWidth) - parseInt(this.style.borderRightWidth);
		x -= (document.all) ? 1 : 18;		// ??? for some reason the <div> is 18px in FF/Crome and 1px in IE wider then it should be even though this.offsetWidth is now equal to "width"
		this.style.width = x + "px";
		this.style.left  = "1px";
		//document.writeln("2. b0 : " + this.style.top + " " + this.style.left + " " + this.style.width + " " + this.style.height + " .offsetWidth : " + this.offsetWidth + "<br>\n");
	} else {
		this.style.left  = left + parseInt( (width - parseInt(this.offsetWidth )) / 2 ) + "px";
	}

//		document.writeln("4. b0 : " + this.style.top + " " + this.style.left + " " + this.style.width + " " + this.style.height + " .offsetWidth : " + this.offsetWidth + "<br>\n");

	//alert("this.offsetHeight = " + this.offsetHeight);
	//this.style.top     = top  + parseInt( (height - parseInt(this.offsetHeight) )/2) + "px";
	//this.style.top     = top  + parseInt(this.offsetHeight * 1.5) + "px";
	if( this.offsetHeight > height ) {
		x = parseInt(height) - parseInt(this.style.paddingTop) - parseInt(this.style.paddingBottom) - parseInt(this.style.borderTopWidth) - parseInt(this.style.borderBottomWidth);
		this.style.top   = "1px";
		this.style.height = x + "px";
		//document.writeln("3. b0 : " + this.style.top + " " + this.style.left + " " + this.style.width + " " + this.style.height + " .offsetHeight : " + this.offsetHeight + "<br>\n");
	} else {
		this.style.top   = top  + parseInt( height / 2 - this.offsetHeight / 2 ) + "px";
	}
//	alert("this.style.top = " + top + " + (" + height + " / 2 - " + this.offsetHeight + " / 2) px => " + this.style.top);

//	alert("Window's height = " + height + "\nthis.offsetHeight = " + this.offsetHeight );
//	if(height < this.offsetHeight) {
//		this.style.top     =  parseInt(this.offsetHeight * 1.5) + "px";
//	} else {
//	}
//	alert("");
}

function _busy_method_hide() {
	this.style.cursor  = "default";
	this.style.display = "none";
}

function parseStyleProperty( p ) {
	switch( p.toLowerCase() ) {
	case ""     : return "0px";	// Note: FF and Crome do not update property if "px" is not added to the number.
	case "thin" : return "1px";
	default     : return p;
	}
}
