(function($) {
	// Only first level submenus
	$.fn.dropright = function() {
		var that = this;
		var thatLis = that.children();
		var submenus = $("li ul", this);
		submenus.each(function(i) {
			var $this = $(this);
			var parentLi = $this.parents("li").eq(0);
			parentLi.css({
				"position": "relative",
				"z-index": 2
			});
			parentLi.hoverIntent({
				interval: 50,
				over: function(){
					this.style.zIndex = 3;
					$this.css("visibility", "visible");
					thatLis.dequeue();
					thatLis.not(this).animate({opacity:0.3}, 300);
				},
				out: function(){
					this.style.zIndex = 2;
					$this.css("visibility", "hidden");
					thatLis.dequeue();
					thatLis.not(this).animate({opacity:1}, 300);
				}
			});
			var border = (parentLi.outerHeight()-parentLi.innerHeight())/2;
			$this.css({
				"left":parentLi.innerWidth() + border,
				"top": -border
			});
		});
	}
})(jQuery)

/* Globo */
function ocultarGlobo() {
	$("#globo_descripcion").addClass("hide");
}
function mostrarGlobo() {
	$("#globo_descripcion").removeClass("hide");
}
function actualizarGlobo($li, left, index) {
	var $globo = $("#globo_descripcion");
	var nombre = $('p.nombre:eq(0)', $li).html();
	var texto = $('div.texto:eq(0)', $li).html();
	$('div.punta a', $globo).html(nombre);
	$('div.punta div.contenido', $globo).html(texto);
	if(left >= ($("#carousel_wrapper").width()/2)) {
		$globo.removeClass("izquierda").addClass("derecha");
	}
	else {
		$globo.removeClass("derecha").addClass("izquierda");
	}
	$("div.punta", $globo).css("background-position", left+"px top");
	mostrarGlobo();
}

/* Detalle */
function ocultarDetalle() {
	var $detalle = $("#detalle");
	$detalle.addClass("hide");
}
function mostrarDetalle() {
	var $detalle = $("#detalle");
	$detalle.removeClass("hide");
	$('.left',$detalle).css({left:'-440px'}).animate({left:'0px'},300);
	$('.right',$detalle).css({right:'-240px'}).animate({right:'0px'}, 300);
	
}
function actualizarDetalle($li, left, index) {
	$detalle = $("#detalle");
	var $fotos = $(".mas_fotos", $li);
	var empresa = $('p.empresa:eq(0)', $li).html();
	var descripcion = $('p.descripcion:eq(0)', $li).html();
	var $left = $('.left', $detalle);
	$detalle.dequeue();
	$detalle.animate({opacity:0}, 100, function(){
		mostrarDetalle();
		$left.html($fotos.clone());
		$left.find('img:first').addClass('show');
		$('.right p.empresa', $detalle).html(empresa);
		$('.right p.descripcion', $detalle).html(descripcion);
		$detalle.animate({opacity:1},200);
	});
}
function next_picture($contenedor) {
	var $imgs = $contenedor.find('.mas_fotos img');
	if ($imgs.length > 1) {
		var $selected = $contenedor.find('.mas_fotos img.show');
		var index = $selected.index();
		index++;
		if (index >= $imgs.length) {
			index = 0;	
		}
		var $select = $imgs.eq(index);
		if ($selected.get(0) != $select.get(0)) {
			$selected.stop().removeClass('show').fadeOut(200,function() { $select.stop().fadeIn(200).addClass('show'); });
		}
	}
}

