/**                     Sirocco -- Administation Office
 *                             -- lib_base.js --
 *
 * Various functions for common usage
 * Require EnvInspector loaded and EventObject available.
 *
 * Javascript version : 1.5
 *
 * @category	Tool
 * @author	Luc Thibault <luc@suhali.net>
 * @copyright	2005 - Luc Thibault - SUHALI WEB DESIGN
 * @license	GNU General Public License
 * @version	2.1
 *
 * GNU General Public License 
 * 	This program is free software ; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation; either version 2 of the License,
 *	or (at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY ; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *	See the GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program ; if not, write to the
 *	Free Software Foundation, Inc.,
 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA ;
 *	or visit http://www.gnu.org/copy_nLeft/gpl.html
 */

 
/*********** EXTENSIONS **************/
/*
Object.extend(String.prototype, {
	stripTags: function () {
		return this.replace(/<\/?[^>]+>/gi, '');
	},
	stripScripts: function () {
		return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
	},
	extractScripts: function () {
		var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
		var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
		return (this.match(matchAll) || []).map(function(scriptTag) {
			return (scriptTag.match(matchOne) || ['', ''])[1];
		});
	},
	evalScripts: function () {
		return this.extractScripts().map(eval);
	},
	escapeHTML: function () {
		var div = document.createElement('div');
		var text = document.createTextNode(this);
		div.appendChild(text);
		return div.innerHTML;
	},
	unescapeHTML: function () {
		var div = document.createElement('div');
		div.innerHTML = this.stripTags();
		return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
	},
	toQueryParams: function () {
		var pairs = this.match(/^\??(.*)$/)[1].split('&');
		return pairs.inject({}, function(params, pairString) {
			var pair = pairString.split('=');
			params[pair[0]] = pair[1];
			return params;
		});
	},
	toArray: function () {
		return this.split('');
	},
	camelize: function () {
		var oStringList = this.split('-');
		if (oStringList.length == 1) return oStringList[0];
		var camelizedString = this.indexOf('-') == 0 ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
		for (var i = 1, len = oStringList.length; i < len; i++) {
			var s = oStringList[i];
			camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
		}
		return camelizedString;
	},
	inspect: function () {
		return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'";
	}
});
String.prototype.parseQuery = String.prototype.toQueryParams;
*/
/*********** PAGE FUNCTIONS **************/

function OffsetX(){
	if (EnvInspector.browser == 'ie')
		return document.documentElement.scrollLeft;
	else	return parseInt(window.pageXOffset);
}
function pageW(){
	if (EnvInspector.browser == 'ie')
		return document.documentElement.offsetWidth;
	else	return parseInt(window.innerWidth);
}
function getInnerWidth(){
	var w = 0;
	if (EnvInspector.browser == 'ie') {
		w = document.documentElement.clientWidth;
	} else { 
		w = window.innerWidth;
		if (document.height > window.innerHeight) w -= 16; // vert scrollbar
	}
	return w;
}

function OffsetY(){
	if (EnvInspector.browser == 'ie')
		return document.documentElement.scrollTop;
	else	return parseInt(window.pageYOffset);
}
function pageH(){
	if (EnvInspector.browser == 'ie')
		return document.documentElement.offsetHeight;
	else	return parseInt(window.innerHeight);
}
function getInnerHeight(){
	var h = 0;
	if (EnvInspector.browser == 'ie') {
		h = document.documentElement.clientHeight
	} else {
		h = window.innerHeight
		if (document.width > window.innerWidth) h -= 16	// horz scrollbar
	}
		return h;
}


/************ CREATE LAYERS *************/

function createDiv (name,x,y,w,h,visible,z,bg,content,special) {
	vis= visible? "visible" : "hidden"
	spe= special? special : '';
	tstW = (w!=null)?' width:'+w+'px;':(EnvInspector.browser == 'op')?'':' width:1px;';
	tstH = (h!=null)?' height:'+h+'px;':(EnvInspector.browser == 'op')?'':' height:1px;';
	bkgd = (EnvInspector.browser == 'op') ? 'background :'+bg+';' : (bg!=null) ? (bg.substring(0,1)!='#')? 'background-image: url('+bg+');' : 'background-color:'+bg+';' : '';
	chaine='<div id="'+name+'" style="position:absolute; top:'+y+'px; left:'+x+'px; '+tstW+tstH+' visibility:'+vis+'; z-index:'+z+'; '+bkgd+ '" '+special+'>\n'+content+'\n</div>\n';
	document.writeln(chaine);
}

/************* IMAGE REPLACE *************/

function imageReplace (id, url) {
	document.getElementById(id).src = url;
}

/*********** ELEMENT POSITION ************/

function elementPosition (elem) {
	positions = new Array()
	elem = document.getElementById(elem);
	if (elem){
		if (EnvInspector.DOM) {
			positions[0] = elem.offsetLeft;
			positions[1] = elem.offsetTop;
			while (elem.offsetParent != null && elem.offsetParent.tagName != 'BODY') {
				elem = elem.offsetParent;
				if (elem.tagName == 'BODY')
					break;
				positions[0] += elem.offsetLeft;
				positions[1] += elem.offsetTop;
			}
		} else {
			positions[0] = elem.x;
			positions[1] = elem.y;
		}
		return positions;
	} else	return null;
}

/************ PRELOAD IMAGES *************/

var loadImg = new Array()
var count = 0;
var valid = new Array()

function preloadImage (url) {
	i = loadImg.length;
	loadImg[i] = new Image;
	loadImg[i].src = url;
}

function testPreload () {
	for (i=0;i<loadImg.length;i++) {
		if (loadImg.length-count == 1) {
			init();
			break;
		} else {
			if ((loadImg[i].complete == true) && (valid[i] != true)) {
				valid[i] = true;
				count++;
			}
			if (i==loadImg.length-1)
				setTimeout("testPreload()", 100);
		}
	}
}

/************** OPEN WINDOW **************/

var variation
var zoom

variation = (EnvInspector.browser == 'ie') ? (EnvInspector.platform == 'mac') ? -25 : 0 : 5;

function openWindow (url,nom,dimx,dimy,type) {
	dimy = dimy - variation;
	featur0 = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+ dimx + ",height=" + dimy;
        featur1 = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width="+ dimx + ",height=" + dimy;
	props = eval('featur'+type);
	win = window.open(url,nom,props)
	win.focus();
	return win;
}

function populateField (table, index, field) {
	while (field.options.length > selectData[table][index].length)
		field.options[(field.options.length - 1)] = null;
	for (var i=0; selectData[table][index].length > i; i++)
		eval('field.options[i] = new Option("'+selectData[table][index][i][1]+'","'+selectData[table][index][i][0]+'")');
}


