/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|		
|		Copyright (c) 2009 ministere de la Defense
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|		
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* sIFR declarations */
sIFR.activate(arnoproregular,arnoprobold);
var arnoproregular = { src: arnopro_regular_swf };
var arnoprobold = { src: arnopro_bold_swf };

sIFR.replace(arnoproregular, {
	selector: '#ColContenu h1',
	css: '.sIFR-root {color:#000000;leading:-7;}',
	transparent:true ,
	tuneHeight : -5
});

sIFR.replace(arnoproregular, {
	selector: '#ColA .termes h1',
	css: ['.sIFR-root {color:#FFFFFF;}',
		'a {text-decoration:none;color:#FFFFFF;}',
		'a:link {color:#FFFFFF;}',
		'a:hover {text-decoration:underline;color:#FFFFFF;}'
	],
	transparent:true ,
	tuneHeight : -12
});

sIFR.replace(arnoproregular, {
	selector: '#ContainerCols h2.une',
	css: '.sIFR-root {color:#FFFFFF;leading:10;}',
	transparent:true ,
	offsetTop:5,
	tuneHeight : -5
});

sIFR.replace(arnoprobold, {
	selector: '#TT strong',
	css: '.sIFR-root {color:#FFFFFF;}',
	transparent:true
});

/* [ Champs focus sur input recherche ]________________________ */

(function($) {
    $.fn.toggleFocus = function() {
        return this.each( function() {
            var input =  $(this);
			if( input.length > 0 ) { 
				var id_input=input.attr("id");
				var form=input.parents("form");
				var label=form.find("label").attr("for",id_input);				
				var old_val=label.text()||"";
				label.css("display","none");
				input.val(old_val);
				var news_val="";
				input.focus(function() {  
					news_val=input.val() ;
					if ( old_val==news_val ) {
						input.val("");
					}
				});
				input.blur(function() {
					if (input.val() != "" ){
						news_val=input.val();
					} else {
						news_val=old_val;
					}
				   input.val(news_val);
				});
			}
        });
    };
})(jQuery);

/* [ 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(ele) {
	if(typeof ele != "string") {
		return;
	}	
	var navRoot = $("#"+ele);
	if ( navRoot.length >0 ) {
		var kids = navRoot.find("li:has('ul')");
		var uls = navRoot.find("li ul");
		var kidsa = navRoot.find("li a");		
		kids.each(function(){
			var li = $(this);
			var afocus = li.find("a:first");
			var ul = li.find("ul");
			li.mouseenter(function(){								
				uls.hide(10);				
				kids.removeClass("over");
				ul.stop(true, true).slideDown(300);
				li.addClass("over");
				return false;
			}).mouseleave(function(){
				kids.removeClass("over");
				ul.hide(10);
				return false;
			});					
			afocus.focus(function(){
				li.trigger("mouseenter");
				return false;
			});			
		});
		
		var kidsNoUL = navRoot.find(" > li").not(':has("ul")');
		kidsNoUL.find("a").focus(function(){
			kids.removeClass("over");							  
			uls.hide(10);
			return false;
		});			
		
	}
}


/* [ Gestion de la taille du texte d’un article ]________________________ */

function SmartSize(args) {
	var cadre = $("#"+args);	
	if( cadre.length > 0 ) {
		/*
		var ftz = cadre.css("fontsize");
		var ftzNum = parseFloat(ftz.substring(0,ftz.length-2));
		*/
		var ftzNum = 12;
           	$("#Tplus").click(function(){
			ftzNum = ftzNum+1;			
			cadre.css("fontSize", ftzNum+"px");		
		 });		
		$("#Tmoins").click(function(){		
			ftzNum = ftzNum-1;		
			cadre.css("fontSize", ftzNum+"px");			
		});	
	}	
}

/* [ Lancement d’une impression pour les navigateurs compatibles ]________________________ */

