function inicializarPlayers(visible, playlist) {
	attachHoverLabels();
	attachClickLabels();
	attachVisibilidadPlayers();
	jcarouselPlayer(visible);
	embedFlowplayer(playlist);
	setVisibility(visible);
}
function attachHoverLabels() {
	$(".player .labels .label").hover(function(){
		var $this = $(this);
		$this.dequeue();
		$this.animate({"margin-left":0}, 100, "easeInOutQuad");
	},function(){
		var $this = $(this);
		if(!$this.hasClass("active")) {
			$this.dequeue();
			$this.animate({"margin-left":15}, 100, "easeInOutQuad");
		}
	});
}
function attachClickLabels() {
	$(".player .labels .label").each(function(i){ attachClickLabel($(this), i); });
}
function attachClickLabel($label, index) {
	$label.click(function() {
		if(!$label.hasClass("active")) {
			$label.siblings(".active").removeClass("active").mouseout();
			$label.addClass("active");
			var $bodies = $label.parents(".player").eq(0).find(".viewer .screen .player_body");
			$bodies.filter(":not(.hide)").fadeOut(500, function(){
				$(this).addClass("hide");
				$bodies.eq(index).removeClass("hide").fadeIn(500);
			});
		}
		return false;
	});
}
function attachVisibilidadPlayers() {
	attachOcultarPlayers();
	attachMostrarPlayers();
}
function attachOcultarPlayers() {
	$(".player.visible .hide_player").unbind("click");
	$(".player.visible").each(function() { attachOcultarPlayer($(this)); });
}
function attachMostrarPlayers() {
	$(".player.hidden .hide_player").unbind("click");
	$(".player.hidden").each(function() { attachMostrarPlayer($(this)); });
}
function attachOcultarPlayer($player) {
	$button = $(".hide_player", $player);
	$button.unbind("click");
	$button.click(function(){ ocultarPlayer($player); return false; });
}
function attachMostrarPlayer($player) {
	$button = $(".hide_player", $player);
	$button.unbind("click");
	$button.click(function(){ mostrarPlayer($player); return false; });
}
function ocultarPlayer($player) {
	$player.dequeue();
	$player.animate({top:-320}, 500, "easeOutBack", function(){
		$player.addClass("hidden").removeClass("visible");
		$(".controls a.play:not(.stop)", $player).click();
		attachMostrarPlayer($player);
	});
}
function mostrarPlayer($player) {
	$player.dequeue();
	$player.animate({top:0}, 500, "easeOutBack", function(){
		$player.addClass("visible").removeClass("hidden");
		attachOcultarPlayer($player);
	});
}
function stopCarousel(carousel, $button) {
	carousel.stopAuto();
	$button.addClass("stop");
}
function startCarousel(carousel, $button) {
	carousel.startAuto();
	$button.removeClass("stop");
}
function jcarouselPlayer(visible) {
	var callback = function(carousel) {
		var $controles = $("#image_gallery_controls");
		$controles.addClass("jcarousel-controls");
		$controles.find("img").addClass("jcarousel-control");
		$controles.find("img").bind("click", function() {
			carousel.scroll(jQuery.jcarousel.intval(this.alt));
			var index = this.alt-1;
			$("#image_gallery_controls").find("li.controlSelected").removeClass("controlSelected");
			$("#image_gallery_controls").find("li:not(.separador)").eq(index).addClass("controlSelected");
			carousel.stopAuto();
			carousel.startAuto();
			return false;
		});
		$("#image_gallery_controls").hover(function(){
			$(this).animate({opacity:1}, 200);
			$(this).dequeue();
		},function(){
			$(this).animate({opacity: 0.5}, 200);
			$(this).dequeue();
		});
		$("#carousel_buttons a.play").click(function(){
			if($(this).hasClass("stop")) {
				startCarousel(carousel, $(this));
			}
			else {
				stopCarousel(carousel, $(this));
			}
			return false;
		});
		carousel.buttonNext = $("#carousel_buttons a.next");
		carousel.buttonPrev = $("#carousel_buttons a.prev");
		carousel.buttonNext.click(function(){ $("#carousel_buttons a.play").removeClass("stop"); return false; });
		carousel.buttonPrev.click(function(){ $("#carousel_buttons a.play").removeClass("stop"); return false; });
		if(!visible) {
			//stopCarousel(carousel, $("#carousel_buttons a.play"));
		}
	};
	$(function() {
		$("#image_gallery").jcarousel({
			wrap:"both",
			buttonNextHTML: null,
			buttonPrevHTML: null,
			scroll: 1,
			visible:1,
			auto:5,
			easing: 'linear',
			animation:200,
			initCallback:callback
		});
	});
}
function embedFlowplayer(playlist) {
	var player = $("#video").flowplayer({
		playlist: playlist,
		theme: "darkblue",
		height: 291,
		width: 518,
		wmode: "opaque",
		stop:false
	});
	return player;
}
function setVisibility(visible) {
	if(visible) {
		$(document).ready(function(){
			$(".player").each(function(i){ mostrarPlayer($(this)); });
		});
	}
}
