// common
$(document).ready(function () {

  $('a.captify').captify();
	
	// manage menu
	var startItem = 0;
	var list = $('#menu ul li');
	var total = list.length;
	var currentItem = 0;
	var count = 0;
	var find = false;
	while(!find && count < total) {
		if($(list[count]).hasClass('current-menu-item') || $(list[count]).hasClass('current-category-ancestor') || $(list[count]).hasClass('current-post-ancestor') || $(list[count]).hasClass('current-menu-item') || $(list[count]).hasClass('current-menu-parent')) {
			find = true;
			currentItem = count;
		} else {
			count++;
		}
	}
	
	if($("#menu").hasClass("noMenu")){
    $('#menu ul').lavaLamp({
  		fx: 'easeInOutExpo',
  		speed: 750,
  		returnDelay: 150,
  		homeLeft:-125,
  		homeHeight:45,
  		homeWidth:125
  	});
  }else {        
  	$('#menu ul').lavaLamp({
  		fx: 'easeInOutExpo',
  		speed: 750,
  		startItem: currentItem,
  		returnDelay: 150
  	});
	}
	
	
	$("#socialNetwork .pictogram a span, #content .tagAndShare a span, #wrapperPaging .paging a span, #contextual .pictureOfDay a span, .wrapperMenu .menu li span").hide();
	$("#socialNetwork .pictogram a, #content .tagAndShare a, #wrapperPaging .paging a, #contextual .pictureOfDay a, .wrapperMenu .menu li a").hover(function(){
	    if(isIE && isIE < 9){
      		$("span", this).show();
		}else {
      		$("span", this).stop().fadeTo(250, 1);
      	}
  	}, function(){
	    if(isIE && isIE < 9){
      		$("span", this).hide();
		} else {
       		$("span", this).stop().fadeTo(500, 0);
      	}
  	})
  	/********
  	 *TEAM
  	 ********/
	$(".autor a.autor-image span").hide();
	$(".autor").hover(function() {
	    if(isIE && isIE < 9){
      		$("a.autor-image span", this).show();
		} else {
      		$("a.autor-image span", this).stop().fadeTo(250, 1);
      	}
  	}, function(){
	    if(isIE && isIE < 9){
      		$("a.autor-image span", this).hide();
		} else {
       		$("a.autor-image span", this).stop().fadeTo(250, 0);
      	}
  	}).click(function(){
        window.location.href = $("a.autor-image", this).attr("href");
	});
  	
  	
  $('#wrapperPaging .paging a.hide').click(function(){
	 return false;
  });
  
   Cufon.replace('.article h2, #menu a, #contextual h3, #footer h3, #contextInfos .category, #comments h3, #respond h3, #comments p.info .url, .title h2, h3.titre');
   
   $("input:text:not(.fixed)").each(function(){
      var defaultVal = $(this).val();
      $(this).focus(function(){
         if($(this).val() == defaultVal){
            $(this).val("");
         }
      }).blur(function(){
          if($(this).val() == ""){
            $(this).val(defaultVal);
         }
      });
   });
   
   // manage bubble mask
   bubble.init();
   
   $("a.fancybox").fancybox({
        width: '90%',
        height: '90%'
   });
   
   $("#respond form input, #respond form textarea").focus(function(){
	  $(this).addClass("focus");
   }).blur(function(){
	  $(this).removeClass("focus");
   });
  
});