function DirectPrint() {
	if (window.print) self.print();
}

/* [ Miscellaneous ]________________________ */

function OpenPopup(url,nom,option) {
	window.open(url,nom,option);
}

/* [ Ajout de la page courante aux favoris ]________________________ */

function bookmarksite() {
	var title = document.title;
	var url = document.location.href;
	if (window.sidebar) { // firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all) {// ie
		window.external.AddFavorite(url, title);
	}
}


/* fn survol sur la une*/
var fnLaUne =  function(){
	if( $('#laune').length > 0 ){
		$('#laune .termes .terme').each(function(i){
			var terme = $(this);
			var list = $("#laune ul.list-imgs li");
				terme.mouseenter(function(){
					$('#laune .termes .terme').removeClass("on");
					list.removeClass("on");
					terme.addClass("on");
					list.eq(i).addClass("on")
				}
			);
		});
	}
}

/* fn survol sur TV defense */
var fnTVDdefense = function(){
	if( $('.row .tv').length > 0 ){
		$('.row .tv').each(function(i){
			var tv = $(this);
			var video = tv.find(".lt .video");
			var p = tv.find(".rt p");
			p.each(function(i){
				$(this).click(function(){
					video.removeClass("on");
					p.removeClass("on");
					$(this).addClass("on");
					video.eq(i).addClass("on");
					return false;
				});
			});			
		});
	}
};

var fnAddfirst = function(){
	/* ajouter un margin-right pour le 1e cell : 20px */
	if( $(".deux .cell").length > 0 ) {
		$(".deux").each(function(){
			 $(this).find(".cell:first").addClass("first");
		});	
	}
	/* 1p sans bg-img */
	if( $(".row .rt p").length > 0 && $.browser.msie ) {
		$(".row .rt p:first").addClass("first");
	}
};

var fnSlider = function(){
	if( $(".slider").length > 0 ) {
		$(".slider").each(function(){
			var slider = $(this);
			var h3 = slider.find("h3");
			var h4 = slider.find("h4");
			var inner = slider.find(".inner-content");
			inner.addClass('hidden');
			h3.click(function(){
				if( inner.is(":visible") ){						
					inner.slideUp(500,function(){
						h4.fadeIn(500);
						h3.removeClass("tog");
					});					
				}else {
					if( h4.length > 0 ) {
						h4.fadeOut(500,function(){
							inner.slideDown(500);
							h3.addClass("tog");
						});
					}else {
						inner.slideDown(500,function(){
							h3.addClass("tog");
						});
					}
				}				
				return false;
			});
		});		
		$(".open h3").trigger('click');
	}
	/* tout ouvrir / fermer pour l'accès direct*/
 
	if( $("#ouvrir-fermer-AccesDirect a").length > 0 ) {
		$("#ouvrir-fermer-AccesDirect a").click(function(){
			var a = $(this);
			a.toggleClass("open");
			if(a.hasClass("open")) {
				a.text(acces_direct_txt2);
				$("#AccesDirect .slider .inner-content").css("display",'block');
				$("#AccesDirect .slider h3").addClass("tog");
			}else {
				a.text(acces_direct_txt1);
				$("#AccesDirect .slider .inner-content").css("display",'none');
				$("#AccesDirect .slider h3").removeClass("tog");
			}
			a.blur();
			return false;
		});
	}
	/* tout ouvrir / fermer pour l'accès cible*/
	if( $("#ouvrir-fermer-AccesCible a").length > 0 ) {
		$("#ouvrir-fermer-AccesCible a").click(function(){
			var a = $(this);
			a.toggleClass("open");
			if(a.hasClass("open")) {
				a.text(acces_cible_txt2);
				$("#AccesCible .slider .inner-content").css("display",'block');
				$("#AccesCible .slider h3").addClass("tog");
			}else {
				a.text(acces_cible_txt1);
				$("#AccesCible .slider .inner-content").css("display",'none');
				$("#AccesCible .slider h3").removeClass("tog");
			}
			a.blur();
			return false;
		});
	}
};

