$(document).ready(function(){
	// For colorbox
	function bodyOFOn(){$('body').addClass('js-overflow-hide');}
	function bodyOFOff(){$('body').removeClass('js-overflow-hide');}

	// Search
	var inputSearch = $('#search input.search');
	var oldInputSearchValue = inputSearch.siblings('label').html();
	inputSearch.val(oldInputSearchValue).addClass('search-disabled');
	inputSearch.addClass('search-disabled').focus(function() {
		if ($(this).val() == oldInputSearchValue) {
			$(this).removeClass('search-disabled').val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass('search-disabled').val(oldInputSearchValue);
		}
	});
	$('#search').submit(function() {
		var cVal = $('input.search', $(this)).val();
		if (cVal == oldInputSearchValue || cVal == '') {
			alert(lg_search_input);
			return false;
		}
		return true;
	});
	
	//Newsletter
	var inputEMail = $('#newsletter input.search');
	var oldInputEMailValue = inputEMail.siblings('label').html();
	inputEMail.val(oldInputEMailValue).addClass('search-disabled');
	inputEMail.addClass('search-disabled').focus(function() {
		if ($(this).val() == oldInputEMailValue) {
			$(this).removeClass('search-disabled').val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass('search-disabled').val(oldInputEMailValue);
		}
	});
	/*$('#newsletter').submit(function() {
		var cVal = $('#newsletter input.search', $(this)).val();
		if (cVal == oldInputEMailValue || cVal == '') {
			alert(lg_email_input);
			return false;
		}
		return true;
	});*/

	// Print
	if($('li.print')) {
		$('li.print').show();
	}
	$('a.print').click(function(){
		print();
		return false;
	});
	
	// Goto Top
	$('a.top').click(function(){
		$('html, body').animate({scrollTop: 0}, 'slow');
		return false;
	});

	// A11Y Main Nav
	$('#nav-main a').focusin(function(){
		var el = $(this).siblings('div');
		el.addClass('show');
		el.find('a').last().blur(function(){
			el.removeClass('show');
		});
	});

	// Opcatity
	$('.opa50').css('opacity', '.5');

	// Roadbook
	$('.sticker-roadbook').bind('click', function(){
		$.colorbox({href:$(this).attr('href'),iframe:true,innerWidth:750,innerHeight:'90%',opacity:'0.2',onOpen:bodyOFOn,onClosed:bodyOFOff});
		return false;
	});
	$('.add-roadbook').live('click', function(){
		$.colorbox({href:$(this).attr('href'),iframe:true,innerWidth:750,innerHeight:'90%',opacity:'0.2',onOpen:bodyOFOn,onClosed:bodyOFOff});
		var element = $(this);
		element.hide();
		$.get($(this).attr('href'), function(data){
			element.replaceWith(data);
		});
		return false;
	});
	$('.remove-roadbook').live('click', function() {
		var element = $(this);
		element.hide();
		$.get($(this).attr('href'), function(data){
			element.replaceWith(data);
		});
		return false;
	});

	// Tooltips
	$('.tooltips').tooltip({effect:'slide',slideOffset:10});

	// Disable click
	$('a.noclick,li.CMS_lvl1 a.CMS_lvl1').click(function(){
		return false;
	});
	
	/*show/hide the map */
	if($('.open-close')) {
		$('.open-close').click(function(){
			var gEffectDuration = 500; //milliseconds

			if ($('.mapContainer').is(":visible")) {			
				$('.mapContainer').slideUp(gEffectDuration);	
				$('.openMap').val(lg_open_the_map);	
				$('.openMap').parent().children('label').text(lg_open_the_map);
			} else {	
				if( map == null){
					gMap();
				}
				$('.mapContainer').slideDown(gEffectDuration);	
				$('.openMap').val(lg_close_the_map);
				$('.openMap').parent().children('label').text(lg_close_the_map);
			}
		});	
	}
	var showMap = $('.show-map');
	if(showMap.length > 0 && map == null){
		gMap();
	}
	if($('.show_on_the_map')) {
		$('.show_on_the_map').click(function(){
			var gEffectDuration = 500; //milliseconds
			var point = 0;
			if ($(this).children('img').length) {
				point = parseInt($(this).children('img').attr('alt')) - 1;
			}
			else {
				point = parseInt($(this).text()) - 1;
			}
			if ($('.mapContainer').is(':visible')) {
				map.setCenter(aMarkers[point].getPosition());
				window.scrollTo(0, 500);
			} else {
				if(map == null){
					gMap();
					//map.setCenter(aMarkers[point].getPosition());
					window.scrollTo(0, 500);
				}
				else {
					map.setCenter(aMarkers[point].getPosition());
					window.scrollTo(0, 500);
				}
				$('.mapContainer').slideDown(gEffectDuration);	
				$('.openMap').val(lg_close_the_map);
				$('.openMap').parent().children('label').text(lg_close_the_map);
			}
			return false;
		});			
	}
	
	// Carrousel d'images
    var imgLinks = $(".imgListContainer .img_carousel");
    var legendLinks = $(".imgListContainer span");
    var imgChooser = $(".chooseImg");
    
    $('.carrousel .shadow').css('overflow','hidden');
    $('.numImg').css('visibility','visible');
    
    imgLinks.addClass('hide');
    $(".imgListContainer .visible").parent().removeClass('hide');
    
    $('.imgListContainer').attr('class','imgListContainerJs');
    
    imgChooser.bind('click keydown', function (event) {
        event.stopPropagation();
        var numImg = $(this).text();
        
        imgChooser.removeClass('repeatNumSelected');
        
        $(this).addClass('repeatNumSelected');
        
        imgLinks.addClass("hide")
        .removeClass("visible");
        
        legendLinks.addClass("hide")
        .removeClass("visible");
        
        sIdImg = this.id.replace('choose','');
        
        $('#'+sIdImg).parent().removeClass("hide")
        .addClass("visible");
        $('#legend_'+sIdImg).removeClass("hide")
        .addClass("visible");
        
    });

	//tabs
	var tabs = $(".tabs");
	if (tabs.length > 0){
		//init
		var contentTabs = $(".contentTab", tabs);
		if(contentTabs.length > 0){
				// Hide all content, not the first
				contentTabs.each(function(i){
						if(i != 0) {
							$(this).hide();
						}
				});
				// Manage tabs click
				var tabA = $("li.tab a", tabs);
				if(tabA.length > 0){
						$(tabA).click(function(){
							var href = $(this).attr('href');
							var target = href.substr(href.indexOf('#', 0)+1);
							// Hide all h2
							$("li.tab h2", tabs).each(function(i){
								$(this).removeClass("active");
							});
							// Active this h2
							$(this).parent().addClass("active");
							// Display tabs content
							$("div.contentTab", tabs).each(function(){
								if($(this).attr("id") != target) {
									$(this).hide();
								} else {
									$(this).show();
								}
							});
							return false;
						});
				}
		}
    }
    
	// Date picker
	$('input.cal').datepicker({
		showOn: 'button',
		buttonImage:'/img/main/calendrier-mini.gif',
		buttonImageOnly:true,
		buttonText:lg_choose_date,
		firstDay:1,
		beforeShow: function(input, inst) {
			if (!input.value) {
				if (input.name == 'DATE_FIN') {
					var inputStart = $(input).parents('form').find('input[name="DATE_DEBUT"]');
				} else if (input.name == 'DISPO_DATE_END') {
					var inputStart = $(input).parents('form').find('input[name="DISPO_DATE_START"]');
				}
				if (inputStart && inputStart[0] && inputStart[0].value && inputStart[0].title) {
					return {'defaultDate':$.datepicker.parseDate( inputStart[0].title.replace('yyyy', 'yy'), inputStart[0].value, {} )}
				}
			}
		}
	});

	// Colorbox
	if(typeof($.fn.colorbox) == 'function') {
		$("a[rel='diapoGrp'], a.popin").colorbox({scalePhotos:true,maxHeight:'90%',maxWidth:'90%',slideshow:true,rel:'',onOpen:bodyOFOn,onClosed:bodyOFOff});
		$("a.popinVideo").colorbox({scalePhotos:true,maxHeight:'90%',maxWidth:'90%',slideshow:true,rel:'',iframe:true,onOpen:bodyOFOn,onClosed:bodyOFOff, innerWidth:425, innerHeight:344,slideshowAuto:false});
		$("a.popinVideoGrandSite").colorbox({scalePhotos:true,maxHeight:'90%',maxWidth:'90%',slideshow:true,rel:'',iframe:true,onOpen:bodyOFOn,onClosed:bodyOFOff, innerWidth:800, innerHeight:481,slideshowAuto:false});
		$("a.popinVideoGrandSiteOpen").colorbox({scalePhotos:true,maxHeight:'90%',maxWidth:'90%',slideshow:true,rel:'',iframe:true,onOpen:bodyOFOn,onClosed:bodyOFOff, innerWidth:800, innerHeight:481,slideshowAuto:false,open:true});
		$("a.popinImg").colorbox({scalePhotos:true,maxHeight:'90%',maxWidth:'90%',slideshow:true,rel:'',onOpen:bodyOFOn,onClosed:bodyOFOff,slideshowAuto:false});
		$("a.voeux2012").colorbox({scalePhotos:true,maxHeight:'90%',maxWidth:'90%',slideshow:true,rel:'',iframe:true,onOpen:bodyOFOn,onClosed:bodyOFOff, innerWidth:1016, innerHeight:648,slideshowAuto:false});

	}
	
	if(typeof($.InFieldLabels) == 'function') {
		$("label.searchlabel").inFieldLabels();
	}
});

//before after ie
;(function($){
	var patterns = {
		text: /^['"]?(.+?)["']?$/,
		url: /^url\(["']?(.+?)['"]?\)$/
	};
	function clean(content) {
		if(content && content.length) {
			var text = content.match(patterns.text)[1],
					url = text.match(patterns.url);
			return url ? '<img src="' + url[1] + '" />': text;
		}
	}
	function inject(prop, elem, content) {
		if(prop != 'after') prop = 'before';
		if(content = clean(elem.currentStyle[prop])) {
			$(elem)[prop == 'before' ? 'prepend' : 'append'](
				$(document.createElement('span')).addClass(prop).html(content)
			);
		}
	}
	$.pseudo = function(elem) {
		inject('before', elem);
		inject('after', elem);
		elem.runtimeStyle.behavior = null;
	};
	if(document.createStyleSheet) {
		var o = document.createStyleSheet();
		o.addRule('.dummy','display: static;');
		o.cssText = 'html, head, head *, body, *.before, *.after, *.before *, *.after * { behavior: none; } * { behavior: expression($.pseudo(this)); }';
	}
	
})(jQuery);

