/*
 * Require jQuery JavaScript Library v1.3.2+
 * http://jquery.com/
 *
 * Copyright © 2011 MBA Multimédia (www.mba-multimedia.com)
 * 
 *
 */
$.tools.dateinput.localize("fr", {
	months: 'janvier,f&eacute;vrier,mars,avril,mai,juin,juillet,ao&ucirc;t,septembre,octobre,novembre,d&eacute;cembre',
	shortMonths: 'jan,f&eacute;v,mar,avr,mai,jun,jul,ao&ucirc;,sep,oct,nov,d&eacute;c',
	days: 'dimanche,lundi,mardi,mercredi,jeudi,vendredi,samedi',
	shortDays: 'dim,lun,mar,mer,jeu,ven,sam'
});

$(function () {
	// PARAGRAPHES
	if ($('.paragraphes').length) {
		$('.fiche .paragraphes').paragraphe({/*hmin:0*/});
		$('.col-d .paragraphes').paragraphe({hmin:50});
	}

	// RETOUR HAUT DE PAGE
	$('#btn-hdp a').attr('href', '').click(function () {
		$('html').stop().scrollTo(0, 500);
		return false;
	});

	// MENU
	$('#header .menuprim-ctn').css({
		height: 0,
		display: "block"
	});
	$('#header .menuprim').hover(openMenu, closeMenu);

	// LISTINGS
	if ($('.element').length) {
		$('.element').hover(cadreOn, cadreOff);
		$('.element-ctn').click(cadreClick);
	}
	
	// CARROUSEL
	//console.log ($(".carrousel .items li").length);			
	if($(".carrousel .items li").length >1) {
		$(".carrousel .scrollable").scrollable({circular:true,mousewheel:true}).navigator().autoscroll({interval:3000});
	}
	if($("#aujourdhui .items li").length >1) {
		$("#aujourdhui .scrollable").scrollable({circular:true,mousewheel:true}).navigator().autoscroll({interval:3000});
	}

	//NEWSLETTER
	if ($("#newsletter").length) {
		$("#newsletter form").jForm ();
	}

	// ONGLETS
	if ($('.tabs').length) {
		$("ul.tabs").tabs("div.panes > div");
	}

	if ($(".prospero.srub_e00").length) {
		//console.log("prospero e00");
	} else {
		// PHOTOTHEQUE
		if ($('#phototheque').length) {
			$("#phototheque .scrollable").scrollable({
				circular: true,
				mousewheel: true
			}).autoscroll({
				interval: 10000
			});
		}

		// OVERLAY
		if ($('#phototheque a[rel]').length) {
			//console.log("a[rel] > overlay");
			$('#phototheque a[rel]').overlay({
				mask: {
					color: '#fff',
					loadSpeed: 200,
					opacity: 0.8
				},

				closeOnClick: true
			});
		}
	}

	// FILTRE LISTING
	if ($('#filtre').length) {
		$('#filtre select').change(function () {
			this.form.submit();
		});
	}

	// LOAD MORE
	if ($(".elements").length) {
		$('.load_more').live("click", function () {

			var href = $(this).attr("href").substring(1);
			var lm = this;

			$.ajax({
				url: "/fr/2011/include/elements.php",
				data: "ajax=1&" + href,
				beforeSend: function () {
					$(lm).parent().append('<img src="/2011/media/gabarit/loader.gif" />');
					$(lm).fadeOut(1000);
				},
				success: function (html) {

					$("#btn-plus-elements").remove();
					$("ul.elements").append(html);
					if ($('.element').length) {
						$('.element').fadeIn('1000');
						$('.element').hover(cadreOn, cadreOff);
						$('.element-ctn').click(cadreClick);
					}
				},
				error: function (jqXHR, textStatus, errorThrown) {
					$(lm).fadeIn(1000);
				}
			});
			$('.elements').ajaxStop(function () {
				$('html').stop().scrollTo($("li.element:last"), 1000);
			})
			return false;

		});
	}

	// SCROLL
	if ($("#scroll").length) {
		autoScroller('#scroll .scroll-ctn', 40);
	}

});

function openMenu() {
	$('#header .menuprim-ctn').stop().animate({
		height: 137
	}, 200);
}

function closeMenu() {
	$('#header .menuprim-ctn').stop().animate({
		height: 0
	}, 400);
}

