﻿if(LBL === undefined) {
	var LBL = {};
}

(function($) {
    var W = this.window;
    $(document).ready(function () {
		if(LBL.IE6){
			LBL.fixPNG();
		}
		LBL.screenW = (window.innerWidth) ? window.innerWidth : (document.documentElement.clientWidth)?document.documentElement.clientWidth:document.body.clientWidth;
		LBL.screentH = (window.innerHeight) ? window.innerHeight : (document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight;
		LBL.initPoy(true);
		//chargement et traitement données clients XML
		var callback = function(msg, textStatus, XMLHttpRequest){
      		//var msg = (typeof selector == "string") ? msg : msg.toString();//on transforme le msg en string
			var ixemel = (typeof msg == "object") ? msg.childNodes[0] : XML.parse(msg).childNodes[0];
			var datas_arr = XML.get_childNodes(ixemel);
			var html_arr = new Array();
			for(i=0; i<datas_arr.length; i++){
				var clientNode = datas_arr[i];
				var image = clientNode.getElementsByTagName('img')[0].firstChild.nodeValue;
				var name = clientNode.getElementsByTagName('name')[0].firstChild.nodeValue;
				var desc = clientNode.getElementsByTagName('desc')[0].firstChild.nodeValue;
				var htmlString_temp = '<li>';
				if(desc){
					htmlString_temp += '<span class="infos"><span class="titre">'+name+'</span><span class="desc">'+desc+'</span></span>';
				}
				htmlString_temp += '<img src="../img/clients/'+image+'" alt="'+name+'" /></li>';
				html_arr.push(htmlString_temp);
			}
			var afficheClients = function(n){
				htmlString = html_arr[n];
				if(n == 0){
					$('#liste').html('');
					$('#liste').css('display', 'block');
				}
				$('#liste').html($('#liste').html() + htmlString);
				var niouli = $('#clients #liste li').eq(n);
				niouli.css('display', 'none');
				niouli.fadeIn(200, function(){
					if(n == html_arr.length - 1){
						$('#clients #liste li').mouseover(function(e){
							if($(this).find('.infos').length){
								$('#roll .middle').html($(this).find('.infos').html());
								var h = $('#roll').outerHeight();
								var pox = ($(this).offset().left - $('#clients').offset().left) + (($(this).outerWidth() - $('#roll').outerWidth())/2);
								var poy = $(this).offset().top - $('#clients').offset().top - h + 4;
								$('#roll').css('left', pox).css('top', poy);
								//$('#roll').fadeIn('fast');
								$('#roll').css('display', 'block');
								//$(this).css('cursor', 'pointer');
								$(this).addClass('rolled');
							}
						}).mouseout(function(e){
							if($(this).find('.infos').length){
								//$('#roll').stop();
								$('#roll').css('display', 'none');
								$(this).removeClass('rolled');
							}
						});
					}
					else{
						afficheClients(n+1);
					}
				});
			}
			afficheClients(0);
		}
		var xmlFile = "xml/clients.xml";
		$.ajax({
	        type: "GET",
	        url: xmlFile,
	        success: callback
	    });
		//formulaire
		$('#contact .form .submit').click(function(e){
			e.preventDefault();
			//verif saisies
			$('#contact .form .errorDetected').fadeOut();
			error = false;
			//name
			if(LBL.isEmpty($('#contact .form #name').val())){
				error = true;
				$('#contact .form #nameLine').addClass('error');
			}
			else{
				$('#contact .form #nameLine').removeClass('error');
			}
			//email
			if(LBL.isEmpty($('#contact .form #email').val())){
				error = true;
				$('#contact .form #emailLine').addClass('error');
			}
			else if(!LBL.isEmail($('#contact .form #email').val())){
				error = true;
				$('#contact .form #emailLine').addClass('error');
			}
			else{
				$('#contact .form #emailLine').removeClass('error');
			}
			//message
			if(LBL.isEmpty($('#contact .form #message').val())){
				error = true;
				$('#contact .form #messageLine').addClass('error');
			}
			else{
				$('#contact .form #messageLine').removeClass('error');
			}
			//envoi form au PHP
			if(!error){
				////document.getElementById('form').getelementsByTagName('form')[0].submit();
				var callback = function(msg, textStatus, XMLHttpRequest){
        			var msg = (typeof selector == "string") ? msg : msg.toString();//on transforme le msg en string 
					//et on l'écrit dans une popin
					$('#contact #serverConfirm .message').html(msg);	
					$('#contact #serverConfirm').fadeIn();	
					//bouton fermeture popin
					$('#contact #serverConfirm .close').click(function(e){
						$('#contact #serverConfirm').fadeOut();
						e.preventDefault();
					});
				}
				var hurle_arr = top.location.href.split("/");
				var country = hurle_arr[hurle_arr.length -2];
				var dataz = "name="+escape($('#contact .form #name').val())+"&email="+$('#contact .form #email').val()+"&message="+escape($('#contact .form #message').val())+"&country="+country;
				$.ajax({
			        type: "GET",
			        url: "../php/contact.php",
			        data: dataz,
			        success: callback
			    });
			}
			else{
				$('#contact .form .errorDetected').fadeIn();
			}
		});
    });
	$(window).resize(function(){
		if(!LBL.moving)LBL.initPoy(false);
	});
})(jQuery);

LBL.IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
// exactly Internet Explorer 7
LBL.IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
// at least Internet Explorer 7
LBL.gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;
// any Internet Explorer (thanks to Dean)
LBL.isMSIE = /*@cc_on!@*/false;
var strChUserAgent = navigator.userAgent; 
var intSplitStart = strChUserAgent.indexOf("(",0); 
var intSplitEnd = strChUserAgent.indexOf(")",0); 
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd); 
LBL.IE6 = (strChMid.indexOf("MSIE 6") != -1) ? true : false;

