$(document).ready(function () {
	/*$("#top-menu div.menu div.item div.submenu").each(function() {
		$(this).width($(this).closest("div.item").width());
	});*/
	
	$("#top-menu div.menu div.item").hover(
		function () {
			$(this).find("div.submenu").stop().slideDown(function () {
				var w = $(this).width();
				$(this).removeAttr("style").show().width(w);
			});
		},
		function () {
			$(this).find("div.submenu").stop().slideUp(function () {
				var w = $(this).width();
				$(this).removeAttr("style").width(w);
			});
		}
	);
	var ANIMATE_HOME = true;
	var HOME_INT = null;
	function loadDataHome(elem,user) {
		if (user==true) {
			ANIMATE_HOME = false;
			clearInterval(HOME_INT);
		}
		else {
			var n = $(elem).parent().prevAll("span.item").size();
			if ((n % 4) == 0) {
				$("#selector-buttons a.next").click();
			}
		}
		var id = $(elem).attr("rel");
		var html = $("#"+id);
		$("#info").fadeOut("fast",function () {
			$(this).html(html.find("div.info").html());
			$(this).fadeIn();
		});
		var img = new Image();
		$(img).addClass("loading");
		img.onload = function () {
			$(img).hide();
			$(img).removeClass("loading");
			$(img).fadeIn();
		};
		img.src = elem.href;
		$("#image").empty().append(img);
		$(elem).parent().siblings().removeClass("active");
		$(elem).parent().addClass("active");
		if (ANIMATE_HOME) {
			HOME_INT = setTimeout(function () {
				if ($(elem).parent().next().size() > 0) {
					loadDataHome($(elem).parent().next().find("a").get(0),false);
				}
			},10000);
		}
	}
	$("#items-container span.item a").click(function (e) {
		e.preventDefault();
		loadDataHome(this,true);
	});

	if ($("#selector-buttons").size() > 0) {
		var pag = 1;
		var npags = Math.ceil($("#items-container span.item").size() / 4);
		var lock = false;
		var scroll = 752;
		loadDataHome($("#items-container span.item:first a").get(0),false);
		if (npags <= 1) {
			$("#selector-buttons a.prev, #selector-buttons a.next").remove();
		}
	}

	$("#selector-buttons a.prev").click(function (e) {
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (lock) return;
			lock = true;
			$("#items-container div.scroll").animate({left: "+="+scroll+"px"},function () {
				lock = false;
				pag--;
				$("#selector-buttons a.next").removeClass("noactive");
				if (pag == 1) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	$("#selector-buttons a.next").click(function (e) {
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (lock) return;
			lock = true;
			$("#items-container div.scroll").animate({left: "-="+scroll+"px"},function () {
				lock = false;
				pag++;
				$("#selector-buttons a.prev").removeClass("noactive");
				if (pag == npags) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	$("#blocks #block1 #desplegables div.item p.title").click(function () {
		if ($(this).parent().hasClass("active")) return;
		$(this).parent().siblings(".active").removeClass("active").animate({height:"21px"});
		$(this).parent().addClass("active").animate({height:"46px"});
	});

	if ($("#block2 #news").size() > 0) {
		var newspag = 1;
		var newsnpags = $("#block2 #news div.new").size();
		var newslock = false;
		if (newsnpags <= 1) {
			$("#block2 #news a.prev, #block2 #news a.next").remove();
		}
	}
	$("#block2 #news a.prev").click(function (e) {
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (newslock) return;
			newslock = true;
			$(this).parent().find("div.scroll").animate({"left":"+=480px"}, function () {
				newslock = false;
				newspag--;
				$("#block2 #news a.next").removeClass("noactive");
				if (newspag == 1) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	$("#block2 #news a.next").click(function (e) {
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (newslock) return;
			newslock = true;
			$(this).parent().find("div.scroll").animate({"left":"-=480px"},function () {
				newslock = false;
				newspag++;
				$("#block2 #news a.prev").removeClass("noactive");
				if (newspag == newsnpags) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	window.onload = function () {
		$("#left-menu #news").each(function () {
			var hnews = 0;
			$("#left-menu #news div.new").each(function () {
				if ($(this).height() > hnews)
					hnews = $(this).height();
			});
			$("#left-menu #news div.new").height(hnews+20);
		});
		
		$("#general").each(function () {
			var h = $(this).height();
			var h2 = $("#left-menu").height();
			if (h > h2) {
				$("#left-menu").height(h);
			}
			else {
				$(this).height(h2);
			}
		});

		if (window.loadMap)
		{
			loadMap();
		}
	}
	
	$("#general div.listado div.item").click(function () {
		document.location.href = $(this).find("a.name").attr("href");
	});
	
	$("#general div.detalle div.images div.selector div.scroll a").click(function (e) {
		e.preventDefault();
		var src = $(this).attr("data-src");
		var imgdiv = $(this).closest("div.images").find("div.image");
		var img = new Image();
		$(img).addClass("loading");
		img.onload = function () {
			$(img).hide();
			$(img).removeClass("loading");
			$(img).fadeIn();
		};
		img.src = src;
		var link = $("<a class=\"ver\" href=\""+this.href+"\"></a>");
		link.fancybox();
		imgdiv.empty().append(img).append(link);
		$(img).attr("src",src);
		$(this).siblings(".active").removeClass("active");
		$(this).addClass("active");
	});

	$("#general div.detalle div.images div.selector div.scroll a:first").click();

	if ($("#general div.detalle div.images").size() > 0) {
		var det_npags = Math.ceil($("#general div.detalle div.images div.selector div.scroll a").size() / 4);
		var det_pag = 1;
		var det_lock = false;
		if (det_npags <= 1) {
			$("#general div.detalle div.images div.selector div.buttons a").remove();
		}
	}
	
	$("#general div.detalle div.images div.selector div.buttons a.next").click(function (e) {
		e.preventDefault();
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (det_lock) return;
			det_lock = true;
			$(this).closest("div.selector").find("div.scroll").animate({"left":"-=364px"},function () {
				det_lock = false;
				det_pag++;
				$(self).prev().removeClass("noactive");
				if (det_pag == det_npags) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	$("#general div.detalle div.images div.selector div.buttons a.prev").click(function (e) {
		e.preventDefault();
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (det_lock) return;
			det_lock = true;
			$(this).closest("div.selector").find("div.scroll").animate({"left":"+=364px"},function () {
				det_lock = false;
				det_pag--;
				$(self).next().removeClass("noactive");
				if (det_pag == 1) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	if ($("#left-menu #news").size() > 0) {
		var newspag = 1;
		var newsnpags = $("#left-menu #news div.new").size();
		var newslock = false;
		if (newsnpags <= 1) {
			$("#left-menu #news a.prev, #left-menu #news a.next").remove();
		}
	}
	$("#left-menu #news a.prev").click(function (e) {
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (newslock) return;
			newslock = true;
			$(this).parent().find("div.scroll").animate({"left":"+=193px"}, function () {
				newslock = false;
				newspag--;
				$("#left-menu #news a.next").removeClass("noactive");
				if (newspag == 1) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	$("#left-menu #news a.next").click(function (e) {
		var self = this;
		e.preventDefault();
		if (!$(this).hasClass("noactive")) {
			if (newslock) return;
			newslock = true;
			$(this).parent().find("div.scroll").animate({"left":"-=193px"},function () {
				newslock = false;
				newspag++;
				$("#left-menu #news a.prev").removeClass("noactive");
				if (newspag == newsnpags) {
					$(self).addClass("noactive");
				}
			});
		}
	});

	$("#header div.content div.carrito").css("cursor","pointer").click(function () {
		document.location = $(this).find("a").attr("href");
	});
});

function imaginanet(item) {
	if ($("#in_credits_cloned").size() > 0) {
		$("#in_credits_cloned").remove();
		return false;
	}
	var html=$("#in_credits").clone().attr("id","in_credits_cloned").prepend("<a class='close' href='#'>X</a>");
	html.css({
		position: "absolute",
		border: "#666 solid 1px",
		padding: "10px 15px",
		background: "#333",
		color: "#FFFFFF",
		width: "140px",
		top: ($(item).offset().top - ($(item).height() + 22))+"px",
		left: ($(item).offset().left - (170 - $(item).width()))+"px",
		display: "block",
		"z-index": 1000
	}).find("a").css({
		color: "#FFF",
		"text-decoration": "none"
	}).end().find("a:first").css({
		position: "absolute",
		top: "2px",
		right: "2px",
		"text-decoration": "none",
		color: "#FFFFFF"
	}).click(function (e) {
		e.preventDefault();
		html.remove();
	});
	$("body").append(html);
}
