/**
 * Net-Createurs simple library
		(string).trim			makes a L/R trim on the string xxxxxx
		
		browserInfo				Browser detection, returns Array[which->browser, majVersion->:main version, touchPad-> nothing, 'iPhone' or 'iPad' }
		NC.tools. :
		getStyle(obj)			Returns the computed styles of the object
		getCSSpointer(cssname)	Returns a pointer to a classname, in order to change its content dynamically
		center(obj)				Centers the object on the screen
		objSize(obj)			Returns the size W/H of the object, even if it is set to display:none
		returnXY(obj)			Returns absolute X and Y positions of obj in the window
		addLoadEvent(func)		Adds func to the onload document event list, preserving existing functions if any	

 */


/* Détection du navigateur utilisé...
On utilise la chaine retournée par navigator.userAgent :
IE5.5 (IE tester)	: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; InfoPath.2; .NET CLR 1.1.4322)  
IE6 (vrai)			: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; InfoPath.2; .NET CLR 1.1.4322)
IE7 (IE tester)		: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; InfoPath.2; .NET CLR 1.1.4322)  
IE8					: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; slcc1; .net clr 2.0.50727; etc.)
FF 3.x				: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Opera 10			: Opera/9.80 (Windows NT 5.1; U; fr) Presto/2.2.15 Version/10.10
Safari 3 PC			: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr-FR) AppleWebKit/525.28 (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1
Chrome				: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.38 Safari/532.0
iPhone				: mozilla/5.0 (iPhone; u; cpu iphone OS 3_1_2 like mac os x; fr-fr) applewebkit/528.18 (KHTML, like Gecko) version/4.0 mobile/7d11 safari/528.16
iPad (source Apple)	: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
Positionne :	nc_nav -> array which='', IE, FF, OP, SA, CH
				majVersion = 0 si nav inconnu, ou nr de version majeur (1, 2, 3 etc.) du navigateur
				iPhone = true/false
*/

var	nc_nav={ which:'', majVersion:0, touchPad:'' },
	x=navigator.userAgent.toLowerCase(),
	isIE=x.indexOf('msie '),
	isFF=x.indexOf('firefox/'),
	isOP=x.substring(0,6)=='opera/',
	isCH=x.indexOf('chrome/'),
	isSA=(isCH==-1? x.indexOf('safari/') : -1);	// Chrome signe Chrome ET Safari
if(x.indexOf('iphone;')!=-1) nc_nav['touchPad']='iPhone';
else if(x.indexOf('ipad;')!=-1) nc_nav['touchPad']='iPad';
if(isSA!=-1 || isOP) voffset=x.indexOf('version/');
if(isIE!=-1) { nc_nav['which']='IE'; nc_nav['majVersion']=parseInt(x.substring(isIE+5)) }
else if(isFF!=-1) { nc_nav['which']='FF'; nc_nav['majVersion']=parseInt(x.substring(isFF+8)) }
else if(isOP) { nc_nav['which']='OP'; nc_nav['majVersion']=parseInt(x.substring(voffset+8)) }
else if(isSA!=-1) { nc_nav['which']='SA'; nc_nav['majVersion']=parseInt(x.substring(voffset+8)) }
else if(isCH!=-1) { nc_nav['which']='CH'; nc_nav['majVersion']=parseInt(x.substring(isCH+7)) };

// L/R trim. Usage = xxxxx.trim()
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,"") };