/* Cliente */
function ocultarCliente() {
	$("#cliente").addClass("hide");
}
function mostrarCliente() {
	$("#cliente").removeClass("hide");
}
function mostrarCargandoCliente() {
	$("#cargando").removeClass("hide");
}
function ocultarCargandoCliente() {
	$("#cargando").addClass("hide");
}
function actualizarClienteOK(data, $li, id) {
	var $cliente = $("#cliente");
	var $img = $("img.foto:eq(0)", $li);
	var src = $img.attr("src");
	var title = $img.attr("title");
	var href = $img.attr("alt");
	var img = '<img src="'+src+'" alt="'+title+'" title="'+title+'" width="150" height="150" class="foto" />';
	if (href != "") {
		img = '<a href="'+href+'" target="_blank">'+img+'</a>';	
	}
	$(".logo_empresa", $cliente).html(img);
	$(".logos_chicos ul", $cliente).remove();
	if(data.length > 0) {
		var $ul = $(document.createElement("ul"));
		var foto;
		for(var i = 1; i < data.length; i++) {
			var foto = "images/galeria/"+data[i].archivo;
			var $liNew = $(document.createElement("li"));
			if (data[i].comentario != null) {
				var $ahref = $(document.createElement("a")).attr({
					href: data[i].comentario,
					target: "_blank"
				});
				$ahref.append($(document.createElement("img")).attr({
					src: foto,
					alt: data[i].titulo,
					title: data[i].titulo
				}));
				$liNew.append($ahref);
			}
			else {
				$liNew.append($(document.createElement("img")).attr({
					src: foto,
					alt: data[i].titulo,
					title: data[i].titulo
				}));
			}
			$liNew.append($(document.createElement("p")).html(data[i].titulo));
			$ul.append($liNew);
		}
		$(".logos_chicos", $cliente).append($ul);
	}
	$cliente.attr("class","");
	$cliente.addClass('cliente-'+id);
}
if($.manageAjax !== undefined) {
	$.manageAjax.create("carouselQueue");	
}
function actualizarCliente($li, left, index) {
	if (!$("#cliente").hasClass($li.attr("id"))) {
		var id = $li.attr("id").split("-").pop();
		$.manageAjax.clear("carouselQueue", true);
		$.manageAjax.add("carouselQueue", {
			data: "album_id="+id,
			type: "POST",
			url: "modulos/galerias/tojson.php",
			dataType: "json",
			beforeSend: function() {
				mostrarCargandoCliente();
				ocultarCliente();
			},
			error: ocultarCargandoCliente,
			success: function(data) { actualizarClienteOK(data, $li, id); },
			complete: function() {
				ocultarCargandoCliente();
				mostrarCliente();
			}
		});
	}
}

function carouselInit(carousel, functions) {
	var $carousel = $("#carousel");
	var $lis = $carousel.children("li");
	var liOriginalWidth = $lis.eq(0).children().eq(0).width();
	var liWidth = $lis.eq(0).width();
	var liNum = $lis.length;
	var actuallyVisible = 6;
	if(liNum < 6) {
		actuallyVisible = liNum;
	}
	$("#carousel_container").width(actuallyVisible*liWidth - (liWidth - liOriginalWidth));
	if(liNum > 6) {
		var $wrapper = $("#carousel_wrapper");
		var $prev = $("#prev");
		var $next = $("#next");
		$wrapper.append($prev);
		$wrapper.append($next);
		$prev.click(function() {
			functions.ocultar();
			$carousel.children("li img").dequeue().animate({opacity:1}, 300);
		});
		$next.click(function() {
			functions.ocultar();
			$carousel.children("li img").dequeue().animate({opacity:1}, 300);
		});
	}
	$lis.each(function(i){
		$(this).hoverIntent({
			interval: 50,
			over:function(event) {
				event.stopPropagation();
				var $li = $(this);
				$li.find('img:first').dequeue().animate({opacity:1}, 300);
				$li.siblings().find('img:first').dequeue().animate({opacity:0.2}, 300);
				var left = 0;
				if(functions.left) {
					var $elem = $li;
					while($elem.attr("id") != 'carousel_container') {
						left += $elem.position().left;
						$elem = $elem.parent();
					}
					left += 15;
				}
				functions.actualizar($li, left, i);
			},
			out:function(){}
		});
	});
}
function carousel() {
	var $carousel = $("#carousel");
	var options = {
		scroll:1,
		//visible:6,
		wrap:"both",
		initCallback:function(carousel) { carouselInit(carousel, funciones($carousel)) }
    };
	if($carousel.children("li").length > 6) {
		options.buttonPrevHTML = '<a id="prev"><img src="images/arrow_prev.png" alt="&lt;&lt;" title="anterior" /></a>';
		options.buttonNextHTML = '<a id="next"><img src="images/arrow_next.png" alt="&gt;&gt;" title="siguiente" /></a>';
	}
	$carousel.jcarousel(options);
}
function funciones($carousel) {
	var timer = null;
	var retorno;
	if($carousel.hasClass("portfolio")) {
		retorno = {
			ocultar: ocultarDetalle,
			mostrar: mostrarDetalle,
			actualizar: actualizarDetalle,
			left: false
		};
		var timer = setInterval(function() { next_picture($('#detalle ,left')); },4000);
	}
	else if($carousel.hasClass("clientes")) {
		retorno = {
			ocultar: ocultarCliente,
			mostrar: mostrarCliente,
			actualizar: actualizarCliente,
			left: false
		};
	}
	else {
		retorno = {
			ocultar: ocultarGlobo,
			mostrar: mostrarGlobo,
			actualizar: actualizarGlobo,
			left: true
		};
	}
	return retorno;
}