function cadreOn() {
	$(this).addClass('on');
}

function cadreOff() {
	$(this).removeClass('on');
}

function cadreClick() {
	window.open($(this).find('a').attr('href'), '_top');
}

(function($) {

	$.fn.paragraphe = function(options) {
			
		if (testNavigateur()) {
			if(!($("#paragraphe-css").length)){
				var style = '<style id="paragraphe-css" type="text/css"><!--\n';
				style += '.paragraphes .paragraphe-ttr { cursor:pointer; }\n';
				//style += '.paragraphes .o .paragraphe-ctn { }\n';
				//style += '.paragraphes .f .paragraphe-ctn { height:0; }\n';
				style += '.paragraphes .o.auto .paragraphe-ctn { display:block; }\n';
				style += '.paragraphes .f.auto .paragraphe-ctn { display:none;}\n';
				style += '.paragraphes .o .btn-moins { display:block; }\n';
				style += '.paragraphes .f .btn-plus { display:block; }\n';
				style += '--></style>\n';
		
				$('head').append(style);
		
				$('.col-d .paragraphe-ttr').hover(cadreOn, cadreOff);
			}
	
			this.find('.paragraphe').each(function () {
				//console.log ("paragraphe");
				var e = $(this);
				if(options.hmax!=null || options.hmin!=null) {
					//console.log ("para not null");
					var hmax = (options.hmax!=null) ? options.hmax : e.find('.paragraphe-ctn').height();
					var hmin = (options.hmin!=null) ? options.hmin : 0;
				} else {
					//console.log ("para null");
					var hmax = "auto";
					var hmin = "auto"; 
					e.addClass("auto");
				}
	
				// État initial
				if (e.is(".o")) {
					openParagraphe(e,hmin,hmax);
				} else if (e.is(".f")) {
					if (hmin == "auto") {
						$(this).find('.paragraphe-ctn').hide();
					} else {
						e.find('.paragraphe-ctn').css({height:hmin});
					}
					closeParagraphe(e,hmin);
				}
	
				// Clic sur titre paragraphe
				$(this).find('.paragraphe-ttr').hover(cadreOn, cadreOff).click(function () {
					openCloseParagraphe(e,hmin,hmax);
				});
	
				// Clic sur bouton ouvrir
				$(this).find('.btn-plus').click(function () {
					openParagraphe(e,hmax);
					return false;
				});
	
				// Clic sur bouton fermer
				$(this).find('.btn-moins').click(function () {
					closeParagraphe(e,hmin);
					return false;
				});
	
			});
		} else {
			// on laisse tout ouvert...
		}

		function openCloseParagraphe(e,hmin,hmax) {
			// Si ni la classe CSS "f" ni la classe CSS "o" n'est spécifiée, le paragraphe doit rester ouvert.
			if (e.is(".o") || e.is(".f")) {
				if ((hmin == "auto" && e.find('.paragraphe-ctn').is(':hidden')) || (hmin != "auto" && e.find('.paragraphe-ctn').height()<=hmin)) {
					openParagraphe(e,hmax);
				} else {
					closeParagraphe(e,hmin);
				}
			}
		}
		
		function openParagraphe(e,h) {
			// masquer le bouton ouvrir
			e.find('.btn-plus').hide(200);
		
			// afficher le bouton fermer
			e.find('.btn-moins').show(200);
		
			// Ouvrir le calque
			if (h == "auto") {
				e.find('.paragraphe-ctn').slideDown('fast');
			} else {
				e.find('.paragraphe-ctn').animate({height:h}, 200);
			}
		}
		
		function closeParagraphe(e,h) {
			// afficher le bouton ouvrir
			e.find('.btn-plus').show(400);
		
			// masquer le bouton fermer
			e.find('.btn-moins').hide(400);
		
			// Fermer le calque
			if (h == "auto") {
				e.find('.paragraphe-ctn').slideUp();
			} else {
				e.find('.paragraphe-ctn').animate({height:h}, 400); 
			}
		}
		
		function testNavigateur() {
			// Désactiver le javascript pour Internet Explorer 6 et plus vieux
			return !(jQuery.browser.msie && jQuery.browser.version.substr(0, 1) < 7);
		}
	}
})(jQuery);
