function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;

runOnLoad(function(){
  $('.beautifuer').each(function(){
    var $link = $(this);
    var image = new Image();
    var img = $(this).attr('src');
    
    $(image).load(function(){
      $link.empty().append($(this));  
    }).attr(img);
    
    $this = $(this);
    $url_big = $(this).parent('a');
    width = $this.width();
    height = $this.height();
    width += 14;
    height += 1;
    var affiche_width = '';
    if($this.hasClass('beautifuer_index'))
      affiche_width = 'width="96"';
      
    $(this).parent().prepend('<div class="img" style="width: '+width+'px; height: '+height+'px;"><div class="fancy_no" style="width: '+width+'px">'
    + ' <div class="fancy_ne">'
    + '  <div class="fancy_n">'
    + '  </div>'
    + ' </div>'
    + '</div>'
    + '<div class="fancy_o" style="width: '+width+'px;">'
    + ' <div class="fancy_e" style="height: '+height+'px;">'
    + '<a href="'+$url_big.attr('href') +'" class="beautifuer_link">'
    + '<img src="'+$this.attr('src')+'" alt="'+$this.attr('alt')+'" style="_margin-bottom: -5px" '+affiche_width +' class="beautifuer"/>'
    + '</a>'
    + ' </div>'
    + '</div>'
    + '<div class="fancy_so" style="width: '+width+'px">'
    + ' <div class="fancy_se">'
    + '  <div class="fancy_s">'
    + '  </div>'
    + ' </div>'
    + '</div></div>');
    
    $(this).remove();
});
	 
   $.fn.fancyzoom.defaultsOptions.imgDir='img/';
    
    $('.beautifuer_link').fancyzoom(/*{showoverlay:true}*/);
  
  $('#sous_menu>li').find('ul').parent().find('a:first').click(function(){
      $('.sous_sous_menu').slideUp();
      $('.s_selected').removeClass('s_selected');
      $(this).parent().addClass('s_selected').find('.sous_sous_menu').slideDown();
      return false;
  });
  
//  $('.last_news').css({'margin-top': -($('.last_news').height()+12)+'px'});
  
  $('#sous_menu li.s_selected .sous_sous_menu').slideDown();



  $('.ms_menu').css('opacity', '0.8');
  var currentMenu = false;
  
  var config = {    
     sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)    
     interval: 50, // number = milliseconds for onMouseOver polling interval    
     over: openMenu, // function = onMouseOver callback (REQUIRED)    
     timeout: 200, // number = milliseconds delay before onMouseOut    
     out: hideMenu // function = onMouseOut callback (REQUIRED)    
};

$("#menu>li").hoverIntent( config )
  
  function openMenu()
  { 
    if($(this).hasClass('sep'))
      return;
      
      $this = $(this);
      
      
      $(this).children("ul").slideDown("fast").animate({'width': 150}, {queue: false});
      
      if($.browser.msie) { 
        var hauteur = $(this).width(); 
        $(this).children("ul").css({marginLeft:"-"+hauteur+"px"}); 
      }
      
      $(this).prev().children("ul").fadeOut("fast");
      $(this).siblings().children("ul").fadeOut("fast");
    
  }
  
  function hideMenu(){
    var width = $(this).width();
     $(this).find('.ms_menu').animate({'width': width}, {duration: 200}).slideUp('fast');
  }
  
  

	function fileBrowserCallBack(field_name, url, type, win) {
		var connector = "../../filemanager/browser.html?Connector=connectors/php/connector.php";
		var enableAutoTypeSelection = true;
		
		var cType;
		tinymcpuk_field = field_name;
		tinymcpuk = win;
		
		switch (type) {
			case "image":
				cType = "Image";
				break;
			case "flash":
				cType = "Flash";
				break;
			case "file":
				cType = "File";
				break;
		}
		
		if (enableAutoTypeSelection && cType) {
			connector += "&Type=" + cType;
		}
		
		window.open(connector, "tinymcpuk", "modal,width=600,height=400");
	}
     
});



function testContact()
{
  var error = '';
  var errorObligatoire = false;
  $('#error').html();
    var new_string = new String($('#mail').val());
    if (!new_string.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$')) {
                error += 'Invalid mail. <br />';
    }
    $('.obligatoire').each(function(){
      if($(this).val() == '')
        errorObligatoire = true;
    });
    
    if(errorObligatoire)
      error += 'Please complete all requested information.';
    
    $('#error').css('color', 'red').html(error); 
    if(error != '')
      return false;

    
}

function testConfirm()
{
  if($('#pwd').val() == '' ||  $('#pwd').val() != $('#confirm').val())
  { 
    $('#error').html('Please check your password and your confirmation password.');
    return false;
  }
  else return true;
} 

function testRegisterCA()
{
  var error = '';
  var errorObligatoire = false;
  $('#error').html();
    var new_string = new String($('#email').val());
    if (!new_string.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$')) {
                error += 'Invalid mail. <br />';
    }
    
    if($('#email').val() != $('#confirm').val())
      error += 'Your mail and your confirmation mail must be the same. <br />';
      
    if($('#pwd').val() != $('#confirmP').val())
      error += 'Your password and your confirmation must be the same.<br />';
      
    
    $('.obligatoire').each(function(){
      if($(this).val() == '')
        errorObligatoire = true;
    });
    
    if(errorObligatoire)
      error += 'Please complete all requested information.';
    
    $('#error').css('color', 'red').html(error); 
    if(error != '')
      return false;

}


(function($) {
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);