$(document).ready(function(){

	$(window).resize(function(){
		positionLightboxItem();
	});

	$("a.lightbox").click(function(e){
		e.preventDefault();
		showLightbox($(this).attr("href"));
	});

});

function showLightbox(href) {

	$('<div id="overlay"></div>')
	.appendTo('body')
	.css({
		"background": "#000",
		"height": "100%",
		"opacity": "0",
		"left": "0px",
		"position": "fixed",
		"top": "0px",
		"width": "100%",
		"z-index": "1000"
	})
	.animate({
		"opacity": "0.0"
	}, "slow")
	.click(function() {
		removeLightbox();
	})
	;

	$('<div id="lightbox"></div>')
	.hide()
	.appendTo('body')
	.css({
		"position": "fixed",
		"margin": "0px",
		"padding": "0px",
		"width": "777px",
		"height": "680px",
		"z-index": "1000",
		"overflow": "hidden",
		"background-color": "transparent",
		"background-image": "url('img/lightbox-background.png')"
	})
	;

	$('<iframe id="lb_content" frameborder="0"></iframe>')
	.ready(function(){
		positionLightboxItem();
	})
	.appendTo('#lightbox')
	.css({
		"border": "none",
		"outline": "none",
		"width": "657px",
		"height": "562px",
		"margin": "60px"
	})
	;

	$("#lightbox #lb_content").attr("src", href);
}


function showLightbox_OffRamp(href) {

	$('<div id="overlay"></div>')
	.appendTo('body')
	.css({
		"background": "#000",
		"height": "100%",
		"opacity": "0.0",
		"left": "0px",
		"position": "fixed",
		"top": "0px",
		"width": "100%",
		"z-index": "1000"
	})
	.animate({
		"opacity": "0.5"
	}, "slow")
	.click(function() {
		removeLightbox();
	})
	;

	$('<div id="lightbox"></div>')
	.hide()
	.appendTo('body')
	.css({
		"position": "fixed",
		"margin": "0px",
		"padding": "0px",
		"width": "600px",
		"height": "300px",
		"z-index": "1000",
		"overflow": "hidden",
		"background-color": "transparent"
	})
	;

	$('<iframe id="lb_content" frameborder="0"></iframe>')
	.ready(function(){
		positionLightboxItem();
	})
	.appendTo('#lightbox')
	.css({
		"border": "none",
		"outline": "none",
		"width": "600px",
		"height": "300px"
	})
	.attr("src", "/off-ramp.php?"+href)
	;
}

function positionLightboxItem() {

	var top = ($(window).height() - $('#lightbox').height()) / 2;
	var left = ($(window).width() - $('#lightbox').width()) / 2;
	if ( top < 0 ) top = 0;
	var lb =
		$('#lightbox').css({
			'top': top,
			'left': left
		});
	if( $.browser.msie ) {
		$(lb).show();
	} else {
		$(lb).fadeIn();
	}
}

function removeLightbox() {
	$("#lightbox #lb_content").css({"overflow-y":"hidden"});
	$('#overlay, #lightbox')
		.fadeOut('slow', function() {
			$(this).remove();
	}).hide();
}
