var ID_COUNTER = 0;
var IE = false;
if ($.browser.msie) {
	IE = true;
}
function uniqueid(prefix) {
	if (prefix == null) {
		prefix = "";
	}
	ID_COUNTER++;
	return prefix + "" + ID_COUNTER;
}
function is_mail(texto) {
	var mailres = true;
	var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
	var arroba = texto.indexOf("@",0);
	if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
	var punto = texto.lastIndexOf(".");
	for (var contador = 0 ; contador < texto.length ; contador++) {
		if (cadena.indexOf(texto.substr(contador, 1),0) == -1) {
			mailres = false;
			break;
		}
	}
	if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
		mailres = true;
	else
		mailres = false;
	return mailres;
}

function img_redim(img) {
	img.src = img.src;
	img.onload = function () {
		dored();
	}
	if (img.complete) {
		dored();
	}
	function dored() {
		var imgw = img.width;
		var imgh = img.height;
		var conw = $("#homebg").width();
		var conh = $("#homebg").height();
		var reli = imgw / imgh;
		var relc = conw / conh;
		if (reli > relc) {
			$(img).css({
				width: "auto",
				height: conh
			});
		}
		else {
			$(img).css({
				height: "auto",
				width: conw
			});
		}
		var imgw = img.width;
		var imgh = img.height;
		var top = Math.ceil((conh - imgh) / 2);
		var left = Math.ceil((conw - imgw) / 2);
		$(img).css({
			top: top+"px",
			left:  left+"px"
		});
	}
}

function show_popup(html) {
	var popup = $(html);

	var id = uniqueid("popup");
	var popupcont = $("<div>");
	popupcont.addClass("popupcontainer");
	popupcont.attr("id",id);
	var popupbg = $("<div>");
	popupbg.addClass("popupbg");
	popupcont.append(popupbg);
	popupcont.append(popup);
	popupcont.find("div.popup").prepend('<a class="close" href="javascript:;"></a>');
	popupcont.find("div.popup a.close").click(function (e) {
		e.preventDefault();
		popupcont.fadeOut(function () {
			popupcont.remove();
		});
	});
	$("body").append(popupcont);
	var h = popup.height();
	var p = popup.outerHeight() - h;
	popup.css({
		maxHeight: $(window).height() - p - 20
	});
	var m = ($(window).height() - popup.outerHeight()) / 2;
	if (m > 0) {
		popup.animate({top:m});
	}
	popupcont.animate({"opacity":1},function () {
		if (IE) {
			popup.css({
				filter: "alpha(opacity=100)"
			});
		}
	});
}

function load_next() {
	if  ($(window).scrollTop() == $(document).height() - $(window).height()) {
		var items = $("#content div.item:not(:visible)");
		var n = 4;
		if (items.size() > 0) {
			if (items.size() < 4) {
				n = items.size()  - 1;
			}
		}
		$("#loading").show();
		var waiting = 0;
		items.each(function (i) {
			var self = this;
			if (i <= n) {
				waiting++;
				var img = $(this).find("img").get(0);
				if ($(img).size() == 1 && !img.complete) {
					img.load(function () {
						$(self).fadeIn(1000,function () {
							waiting--;
							if (waiting == 0) {
								$("#loading").hide();
							}
						});
					});
				}
				else {
					$(this).fadeIn(1000, function () {
						waiting--;
						if (waiting == 0) {
							$("#loading").hide();
						}
					});
				}
			}
		});
		if (waiting == 0) {
			$("#loading").hide();
		}
	}
}
var INTERVAL = null;
$(document).ready(function () {
	INTERVAL = setInterval(function () {
		load_next();
		if ($("#content div.item:not(:visible)").size() == 0) {
			clearInterval(INTERVAL);
		}
	},1000);
	$("#homebg").each(function () {
		var img = $(this).find("img").get(0);

		img_redim(img);
		$(img).fadeIn();

		$(window).resize(function () {
			img_redim($("#homebg > img").get(0));
		});
	});
	$( "#main_menu" ).draggable();
	$( "#main_menu" ).css({
		position: "fixed"
	});

	$( "#main_menu ul.main > li > a" ).click(function () {
		$(this).next().slideToggle();
	});

	$("#content").each(function () {
		var self = this;
		$(this).find("div.item").each(function (i) {
			if (i>7) $(this).hide();
		});
		$(window).scroll(function () {
			load_next();
		});
	});

	$(window).resize(function () {
		$( "#main_menu" ).css({
			left: "auto",
			right: "5%",
			top: "30%"
		});
	});

	$("#newsletter a.send").click(function (e) {
		e.preventDefault();
		var valid = true;
		var form = $(this).closest("form");
		var name = form.find("[name=name]");
		var email = form.find("[name=email]");
		if ($.trim(name.val()) == "") {
			valid = false;
			name.css("background-color","#FF8888");
		}
		else {
			name.css("background-color","");
		}
		if (!is_mail($.trim(email.val()))) {
			valid = false;
			email.css("background-color","#FF8888");
		}
		else {
			email.css("background-color","");
		}
		if (valid) {
			$(this).closest("form").submit();
		}
	});

	$("a.popup").click(function (e) {
		e.preventDefault();
		var url = $(this).attr("href");
		$.ajax({
			url: url,
			type: "get",
			data: {time: (new Date()).getTime(),popup:1},
			success: function (r) {
				show_popup(r);
			}
		});
	});
});

$(window).load(function () {
	if ($(document).height() <= $(window).height()) {
		$(window).scroll();
	}
});
