var msie=document.all;
var mozi=document.getElementById&&!document.all;

String.prototype.rtrim = function (c) {
	if (this.length > 0 && this[this.length - 1] == c) {
		v = this.substring(0, this.length - 1);
	} else {
		v = this;
	}
	return v;
}

function afficherFlashInfo() {
	var $fiCaches = $('.flash-info:hidden');
	var n = $fiCaches.length;
	var r = Math.floor(Math.random() * n);
	var $fiAff = $('.flash-info:visible');
	if (!$fiAff.length) {
		$fiCaches.eq(r).fadeIn('slow');
	} else {
		$fiAff.fadeOut('slow', function() { $fiCaches.eq(r).fadeIn('slow'); });
	}
	if ($fiAff.length + n > 1) {
		setTimeout(afficherFlashInfo, 10000);
	}
}

function afficherSommairePageDetail() {
	var pageId = $(this).attr("pageId");
	$(".pageDetail").fadeOut("fast");
	$("#pageDetail_" + pageId).fadeIn("fast");
	$(".sommaire").find(".lienDetail").removeClass("lienPageDetailOn");
	$("#lienPageDetail_" + pageId).addClass("lienPageDetailOn");
	return false;
}

function cacherMusconfPreview() {
	$('div.musconfPreview:visible').each(function(i, e) {
		var id = $(e).find('object:first').attr('id');
		$(e).hide().find('object:first').replaceWith('<div id="' + id + '">Preview</div>');
	});
}

function lancerRecherche() {
	document.forms["formRechercheSite"].submit();
}

$(document).ready(function(){
	$(".menu1On,.menu1Off").mouseover(function() {
		$(this).find(".md:first").css("display", "block");
	}).mouseout(function() {
		$(this).find(".md:first").css("display", "none");
	});
	$(".md").find("a:first").css("paddingTop", "0");
	$(".md").find("a:last").css("border", "none");
	$('.rb1').find('.margeD:last').find('.sep2').hide();
	$('.rb1').find('ul > li:last').css('background-image', 'none');
	
	// init lien pages sommaire
	//~ $(".sommaire").find(".lienDetail").find("a:first").click(afficherSommairePageDetail);
	$(".lienPageDetailPrec").each(function(i,e) {
		var pagePrecId = $(e).parents(".pageDetail:first").prev().attr("pageId");
		if (pagePrecId != undefined) {
			//~ $(e).find("a:first").attr("pageId", pagePrecId).click(afficherSommairePageDetail);
		} else {
			//~ $(e).css("opacity","0.1").find("a").mouseover(function() {return false;});
		}
	});
	$(".lienPageDetailSuiv").each(function(i,e) {
		var pageSuivId = $(e).parents(".pageDetail:first").next().attr("pageId");
		if (pageSuivId != undefined) {
			//~ $(e).find("a:first").attr("pageId", pageSuivId).click(afficherSommairePageDetail);
		} else {
			//~ $(e).css("opacity","0.1").find("a").attr("href", "");
		}
	});
	
	$(".btnDeroule").click(function() {
		$(this).parents(".lienDetail:first").hide();
		$(this).parents(".deroule:first").find(".detail:first").show();
		return false;
	});
	
	$(".btnEnroule").click(function() {
		$(this).parents(".detail:first").hide();
		$(this).parents(".deroule:first").find(".lienDetail:first").show();
		return false;
	});
	
	$(".menu2Prive").tooltip();
	
	var lienVoirConfs = $('#corpsC').find('div.lienVoirConfs:first').html();
	lienVoirConfs += '<p class="notice" style="color:#555;padding-top:6px;">&#133;&nbsp;et profitez d&#39;une restitution pleine écran en qualité optimale</p>';
	$('div.musconfPreview').not(':last').append('<div class="lienDetail" style="padding-top:12px;text-align:center;">' + lienVoirConfs + '</div>');
	$('a.musconfPreview').click(function() {
		cacherMusconfPreview();
		var musconfId = $(this).attr('musconfId');
		var preview = $(this).attr('preview');
		var idCible = 'musconfPreview_' + musconfId;
		$('#' + idCible).parents('div.musconfPreview:first').show();
		var flashvars = {};
		flashvars.file = g_appliUrlDocuments + 'musconf/' + musconfId + '/' + preview;
		flashvars.autostart = "true";
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		swfobject.embedSWF(g_webroot + 'flash/flvplayer.swf', idCible, '320', '250', '9.0.0', g_webroot + 'flash/expressInstall.swf', flashvars, params, attributes);
		return false;
	});
	
	$('a.clicStat').mousedown(function() {
		var params = {};
		params.cat = $(this).attr('cat');
		params.itemId = $(this).attr('itemId');
		$.post(	g_webroot + "ajax_clicStat.php", params);
		return true;
	});
	
	$('a.clicStatRub').mousedown(function() {
		var params = {};
		params.cat = $(this).attr('cat');
		params.itemId = $(this).attr('itemId');
		$.post(	g_webroot + "ajax_clicStatRub.php", params);
		return true;
	});

	$('div.wysiwyg').find('img[align=left]').css('margin', '0 10px 6px 0');
	$('div.wysiwyg').find('img').filter(function(){return $(this).css('float')=='left';}).css('margin', '0 10px 6px 0').css('clear','left');
	$('div.wysiwyg').find('img[align=right]').css('margin', '0 0 10px 10px');
	$('div.wysiwyg').find('img').filter(function(){return $(this).css('float')=='right';}).css('margin', '0 0 6px 10px').css('clear','right');
	$('div.wysiwyg').find('img').filter(function(){return !$(this).css('float') && !$(this).attr('align');}).css('margin', '10px 6px');
	$('div.wysiwyg').find('img[longdesc]').each(function(i,e) {
		var legende = '<p class="legende">' + $(e).attr('longdesc') + '</p>';
		$(e).wrap('<div class="imgAvecLegende"></div>')
			.parents('div.imgAvecLegende:first').css('float', $(e).css('float')).css('margin', $(e).css('margin')).css('clear', $(e).css('clear'))
			.append(legende)
			.find('img').css('float', 'none').css('margin', '0');
	});
	
	afficherFlashInfo();
});

