function scrollTop() {
	if (document.all) {
		return window.document.documentElement.scrollTop;
	}
	else {
		return window.pageYOffset;
	}
}

function scrollLeft() {
	if (document.all) {
		return window.document.documentElement.scrollLeft;
	}
	else {
		return window.pageXOffset;
	}
}

function show_image_title(obj, evt) {
	hide_image_title();
	var text = $(obj).attr("data-title");
	var text2 = $(obj).attr("data-alt");
	if (text2 == null) text2 = "";
	var X = evt.clientX + scrollLeft() + 10;
	var Y = evt.clientY + scrollTop() + 10;
	var html = $('<div class="image-title"><div class="text">'+text+'</div><div class="ficha">'+text2+'</div></div>').css({
		top: Y+"px",
		left: X+"px"
	});
	obj.elem = html;
	$("body").append(obj.elem);
}

function hide_image_title() {
	$("body > div.image-title").remove();
}

function set_fixed() {
	var max = 0;
	$("div.popup:visible").each(function () {
		var offset = parseInt($(this).css("top")) + parseInt($(this).outerHeight());
		if (offset > max) max = offset;
	});
	var h = $(window).height();
	if (max > h) {
		$("body").height(max);
		$(window).scroll(function (e) {
			var top = scrollTop();
			$("#fixed").css("top",-top+"px");
		});
		//$("#fixed").css("position","absolute");
	}
	else {
		$("body").height(h);
		$(window).unbind("scroll");
		$("#fixed").css("top",0);
		//$("#fixed").css("position","fixed");
	}
}

function add_events(from) {

	$("input[type=text]").each(function () {
		var bgcolor = $(this).closest("div.popup").css("background-color");
		$(this).css("background-color",bgcolor);
	});
	$("div.popup").each(function () {
		if ($(this).find("div.move").size() == 0) {
			$(this).prepend('<div class="move"></div>');
		}
	}).draggable({
		handle:'div.move',
		start: function (event, ui) {
			$("div.popup").css("z-index","");
			$(ui.helper).css("z-index",10000);
		},
		stop: function () {
			set_fixed();
		}
	});

	if (from == null) from = $("body");

	$("#images [title], #overview div.images [title]").each(function () {
		var title = $(this).attr("title");
		if (title != null) {
			var alt = $(this).attr("alt");
			$(this).attr("data-title", title);
			if (alt != null) {
				$(this).attr("data-alt", alt);
			}
			$(this).removeAttr("title");
			$(this).removeAttr("alt");
		}
	});

	$("[data-title]")
		.unbind()
		.mouseover(function (e) {
			hide_image_title();
			show_image_title(this, e);
		})
		.mousemove(function (e) {
			show_image_title(this, e);
		})
		.mouseout(function () {
			hide_image_title();
		});
	from.find(".fancy").fancybox();
	$("iframe.sharethis").unbind();
	$("iframe.sharethis").mouseenter(function () {
		$(this).width(1024);
		$(this).height(768);
		$(this).css({overflow:"visible"});
	});
	$("iframe.sharethis").mouseleave(function () {
			$(this).width(100);
			$(this).height(20);
			$(this).css({overflow:"hidden"});
	});
}

