/*
 * 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});
	}
	
	// SOMMAIRE
	if($('.sommaire').length) {
		$('.sommaire li a').each(function(){
			var o = parse_url ($(this).attr('href'));
			$(this).click(function () {
				$('html').stop().scrollTo($('#'+o.fragment), 200, function(){ 
					$('#'+o.fragment).find('.btn-plus').trigger('click');
					//
				});
				return false;
			});
		});
	}
	
	// RETOUR HAUT DE PAGE
	$('#btn-hdp a').attr('href', '').click(function () {
		$('html').stop().scrollTo(0, 500);
		return false;
	});

	// MENU
	$('#menu-panes').css({ height:0, display:"block" });
	$('#header .menuprim').hover(openMenu, closeMenu);
	$("#menu-tabs").tabs("#menu-panes .lvl-0", {event:'mouseover', initialIndex:null, onClick:openMenu });

	
	// LISTINGS
	if ($('.element').length) {
		$('.element').hover(cadreOn, cadreOff);
		$('.element-ctn').click(cadreClick);
	}	
	
	// CARROUSEL
	if($(".carrousel .items li").length >1) {
		$(".carrousel .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");
	}
	
	// OVERLAY
	/*if ($('a[rel]').length){
		$('a[rel]').overlay({
			mask: {
				color: '#fff',
				loadSpeed: 200,
				opacity:0.8
			},
			//top:'2%',
			closeOnClick:true
		});
	}*/
	
	// FILTRE LISTING
	if($('#filtre').length){
		$('#filtre select').change(function(){ this.form.submit(); });
	}
	
	// REFLECTION
	//$("img.reflect").reflect({});
	
}); 

function openMenu() {
	var h = $("#menu-panes ul").outerHeight();
	$('#menu-panes').stop().animate({
		height: h
	}, 200);
}

function closeMenu() {
	$('#menu-panes').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 .paragraphe-ctn { display:block; }\n';
				style += '.paragraphes .f .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);
				var hmax = (options.hmax!=null) ? options.hmax : e.find('.paragraphe-ctn').height();
				var hmin = (options.hmin!=null) ? options.hmin : 0;
			
				// État initial
				if (e.is(".o")) {
					openParagraphe(e,hmin,hmax);
				} else if (e.is(".f")) {
					e.find('.paragraphe-ctn').hide();
					//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;
				});
				
			});
			
			// console.log(window.location.hash);
			if ($(window.location.hash).length){
				$('html').stop().scrollTo($(window.location.hash), 500);
				$(window.location.hash).find('.btn-plus').trigger('click');
			}
	} 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 (e.find('.paragraphe-ctn').is(':hidden')){
				//if (e.find('.paragraphe-ctn').height()<=hmin) {
					openParagraphe(e,hmax);
				} else {
					closeParagraphe(e,hmin);
				}
			}		
		}

		function openParagraphe(e,h) {
			//console.log ("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
			e.find('.paragraphe-ctn').slideDown('fast', function(){$('html').stop().delay(200).scrollTo($(e), 200);});
			//e.find('.paragraphe-ctn').animate({height:h}, 200);
		}

		function closeParagraphe(e,h) {
			//console.log ("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
			e.find('.paragraphe-ctn').slideUp();
			//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);

function parse_url(str, component) {
	// http://kevin.vanzonneveld.net
	// +      original by: Steven Levithan (http://blog.stevenlevithan.com)
	// + reimplemented by: Brett Zamir (http://brett-zamir.me)
	// + input by: Lorenzo Pisani
	// + input by: Tony
	// + improved by: Brett Zamir (http://brett-zamir.me)
	// %          note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
	// %          note: blog post at http://blog.stevenlevithan.com/archives/parseuri
	// %          note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
	// %          note: Does not replace invalid characters with '_' as in PHP, nor does it return false with
	// %          note: a seriously malformed URL.
	// %          note: Besides function name, is essentially the same as parseUri as well as our allowing
	// %          note: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
	// *     example 1: parse_url('http://username:password@hostname/path?arg=value#anchor');
	// *     returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'}
	var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'fragment'],
		ini = (this.php_js && this.php_js.ini) || {},
		mode = (ini['phpjs.parse_url.mode'] && ini['phpjs.parse_url.mode'].local_value) || 'php',
		parser = {
			php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
			strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
			loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this)
		};
	var m = parser[mode].exec(str),
		uri = {},
		i = 14;
	while (i--) {
		if (m[i]) {
			uri[key[i]] = m[i];
		}
	}
	if (component) {
		return uri[component.replace('PHP_URL_', '').toLowerCase()];
	}
	if (mode !== 'php') {
		var name = (ini['phpjs.parse_url.queryKey'] && ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey';
		parser = /(?:^|&)([^&=]*)=?([^&]*)/g;
		uri[name] = {};
		uri[key[12]].replace(parser, function ($0, $1, $2) {
			if ($1) {
				uri[name][$1] = $2;
			}
		});
	}
	delete uri.source;
	return uri;
}
