/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","auto");
		});
	}
})(jQuery);

var host = window.location.host || window.location.hostname;

var addthis_config = {
        services_compact: 'facebook, twitter,linkedin, viadeo, more',
        services_exclude: 'print, email',
        ui_click: true
}

$(document).ready(function() {

	/* Liens externes */
	$('a.extern').attr('target','_blank');
	$('a[href^="http://"]').attr('target','_blank'); // Dans une autre règle au cas où il faille être plus précis

	/* Menu */
	var toMenu, toMenuShown;
	var isMenuShown = false;
	$('#menu li.menu').each(function(){
		$(this).children('a').attr('title','');
		var subMenu = $(this).children('.subMenu');
		subMenu.children(':last').addClass('last');
		var subMenuContainer = $("<div class='" + subMenu.attr('class') + "'></div>").appendTo($(this));
		subMenu.attr('class','');
		subMenuContainer.append("<div class='tl'><div class='tr'></div></div><div class='ctl'><div class='ctr'><div class='ct'></div></div></div><div class='bl'><div class='br'></div></div>");
		subMenuContainer.find('.ct')
			.append(subMenu)
			.append($("<p class='spacer'></p>"));
		subMenuContainer.find('.tl').css('width',subMenuContainer.find('.ctl').css('width'));

		$(this).hover(
			function() {
				clearTimeout(toMenu);
				clearTimeout(toMenuShown);
				if(isMenuShown) {
					showMenuContainer(subMenuContainer);
				} else {
					toMenu = setTimeout(function(){
						showMenuContainer(subMenuContainer);
					}, 300);
				}
				isMenuShown=true;
			}, function() {
				clearTimeout(toMenu);
				subMenuContainer.hide();
				toMenuShown = setTimeout(function(){isMenuShown=false;},30);
			}
		);
	});
	function showMenuContainer(o) {
		o.show();
		if($.browser.msie && parseFloat($.browser.version) <= 7) {
			var w = o.find('.ctl').width() - 6;
			o.find('.tl').width(w);
			o.find('.bl').width(w);
		}
	}
	
	// Marquage Xiti
	// Menu principal
	$('#menu a[href]').each(function(){
		if($(this).attr('href') != "#") {
			$(this).click(function(){
				return xt_click(this,'C','1','Navigation::Menu_principal','N');
			});
		}
	});
	// Menu contextuel
	$('#context .ctx_bloc a[href]').click(function(){
		return xt_click(this,'C','1','Navigation::Menu_contextuel','N');
	});
	$('#context .ctx_voir_aussi a[href]').click(function(){
		return xt_click(this,'C','1','Navigation::Menu_contextuel','N');
	});

	/* @todo : transformer en plug-in */
	if($('#problematiques.inter').size()) {
		var currentProblematique;
		var imgFermerProblematique = null;

		function showProblematique() {
			if(imgFermerProblematique == null) {
				imgFermerProblematique = $('<img />').attr({className:'btFermer', src:'/extension/ezcade/design/ezcade/images/profil/fermer.png'}).hide();
				$('#problematiques').append(imgFermerProblematique);
			}
			var id = $(this).attr('id');
			$(this).parent().find('.problematique').each(function(){
				$(this).fadeOut('fast',function(){
					if($(this).attr('id') == id) {
						$(this).addClass('problematiqueSelected').fadeIn('fast')
							.unbind('click')
							.click(hideProblematique);
						imgFermerProblematique.show().click(hideProblematique).fadeIn('fast');
						currentProblematique = $(this);
					}
				});
			});
		}

		function hideProblematique() {
			imgFermerProblematique.hide();
			currentProblematique.fadeOut('fast',showProblematiques);
		}

		function showProblematiques(){
			$('#problematiques .problematiques li.problematique').each(function(){
				$(this).removeClass('problematiqueSelected').fadeIn('fast').unbind('click').click(showProblematique);
			});
		}

		$('#problematiques .problematiques li.problematique').click(showProblematique);
		$('#problematiques .problematiques li.problematique ul.questions a').click(function(e){e.stopPropagation(); $(this).blur()});
	}

	/* Onglets */
	/* @todo : créer un plug-in ? */
	$('.onglets').each(function(){
		var onglets = $(this);
		var currentOnglet = null, currentContent = null;
		$(this).find('li').each(function(){
			var content = $('#' + $(this).attr('id') + 'Contenu');
			var onglet = $(this);
			if($(this).hasClass('selected')) {
				currentOnglet = onglet;
				currentContent = content;
			} else if(content.hasClass('active')) {
				//content.removeClass('active');
				currentContent = content;
			} else {
				content.hide();
			}
			$(this).find('a').click(function (){
				if(currentOnglet == null || currentOnglet != onglet) {
					if(currentOnglet) currentOnglet.removeClass('selected');
					// if(currentContent) currentContent.fadeOut('fast', function(){content.fadeIn('fast');});
					if(currentContent) IEFade('out', currentContent, 'fast', function(){IEFade('in', content, 'fast');});
					// else content.fadeIn('fast');
					else IEFade('in', content, 'fast');
					onglet.addClass('selected');
					currentOnglet = onglet;
					currentContent = content;
				}
				$(this).blur();

				// Cas particuliers ou le lien doit être suivi
				return false;
			});
		});
	});

	/* Dropdown */
	var currentDropdownId = "";
	$(".dropdown").each(function(){
		var dropdown = $(this);
		var dropdownId = dropdown.attr('id');
		var mainLink = $(this).find('dt a');
		var listContainer = $(this).find('dd');
		var list = $(this).find('dd ul');
		mainLink.click(function() {
			$(".dropdown[id!=" + dropdownId + "] dd").hide();
			listContainer.toggle();
			$(this).blur();
			return false;
		})
		if(!dropdown.is('.directLink')) {
			list.find('li a').click(function() {
				list.find('li').removeClass('selected');
				var text = $(this).html();
				var value = $(this).find('span.value').html();
				mainLink.find('span').html(text);
				listContainer.hide();
				dropdown.trigger('callback', value, text);
				currentDropdownId = dropdownId;
				$(this).parent().addClass('selected');
				return false;
			});
		}
	});

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown"))
			$(".dropdown dd").hide();
	});
	
	/* Lien projet vision */
	/*
	var liens_vision = $('#projetVision .lien_vision');
	if(liens_vision.size()) {
		liens_vision.each(function(){
			var isSelected = $(this).is('.selected');
			var projets = $(this).find('ul');
			var move = false;
			if(!isSelected) {
				$(this).find('a').mouseenter(function(){
					if(move)
						return;
					move = true;
					projets.slideDown('fast',function(){move=false;});
				}).end().mouseleave(function(){
					if(move)
						return;
					move = true;
					projets.slideUp('fast',function(){move=false;});
				});
			}
		});
		$('.blocVisions').mouseleave(function(){
			$(this).find('.lien_vision').each(function(){
				if($(this).is('.selected')) return;
				$(this).find('ul').hide();
			})
		});
	}*/

	/* Zoom projet vision */
	/*
	var hideProjetVision = $('#hideProjetVision');
	if(hideProjetVision.size()) {
		hideProjetVision.show().click(function(){
			$('.blocNotreVision .intro,.blocNotreVision .enSavoirPlus,.blocVisions,#hideProjetVision').fadeOut('fast',function(){
				$('#showProjetVision').fadeIn('fast');
			});
		});
	}*/
	var showProjetVision = $('#showProjetVision');
	if(showProjetVision.size()) {
		showProjetVision.click(function(){
			if($(this).is('.closed')) {
				$('.blocNotreVision .intro,.blocNotreVision .enSavoirPlus,.blocVisions,#hideProjetVision').fadeIn('fast',function(){
					showProjetVision.removeClass('closed');
				});
			} else {
				$('.blocNotreVision .intro,.blocNotreVision .enSavoirPlus,.blocVisions,#hideProjetVision').fadeOut('fast',function(){
					showProjetVision.addClass('closed');
				});
			}
		});
	}

	// Slider projets visions
	if($('#projetsVision li').size() > 1) {
		$('#projetsVision').easySlider({
			auto: true, 
			continuous: true,
			controlsShow: true,
			speed: 600,
			pause: 4000,
			prevText: '',
			nextText: ''
		});
	}
	
	// Sliders reference
	if($('#imageHeader .slideshow li').size() > 1) {
		$('#imageHeader .slideshow').easySlider({
			auto: true, 
			continuous: true,
			controlsShow: false,
			speed: 1000,
			pause: 4000
		});
	}
	
	// Diaporamas références
	$("a[rel=diaporama]").fancybox({
	});
	$('.reference #imageHeader a.btDiaporama').click(function(){
		$("a[rel=diaporama]:first").trigger('click');
		return false;
	});

	// Diaporamas
	$("a.fancybox").fancybox({
	});
	
	//fancybox videowall
	$(".videos li a").fancybox();
	
	// Accordeon
	$('.pToggle').each(function(){
		var ic = $(this).find('.innerContent');
		$("<a class='toggle'></a>").appendTo($(this))
			.click(function(){
				ic.slideToggle('fast');
				$(this).blur();
				if($(this).is('.up'))
					$(this).removeClass('up');
				else
					$(this).addClass('up');
			});
		ic.hide();
	});

	// Contacts
	if($("#resultContacts").size()) {
		$('#resultContacts > div:not(.selected)').hide();
		$('#filtreContacts').bind('callback', function(event, value, text){
			$('#resultContacts > div').hide();
			$('#contact_' + value).show();
		});
	}

	// Carte des parcs tertiaires
	$('.paragraphe_carte_parcstertiaires').each(function(){
		var mainImage = $(this).find('img');
		var p = $(this);
		$(this).find('.contentsParcsTertiaires > li').hide().append("<a href='#' class='fermer'>X</a>");
		$(this).find('.contentsParcsTertiaires > li a.fermer').click(function(){
			p.find('.contentsParcsTertiaires > li').hide();
			return false;
		});
		$(this).find('area').each(function(){
			var aId = $(this).attr('id');
			$(this).attr('href','#')
				.hover(
					function(){mainImage.removeClass().addClass(aId.substring(2))},
					function(){mainImage.removeClass(aId.substring(2));})
				.click(function() {
					p.find('.contentsParcsTertiaires > li').hide();
					$('#c' + aId.substring(1)).show();
					return false;
				});
		});
	});
	
});

function IEFade(mode, obj, speed, callback) {
	if($.browser.msie) {
		if(mode == 'out') obj.hide();
		else obj.show();
		if(callback) callback();
	} else {
		if(mode == 'out') obj.fadeOut(speed, callback);
		else obj.fadeIn(speed, callback);
	}
}

function log($msg) {
	if(window.console) console.log($msg);
}