function load_content(href,target,callback,data) {
	$("#autocomplete_search").remove();
	href = href.replace(/#!/,"");
	if (data==null) {
		var data = {};
	}
	data.ajax = 1;
	data.time = new Date().getTime();
	$.ajax({
		url: href,
		type: "get",
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		data: data,
		beforeSend: function () {
			if (target != "detail") {
				$("#content, #h1, #h2, #press-office, #detail").addClass("loading").find("div.content").empty();
			}
			else {
				$("#detail").addClass("loading").find("div.content").empty();
				$("#div_detail2").addClass("loading").find("div.content").empty();
			}
			$("body").append("<div id='loading_layer'><div></div></div>");
		},
		success: function (r) {
			$("#loading_layer").remove();
			var div = $("<div></div>");
			$(div).get(0).innerHTML = r;
			// SCRIPTS
			var script = div.find("script");
			if ($("body > div.script").size() == 0) {
				$("body").append('<div class="script" style="display: none;"></div>');
			}
			$("body > div.script").html(script);
			// DETALLE, si hay, no hacemos nada mas.
			var content = div.find("[rel=detail]");
			var elem = $("#detail");
			if (content.size() > 0) {
				elem.removeClass("loading").find("div.content").html(content.contents());
				if (!elem.is(":visible")) {
					elem.fadeIn();
				}
				add_events(elem);
				// DIV DETAIL 2
				var elem = $("#div_detail2");
				if (div.find("[rel=div_detail2]").size() > 0) {
					var content = div.find("[rel=div_detail2]").clone();
					if (content.size() > 0) {
						elem.css("background-color","#FFFFFF");
						var color = content.attr("data-color");
						if (color == null || color == "undefined" || $.trim(color) == "") color = "FFFFFF";
						elem.css("background-color","#"+color);
						elem.css("top","");
						elem.css("left","");
						elem.removeClass("loading").find("div.content").html(content.contents());
						add_events(elem);
					}
				}
				else {
					elem.hide();
				}
				set_fixed();
			}
			else {
				elem.hide();

			// IMAGENES
				var content = div.find("[rel=images]").clone();
				var elem = $("#images");
				if (content.size() > 0) {
					elem.removeClass("loading").html(content.contents());
					if (!elem.is(":visible")) {
						elem.fadeIn();
					}
					add_events(elem);
				}

			// CONTENIDO
				var content = div.find("[rel=content]").clone();
				var elem = $("#content");
				if (content.size() > 0) {
					var color = content.attr("data-color");
					if (color == null) color = "FFFFFF";
					elem.css("background-color","#"+color);
					elem.removeClass("loading").find("div.content").html(content.contents());
					if (!elem.is(":visible")) {
						elem.fadeIn();
					}
					add_events(elem);
				}
				else {
					elem.hide();
				}

			// CONTENIDO 2
				var content = div.find("[rel=content2]").clone();
				var elem = $("#content2");
				if (content.size() > 0) {
					var color = content.attr("data-color");
					if (color == null) color = "FFFFFF";
					elem.css("background-color","#"+color);
					elem.removeClass("loading").find("div.content").html(content.contents());
					if (!elem.is(":visible")) {
						elem.fadeIn();
					}
					add_events(elem);
				}
				else {
					elem.hide();
				}

			// DESTACADO 1
				var content = div.find("[rel=h1]").clone();
				var elem = $("#h1");
				if (content.size() > 0) {
					elem.css("top",(parseInt($("#main").css("top")) + parseInt($("#main").outerHeight()) + 10) + "px");
					elem.removeClass("loading").find("div.content").html(content.contents());
					if (!elem.is(":visible")) {
						elem.fadeIn();
					}
					add_events(elem);
				}
				else {
					elem.hide();
				}

			// DETACADO 2
				var content = div.find("[rel=h2]").clone();
				var elem = $("#h2");
				if (content.size() > 0) {
					elem.removeClass("loading").find("div.content").html(content.contents());
					if (!elem.is(":visible")) {
						elem.fadeIn();
					}
					add_events(elem);
				}
				else {
					elem.hide();
				}
			// DETACADO PRENSA
				var content = div.find("[rel=press-office]").clone();
				var elem = $("#press-office");
				if (content.size() > 0) {
					elem.css("top",(parseInt($("#main").css("top")) + parseInt($("#main").outerHeight()) + 10) + "px");
					elem.removeClass("loading").find("div.content").html(content.contents());
					if (!elem.is(":visible")) {
						elem.fadeIn();
					}
					add_events(elem);
				}
				else {
					elem.hide();
				}
			// DIV OCULTO
				var content = div.find("[rel=div_oculto]").clone();
				var elem = $("#div_oculto");
				if (content.size() > 0) {
					elem.css("top",(parseInt($("#main").css("top")) + parseInt($("#main").outerHeight()) + 10) + "px");
					elem.removeClass("loading").find("div.content").html(content.contents());
					add_events(elem);
				}
				else {
					elem.hide();
				}
			// DIV OCULTO 2
				var content = div.find("[rel=div_oculto2]").clone();
				var elem = $("#div_oculto2");
				if (content.size() > 0) {
					elem.css("top","");
					elem.css("left","");
					elem.removeClass("loading").find("div.content").html(content.contents());
					add_events(elem);
				}
				else {
					elem.hide();
				}
			// MIRAMOS LA ALTURA
				set_fixed();

			// CLASE
				var cleanhref = href.split("?")[0];
				var links = $("a[href=#!"+cleanhref+"]");
				if (links.size() > 1) {
					links.each(function (i) {
						set_active($("a[href=#!"+cleanhref+"]").get(i));
					});
				}
				else {
					if (links.closest("#div_hijos").size() > 0) {
						var id = links.closest("div.content").attr("id");
						set_active($("a[rel="+id+"]").get(0));
					}
					set_active($("a[href=#!"+cleanhref+"]").get(0));
				}
			}
			$("div.popup").css("z-index","");
			get_city();
			set_click_events();
			if (typeof callback == "function") callback();
			if ($(".st_sharethis").size() > 1) {
				$(".st_sharethis:not(:first)").replaceWith($("#sharethis .st_sharethis").clone(true));
			}
		}
	});
}
function set_active(elem) {
	$(elem).siblings().removeClass("active");
	$("#div_hijos a.active").removeClass("active");
	$(elem).addClass("active");

	var rel = $(elem).attr("rel");
	if ($.trim(rel) != "") {
		if ($("#"+rel).size() > 0) {
			$("#"+rel).siblings().hide();
			$("#"+rel).show();
		}
		else {
			$("#div_hijos > div").hide();
		}
	}
	$("[href="+$(elem).attr("href")+"]").each(function () {
		$(this).addClass("active");
	});
}

function show_tags() {
	$("#div_tags a").removeClass("active");
	$("#div_tags > div").show();
	$("#main div.search").show();
	if ($("#div_hijos a.active").size() == 0) {
		if ($("#div_tags a:first.clicked").size() == 0) {
			$("#div_tags a:first").addClass("clicked").click();
		}
	}
}

function remove_clicked() {
	$("#div_tags a").removeClass("clicked")
}

function hide_tags() {
	$("#div_tags > div").hide();
	$("#main div.search").hide();
}

function show_prensa() {
	$("#div_prensa > div").show();
}

function hide_prensa() {
	$("#div_prensa > div").hide();
}

function golink() {
	var url = document.location.href;
	if (url.indexOf("#!") >= 0) {
		var link = "#!"+url.split("#!")[1];
		$("a[href="+link+"]").click();
	}
}

function set_click_events() {
	$("a[href*=#!], area[href*=#!]").unbind("click").bind("click",function (e) {
		e.preventDefault();
		if ($(this).closest("#main").size() == 1) {
			$("body div.popup:not(#main)").hide();
		}
		load_content($(this).attr('href'), $(this).attr('target'), function () {});
		document.location.href=$(this).attr('href');
	});
}

$(document).ready(function () {
	set_click_events();

	$("div.popup a.close").click(function (e) {
		e.preventDefault();
		$(this).closest("div.popup").fadeOut(function () {
			if ($(this).attr("id") != "div_oculto" && $(this).attr("id") != "div_detail2" && $(this).attr("id") != "div_oculto2" && $(this).attr("id") != "div_credits") {
				$(this).find("div.content").empty();
			}
			set_fixed();
		});
	});

	add_events();

	$("#press-office").css({
		top: (parseInt($("#main").css("top")) + parseInt($("#main").outerHeight()) + 15) + "px"
	});

	$("#slider").slider({
		step: 1,
		value: 0,
		min: 0,
		max: 400,
		slide: function (event, ui) {
			var val = ui.value;
			$("#overview a").each(function () {
				var w = 50 + val;
				var h = 50 + val;
				$(this).css({
					width: w,
					height: h
				});
			});
		}
	});

	$("#div_tags div.content a").live("click",function (e) {
		e.preventDefault();
		$("#div_tags div.content a").removeClass("active");
		$(this).addClass("active");
	});

	$("#search")
		.keyup(function (e) {
			var self = this;
			if (e.keyCode == 40) { // DOWN
				var next = $("#autocomplete_search a.selected").next();
				$("#autocomplete_search a").removeClass("selected");
				if (next.is("a")) {
					next.addClass("selected");
				}
				else {
					$("#autocomplete_search a:first").addClass("selected");
				}
				return;
			}
			if (e.keyCode == 38) { // UP
				var next = $("#autocomplete_search a.selected").prev();
				$("#autocomplete_search a").removeClass("selected");
				if (next.is("a")) {
					next.addClass("selected");
				}
				else {
					$("#autocomplete_search a:last").addClass("selected");
				}
				return;
			}
			if (e.keyCode == 13) { // ENTER
				var selected = $("#autocomplete_search a.selected");
				if (selected.size() > 0) {
					$("#autocomplete_search a.selected").click();
				}
				else {
					load_content($(self).attr("data-link"),null,null,{cercarp:this.value});
				}
				return;
			}
			$("#autocomplete_search").remove();
			var value = $.trim($(this).val());
			var self = this;
			if (value.length > 0) {
				var div = $("<div>");
				div.attr("id","autocomplete_search");
				var left = $("#search").position().left;
				var top = parseInt($("#search").position().top) + parseInt($("#search").outerHeight());
				div.css({
					"left":left,
					"top":top
				});
				$.ajax({
					url: "/ajax_search.php",
					type: "post",
					data: {productos:1, text: value},
					success: function (r) {
						$("#autocomplete_search").remove();
						div.html(r);
						div.insertAfter($(self));
						set_click_events();
						$("body").click(function () {
							$("#autocomplete_search").remove();
							$(this).unbind("click");
						});
					}
				});
			}
		});
});

function get_city() {
	$(document).ready(function () {
		function retailers(elem,e) {
			var self = elem;
			if (e.keyCode == 40) { // DOWN
				var next = $("#autocomplete_search a.selected").next();
				$("#autocomplete_search a").removeClass("selected");
				if (next.is("a")) {
					next.addClass("selected");
				}
				else {
					$("#autocomplete_search a:first").addClass("selected");
				}
				return false;
			}
			if (e.keyCode == 38) { // UP
				var next = $("#autocomplete_search a.selected").prev();
				$("#autocomplete_search a").removeClass("selected");
				if (next.is("a")) {
					next.addClass("selected");
				}
				else {
					$("#autocomplete_search a:last").addClass("selected");
				}
				return false;
			}
			if (e.keyCode == 13) { // ENTER
				var selected = $("#autocomplete_search a.selected");
				if (selected.size() > 0) {
					$("#autocomplete_search a.selected").click();
				}
				var value = $("<div>").text(elem.value).html();
				load_content($(self).attr("data-link"),null,null,{codigo_postal:value});
				return false;
			}
			$("#autocomplete_search").remove();
			var value = $.trim($(elem).val());
			var self = elem;
			if (true) {
				var div = $("<div>");
				div.attr("id","autocomplete_search");
				var left = $("#retailers").position().left;
				var top = $("#retailers").position().top + $("#retailers").outerHeight();
				div.css({
					"left":left,
					"top":top
				});
				$.ajax({
					url: "/ajax_search.php",
					type: "post",
					data: {ciudades:1, text: value},
					success: function (r) {
						$("#autocomplete_search").remove();
						div.html(r);
						div.insertAfter($(self));
						$("body").click(function () {
							$("#autocomplete_search").remove();
							$(this).unbind("click");
						});
					}
				});
			}
		}
		$("#retailers").unbind()
		$("#retailers")
			.keyup(function (e) {
				retailers(this,e);
			})
			.focus(function (e) {
				retailers(this,e);
			})
			.next().click(function (e) {
				e.preventDefault();
				e.keyCode = 13
				retailers($(this).prev().get(0),e);
			});
	});
}

window.onload = function () {
	set_fixed();
}

window.onresize = function () {
	set_fixed();
}
