/**
 * Remove element in array
 *
 * @author		Michael Jolin
 * @since		2006,07,20
**/
Array.prototype.remove = function(from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
}; 

/**
 * Check if value is in array
 *
 * @author		Michael Jolin
 * @since		2006,07,20
**/
Array.prototype.inArray = function( value ) {
	for( var i=0; i<this.length; i++ ) {
		if( this[i] == value )
			return i;
	}
	return -1;
}

/**
 * Capitalize the string
 *
 * @author		Michael Jolin
 * @since		2009,04,08
**/
String.prototype.capitalize = function() {
	var tmpStr, tmpChar, preString, postString, strlen;

	tmpStr = this.toLowerCase();
	stringLen = tmpStr.length;
	if( stringLen > 0 ) {
		for( i=0; i<stringLen; i++ ) {
			if( i == 0 ) {
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,stringLen);
				tmpStr = tmpChar + postString;
			} else {
				tmpChar = tmpStr.substring(i,i+1);
				if( tmpChar == ' ' && i < ( stringLen - 1 ) ) {
					tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
					preString = tmpStr.substring(0,i+1);
					postString = tmpStr.substring(i+2,stringLen);
					tmpStr = preString + tmpChar + postString;
				}
			}
		}
	}
	return tmpStr;
}

String.prototype.repeat = function( nb ) {
	return new Array( nb + 1 ).join( this );
}

String.prototype.trim = function() {
	return this.replace( /^\s+|\s+$/g, '' );
}

String.prototype.fromWord = function() {
	var swapCodes = [146, 150]; // ascii codes from char at
	var swapStrings = ["'", "-"];
	var output = '';

	for( i=0; i<this.length; i++ ) {
		var pos = swapCodes.inArray( this.charCodeAt( i ) );
		output += ( pos > -1 ? swapStrings[pos] : this.charAt( i ) );
	}
	return output;
}

/**
 * Object constructor
 *
 * @author		Michael Jolin
 * @since		2009,03,20
**/
var ICZ = {
	language: 1,
	labels: {},
	html: {
		getValues: function() {
			var params = {};
			var str = location.href.substr( location.href.indexOf( '?' ) + 1, location.href.length );
			var attr = str.split( '&' );
			var info = [];
			var nb = attr.length;

			for( var i=0; i<nb; i++ ) {
				info = attr[i].split( '=' );
				params[info[0]] = info[1];
			}

			return params;
		},

		/**
		 * Open new browser window
		 *
		 * @author		Michael Jolin
		 * @since		2009,04,22
		**/
		newWindow: function( _url, _title, _params ) {
			var strParam = '';
			var width = screen.width / 1.5;
			var height = screen.height / 1.5;
			var left = ( screen.width - width ) / 2;
			var top = ( screen.height - height ) / 2;

			var params = {
					width: width,
					height: height,
					left: left,
					top: top,
					menubar: 0,
					toolbar: 0,
					status: 0,
					scrollbars: 1,
					resizable: 1,
					directories: 0,
					copyhistory: 0
				};

			if( !_params ) _params = {};

			for( param in params ) {
				strParam += ( strParam == '' ) ? param + '=' : ',' + param + '=';
				strParam += ( _params[param] && _params[param] != '' ) ? _params[param] : params[param];
			}

			try {
				return window.open( _url, _title, strParam );
			} catch( err ) {
				alert( ICZ.getLabel( 'canotOpenNewWindow' ) );
			}
		},

		/**
		 * Detect browser
		 *
		 * @author		Michael Jolin
		 * @since		2009,04,10
		**/
		checkIt: function( _brw, _ver ) {
			var detect = navigator.userAgent.toLowerCase();

			switch( _brw ) {
				case 'msie':
					_ver = detect.indexOf( _brw + ' ' + _ver + '.' ) + 1;
					break;
				case 'firefox':
					_ver = detect.indexOf( _brw + '/' + _ver + '.' ) + 1;
					break;
				default:
					_ver = detect.indexOf( _ver + '.' ) + 1;
					break;
			}
			_brw = detect.indexOf( _brw ) + 1;

			return ( _ver ) ? ( _brw && _ver ) : _brw;
		},

		/**
		 * Copy html element for insert somewhere else
		 *
		 * @author		Michael Jolin
		 * @since		2009,04,24
		**/
		copyElement: function( _elm ) {
			if( typeof( _elm ) == 'string' )
				_elm = ICZ.$( _elm );

			return _elm.cloneNode( true );
		},

		/**
		 * Replace a html element by a other
		 *
		 * @author		Michael Jolin
		 * @since		2009,04,10
		**/
		replaceElement: function( _old, _new ) {
			_old.parentNode.replaceChild( _new, _old );
		}
	},

	/**
	 * Add onload event
	 *
	 * @author		Guillaume Lacroix
	 * @since		2007,11,28
	**/
	addLoadEvent: function( _func ) {
		var oldonload = window.onload;
		if( typeof window.onload != 'function' )
			window.onload = _func;
		else
			window.onload = function() {
					oldonload();
					_func();
				}
	},

	/**
	 * Set the label from sql
	 *
	 * @author		Michael Jolin
	 * @since		2009,03,31
	**/
	setLabel: function( _labels ) {
		if( $ ) {
			$.ajax({
					async: false,
					url: '/INCLUDE/ajax.php',
					type: 'POST',
					data: {
							funct: 'labels',
							language: this.language,
							label: _labels.join( ',' )
						},
					dataType: 'json',
					success: function( data ) {
							for( name in data )
								ICZ.labels[name] = data[name];
						}
				});
		}
	},

	/**
	 * Get label
	 *
	 * @author		Michael Jolin
	 * @since		2009,03,31
	**/
	getLabel: function( _label ) {
		if( this.labels[_label] )
			return this.labels[_label];
		else
			return _label + '?';
	},

	/**
	 * Return the html object with id or tag name
	 *
	 * @author		Michael Jolin
	 * @since		2009,04,24
	**/
	$: function( _id ) {
		if( document.getElementById( _id ) )
			return document.getElementById( _id );
		else if( document.getElementsByTagName( _id ).length > 0 )
			return document.getElementsByTagName( _id );
		else
			return null;
	}
};