if(typeof(NC)=='undefined') NC={};
NC.tools= {

	// =================================================
	// Lecture dynamique des style CSS réels (calculés) d'un objet
	// obj = ID de l'objet, ou objet lui même
	getStyle:function(obj) {
		if(typeof(obj)=='string') obj=document.getElementById(obj);
		return typeof obj.currentStyle=='undefined' ?
	  	  document.defaultView.getComputedStyle(obj, null)		// tous les navigateurs sauf IE
	  	: obj.currentStyle;										// IE
	},


	// =================================================
	// Retourne un pointeur vers la classe cssname si elle existe, ou null sinon
	// Permet de modifier une classe CSS globale document, par exemple : sfa_css.style['leftPadding']='8px'
	getCSSpointer:function(cssname) {
		var i=0,CSStab=new Array(),sfa_css=null,css='';
		if(document.styleSheets) do {
			css=typeof(document.styleSheets[i]);
			if(css!='undefined' && css!='unknown') {
				css=document.styleSheets[i];
				if (css.rules) CSStab=document.styleSheets[i].rules;				// IE
				else if(css.cssRules) CSStab=document.styleSheets[i].cssRules;	// Le rese
				else CSStab=new Array();
				for(var j in CSStab) if(CSStab[j].selectorText=='.'+cssname) sfa_css=CSStab[j]['style']; //;for(var k in sfa_css) alert(k +' -> '+sfa_css[k]) }
				// cssText = chaine de texte du style (PADDING-RIGHT : 8px) ou paddingRight=8px
				i++;
			}
			else css='';
		} while(css && sfa_css==null);
		return sfa_css;
	},


	// =================================================
	// Centrage de l'objet "obj" à l'écran, obj peut être l'ID de l'objet à centrer, ou l'objet lui même
	// Rattache automatiquement l'objet à la racine du document, afin d'obtenir un centrage correct
	// Utilise objSize
	center:function(obj) {
		if(typeof(obj)=='string') obj=document.getElementById(obj);
		if(obj) {
			if(obj.parentNode.tagName!='BODY') document.body.appendChild(obj);	// rattache l'objet à la racine du body
			if(obj.style.position!='absolute') obj.style.position='absolute';
			var size=this.objSize(obj),										// Dimensions de l'objet à centrer
				win=this.winInfo();											// et celles de l'écran
			obj.style.left=win.left+Math.round((win.width-size[0])/2)+'px';		// Centrage X
			obj.style.top=win.top+Math.round((win.height-size[1])/2)+'px';		// Centrage Y
		}
		return false;
	},


	/* =================================================
	 * Retourne un tableau avec des infos sur la fenêtre : [width] / [height] = largeur / hauteur visible du document, [left) / [top] = offset left et top,
	 * [totalWidth] / [totalHeight] = largeur / hauteur totale du document
	 */
	winInfo:function() {
		var	n=navigator.userAgent.toLowerCase(),
			doc=document.compatMode && document.compatMode=="BackCompat" ? document.body : document.documentElement,
			doc1=n.indexOf("safari")>-1 ? document.body : doc;			// Forcage obligatoire avec Safari PC jusque version 4 incluse
		return { width:doc.clientWidth, height:doc.clientHeight, left:doc1.scrollLeft, top:doc1.scrollTop,
			totalHeight:Math.max(doc1.scrollHeight,doc.clientHeight), totalWidth:Math.max(doc1.scrollWidth,doc.clientWidth) }
	},

	// =================================================
	// Retourne la taille X / Y de l'objet obj, qui peut être l'ID d'un objet ou l'objet lui même
	objSize:function(obj) {
		if(typeof(obj)=='string') obj=document.getElementById(obj);
		if(obj) { this.makeReal(obj,1); ret=Array(obj.offsetWidth,obj.offsetHeight); this.makeReal(obj,0) }
		else ret=Array(0,0);
		return ret
	},

	// =================================================
	// Retourne la position X/Y absolue de l'objet X dans la fenêtre
	// Remonte l'arborescence des nodes container pour ajouter les offset des <div> quand c'est nécessaire
	// X peut être l'id de l'objet, ou l'objet lui même (détection auto)
	// Problèmes :
	//	-	Bug IE6/7 avec les <div> relative. S'ils sont dans un <td> centré verticalement, IE6/7 ajoute 2 fois la marge du haut dûe au centrage
	//		la variable last_h permet de corriger cette erreur. A voir selon les montages...
	//	-	Bug tous : avec les <div> static. Leur position X/Y ne doit pas être prise en compte s'ils sont suivis d'un div autre que absolu
	returnXY:function(x) {
		var	nav=navigator.userAgent.toLowerCase(),isIE=nav.indexOf('msie '),ielower=isIE!=-1 && parseInt(nav.substring(isIE+5),10)<8,
			x0=0, y0=0;
		if(typeof(x)=='string') x=document.getElementById(x);
		if(x) {
			var x0=x.offsetLeft, y0=x.offsetTop, x1, y1,prevpos, thispos='',last_h=0;
			while(x.parentNode && x.parentNode.tagName!='BODY') {
				x=x.parentNode;
				if(x.tagName=='DIV') {
					x1=typeof(x.offsetLeft)=='number' ? x.offsetLeft : 0;
					y1=typeof(x.offsetTop)=='number' ? x.offsetTop : 0;
					prevpos=thispos;
					thispos=typeof(document.defaultView)=='undefined' ?		// IE 6/7. Ne pas tester sur currentStyle car Opera10 le reconnait
						x.currentStyle.position : document.defaultView.getComputedStyle(x, null).position;

					switch(thispos) {
					case 'static':
						if(prevpos && prevpos!='absolute') { x1=0; y1=0 }
						else if(!prevpos && !ielower)  { x1=0; y1=0 };		// Chablais, sauf le ielower (IE 6/7) -> pages modèle marchands PP
						break;
					case 'relative':
						if(ielower) {
							h=this.getStyle(x)['height'];
							if(h!='auto') last_h=parseInt(h,10);
						}
						break;
					}
					x0+=x1; y0+=y1;
				}
				else if(x.tagName=='TD') {
					if(ielower && last_h) {y0-=x.offsetHeight-last_h; last_h=0 };		// bug IE6/IE7
				}
			}
		}
		return(new Array(x0,y0));
	},


	// =================================================
	// Multiple onload function created by: Simon Willison
	// http://simonwillison.net/2004/May/26/addLoadEvent/
	// usage : NC.tools.addLoadEvent( function() {fct_name()} )
	addLoadEvent:function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') { window.onload = func; }
		else {
			window.onload = function() {
				if (oldonload) { oldonload(); }
				func();
			}
		}
	},

	// Multiple onresize function
	// if onscroll=true -> funciton will be lauched at each body scroll, too (used for rough on screen auto-center)
	addResizeEvent:function(func,onscroll) {
		var oldonresize = window.onresize;
		if (typeof window.onresize != 'function') { window.onresize = func; }
		else {
			window.onresize = function() {
				if (oldonresize) { oldonresize(); }
				func();
			}
		}
		if(typeof(onscroll)!='undefined' && onscroll) this.addScrollEvent(func);
	},

	// Multiple onscroll function
	addScrollEvent:function(func) {
		var oldonscroll = window.onscroll;
		if (typeof window.onscroll != 'function') { window.onscroll = func; }
		else {
			window.onscroll = function() {
				if (oldonscroll) { oldonscroll(); }
				func();
			}
		}
	},


	// =================================================
	// Parse an object SRC
	parseSRC:function(obj) {
		var ret={ path:'', fullname:'', name:'', extension:'' };
		if(typeof(obj)=='string') obj=document.getElementById(obj);
		if(obj.src) {
			var x=obj.src.split('/');
			ret.fullname=x.pop();
			ret.path=x.join('/') + (x.length ? '/' : '');
			x=ret.fullname.split('.');
			ret.extension=x.length ? x.pop() : '';
			ret.name=x.join('.');
		}
		return ret;
	},

	// =================================================
	// verify an email address, return true if address OK, or false otherwise
	emailCheck:function(email) {
		var email_addressRegxp = /^[!#-?A-~]+@([-_a-z0-9]{2,}\.)?[-_a-z0-9]{2,}(\.[a-z0-9]{2,4}){1,2}$/i;
		return email_addressRegxp.test(email);
	},

	// =================================================
	// verify a URL, return true if address OK, or false otherwise
	urlCheck:function(url) {
		var url_addressRegxp = /^(http(s)?:\/\/)?([-_a-z0-9]{2,}\.)?[-_a-z0-9]{2,}(\.[a-z0-9]{2,4}){1,2}$|(\.[a-z0-9]{2,4}){1,2}(\/[\x20-\x7E]+)*$/i;
		return url_addressRegxp.test(url) && !(/"/.test(url));
	},

	// =================================================
	// alpha fade in /out init
	// params : id, [from], to, step, [display (block (default) or inline)], [delay]
	fademem: new Array(),

	fade:function(params) {
		var object=params['id'],alpha;
		if(typeof(object)=='string') object=document.getElementById(object);
		if(object) {

			// clear any previous fadein/out on this object
			if(this.fademem[params.id]) clearTimeout(this.fademem[params.id]);

			// calculate object start opacity
			var style=this.getStyle(object),
				display=style.display;
			if(params.from || params.from=='0') alpha=params.from;
			else {
				if(display=='none') alpha=0;
				else {
					if(style.opacity!='' && style.opacity!='undefined') var alpha=style.opacity*100;
					else if(style.filter && style.filter!='undefined') {
						var i=style.filter.indexOf('opacity=');
						if(i>-1) alpha=parseInt(style.filter.substring(i+8),10);
					}
					else alpha=100;
				}
			}

			// calculate object end opacity
			if(params.to<0) params.to=0; else if(params.to>100) params.to=100;

			// calculate actual step
			params.step=Math.abs(params.step);
			if(params.to<alpha) params.step=-params.step;

			// launch fadein/out
			setTimeout(
				(display=='none' ?
					  "document.getElementById('"+params['id']+"').style.display='"
					+ (params['display'] ? params['display'] : 'inline')
					+ "';" : '')
				+ "NC.tools.alphaStep('" + params.id + "'," + alpha + "," + params.to + "," + params.step + ")"
				,params['delay'] ? params['delay'] : 1);
		}
	},

	alphaStep:function(id,alpha,target,step) {
		var object=document.getElementById(id);
		if(alpha<target && step<0) alpha0=target; else if(alpha>target && step>0) alpha0=target; else alpha0=alpha;
		object.style.opacity=alpha0/100;
		object.style.filter='alpha(opacity=' + alpha0 + ')';
		if(alpha==alpha0 && alpha!=target) this.fademem[id]=setTimeout("NC.tools.alphaStep('" + id + "'," + (alpha+step) + "," + target + "," + step + ")", 50);
		else this.fademem[id]=false;
	},

	// *************** BODY MASK MANAGEMENT ************************ //
	// Set the mask size and opacity. Call it after the document is ready (onload) and each time the window is resized
	// params['show'] : true -> forces mask display and resize. Nothing or false -> do nothing if the mask is not visible (call through an "onresize" event)
	setMask:function (params) {
		var mask=document.getElementById(params['id']),
			opacity=params['opacity'] ? params['opacity'] : 0.5;
		if(mask && (params['show'] || mask.style.display!='none')) {
			if(mask.style.position!='absolute') mask.style.position='absolute';
			if(mask.parentNode.tagName!='BODY') document.body.appendChild(mask);	// rattache le masque à la racine du body
			mask=mask.style;
			mask.width=0;
			mask.height=0;
			var screen=NC.tools.winInfo();
			mask.width=screen.totalWidth+'px';
			mask.height=screen.totalHeight+'px';
			mask.left='0px';
			mask.top='0px';
			mask.opacity=opacity;
			mask.filter='alpha(opacity=' + (opacity*100)+ ')';
			mask.display='block';
		}
	},


	// *************** COOKIE MANAGEMENT ************************ //
	
	// Read cookie "name"
	getCookie:function(name) {
		var cookie='', i=document.cookie.indexOf(name+"="), content='';
		if (i>-1) {
			var	j=document.cookie.indexOf(';',i+name.length+1);
			if(j==-1) j=document.cookie.length;
			content=document.cookie.substring(i+name.length+1,j);
		}
		return content;
	},


	/* Store/delete a cookie
	 * Params = {name,value,expires,path,domain,secure}
	 * path -> nothing (= '/') or '/xxxxx' = from where this cookie will be visible
	 * value = content of the cookie, unescaped, or nothing -> delete the cookie
	 * expires : nothing -> cookie is deleted when the browser is closed, or numeric value -> seconds before the cookie dies
	 * domain = nothing or '.mydomain.com' for subdomains (to verify)
	 * secure -> true -> the cookie can only be used from a SSL server (https://etc.)
	 * Only the name is required. If value='' -> delete the cookie 'name', if such
	 * MAX	- total cookies on a browser = 300
	 *		- total cookies from one server or domain = 20
	 * 		- cookie size = 4KB 
	 */
	storeCookie:function(params) {
		if(params['expires']) {
			var expire = new Date();
			expire.setTime(expire.getTime() + (params['expires']*1000));
			params['expires'] = expire.toGMTString();
		}
		document.cookie=
			  params['name'] + '='
			+ (params['value'] ?
				  escape(params['value']) + (params['expires'] ? '; expires=' + params['expires'] : '')
				: '; expires=Thu, 01-Jan-70 00:00:01 GMT')
			+ '; path=' + (params['path'] ? params['path'] : '/')
			+ (params['domain'] ? '; domain=' + params['domain'] : '')
			+ (params['secure'] ? '; secure' : '')
		;
	},


	// *************** PRIVATE FUNCTIONS, DO **NOT** CHANGE ************************ //

	// Rend l'objet obj réel s'il est display:none, afin de pouvoir lire certaines de ses propiétés comme sa taille, ou de modifier les objets qu'il contient (IE6/7)
	// Appeler une fois avec cde=1 -> rend l'objet réel, puis avec cde=0 -> le replace dans son état d'origine
	// Attention avec IE6/7, si on rend un conteneur réel pour modifier un objet dedans il faut attendre ~300 msec avant de restituer l'état du conteneur 
	makeReal:function(obj,cde) {
		if(typeof(obj)=='string') obj=document.getElementById(obj);
		if(obj) {
			if(cde) {
				var style=this.getStyle(obj);
				if(style.display=='none') {
					obj.sf_changed=true;
					obj.sf_opacity=typeof(style.opacity)=='undefined' ? 0 : style.opacity;
					obj.sf_filter=typeof(style.filter)=='undefined'? '' : style.filter;
					obj.style.opacity=0; obj.style.filter='alpha(opacity=100)'; obj.style.display='block';
				}
				else obj.sf_changed=false;
			}
			else {
				var changed=obj.sf_changed;
				if(typeof(changed)!='undefined' && changed) {
					obj.style.display='none';
					obj.style.opacity=obj.sf_opacity;
					obj.style.filter=obj.sf_filter;
					obj.sf_changed=false;
				}
			}
		}
	}

}

