/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|		
|		Copyright (c) 2009
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|		
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


function $id(sID) {
	return document.getElementById(sID);
};

/* - - - - - - - - - - - - - - - - - - - - - [ Ajout aux favoris ] */
function bookmarksite(){var a=document.title;var b=document.location.href;if(window.sidebar){window.sidebar.addPanel(a,b,"")}else if(window.opera&&window.print){var c=document.createElement('a');c.setAttribute('href',b);c.setAttribute('title',a);c.setAttribute('rel','sidebar');c.click()}else if(document.all){window.external.AddFavorite(b,a)}}



/* ______________________[ 01 | Interactivit� du menu principal (menu horizontal) ]________________________ */

/* A special thanks goes to Eric Shepherd for his ALA article about �Hybrid CSS Dropdowns�: http://www.alistapart.com/articles/hybrid/ 
and to Patrick Griffiths and Dan Webb for their htmldog.com article �Sons of Suckerfish�: http://www.htmldog.com/articles/suckerfish/ */

function SmartHover(who) {
	if (document.all&&document.getElementById&&document.getElementsByTagName&&$id(who)) {
		navRoot=$id(who);
		for (i=0;i<navRoot.childNodes.length;i++) {
			node=navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {this.className+=" over";}
				node.onmouseout=function() {this.className=this.className.replace(" over", "");}
			}
		}
	}
	
	if($id(who)) {
		SmartFocus(who);
	}
};

function SmartFocus(who) {
	var navLnk=document.getElementById(who).getElementsByTagName("A");
	var navItm=document.getElementById(who).getElementsByTagName("LI");
	for (var n=0;n<navItm.length;n++) {
		if (navItm[n].className!="on") navItm[n].className="y";
	}
	for (var i=0;i<navLnk.length;i++) {
		navLnk[i].onfocus=function() {
			if (this.parentNode.parentNode.id==who) {
				this.parentNode.className+=" over";
			} else {
				this.parentNode.parentNode.parentNode.className+=" over";
			}
		}
		navLnk[i].onblur=function() {
			if (this.parentNode.parentNode.id==who) {
				this.parentNode.className=this.parentNode.className.replace(" over", "");
			} else {
				this.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.className.replace(" over", "");
			}
		}
	}
}


/* ______________________[ 02 | Gestion de la taille du texte d�un article ]________________________ */

function SmartSize() {
	var args=SmartSize.arguments;
	if (document.getElementById&&document.getElementById("Tplus")&&document.getElementById("Tmoins")) {
		var cibleplus=document.getElementById("Tplus");
		var ciblemoins=document.getElementById("Tmoins");
		cibleplus.onclick=function() {
			for (n=0;n<args.length;n++) {
				if (document.getElementById(args[n])) {
					var cibletxt=document.getElementById(args[n]);
					var sizestr=cibletxt.style.fontSize.substring(0,cibletxt.style.fontSize.length-2);
					var sizeinter=cibletxt.style.lineHeight.substring(0,cibletxt.style.lineHeight.length-2);
					sizestr=sizestr!=""?parseInt(sizestr):12;
					cibletxt.style.fontSize=sizestr+1+"px";
				}
			}
		}
		ciblemoins.onclick=function() {
			for (n=0;n<args.length;n++) {
				if (document.getElementById(args[n])) {
					var cibletxt=document.getElementById(args[n]);
					var sizestr=cibletxt.style.fontSize.substring(0,cibletxt.style.fontSize.length-2);
					var sizeinter=cibletxt.style.lineHeight.substring(0,cibletxt.style.lineHeight.length-2);
					sizestr=sizestr!=""?parseInt(sizestr):12;
					cibletxt.style.fontSize=sizestr-1+"px";
				}
			}
		}
	}
}


/* ______________________[ 03 | Lancement d�une impression pour les navigateurs compatibles ]________________________ */

function DirectPrint() {
	if (window.print) self.print();
}


/* ______________________[ 04 | Miscellaneous ]________________________ */

function OpenPopup(url,nom,option) {
	window.open(url,nom,option);
}


/* Roll over */
function findimg() {
	var imgs,i;
	imgs=document.getElementsByTagName('img');
	for(i=0;i<imgs.length;i++) {
		if (/roll/.test(imgs[i].className)) {
			imgs[i].onmouseover=function(){roll(this);};
			imgs[i].onmouseout=function(){roll(this);};
		}
	}
};

