

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				DOM UTILITY																   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function getFlashByName(movieName) {
	if( swfobject ) return swfobject.getObjectById(movieName);
	else {
		if (navigator.appName.indexOf("Microsoft") != -1) return window[movieName];
		else return document[movieName];
	}
}

function fixZIndex(elem){
	var beginZIndex = $(elem).parents().size() * 10;
	$(elem).css("zIndex",beginZIndex).parents().each(function(){
		beginZIndex = beginZIndex - 10;
		$(this).css("zIndex",beginZIndex);
	});
}

function getRealHeight(jQueryObj,hideWithHeight){
	if( hideWithHeight != true ) hideWithHeight = false;
	
	if( hideWithHeight ) {
		jQueryObj.css('height','auto');
		var realHeight = jQueryObj.height();
		jQueryObj.css('height','');
	} else {
		jQueryObj.show();
		var realHeight = jQueryObj.height();
		jQueryObj.hide();
	}
	return realHeight;
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				USEFULL																	   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function writeDebug (str,clear) {
	if( clear != true ) clear = false;
	if( ! $("ul#debug","body").size() )
		$("<ul />").attr("id","debug").css({
			"position": "absolute",
			"top": 0,
			"left": 0
		}).appendTo("body");
		
	if( $("ul#debug li","body").size() >= 30 && clear ) $("ul#debug","body").empty();
	
	$("ul#debug","body").append( $("<li />").html( str ) );
}

function cloneObject(srcInstance) {
	if(typeof(srcInstance) != 'object' || srcInstance == null)
		return srcInstance;
		
	var newInstance = srcInstance.constructor();
	
	for(var i in srcInstance) newInstance[i] = clone(srcInstance[i]);
	
	return newInstance;
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				COLDFUSION UTILS														   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function jsonCFQueryToArray( structCfQuery , emptyStringForNull , trimStringValues ) {
	structCfQuery = structKeyLCase(structCfQuery);
	
	if( emptyStringForNull != false ) emptyStringForNull = true;
	if( trimStringValues != false ) trimStringValues = true;
	
	var output = [];
	
	$.each(structCfQuery.data,function(index,item){
		var newLine = {};
		$.each(structCfQuery.columns,function(indexCol,itemCol){
			if( emptyStringForNull && item[indexCol] == null ) item[indexCol] = "";
			if( trimStringValues && IsString(item[indexCol]) ) item[indexCol] = $.trim(item[indexCol]);
			newLine[itemCol.toLowerCase()] = item[indexCol];
		});
		output.push(newLine);
	});
	return output;
}

function CFQuerySearch( cfQueryArray , column, value ){
	var searchReturn = {};
	$.each(cfQueryArray,function(index,item){
		if( column in item && item[column] == value ) {
			searchReturn = item;
		}
	});
	return searchReturn;
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				STRUCTURE MANIPULATION												   	   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function structKeyLCase(struct,recursive) {
	if( typeof struct == "undefined" ) return {};
	if( recursive != true ) recursive = false;
	var newStruct = {};
	$.each(struct,function(key,val){
		if( recursive && typeof val == "object" && val != null ) val = structKeyLCase( val , recursive );
		if( typeof key == "string" ) key = key.toLowerCase();
		newStruct[key] = val;
	});
	return newStruct;
}

function structToString(struct,separator) {
	if( typeof(separator) != "String" ) separator = "&";
	var newString = "";
	$.each(struct,function(key,val){
		if( newString.length ) newString += separator;
		newString += key + "=" + encodeURIComponent(val);
	});
	return newString;
}



/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				STRING/NUMBER MANIPULATION												   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
            
/* Format un nombre */
/* Ex: addSeparatorsNF( 10000, ".", ".", "." ) = 10.000 */
function addSeparatorsNF(nStr, sep, inD, outD)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function arroudiPalier ( nombre, palier ) {
	return palier * parseInt(nombre / palier);
}

function trim(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');	
}

function listFind(list,str,separator) {
	if( !separator ) separator = ",";
	var splittedList = list.split(separator);
	return splittedList.find(str,false);
}



/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				TYPES DETECTION															   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* True when 'o' is not a member of a function. */
function isAlien(o) {return isObject(o) && ! isFunction(o.constructor);}
/* True when 'o' is a native JavaScript array */
function isArray(o) {return isObject(o) && o.constructor == Array;} 
/* True when 'o' is the meta-type 'boolean' */
function isBoolean(o) {return 'boolean' === typeof o;} 
/* True when 'o' is an object having the 'getMonth' method */
function isDate(o) {return isObject(o) && o.getMonth;} 
/* True when 'o' is set and has children nodes or a node type */
function isDomElement(o) {return o && ("undefined" !== typeof o.childNodes || o.nodeType);} 
/* True when 'o' is set, the native JavaScript event is defined, and 'o' has an event phase */
function isEvent(o){return o && "undefined" != typeof Event && o.eventPhase;} 
/* True when 'o' is exactly equal to 'null' */
function isNull(o) {return null === o;} 
/* True when 'o' is the meta-type 'function' */
function isFunction(o) {return 'function' == typeof o;} 
/* True when 'o' is the meta-type 'number' and is finite */
function isNumber(o) {return "number" == typeof o && isFinite(o);} 
function IsNumeric(input) {return (input - 0) == input && input.length > 0;}
/* True when 'o' is the meta-type 'object' or 'function' */
function isObject(o) {return (o && "object" == typeof o) || isFunction(o);} 
/* True when 'o' has any value, including 0 and false, both of which are normally falsy */
function isSet(o) {return ((o || false === o || 0 === o) && ! isNull(o) && '' !== o && ! isUndefined(o));} 
/* True when 'o' is of the meta-type 'string' */
function isString(o) {return 'string' == typeof o;} 
//function IsString(obj) {return !!obj.charAt;}
/* True when 'o' is of the meta-type 'undefined' */
function isUndefined(o) {return 'undefined' == typeof o;} 
/* True when 'o' does not have any member values set that are not functions */
function isEmpty(o) {
	var i, v;
	if (isObject(o)) {
		for (i in o) {
			v = o[i];
			if (! isUndefined(v) && ! isFunction(v)) {
				return false;
			}
		}
	}
	return true;
}

/* Others Validations */
function IsEmail(str) {
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(str);	
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				NATIVE OBJECT EXTENDS													   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* ARRAY */
Array.prototype.max = function() {
	return this[ this.maxIndex() ];
}
Array.prototype.maxIndex = function() {
	var max = 0;
	for (var i = 0; i < this.length; i++) 
		if (this[i] > this[max]) 
			max = i;
	return max;
}
Array.prototype.min = function() {
	return this[ this.minIndex() ];
}
Array.prototype.minIndex = function() {
	var min = 0;
	for (var i = 0; i < this.length; i++) {
		if (this[i] < this[min]) 
			min = i;
	}
	return min;
}
Array.prototype.nearestIndex = function(value) {
	if( typeof value == "undefined" ) value = 0;
	
	var difference = [];
	for(var i = 0; i < this.length; i++) { 
		difference.push( value - this[i] );
	}
	
	return difference.abs().minIndex();
}
Array.prototype.abs = function() {
	for (var i = 0; i < this.length; i++) this[i] = Math.abs(this[i]);
	return this;
}
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}
Array.prototype.find = function(searchStr,strictComparaison) {
	if( strictComparaison != false ) strictComparaison = true
	var returnArray = -1;
	for (i=0; i<this.length; i++) {
		if (typeof(searchStr) == 'function') {
			if (searchStr.test(this[i])) {
				if (!returnArray) { returnArray = [] }
				returnArray.push(i);
			}
		} else {
			if ( ( strictComparaison && this[i]===searchStr ) || (!strictComparaison && this[i]==searchStr) ) {
				if (returnArray == -1) { returnArray = [] }
				returnArray.push(i);
			}
		}
	}
	return returnArray;
}

// Finds the elements of an array which satisfy a filter function. The original array is not affected.
Array.prototype.grep = function(callback, inv) {
	var ret = [];
	// Go through the array, only saving the items
	// that pass the validator function
	for ( var i = 0, length = this.length; i < length; i++ ) {
		if ( !inv !== !callback( this[ i ], i ) ) {
			ret.push( this[ i ] );
		}
	}
	return ret;
}


/* STRING */

/* Cross-Browser Split 1.0.1
(c) Steven Levithan <stevenlevithan.com>; MIT License
An ECMA-compliant, uniform cross-browser split method */

var cbSplit;

// avoid running twice, which would break `cbSplit._nativeSplit`'s reference to the native `split`
if (!cbSplit) {

cbSplit = function (str, separator, limit) {
    // if `separator` is not a regex, use the native `split`
    if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
        return cbSplit._nativeSplit.call(str, separator, limit);
    }

    var output = [],
        lastLastIndex = 0,
        flags = (separator.ignoreCase ? "i" : "") +
                (separator.multiline  ? "m" : "") +
                (separator.sticky     ? "y" : ""),
        separator = RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues by working with a copy
        separator2, match, lastIndex, lastLength;

    str = str + ""; // type conversion
    if (!cbSplit._compliantExecNpcg) {
        separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags); // doesn't need /g or /y, but they don't hurt
    }

    /* behavior for `limit`: if it's...
    - `undefined`: no limit.
    - `NaN` or zero: return an empty array.
    - a positive number: use `Math.floor(limit)`.
    - a negative number: no limit.
    - other: type-convert, then use the above rules. */
    if (limit === undefined || +limit < 0) {
        limit = Infinity;
    } else {
        limit = Math.floor(+limit);
        if (!limit) {
            return [];
        }
    }

    while (match = separator.exec(str)) {
        lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser

        if (lastIndex > lastLastIndex) {
            output.push(str.slice(lastLastIndex, match.index));

            // fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
            if (!cbSplit._compliantExecNpcg && match.length > 1) {
                match[0].replace(separator2, function () {
                    for (var i = 1; i < arguments.length - 2; i++) {
                        if (arguments[i] === undefined) {
                            match[i] = undefined;
                        }
                    }
                });
            }

            if (match.length > 1 && match.index < str.length) {
                Array.prototype.push.apply(output, match.slice(1));
            }

            lastLength = match[0].length;
            lastLastIndex = lastIndex;

            if (output.length >= limit) {
                break;
            }
        }

        if (separator.lastIndex === match.index) {
            separator.lastIndex++; // avoid an infinite loop
        }
    }

    if (lastLastIndex === str.length) {
        if (lastLength || !separator.test("")) {
            output.push("");
        }
    } else {
        output.push(str.slice(lastLastIndex));
    }

    return output.length > limit ? output.slice(0, limit) : output;
};

cbSplit._compliantExecNpcg = /()??/.exec("")[1] === undefined; // NPCG: nonparticipating capturing group
cbSplit._nativeSplit = String.prototype.split;

} // end `if (!cbSplit)`

// for convenience...
String.prototype.split = function (separator, limit) {
    return cbSplit(this, separator, limit);
};


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*				COOKIE MANAGING															   */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name,caseSensitive) {
	if( caseSensitive != false ) caseSensitive = true;
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.search( new RegExp(nameEQ, (caseSensitive ? "" : "i") ) ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
