//
// JQuery Addon for IPB 1.3.1 
// 
// @author: kio
//

$(document).ready(function() {

	// Parcours les img et réduit celles qui sont trop large
	$(".postcolor img").each(function() {
		var e = $(this);
		if(e.width() > 590) {
			//alert( e.width() );			
			e.addClass("resize");
			e.attr({title: "Agrandir l'image"});
		}
	});
	
	
	// Replace dailymotion link
	$(".postcolor a[href*=dailymotion]").each(function() {
		var e = $(this);
		var url = e.attr("href");
		var res = url.match("video\/(.*)");
		var vid = res[1];
		var txt = '<object width="560" height="336"><param name="movie" value="http://www.dailymotion.com/swf/'+vid+'&related=1"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/'+vid+'&related=1" type="application/x-shockwave-flash" width="560" height="336" allowFullScreen="true" allowScriptAccess="always"></embed></object>';
		e.replaceWith(txt);
	});

	// Replace youtube link
	$(".postcolor a[href*=youtube]").each(function() {
		var e = $(this);
		var url = e.attr("href");
		var res = url.match("[\\?&]v=([^&#]*)");
		var vid = res[1];
		var txt = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v'+vid+'&hl=fr&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+vid+'&hl=fr&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
		e.replaceWith(txt);
	});


	// Hide all spoiler 
	$("td.spoilermain").each(function() {
		$(this).hide();
		$(this).parent().prev().children("td.spoilertop").append(' <button>Voir</button>');
	});

	// Show spoiler on click
	$("td.spoilertop").click( function() {
		var sp = $(this).parent().next().children("td.spoilermain");
		sp.toggleClass("spoiler-visible");
		sp.toggle();
		var t = "";
		if( sp.hasClass("spoiler-visible") ) { t="Cacher";}
		else { t="Voir"; }
		$(this).children("button").text(t);
	});



	// Event click sur img
	$("img.resize").click( function() {
		var e = $(this);
		e.toggleClass("resize");
		e.toggleClass("full");
		
		if( e.hasClass("resize") ){
			e.attr({title: "Agrandir l'image"});
			e.next("div.vide").remove();
		}
		if( e.hasClass("full") ){
			e.attr({title: "Réduire l'image"});
			e.after('<div class="vide"></div>');
			var h = e.height();
			e.next("div.vide").css("height", h);    
		}
	});

})