function roll(o) {
	var src,ftype,newsrc;
	src=o.src;
	ftype=src.substring(src.lastIndexOf('.'), src.length);
	if(/_over/.test(src)) {
		newsrc=src.replace('_over','');
	} else {
		newsrc=src.replace(ftype, '_over'+ftype);
	}
	o.src=newsrc;
};


/* ______________________[ 06 | Ajout de la page courante aux favoris ]________________________ */

/* Mettre la page courante en favoris � MSIE and Firefox */
function AddBookmark(btn) {
	if (document.getElementById&&document.getElementById(btn)) {
		var el=document.getElementById(btn);
		var operaStr="� Ctrl + T � pour ajouter aux favoris.";
		if (document.all&&navigator.userAgent.indexOf("Opera")==-1) {
			el.onclick=function() {
				window.external.AddFavorite(document.location,document.title);
			}
		} else if (navigator.userAgent.indexOf("Firefox")!=-1) {
			el.onclick=function () {
				window.sidebar.addPanel(document.title,document.location,"");
			}
		} else if (navigator.userAgent.indexOf("Opera")!=-1) {
			el.title=operaStr;
			window.status=operaStr;
		}
	}
}

/* Get elements by class name(s) */
function getElementsByClassName(oElm, strTagName, oClassNames){		//src element, target element tag, class name(s)
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	} else {
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
};


function AddToolButton(sContainerID, oParams){
	var DOM = document.createElement && document.getElementById && document.getElementById(sContainerID);
	if(DOM){
		var cible=document.getElementById(sContainerID).getElementsByTagName('UL');
		
		// no container list? let's create it
		if(cible.length == 0) {
			var oUL = document.createElement('UL');
			document.getElementById(sContainerID).appendChild(oUL);
		}
		cible = document.getElementById(sContainerID).getElementsByTagName('UL');
		
		if(cible){
			var oLi = document.createElement("LI");
			var oLink = document.createElement("A");
			var oImg = document.createElement("IMG");
			
			with(oLink){
				id = oParams.linkId;
				title = oParams.imgAlt;
				href = oParams.linkHREF? oParams.linkHREF : "javascript:;";
				onclick = oParams.onclick? oParams.onclick : "";
			};
			with(oImg){
				src = oParams.imgSrc;
				alt = oParams.imgAlt;
				className = oParams.imgClass;
			};
			oLink.appendChild(oImg);
			oLi.appendChild(oLink);
			
			// insertion&hellip; avant un element existant
			if(oParams.insertBefore && document.getElementById(oParams.insertBefore)) {
				var oNext = document.getElementById(oParams.insertBefore).parentNode;
				cible[0].insertBefore(oLi,oNext);
			}
			else{
				// ou en fin de liste
				cible[0].appendChild(oLi);
			}
		}	// end if cible
	}	// end if DOM
};	// end AddToolButton()


// JavaScript Document

(function($jQ) {
    $jQ.fn.toggleFocus = function() {
        return this.each( function() {
            var input =  $jQ(this) ;
            var label = input.prevAll("label:first");
                label.css("display","none");
            var label_text = label.text() || "" ;
                insertMsg(label_text);
            /*  add value in input  */   
            function insertMsg( msg ) {
                input.val(msg);
            }   
           
            input.focus( function() {  
                var valeur = input.val() ;
                if ( label_text == valeur ) {
                    insertMsg("");
                }
                input.blur(function() {
                    if (input.val() != "" ){
                        valeur = input.val();
                    } else {
                        valeur = label_text ;
                    }
                   insertMsg(valeur);
                });
            });  
          
        });
    };              
})(jQuery);

jQuery(document).ready(
	function($jQ) {
		$jQ("#mot_cle").toggleFocus();
	}
);

(function($jQ){	
	$jQ.fn.tabulations = function(options){				
		return this.each(function() {
		  var current = options-1 | -1;
		  var actif = null;
		  var onglet =  $jQ(this).find('li');
      var ongleta = onglet.find('a');        
      function defile(){
        if(current<$jQ(onglet).length-1){
          current += 1;
        }
        else{
          current = 0;
        }   
        onglet.removeClass("selected");
        onglet.eq(current).addClass("selected");
        onglet.eq(current).parents(".Tabulations").find(".contenuTabs").hide();
        onglet.eq(current).parents(".Tabulations").find(".contenuTabs:eq("+current+")").fadeIn();
        return false;
      };      
      function Dstart (){
        actif = setInterval(function(){ defile();}, 4000);
      };      
      function Dstop (){
				clearInterval(actif);
      };      
      defile();
      Dstart();      
      $jQ(".Tabulations").mouseenter(function(){Dstop();});
      $jQ(".Tabulations").mouseleave(function(){Dstart();});      
      ongleta.click(function(){
        current = $jQ(this).parent().index()-1;
        defile();
        return false;
      });			
		});
	};	
})(jQuery);
         

