/**********************************************************
              Déclaration de fonctions
***********************************************************/

function initCalendrierNav(i) {
	var calendrier = $(this);
	$('caption a', calendrier).click(function() {
		calendrier.addClass('loading');
		var href = this.href.replace(/&amp;/g, '&');
		var hrefBase = href.replace(/\?.*$/, '');
		var queryString = href.replace(/^[^?]*\?/, '').split('&');
		var qsObject = {};
		$.each(queryString, function(key, val) {
			val = val.split('=');
			qsObject[val[0]] = val[1];
		});
		qsObject.page = 'calendrier';
		$.get(hrefBase, qsObject, function(data) {
			data = $(data);
			$(calendrier).hide();
			$(calendrier).after(data);
			$(calendrier).remove();
			$('.calendrier').each(initCalendrierNav);
		});
		return false;
	});
}

function handleTargetBlank() {
	$('a.targBlank').click(function() { window.open( $(this).attr('href') ,'',''); return false; });
}

/**/
function handleVideoPopup() {
	$('a.popup_video').click(function() {
		window.open('?page=popup_video&file='+this.href, 'popup_video', 'width=384,height=286');
		return false;
	});
}
/**/
/*/
function handleVideoPopin() {
	$('a.popup_video')
		.popinTrigger(
			 function() { return this.clone(); }
			,{
				 css				: {
										background		: 'black'
										,'padding'	: '10px'
									}
				,waitBeforeShowingPopin	: false
				,cssPreShow			: {
										opacity		: 0
									}
				,skin				: 'closeBtn'
				,preShow			: [function() {
										this.size({width: 384, height: 285});
									}, 1]
				,postShow			: [function() {
										var continuer = arguments.callee.continuer;
										this.content().find('.popup_video').mediaCatchup({
											callback: function() {
												continuer();
											}
										});
										return false;
									}, 1]
			}
		);
}
var handleVideoPopup = handleVideoPopin;
/**/

function gestion_bookmark() {
	$('#bookmarking .bookmark a').click(function() {
		var href = this.href;
		var loc = window.location.toString();
		if(loc.match(/^.*#/))
			var ancre = window.location.toString().replace(/^.*#/, '#');
		else
			var ancre = '';

		href = href.replace(/(&url=[^&]*)&/, '$1'+encodeURIComponent(ancre)+'&');
		href = href.replace(/(&title=)[^&]*&/, '$1'+encodeURIComponent(document.title)+'&');
		window.open(href, 'AddThis', 'scrollbars=yes,menubar=no,width=620,height=520,resizable=yes,toolbar=no,location=no,status=no,screenX=200,screenY=100,left=200,top=100');
		return false;
	});
}
function gestion_envoi_ami() {
	$('#bookmarking .envoi_ami a').click(function() {
		var lien = this.href;
		var ancre = window.location.toString().replace(/^.*#/, '#');
		window.open(lien+ancre, 'envoi_ami', 'width=455,height=400,toolbar=no,status=no,menubar=no,location=no,directories=no,resizable=yes');
		return false;
	});
}

function ie_corrections() {
	/*if($('link[@href$=mep_nf.css]').length && $('#contenu_principal').height() <= 700)
		$('#contenu_principal').height(700);
	if($('#page').height() <= 850)
		$('#page').height(850);*/
}


/**********************************************************
                     Instructions
***********************************************************/
$(function() {
	$('.calendrier').each(initCalendrierNav);
	if($.browser.msie) {
		ie_corrections();
	}
	handleTargetBlank();
	handleVideoPopup();
	gestion_bookmark();
	gestion_envoi_ami();
});

if($('#erreur').length) {
	$('#erreur').remove().popin();
} else {
	$(function() {
		if($('#erreur').length) {
			$('#erreur').remove().popin();
		}
	});
}

