$(document).ready(function() {
	Carousel('mycarousel');
	Accordion('publication');
	Infobulle();
	Actualites();
	Menu3eme();
	$('#menu>ul>li>ul').each(function(i, elem) {
		$(elem).mouseover(function() {			
			$(this).parent().addClass('active');
		});	
	});
	var ancre = '';
	var rubPublication = '';
	var laPublication = '';
	if($('#lesPublications').length != 0) {
		ancre = window.location.hash.split('#');
		if(ancre[1] != 'undefined') {
			if(ancre[1].indexOf("_") != '-1') {
				tmpPublication = ancre[1].split('_');
				rubPublication = tmpPublication[0];
				laPublication = '#'+ancre[1];
			} else {
				rubPublication = ancre[1];
			}
		}
		if(rubPublication != '') {
			switchAccordeon(rubPublication);
		}
		if(laPublication != '') {
			switchPublication(laPublication);
		}
		jQuery('a.ancre').click(function(){
			ancre = jQuery(this).attr("href").split('#');
			rubPublication = ancre[1];
			
			switchAccordeon(rubPublication);
		});
		
		jQuery('.publications-ajax').click(function(){
			jQuery('#zoneAffichagePublications').empty().html('<p style="text-align:center;margin-top:100px;"><img alt="" src="/sites/all/themes/afipeb/images/publications-loader.gif"><br><br><strong style="color:#0071B9;">Chargement</strong></p>');
			var urlPublication = jQuery(this).attr('href')+' #contenuAjax';
			var hashPublication = jQuery(this).attr('id');
			locationWrapper.put(hashPublication);
			jQuery('#zoneAffichagePublications').load(urlPublication, function(){
				jQuery('#zoneAffichagePublications').removeClass('afficheIntro');
				jQuery('#zoneAffichagePublications').addClass('afficheDetail');
			});
			return false;
		});
	}
	$('.actu_vignette').click(function(){
		var contentImage = '<img src="'+$(this).attr('href')+'" alt="" />';
		TINY.box.show(contentImage,0,0,0,1);
		return false;
	});
});
var Carousel = function(id){
	$('#'+id).jcarousel({
     	scroll: 1,
		wrap: 'both'
    });
},
Accordion = function(id){
	$('#'+id).accordion({
		header : 'h2'
	});
},
Infobulle = function(){
	$('.numbers>li>a').hover(function(){
		var id = $(this).attr('rel'),
			x = parseInt($(this).offset().left) - (parseInt($('#'+id).width()/3 )),
			y = parseInt($(this).offset().top) - (parseInt($('#'+id).height()) - 10);
			
		$('#'+id).css({
			height : $('#'+id).height(),
			left : x,
			top : y,
			display:'block'
		})
	},
	function(){
		var id = $(this).attr('rel');
		$('#'+id).removeAttr('style');
	});
},
Actualites = function(){
	$('#showActus-event>a').toggle(function(){
		$(this).addClass('active');
		$('#showActus').css('display', 'block');
	},
	function(){
		$(this).removeClass('active');
		$('#showActus').css('display', 'none');
	});
},
Menu3eme = function(){
	/* Accordion */
	// $('#menu li li').css('position', 'relative');
	// $('#menu').accordion({
		// header : 'h2',
		// autoheight : false,
		// active : false
	// })
	/*
	$('#menu h2').next().hide();
	$('#menu h2').next().addClass('firstUl');
	$('#menu h2:first').next().show();
	$('.firstUl').css('overflow', 'visible');
	$('#menu h2 a').click(function() {
		$('.firstUl').hide();	
		$(this).parent().next().show();	
		return false;
	});
	*/
	$('#menu li ul').hide();
	$('#menu ul li ul').addClass('firstUl');
	
	$('#menu ul li.active-trail ul').show();
	$('#menu ul li ul.firstUl li ul').hide();
	//$('#menu a.niv1:first').next().show();
	
	$('.firstUl').css('overflow', 'visible');
	$('#menu a.niv1').click(function() {
		$('.firstUl').hide();
		$(this).next().show();
		return false;
	});
	
	
	// .bind('change.ui-accordion', function(event, ui) { 
    	// $('#menu ul').css('overflow', 'visible');
		// $('#menu li li').css('display', 'none');
		// $('#menu li.selected li').css('display', 'block');		
	// });
	
	$('#menu>ul>li').find('li').hover(function(){
		$(this).addClass('active');
		$(this).children('ul').fadeIn('fast');
	},
	function(){
		$(this).removeClass('active');
		$(this).children('ul').fadeOut('fast');
	});
};
var switchAccordeon = function(rubrique){
	switch(rubrique){
		default:
		case "focus":
			jQuery("#publication").accordion("activate", 0);
			break;
		case "pasapas":
			jQuery("#publication").accordion("activate", 1);
			break;
		case "fondamentaux":
			jQuery("#publication").accordion("activate", 2);
			break;
		case "dossiers":
			jQuery("#publication").accordion("activate", 3);
			break;
		case "autres":
			jQuery("#publication").accordion("activate", 4);
			break;
	}
};
var switchPublication = function(laPublication){
	jQuery('#zoneAffichagePublications').empty().html('<p style="text-align:center;margin-top:100px;"><img alt="" src="/sites/all/themes/afipeb/images/publications-loader.gif"><br><br><strong style="color:#0071B9;">Chargement</strong></p>');
	var urlPublication = jQuery(laPublication).attr('href')+' #contenuAjax';
	jQuery('#zoneAffichagePublications').load(urlPublication, function(){
		jQuery('#zoneAffichagePublications').removeClass('afficheIntro');
		jQuery('#zoneAffichagePublications').addClass('afficheDetail');
	});
};
var locationWrapper = {
	put: function(hash, win) {
		(win || window).location.hash = this.encoder(hash);
	},
	get: function(win) {
		var hash = ((win || window).location.hash).replace(/^#/, '');
		return $.browser.mozilla ? hash : decodeURIComponent(hash);
	},
	encoder: encodeURIComponent
};