/**
 *
 * Can show a tooltip over an element
 * Content of tooltip is the title attribute value of the element
 * Tested with Firefox, IE6, IE5.5, IE7, Konqueror
 *
 * To use it :
 * 1.include this script on your page
 * 2.insert this element somewhere in your page
 *       <div id="tooltip"></div>
 * 3. style it in your CSS stylesheet (set color, background etc..). You must set
 *     this two style too :
 *     div#tooltip { position:absolute; visibility:hidden; ... }
 * 4.the end. test it ! :-)
 *
 * @version 1.1
 * @copyright 2004-2007 Laurent Jouanneau. 
 * @link http://ljouanneau.com/soft/javascript
 * @licence release under LGPL Licence
 */


// the tooltip object
var tooltip = {
    // setup properties of tooltip object
    id:"tooltip",
    offsetx : 10,
    offsety : 10,
    _x : 0,
    _y : 0,
    _tooltipElement:null,
    _saveonmouseover:null
}

/**
* Open ToolTip. The title attribute of the htmlelement is the text of the tooltip
* Call this method on the mouseover event on your htmlelement
* ex :  <div id="myHtmlElement" onmouseover="tooltip.show(this)"...></div>
*/
tooltip.show = function (htmlelement, htmlsource) {
    // we save text of title attribute to avoid the showing of tooltip generated by browser
    var text=htmlelement.getAttribute(htmlsource);
    htmlelement.setAttribute(htmlsource,"");
	  //htmlelement.removeAttribute("alt");
	  //htmlelement.removeAttributeNode(htmlelement.attributes[1]);
    htmlelement.setAttribute(htmlsource+"_saved",text);

	if(document.getElementById){
        this._tooltipElement = document.getElementById(this.id);
	}else if ( document.all ) {
        this._tooltipElement = document.all[this.id].style;
	}

    this._saveonmouseover = document.onmousemove;
    document.onmousemove = this.mouseMove;

    this._tooltipElement.innerHTML = "<p>"+text+"</p>";

    this.moveTo(this._x + this.offsetx , this._y + this.offsety);

    if(this._tooltipElement.style){
        this._tooltipElement.style.visibility ="visible";
    }else{
        this._tooltipElement.visibility = "visible";
    }
   return false;
}

/**
* hide tooltip
* call this method on the mouseout event of the html element
* ex : <div id="myHtmlElement" ... onmouseout="tooltip.hide(this)"></div>
*/
tooltip.hide = function (htmlelement, htmlsource) {
    htmlelement.setAttribute(htmlsource,htmlelement.getAttribute(htmlsource+"_saved"));
    htmlelement.removeAttribute(htmlsource+"_saved");

    if(this._tooltipElement.style){
        this._tooltipElement.style.visibility ="hidden";
    }else{
        this._tooltipElement.visibility = "hidden";
    }
    document.onmousemove=this._saveonmouseover;
}



// Moves the tooltip element
tooltip.mouseMove = function (e) {
   // we don't use "this" because this method is assign to an event of document
   // and so is dereferenced
    if(e == undefined)
        e = event;

    if( e.pageX != undefined){ // gecko, konqueror,
        tooltip._x = e.pageX;
        tooltip._y = e.pageY;
    }else if(event != undefined && event.x != undefined && event.clientX == undefined){ // ie4 ?
        tooltip._x = event.x;
        tooltip._y = event.y;
    }else if(e.clientX != undefined ){ // IE6,  IE7, IE5.5
        if(document.documentElement){
            tooltip._x = e.clientX + ( document.documentElement.scrollLeft || document.body.scrollLeft);
            tooltip._y = e.clientY + ( document.documentElement.scrollTop || document.body.scrollTop);
        }else{
            tooltip._x = e.clientX + document.body.scrollLeft;
            tooltip._y = e.clientY + document.body.scrollTop;
        }
    /*}else if(event != undefined && event.x != undefined){ // IE6,  IE7, IE5.5
        tooltip.x = event.x + ( document.documentElement.scrollLeft || document.body.scrollLeft);
        tooltip.y = event.y + ( document.documentElement.scrollTop || document.body.scrollTop);
    */
    }else{
        tooltip._x = 0;
        tooltip._y = 0;
    }
    tooltip.moveTo( tooltip._x +tooltip.offsetx , tooltip._y + tooltip.offsety);

}