var fnImpair = function(){
	if( $('div.slider').length > 0 ){
		$('div.slider ul li').each(function(i){
			if( i%2 == 0 ) {
				$(this).addClass("impair");
			}
		});
	}
};

var fnjcarousel = function(){
	if($('#carousel').length > 0) {
		$('#carousel').jcarousel();		
	}
};

var fnOnglets = function(){
	if( $("div.onglet").length > 0 ) {
		$("div.onglet").each(function(){
			var onglet = $(this);
			var inner = onglet.find(".inner-onglet");
			var allLi = onglet.find("ul.menu-onglet li");
			inner.slice(1).css("display","none"); 
			allLi.each(function(i){
				var own = $(this);
				var a = own.find("a");								
				a.click(function(){
					a.blur();
					if( own.hasClass("active") ) {
						return false;
					}
					allLi.removeClass("active");
					own.addClass("active");
					inner.css("display","none");
					//inner.eq(i).css("display","block");
					inner.eq(i).fadeIn(500);
					return false;
				});				
			});
		});
	}
};

var fnFAQcheckboxes = function() {
	if($('.faq .cocher').length>0) {
		$('.faq .cocher').css('display','block');
	}
	$('#s1').click(
        function() {
            $(".faq input[type='checkbox']").attr('checked', $('#s1').is(':checked'));
    });
	$('#s2').click(
        function() {
            $(".faq input[type='checkbox']").removeAttr('checked');
    });
};

var fnthumbnailZoomVideo = function() {
	//set and get some variables
	var thumbnail = {
		imgIncrease : 100, /* the image increase in pixels (for zoom) */
		effectDuration : 400, /* the duration of the effect (zoom and caption) */
		/* 
		get the width and height of the images. Going to use those
		for 2 things:
			make the list items same size
			get the images back to normal after the zoom 
		*/
		imgWidth : $('.thumbnailWrapper ul li').find('img').width(), 
		imgHeight : $('.thumbnailWrapper ul li').find('img').height() 
	};
	//make the list items same size as the images
	$('.thumbnailWrapper ul li').css({ 
		'width' : thumbnail.imgWidth, 
		'height' : thumbnail.imgHeight 
	});
	//when mouse over the list item...
	$('.thumbnailWrapper ul li').hover(function(){
		$(this).find('img').stop().animate({
			/* increase the image width for the zoom effect*/
			width: parseInt(thumbnail.imgWidth) + thumbnail.imgIncrease,
			/* we need to change the left and top position in order to 
			have the zoom effect, so we are moving them to a negative
			position of the half of the imgIncrease */
			left: thumbnail.imgIncrease/2*(-1),
			top: thumbnail.imgIncrease/2*(-1)
		},{ 
			
			"duration": thumbnail.effectDuration,
			"queue": false
		});
		//show the caption using slideDown event
		$(this).find('.caption:not(:animated)').slideDown(thumbnail.effectDuration);
	//when mouse leave...
	}, function(){
		//find the image and animate it...
		$(this).find('img').animate({
			/* get it back to original size (zoom out) */
			width: thumbnail.imgWidth,
			/* get left and top positions back to normal */
			left: 0,
			top: 0
		}, thumbnail.effectDuration);
		//hide the caption using slideUp event
		$(this).find('.caption').slideUp(thumbnail.effectDuration);
	});
	
	$('.thumbnailWrapper li').each(function(i){
		var n=i+1;
		if(n%3==0) {
			$(this).addClass('nomargin');
		}
	});
};

/* [ Lancement script ]________________________ */

