jQuery(function($) {
	var body = $(document.body);
	var newsletter_bg = $("#newsletter_bg");
	var newsletter_frame = $("#newsletter_frame");
	var newsletter_iframe = $("#newsletter_iframe");
	
	newsletter_bg.css("opacity", "0.3");
	body.append(newsletter_frame);
	
	
	$("#subscribe_newsletter").bind("click", function() {
		var email = $.trim($("#email_newsletter").val());
		var link = "http://www.slidein.travel/portal-slidein/?.s=newsletter&.o=subscribe&email=";
		
		if (email != "") {
			newsletter_iframe.attr("src", link + encodeURIComponent(email));
			newsletter_frame
				.css("width", body.width())
				.css("height", body.height())
				.css("display", "block");
		} else {
			alert("Deve indicar o email a registar!");
		}
	});
	$("#newsletter_close").bind("click", function() {
		newsletter_iframe.attr("src", "about:blank");
		newsletter_frame.css("display", "none");
	});
});


jQuery(function($) {
	function half(x) {
		return x % 2 ? (x-1)/2 : x/2;
	}
	
	function innerWidth() {
		if (self.innerHeight) // all except Explorer
		{
			return self.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			return document.documentElement.clientWidth;
		}
		else if (document.body) // other Explorers
		{
			return document.body.clientWidth;
		}
	}
	function innerHeight() {
		if (self.innerHeight) // all except Explorer
		{
			return self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			return document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			return document.body.clientHeight;
		}
	}

	var opts = {
		closeClass: "popup_close",
		closeTitle: "",
		persist: false,
		containerCss: { width: "300px", height: "200px" }
	};
	var minWidth = 300;
	var minHeight = 200;
	
	var px = "px";
	
	var container = $(
		'<div class="popup_container" style="display: none">' +
			'<table cellpadding="0" cellspacing="0" border="0"><tr>' +
				'<td align="center" valign="middle" class="popup_content"><img class="album_full_image" src="images/loader.gif"/></td>' +
			'</tr></table>' +
			'<div class="popup_footer">' +
				'<input type="submit" class="submit_btn popup_close" value="Fechar">' +
			'</div>' +
		'</div>'
	).appendTo(document.body);
	var content = container.find(".popup_content");
	var album_image = content.find(".album_full_image");
	
	function updateModal(img) {
		var maxWidth = innerWidth() - 50;
		var maxHeight = innerHeight() - 150;
		
		// get image dimensions
		var cWidth = img.width;
		var cHeight = img.height;
		// adjust contents area
		if (cWidth > maxWidth) cWidth = maxWidth;
		if (cHeight > maxHeight) cHeight = maxHeight;
		if (cWidth < minWidth) cWidth = minWidth;
		if (cHeight < minHeight) cHeight = minHeight;
		
		// resize image if needed to fit
		if (cWidth < img.width || cHeight < img.height) {
			var r;
			if (cWidth / img.width < cHeight / img.height) {
				r = cWidth / img.width;
			} else {
				r = cHeight / img.height;
			}
			img.width = Math.round(img.width * r);
			img.height = Math.ceil(img.height * r);
		}
		
		// bounds check again
		cWidth = img.width;
		cHeight = img.height;
		if (cWidth > maxWidth) cWidth = maxWidth;
		if (cHeight > maxHeight) cHeight = maxHeight;
		if (cWidth < minWidth) cWidth = minWidth;
		if (cHeight < minHeight) cHeight = minHeight;
		
		var modalContainer = $("#modalContainer").hide();
		var container = modalContainer.find(".popup_container");
		var content = container.find(".popup_content");
		var parent = container.parent();
		container
			.css("display", "none")
			.appendTo(document.body)
		;
		content
			.empty()
			.css("width", (10+cWidth) + px)
			.css("height", (10+cHeight) + px)
			.append(img)
		;
		var w = container.width();
		var h = container.height();
		container.appendTo(parent).css("display", "block");
		
		modalContainer.css("width", w + px);
		modalContainer.css("height", h + px);
		if (!$.browser.msie || $.browser.version >= 7) {
			modalContainer.css("marginLeft", "-" + half(w) + px);
			modalContainer.css("marginTop", "-" + half(h) + px);
		}
		modalContainer.show();
	}

	$(".album_image").bind("click", function() {
		var cancel = false;
		opts.onClose = function() {
			$.modal.close();
			cancel = true;
		}
		var cWidth = minWidth;
		var cHeight = minHeight;
		
		content.css({
			width: (10+cWidth) + px,
			height: (10+cHeight) + px
		});
		
		var w = container.width();
		var h = container.height();
		
		opts.containerCss.width = w + px;
		opts.containerCss.height = h + px;
		if (!$.browser.msie || $.browser.version >= 7) {
			opts.containerCss.marginLeft = "-" + half(w) + px;
			opts.containerCss.marginTop = "-" + half(h) + px;
		}
		
		container.clone().modal(opts)
		
		var src = this.src.substring(0,this.src.length-6);
		setTimeout(function() {
			$(new Image())
				.bind("load", function() {if (!cancel) updateModal(this);})
				.attr("src", src)
			;
		}, 500);
	});
});
