/* http://www.innovatingtomorrow.net/2008/04/30/preloading-content-jquery */
jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++)
    jQuery("<img>").attr("src", arguments[i]);
}

// BOF fonction d'interface (chargée sur toutes les pages)
function interface() {
	// Ajout de la fonctionnalités d'ascenceur
	if(jQuery("#middle").height()>800) {
		jQuery("#main").append('<p class="gobacktop"><a href="#header">Remonter</a></p>');
		jQuery(".gobacktop a").click(function() {
			jQuery.scrollTo("0px",800);
			return false;
		});
	}
	// Modification à la volée des liens vers les billets de blog
	jQuery(".bloglink a").each(function() {
		jQuery(this).attr("href",jQuery(this).attr("href").substring(0,jQuery(this).attr("href").lastIndexOf("/")+1))
	});
	
	// Modification des menus principal et secondaire
	jQuery(".second_level").each(function() {
		jQuery(this).children("li:first-child").addClass("first");								  	
		jQuery(this).children("li:last-child").addClass("last");
	});
	jQuery(".submenu ul").each(function() {
		jQuery(this).children("li:first-child").addClass("first");
		jQuery(this).children("li:last-child").addClass("last");
	});
	jQuery("li.toSlide ul.second_level").css("opacity",0.9)
	
	// Ajout de focus sur les formulaires
	jQuery("form *").focus(function() {
		jQuery(this).addClass("focus");
	});
	
	jQuery("form *").blur(function() {
		jQuery(this).removeClass("focus");
	});
	
	// Changement des titres "Voir le blog" pour le personnaliser selon l'espace
	jQuery("#actu .bottomcorner .bloglink").each(function() {
		var departement = jQuery("body").attr('class');
		switch (departement)
		{
			case "accueil":
			var dpt = "Direction";
			break;
			case "maintenance":
			var dpt = "Maintenance";
			break;
			case "formation":
			var dpt = "Formation";
			break;
			case "internet": 
			var dpt = "Internet";
			break;
		}
		jQuery("#actu .bottomcorner .bloglink .LinkIn").html("Blog<br />"+dpt);
	});
	
	// Changement de l'image de background du bloc telemaintenance en fonction des espaces
	var departement = jQuery("body").attr('class');
	jQuery(".telemaintenance a").hover(function(){
		jQuery(".telemaintenance").css({ "background" : "url(/images/hotline_btn_"+departement+"_hover.png) no-repeat"});
	},function(){
		jQuery(".telemaintenance").css({ "background" : "url(/images/hotline_btn_"+departement+".png) no-repeat" });
	});
	
	//  Gestion du bon affichage de la carte
	jQuery("#divmap_1").css({"margin" : "0"});
	
	// Gestion de la page hotline en fonction de l'origine du clic
	jQuery("body.support_hotline").each(function(){
		var url = location.href;
		if(url.match("dpt")){
			var dpt = location.href.substring(location.href.indexOf("?dpt=")+5, location.href.length);
			if(dpt == 'formation' || dpt == 'maintenance' || dpt == 'internet'){
				jQuery("body").addClass(dpt);
				jQuery("#faq_box").load("/"+dpt+"/ div.support-technique");
			}
		}
	});
	
	// Gestion des effets d'affichages sur les pages de blog
	jQuery(".blogPost .linksmore a").each(function(i) {
		jQuery(this).attr('class','item'+i);
		jQuery.ajax({
			type: "GET",
			url: jQuery(this).attr("href"),
			cache: false,
			success: function(data){ 
				html=jQuery(data).find(".simpleText").html();
				if (html.length!=0)
					jQuery('.item'+i).click(moar);
				else
					jQuery('.item'+i).remove();
			}
		});
	});
	
	// preload des images du bloc support technique
	jQuery.preloadImages("/images/hotline_btn_internet_hover.png", 
						 "/images/hotline_btn_maintenance_hover.png", 
						 "/images/hotline_btn_formation_hover.png");
	
	// ajout des target sur les liens externes
	jQuery("a.LinkOut").each(function(){
		jQuery(this).attr("target","_blank");
	 });
	
	// ajout du header animé pour la page d'accueil
	if(jQuery("body.accueil").length!=0){
		jQuery("#actu .middle").modha_animation(
										772, 
										292, 
											['/images/animation/header_generique.jpg','/images/animation/header_technique.jpg', '/images/animation/header_formation.jpg','/images/animation/header_communication.jpg','/images/animation/header_developpement.jpg'], "#FFFFFF", 4000, 6000, 2);
		jQuery("#actu .middle").css({'padding' : '0 3px', 'border' : '1px solid #BDBCBC', 'border-width' : '0 1px'});
		jQuery("#actu .bottomcorner .bloglink").remove();
	}
}
// EOF fonctions interface
function moar() {
	href = jQuery(this).attr("href");
	base = jQuery(this).parent().parent().parent();
	jQuery.ajax({
		type: "GET",
		url: href,
		cache: false,
		success: function(data){ 
			html=jQuery(data).find(".simpleText").html();
			urlless = '<a href="'+href+'">Réduire</a>';
			base.children(".injection").html(html);
			base.children(".injection").slideDown("slow");
			base.find(".linksmore").html(urlless);
			jQuery(".linksmore a").click(less);
		}
	});
	return false;
}

function less() {
	href = jQuery(this).attr("href");
	base = jQuery(this).parent().parent().parent();
	base.children(".injection").slideUp("slow");
	base.find(".linksmore").html('<a href="'+href+'">En savoir plus</a>');
	jQuery(".blogPost .linksmore a").click(moar);
	return false;
}

/*
	Cross-Browser Split 0.3
	By Steven Levithan <http://stevenlevithan.com>
	MIT license
	Provides a consistent cross-browser, ECMA-262 v3 compliant split method
*/
String.prototype._$$split = String.prototype._$$split || String.prototype.split;

String.prototype.split = function (s /* separator */, limit) {
	if (!(s instanceof RegExp))
		return String.prototype._$$split.apply(this, arguments);

	var	flags = (s.global ? "g" : "") + (s.ignoreCase ? "i" : "") + (s.multiline ? "m" : ""),
		s2 = new RegExp("^" + s.source + "$", flags),
		output = [],
		origLastIndex = s.lastIndex,
		lastLastIndex = 0,
		i = 0, match, lastLength;

	if (limit === undefined || +limit < 0) {
		limit = false;
	} else {
		limit = Math.floor(+limit);
		if (!limit)
			return [];
	}

	if (s.global)
		s.lastIndex = 0;
	else
		s = new RegExp(s.source, "g" + flags);

	while ((!limit || i++ <= limit) && (match = s.exec(this))) {
		var emptyMatch = !match[0].length;

		if (emptyMatch && s.lastIndex > match.index)
			s.lastIndex--;

		if (s.lastIndex > lastLastIndex) {
			if (match.length > 1) {
				match[0].replace(s2, function () {
					for (var j = 1; j < arguments.length - 2; j++) {
						if (arguments[j] === undefined)
							match[j] = undefined;
					}
				});
			}

			output = output.concat(this.slice(lastLastIndex, match.index));
			if (1 < match.length && match.index < this.length)
				output = output.concat(match.slice(1));
			lastLength = match[0].length;
			lastLastIndex = s.lastIndex;
		}

		if (emptyMatch)
			s.lastIndex++;
	}
	output = lastLastIndex === this.length ?
		(s.test("") && !lastLength ? output : output.concat("")) :
		(limit ? output : output.concat(this.slice(lastLastIndex)));
	s.lastIndex = origLastIndex;
	return output;
};