// Move the tooltip element
tooltip.moveTo = function (xL,yL) {
    if(this._tooltipElement.style){
        this._tooltipElement.style.left = xL +"px";
        this._tooltipElement.style.top = yL +"px";
    }else{
        this._tooltipElement.left = xL;
        this._tooltipElement.top = yL;
    }
}

/* ______________________[ 08 | Lancement des scripts ]________________________ */

window.onload=function() {
	SmartHover("NavigationPrincipale");
	
	AddToolButton("ToolBox",{linkId:"Tmoins",imgSrc:"img/tools/ico_moins.gif",imgClass:"roll",imgAlt:"text petit",insertBefore:"PrintThis"});
	AddToolButton("ToolBox",{linkId:"Tplus",imgSrc:"img/tools/ico_plus.gif",imgClass:"roll",imgAlt:"text grand",insertBefore:"Tmoins"});
	AddToolButton("ToolBox",{linkId:"Favori",imgSrc:"img/tools/ico_fav.gif",imgClass:"roll",imgAlt:"Ajouter favoris",insertAfter:"PrintThis"});
	AddBookmark("Favori");
	findimg();
	SmartSize("ColB","ColC","ColD","ColContenu","ColContextuelle");
	if($id("plansite")) {
		var SiteMap = new SiteMapManager("plansite").setToggleSwitch(true,"plansite").apply();		
	}	
	/* IE 5-6 background flicker fix */
	try {document.execCommand('BackgroundImageCache', false, true);}
	catch(e) { }
	
	(function($jQ) {
		if( $jQ(".financiere li.on a").length > 0) {  
			var idA = $jQ(".financiere li.on a").attr("id");
			if( idA.length > 0) {
				if( /link/.test(idA) ) {
					idA = idA.replace("link","");
				}
				ancienOnglet = idA;	
			} else { return; }
		}
	})(jQuery);
	 
	(function($jQ) {
  	if( $jQ(".ongletsTabs").length > 0 ){
  		$jQ('.Tabulations .ongletsTabs').tabulations();
  	}
	})(jQuery);
	
  (function($jQ) {
	  if( $jQ(".infobulle").length > 0) {
      $jQ("body").append("<div id='tooltip'></div>");  
    	$jQ(".infobulle").mouseover(function () {$jQ("#tooltip").addClass("bulleStyle"); tooltip.show(this, "title");});
    	$jQ(".infobulle").mouseout(function () {$jQ("#tooltip").removeClass("bulleStyle"); tooltip.hide(this, "title")});
    }
	 })(jQuery);
   
   (function($jQ) {
      function makeSublist(parent,child,isSubselectOptional,childVal)
      {
        $jQ("body").append("<select style='display:none' id='"+parent+child+"'></select>");
        $jQ('#'+parent+child).html($jQ("#"+child+" option"));
        var parentValue = $jQ('#'+parent).attr('value');
        $jQ('#'+child).html($jQ("#"+parent+child+" .sub_"+parentValue).clone());
        childVal = (typeof childVal == "undefined")? "" : childVal ;
        $jQ("#"+child+' option[@value="'+ childVal +'"]').attr('selected','selected');
        $jQ('#'+parent).change(function(){
            var parentValue = $jQ('#'+parent).attr('value');
            $jQ('#'+child).html($jQ("#"+parent+child+" .sub_"+parentValue).clone());
            if(isSubselectOptional) $jQ('#'+child).prepend("<option value='none'> -- Select -- </option>");
            $jQ('#'+child).trigger("change");
            $jQ('#'+child).focus();
        });
      }
      makeSublist('domaine','sous_domaine', true, '');	
      makeSublist('type','domaine', false, '1');
    })(jQuery); 
    
    (function($jQ) {
    	if( $jQ(".devispersonnalise select[id='type']").length > 0 ){
    	  $jQ("#valid-global, #valid-1, #valid-2").hide();
    	  $jQ(".devispersonnalise select[id='type']").change(function(){
          $jQ("#valid-global").slideDown('', function(){$jQ("#valid-1").slideDown();});
        });
        $jQ(".devispersonnalise #valid-1 select").change(function(){
          $jQ("#valid-2").slideDown();
        });
    		
    	}         
  	})(jQuery);
    
    (function($jQ) {
    	if( $jQ("#email_confirmation").length > 0) {
        $jQ("#email_confirmation").bind('paste', function(){ return false; });  
  			$jQ("#email").bind('copy', function(){ return false; });
  		}    
  	})(jQuery);
	
}