jQuery(document).ready(function($){	
	//if($("#mot_cle").length>0) {$("#mot_cle").toggleFocus();}
	
	/* Navigation */
	if($("#NavigationPrincipale").length>0) {
		SmartHover("NavigationPrincipale");
	}
	
	fnthumbnailZoomVideo();
	
	if($("#BG").length>0&&$("#CT").length>0) {
		switch($("html").attr('lang')) {
			case 'en':
				btpauseStr='Pause animation';
				btplayStr='Play animation';
				break;
			case 'es':
				btpauseStr='Pause animation';
				btplayStr='Play animation';
				break;
			default:
				btpauseStr='Mettre l’animation en pause';
				btplayStr='Jouer l’animation';
				break;
		}
		btpause='<div id="pause"><a href="javascript:;" id="pause1" title="'+btpauseStr+'"><strong>'+btpauseStr+'</strong></a></div>';
		$("#CT .inner #Logo").after(btpause);
		$("#BG").slideshow({
			timeout:temps_pause,
			pauselink:'pause1',
			pausecallback:function(self){
				self.addClass('on');
				self.attr('title',btplayStr);
				self.html('<strong>'+btplayStr+'</strong>');
			},
			playcallback:function(self){
				self.removeClass('on');
				self.attr('title',btpauseStr);
				self.html('<strong>'+btpauseStr+'</strong>');
			}
		});
	}
		
	if($('#NavigationPrincipale > li:first').length > 0) {
		$("#NavigationPrincipale > li:first").addClass("firstChild");
	}
	
	if($('#PiedDePage ul li:first').length > 0) {
		$("#PiedDePage ul li:first").addClass("firstChild");
	}
	
	if($('#MenuSecondaire li:first').length > 0) {
		$("#MenuSecondaire li:first").addClass("firstChild");
	}
	if($('#ContainerCols .row .r-bloc:first').length > 0) {
		$("#ContainerCols .row .r-bloc:first").addClass("firstChild");
	}
	
	/* arrondis */	
	if($('#EnTete #Localisation ul li').length > 0) {
		$('#EnTete #Localisation ul li').cornerz();
	}
	if($('#ColContextuelle div#carte .inner').length > 0) {
		$('#ColContextuelle div#carte .inner').cornerz();
	}
	if($('#ColContextuelle .CloudLinks').length > 0) {
		$('#ColContextuelle .CloudLinks').cornerz();
	}
	if($('.bl-imgFlow').length > 0) {
		$('.bl-imgFlow').cornerz();
	}
	if($('.r-bloc').length > 0) {
		$('.r-bloc').cornerz({fixIE:true});
	}
	
	fnLaUne();
	fnAddfirst();
	fnTVDdefense();
	fnSlider();
	fnImpair();
	fnjcarousel();
	fnFAQcheckboxes();
	
	/* toolBox */		
	if($("#toolBox").length>0) {
		$("#print").click(function(){
			DirectPrint();
			return false;
		});
		$("#favoris").click(function(){
			bookmarksite();
			return false;
		});
		SmartSize("ColContenu");
	}
	
	/* onglet ds la page actualites */
	fnOnglets();
	if($(".scroll-pane").length>0) {
		$('.scroll-pane').jScrollPane({'showArrows':true});
	}
	if($(".scroll-pane-large").length>0) {
		$('.scroll-pane-large').jScrollPane({'showArrows':true});
	}
	$("a.lightbox").lightBox();
	
	
	$(function() {
		$('.ad-gallery li img').each(function(){
			var img = $(this);
			var li = $(this).parents('li');
			var legend = li.find('.legende');
			legend.hide();
			img.data('ad-desc', legend.html());
		});
		var galleries = $('.ad-gallery').adGallery({effect:'fade',loader_image:'/extension/dicod_design/design/dicod/images/ad-gallery/loader.gif',slideshow : {start_label : '<span>Lecture</span>', stop_label : '<span>Pause</span>'}});
		$('#toggle-slideshow').click(
		  function() {
			galleries[0].slideshow.toggle();
			return false;
		  });
	  });

});