// manage bubble mask
var bubble = {
	init: function () {
		bubble.updateMask();
		
		$(window).resize(function () {
			bubble.updateMask();
		});
		
		bubble.makeTabOffset();
		if(!isIE || isIE >= 8){
			$(window).scroll(function () {
				bubble.updateDate();
			});
		}
		
		if(isIE && isIE < 8){
			$('#contextInfos').hide();
		} else {
			$('.article .date').hide();
		}
	},
	config: {
	    offsetCursor: 53,
	    previousItem: 0,
	    duration: 200,
	    classPicto: 'home'
	},
	updateDate: function (method) {
	    
	    var scrollTop = $(window).scrollTop();
	    var current = 0;
	    var tab = bubble.makeTabOffset();
	    scrollTop = scrollTop + tab[0]['top'] + bubble.config.offsetCursor;
	    var found = false;
	    var i = 0;
	    var date = '';
	    var newClass = '';
	     
	    while((i < tab.length) && (!found)) {
	        if(scrollTop <= tab[i]['bottom']) {
	            current = i;
	            found = true;
	            var dateObj = $('.article:eq('+i+')').find('.date');
	            if($('span', dateObj).text() != '') {		    
	            	date = $('span', dateObj).text();
		            var dateTemp = date.trim();			   
		            dateTemp = dateTemp.split('/');
		            date = dateTemp[0]+"/"+dateTemp[1];			   
				} else {
					date = '';
				}
	            newClass = $('.article:eq('+i+')').attr('class').split(' ')[1];		    
	            bubble.config.classPicto = newClass;
	        }
	        i++;
	    }
	    
	    if(method == 'init') {
	        if(!found) {
	            $('#contextInfos .content .mask').show().addClass('hidden');
	        } else {
	            $('#contextInfos .category').html(date);
	            bubble.updatePicto(newClass);
	            Cufon.replace('#contextInfos .category');
	        }
	    } else {
	        if(!found) {
	            bubble.fadeOut();
	        } else {
	            if($('#contextInfos .content .mask').hasClass('hidden')) {
	                bubble.fadeIn();
	            }
	            if(current != bubble.config.previousItem) {
    	            bubble.config.previousItem = current;
    	            
    	            var duration = bubble.config.duration;
    	            
    	            $('#contextInfos .category, #contextInfos .picto').stop().fadeTo(duration, 0, function () {
    	                $('#contextInfos .category').html(date);		
    	                bubble.updatePicto(newClass);
    	                Cufon.replace('#contextInfos .category');
    	                $('#contextInfos .category, #contextInfos .picto').fadeTo(duration, 1);
    	            });
    	        }
	        }
	    }
	    
	},
	updatePicto: function (newClass) {
		bubble.config.classPicto = newClass;
		$('#contextInfos .content .bgWrapper').removeClass('marketing').removeClass('techno').removeClass('design').removeClass('home').addClass(newClass);
  	},
	fadeOut: function () {
	    var elmt = $('#contextInfos .content .mask');
	    var duration = bubble.config.duration;
	    elmt.stop().fadeTo(duration, 1);
	    elmt.addClass('hidden');
	},
	fadeIn: function () {
	    var elmt = $('#contextInfos .content .mask');
	    var duration = bubble.config.duration;
	    elmt.stop().fadeTo(duration, 0);
	    elmt.removeClass('hidden');
	},
	makeTabOffset: function () {
	    
	    var tab = new Array;
	    $('.wrapperArticle').each(function (e) {
	        var elmt = $(this);
	        tab[e] = new Array;
	        tab[e]['top'] = parseInt(elmt.offset().top);
	        var bottom = tab[e]['top'] + elmt.height();
	        tab[e]['bottom'] = bottom;
	    });
		if(tab.length == 0) {
			tab = false;
		}
	    return tab;
	},
	windowHeight: function () {
		var windowHeight = $(window).height();
		return windowHeight;
	},
	offsetTop: function () {
	    var top = $('#contextInfos .content').offset().top;
	    return top;
	},
	updateMask: function () {
		var windowHeight = bubble.windowHeight();
		var height1 = $('#contextInfos .borderTop').height();
		var height2 = $('#contextInfos .content').height();
		
		var targetHeight = windowHeight - height1 - height2;
		
		$('#contextInfos .borderBottom, #contextInfos .borderBottom *').css('height', targetHeight+'px');
	}

};
$(window).load(function () {
	// call carousel
	manageCarousel.init('#carousel');
	manageCarousel.init('#carouselFormations');
  if ($('.wrapperArticle').length > 0) {
		bubble.updateDate('init');
		
	} else {
		$('#contextInfos .content .mask').show();
	}
});

//Initialize first demo:
ddaccordion.init({
	headerclass: "titre", //Shared CSS class name of headers group
	contentclass: "contenu", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: false, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [0,1,6,7], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: false, //persist state of opened contents within browser session?
	toggleclass: ["", "open"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["none", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})


