var g_diapoCursor = 0;

$(document).ready(function () {
	// - "Description" text
	$(".bttn_desc").html("Description");

	//Colorbox et colorbox type bttn_echantillon
	$(".colorbox").colorbox({maxHeight: "90%", maxWidth: "90%", initialWidth: "500px", initialHeight: "500px"}, function() {
		//Images galeries
		$(".ajaxColorbox").live('click', function() {
			$(".ajaxColorbox").css("display", "none").appendTo('body');
			//$(".ajaxColorbox").appendTo('body');
		});
		$(".ajaxColorbox").colorbox({maxHeight: "85%", maxWidth: "90%", photo: true, slideshow: true, slideshowAuto: false});
	});

	//Supprime les images ajaxColorbox (échantillon) du DOM
	$().bind('cbox_closed', function() {
		$(".ajaxColorbox").remove();
	});

	$(".bttn_echantillon").click(function() {
		$(this).css('background-color', '');
		$(".bttn_echantillon_selected").removeClass("bttn_echantillon_selected");
		$(this).addClass("bttn_echantillon_selected");
	});

	//Animation couleur
	/* Ne passe pas sous IE
	 * $(".bttn_echantillon").animate({ backgroundColor : 'blue'}, 700).animate({ backgroundColor : 'red'}, 700).animate({ backgroundColor : 'orange'}, 700)
	 * .animate({ backgroundColor : 'rgb(45, 214, 242)'}, 700);
	 */

	//Centrage vertical des screenshots sur page News
	$(".blocNews img.screenshot, .blocCategorie img.screenshot").each(function() { updateScreenshotMargin(this); });
	$(".blocNews img.screenshot, .blocCategorie img.screenshot").load(function() { updateScreenshotMargin(this); });

	//Lien de sortie
	$("a.link_out").attr("target", "_blank");
	$("a.link_out").click(function() {
		var href = $(this).attr("href");
		href = encodeURIComponent(href);
		//alert(href);
		$.post("out.php", {url : href}, function(data) {
			//alert(data);
		}, "html");
		return true;
	});

	//Clic général sur le nom d'une catégorie
	$(".one_categorie").click(function() {
		var lien = $(this).find("a");
		location.href = lien.attr("href");
	});

	//Animation menu haut
	$("#menu ul li a.active").each(function() {
		var pWidth = $(this).width();
		var pLeft = $(this).position().left;
		$("#menu #hovereffect").css({display: "block"}).animate({width: pWidth, left: pLeft}, 500);
	});

	$("#menu ul li a").hover(function() {
		var hoverBar = $("#menu #hovereffect");
		hoverBar.stop(true);

		var pWidth = $(this).width();
		var pLeft = $(this).position().left;
		
		hoverBar.animate({width: pWidth, left: pLeft}, 500, "swing",
			function() {
				var active = $("#menu ul li a.active");
				var pLeftActive = active.position().left;
				var pWidthActive = active.width();
				//$(this).animate({backgroundColor: "white"}, 3000).animate({left: pLeftActive, width: pWidthActive}, 500);
				$(this).animate({opacity: $(this).css('opacity')}, 3000).animate({left: pLeftActive, width: pWidthActive}, 500);
			}
		);
	}, function() {
		
	});

	// Liste des catégories diapo
	$("ul#diapoCategories").each(function() {
		var i, object = $(this);
		// - hide li's
		object.children("li:not(.diapoButton):gt(5)").hide("fast", function() {  });
		
		object.slideDown("slow", function(){
			//updateDiapo(object, 'next');
		});
	});

	$("#diapoNext").click(function() { updateDiapo($(this).parent("ul"), 'next'); });
	$("#diapoPrevious").click(function() { updateDiapo($(this).parent("ul"), 'previous'); });

});

//Ajout Favoris
$(window).ready(function () {
	$("#add_favorite").load(function() {
		var siteName = $("#logoSite").attr("alt");
		if($.browser.mozilla) {
			$(this).click(function() {window.sidebar.addPanel(siteName, document.URL, "");});
		}
		else if($.browser.msie) {
			$(this).click(function() { window.external.AddFavorite(document.URL, siteName);});
		}
		else {
			//alert("Navigateur non supporté :(");
			$(this).hide();
		}
	});
});

function updateScreenshotMargin(domElement) {
	var height = $(domElement).height();
	var parent = ($(domElement).parents(".blocNews").length > 0) ? $(domElement).parents(".blocNews") : $(domElement).parents(".blocCategorie");
	$(domElement).css("margin-top", (parent.height() - height - parent.css("padding-top").replace('px', '')) / 2 );
}

function updateDiapo(objet, action) {
	if(objet != null) {
		objet.children("li:not(.diapoButton):visible").fadeOut("normal", function() {
			if(objet.children("li:not(.diapoButton):visible").length == 0) {
				var total = objet.children("li:not(.diapoButton)").length;
				
				if(action == 'next') {
					g_diapoCursor = g_diapoCursor+5;
				}
				else if(action == 'previous') {
					g_diapoCursor = g_diapoCursor-5;
				}

				if(g_diapoCursor < 0) { g_diapoCursor = total + g_diapoCursor; }
				else if(g_diapoCursor >= total) { g_diapoCursor = g_diapoCursor - total; }
				
				if( (g_diapoCursor) == 0) {
					objet.children("li:not(.diapoButton):lt(5)").show();
				}
				else if(g_diapoCursor+4 >= total) {
					objet.children("li:not(.diapoButton):gt("+(g_diapoCursor-1)+"):lt("+(total - 1)+")").show();

					if( ((g_diapoCursor+4) - total) == 0) {	objet.children("li:not(.diapoButton):eq(0)").show(); }
					else { objet.children("li:not(.diapoButton):lt("+( (g_diapoCursor+4) - total + 1)+")").show(); }
				}
				else {
					objet.children("li:not(.diapoButton):gt("+(g_diapoCursor-1)+"):lt("+(g_diapoCursor+4)+")").show();
				}
			}
		});
	}
}