/*		Web 2.0 stuff		*/

(function($) {
	$.hasCSS = function() {
		$('body').append(
                $(document.createElement('div')).attr('id','css_test')
                .css({ width:'1px', height:'1px', display:'none' })
        );
        var _v = ($('#css_test').width() != 1) ? false : true;
        $('#css_test').remove();
        return _v;
	};
})(jQuery);

var $jQ = jQuery.noConflict();

$jQ(document).ready(function() {
  
/* NAVIGATION PRINCIPALE */ 
	(function($jQ) {
    $jQ.fn.SmartJqHover = function() {//alert('oui');
        return this.each( function() {
           // alert('oui');
            var divs = $jQ(this);
            divs.bind("mouseover mouseout",
              function() {
                  $jQ(this).toggleClass("over");  
              }
            );
        });
    };              
})(jQuery)

	$jQ("#NavigationPrincipale div, #Comprendre li, #Etapes li").SmartJqHover();

/* TOOLTIP */	
	
  $jQ(".infobulle").mouseover(function () {$jQ("#tooltip").addClass("bulleStyle"); tooltip.show(this, "title");});
	$jQ(".infobulle").mouseout(function () {$jQ("#tooltip").removeClass("bulleStyle"); tooltip.hide(this, "title")});
	
	$jQ("#CarteFrance area").mouseover(function () {tooltip.show(this, "alt")});
	$jQ("#CarteFrance area").mouseout(function () {tooltip.hide(this, "alt")});
	
	$jQ("#CarteFrance area").each(function(){		
		$jQ(this).click(function(){
			var alt = $jQ(this).attr("alt");
			$jQ("select#orgaRegion option").each(
                                function(){
                                        if ($jQ(this).attr("value")===alt){
                                                 $jQ(this).attr("selected", "selected");
                                        }
                                        $jQ("div.sbl span:first").text(alt);
                                        $jQ("div.jNiceSelectWrapper:first li").each(function(){
                                        if ($jQ(this).text()===alt){
                                                $jQ(this).parent().find('a.selected').removeClass('selected');
                                                $jQ(this).find('a').addClass('selected');	
                                        }
                                });
			});
			$jQ("#SearchOrganisme").submit();
		});
	});
       

        
        /* Schéma */
        $jQ("#schema ul").mouseover(function () {
                $jQ("#schema img").attr("src", "img/schema/schema_"+$jQ(this).parent().attr("id")+".gif");
                $jQ($jQ(this).parent()).addClass("on");
        });
	$jQ("#schema img").mouseover(function () {
                        $jQ(this).attr("src", "img/schema/schema_schfor.gif");
                        $jQ("#schema div").removeClass("on");
        });
        
        /* BLOC OUVRANT FERMANT (historique et autres) */
        
        $jQ("#Historique dt:gt(0), .openCloseForm h2, .openClose h2, .openClose .eqH2").addClass("off");
        $jQ("#Historique dt:gt(0), .openCloseForm h2, .openClose h2, .openClose .eqH2").parent().find("dd, .inner2, .toclose").hide("fast");
      	$jQ("#Historique dt, .openCloseForm h2, .openClose h2, .openClose .eqH2").click(
      		function() {
      		  var Closer = $jQ(this);
            Closer.parent().find("dd, .inner2, .toclose").slideToggle("fast",function(){
              Closer.toggleClass("off");
            });
      		}
	      );
        
        /* Toggle */
        $jQ(".Toggle a").click(
		function() {
                        $jQ(this).toggleClass("close");
                        if ($jQ(this).hasClass("close")){
                                $jQ(this).html("<span>- Tout fermer</span>");
                                $jQ("#Historique dt, .openClose h2, .openClose .eqH2").removeClass("off");
                                $jQ("#Historique dt,.openClose h2, .openClose .eqH2").parent().find("dd, .inner2").show("fast");
                        }
                        else {
                                $jQ(this).html("<span>+ Tout ouvrir</span>");
                                $jQ("#Historique dt, .openClose h2, .openClose .eqH2").addClass("off");
                                $jQ("#Historique dt,.openClose h2, .openClose .eqH2").parent().find("dd, .inner2").hide("fast");
                        }
			
		}
	);
        
        if($id("plansite")) {
		var SiteMap = new SiteMapManager("plansite").setToggleSwitch(true,"plansite").apply();
	}
        
        /* lien de type bouton arrondi */
        $jQ("div.button a, .Toggle a, .Toggle a, h4.Nresult").each(
                function() {
                        $jQ(this).parent().removeClass('button').addClass('niceBtn');
                        $jQ(this).wrap('<div class="atl"><div class="atr"><div class="abl"><div class="abr"></div></div></div></div>');
                        var $wrapper = $jQ(this).parents('div.atl');
                        $wrapper.css("width", $jQ(this).width()+15);
        });
        
        /* Cocher / Décocher */
        $jQ("p.ToogleForm a").click(
                function(){
                        if($jQ(this).hasClass("coche")){
                                $jQ(this).removeClass("coche").parents('form').find("input:checkbox").removeAttr("checked");  
                        }
                        else {
                                $jQ(this).addClass("coche").parents('form').find("input:checkbox").attr("checked", "checked");  
                        }
                }
        );
  
  
/* ____________ JBLUR ______________*/  
  
// Fonction pour les input avec label intégrés
	(function($jQ) {
    $jQ.fn.toggleFocus = function() {
      return this.each( function() {
        var input =  $jQ(this) ;
        var label = input.parents("form").find("label");
        label.each(
          function (){
             if ($jQ(this).attr("for")==input.attr("id")){
              if ($jQ(this).find(".jblurval").text()!=""){
                label_text = $jQ(this).find(".jblurval").text();
                $jQ(this).find(".jblurval").css("display","none");
              }
              else {
                $jQ(this).css("display","none");
                var label_text = $jQ(this).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);
                  });
              });
              insertMsg(label_text);
            }
        });
      });
    };              
  })(jQuery)

	$jQ('.jblur').toggleFocus();
  
        
/* ____________ EGALISE LES HAUTEURS DE COLONNES ______________*/
       /**
       *
       * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
       * Licensed under the MIT License:
       * http://www.opensource.org/licenses/mit-license.php
       * 
       */
       
      (function($jQ) {
      	/**
      	 * equalizes the heights of all elements in a jQuery collection
      	 * thanks to John Resig for optimizing this!
      	 * usage: $("#col1, #col2, #col3").equalizeCols();
      	 */
      	 
      	$jQ.fn.equalizeCols = function(){
      		var height = 0,
      			reset = $jQ.browser.msie ? "1%" : "auto";
      		return this
      			.css({"height" : reset})
      			.each(function() {
      				height = Math.max(height, this.offsetHeight);
      				ht = "auto";
      				minH = Math.max(height, this.offsetHeight);
      				if (jQuery.browser.msie &&parseInt(jQuery.browser.version)<=6) {
      				 ht = Math.max(height, this.offsetHeight);
      				}
      			})
      			.css({"height" : ht, "minHeight" : minH})
      			.each(function() {
      				var h = this.offsetHeight;
      				if (h > height) {
      					$jQ(this).css({"height" : height - (h - height)});
      				};
      			});
      	};
      	
      })(jQuery);
      
      if ($jQ('.infosInline').length>0){
        $jQ('.infosInline').each(
          function(){
            $jQ(this).find(".infosA .inner2").equalizeCols();
          }
        );
      }
      
/* _____________________*/
      
});