//centrage vertical
LBL.initPoy = function(anim){
	LBL.screenW = (window.innerWidth) ? window.innerWidth : (document.documentElement.clientWidth)?document.documentElement.clientWidth:document.body.clientWidth;
	LBL.screentH = (window.innerHeight) ? window.innerHeight : (document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight;
	if(LBL.screentH > 590){
		var poy = (LBL.screentH - 590) / 2;
	}
	else{
		var poy = (LBL.isMSIE) ? -5 : 10;
	}
	if(anim){
		LBL.moving = true;
		$('#page').animate({marginTop:poy}, 1000, function(){LBL.moving = false;});
	}
	else{
		$('#page').css('marginTop', poy);
	}
}

// corrige les PNGs sur IE5.5 et IE6
LBL.fixPNG = function(){
	// test IE
	if(document.all != null && window.opera == null){
		// test IE 6
		if(window.XMLHttpRequest == null && document.implementation != null && document.compatMode != null){
			// recuperation des images
			var imgs = document.getElementsByTagName('img');
			for(var i = 0, img; img = imgs[i]; i++){
				if(/png$/i.test(img.src)){
					var src = img.src;
					img.src = '../img/dot.gif';
					img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"',sizingMethod='image');";
				}
			}
		}
	}
}
//verification saisie email
var regExpObj = {
	empty: /^\s*$/,
	email: /^\s*[\w-]+(\.[\w-]+)*@([\w-]+\.)+[A-Za-z]{2,7}\s*$/
}
LBL.isEmail = function(string){
	//returns true if string is email
	return regExpObj.email.test(string);
}
LBL.isEmpty = function(string){
	//returns true if string is empty
	return regExpObj.empty.test(string);
}

// biblio XML
var XML = {};
XML.newDocument = function(rootTagName, namespaceURL) {   
 if (!rootTagName) rootTagName = "";   
  if (!namespaceURL) namespaceURL = "";   
  if (document.implementation && document.implementation.createDocument) {   
    // This is the W3C standard way to do it   
    return document.implementation.createDocument(namespaceURL, rootTagName, null);   
  }   
  else { // This is the IE way to do it   
    // Create an empty document as an ActiveX object   
    // If there is no root element, this is all we have to do   
    var doc = new ActiveXObject("MSXML2.DOMDocument");   
    // If there is a root tag, initialize the document   
    if (rootTagName) {   
      // Look for a namespace prefix   
      var prefix = "";   
      var tagname = rootTagName;   
      var p = rootTagName.indexOf(':');   
      if (p != -1) {   
        prefix = rootTagName.substring(0, p);   
        tagname = rootTagName.substring(p+1);   
      }   
      // If we have a namespace, we must have a namespace prefix   
      // If we don't have a namespace, we discard any prefix   
      if (namespaceURL) {   
        if (!prefix) prefix = "a0"; // What Firefox uses   
      }   
      else prefix = "";   
      // Create the root element (with optional namespace) as a   
      // string of text   
      var text = "<" + (prefix?(prefix+":"):"") +  tagname +   
          (namespaceURL   
           ?(" xmlns:" + prefix + '="' + namespaceURL +'"')   
           :"") +   
          "/>";   
      // And parse that text into the empty document   
      doc.loadXML(text);   
    }   
    return doc;   
  }   
};  
 /**  
 * Synchronously load the XML document at the specified URL and  
 * return it as a Document object  
 */  
XML.load = function(url) {   
	//NE MARCHE PAS AVEC SAFARI PC CAR xmldoc.load EST UNDEFINED...
   // Create a new document with the previously defined function   
   var xmldoc = XML.newDocument();  
   xmldoc.async = false;  // We want to load synchronously   
   xmldoc.load(url);      // Load and parse   
    return xmldoc;         // Return the document   
};   
/**  
* Asynchronously load and parse an XML document from the specified URL.  
* When the document is ready, pass it to the specified callback function.  
* This function returns immediately with no return value.  
*/  
XML.loadAsync = function(url, callback) {   
    var xmldoc = XML.newDocument();   
    // If we created the XML document using createDocument, use   
    // onload to determine when it is loaded   
    if (document.implementation && document.implementation.createDocument) {   
        xmldoc.onload = function() { callback(xmldoc); };   
    }   
    // Otherwise, use onreadystatechange as with XMLHttpRequest   
    else {   
        xmldoc.onreadystatechange = function() {   
            if (xmldoc.readyState == 4) callback(xmldoc);   
        };   
    }   
    // Now go start the download and parsing   
    xmldoc.load(url);   
};  
/**  
* Parse the XML document contained in the string argument and return  
* a Document object that represents it.  
*/  
XML.parse = function(text) {   
    if (typeof DOMParser != "undefined") {   
        // Mozilla, Firefox, and related browsers   
        return (new DOMParser()).parseFromString(text, "application/xml");   
    }   
    else if (typeof ActiveXObject != "undefined") {   
        // Internet Explorer.   
        var doc = XML.newDocument();  // Create an empty document   
        doc.loadXML(text);            // Parse text into it   
        return doc;                   // Return it   
    }   
    else {   
        // As a last resort, try loading the document from a data: URL   
        // This is supposed to work in Safari. Thanks to Manos Batsis and   
        // his Sarissa library (sarissa.sourceforge.net) for this technique.   
        var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);   
        var request = new XMLHttpRequest();   
        request.open("GET", url, false);   
        request.send(null);   
        return request.responseXML;   
    }   
};
//petites fonctions pour palier pb de whitespaces FF
//à utiliser à la place des propriétés natives nextSibling, previousSibling ou firstChild 
XML.get_nextSibling = function(n){
	y = n.nextSibling;
	while (y.nodeType!=1){
		y = y.nextSibling;
	}
	return y;
}
XML.get_previousSibling = function(n){
	y = n.previousSibling;
	while (y.nodeType!=1){
		y = y.previousSibling;
	}
	return y;
}
XML.get_firstChild = function(n){
	y = n.firstChild;
	while (y.nodeType!=1){
		y = y.nextSibling;
	}
	return y;
}
XML.get_childNodes = function(n){
	var arr = new Array(), i = 0, l = n.childNodes.length;
	for ( i = 0; i < l; i++ ) {
		if ( n.childNodes[ i ].nodeType == 1 ) {
			arr.push( n.childNodes[ i ] );
		}
	}
	return arr;
}
var ie = /MSIE/.test(navigator.userAgent);
var moz = !ie && navigator.product == "Gecko";

