// -----------------------------------------------------------------------------------
//
//	Masterbox v1.0
//	by Ricardo Gerald Garcia Rodriguez - MasterOjitos - http://www.masterojitos.com
//	Last Modification: 20/11/2009
//
//	For more information, visit:
//	http://www.masterojitos.com/projects/masterbox/
//
// -----------------------------------------------------------------------------------

var masterbox = false;

$(window).load(function(){
	$('a').each(function (i){
		if($(this).attr('rel') == "Masterbox"){
			$(this).click(function(){
				Masterbox_params = ($(this).attr('title') == "") ? $(this).attr('rev') : $(this).attr('rev') + ",title=" + $(this).attr('title');
				Masterbox($(this).attr('href'), Masterbox_params);
				return false;						
			});
		}
	});
	$(document.body).append('<div id="Masterbox_container"><div id="Masterbox_head"><div id="Masterbox_title"></div><div id="Masterbox_close" onclick="Masterbox_close()">Close X</div></div><div id="Masterbox_body"></div></div><div id="Masterbox_overlay"></div>');
});

function Masterbox(Masterbox_url, Masterbox_params){
	$("#Masterbox_overlay").css({width: $(window).width() + 'px', height: $(window).height() + 'px'});
	masterbox = true;
	if(Masterbox_url == "") return false;
	Masterbox_title = "";
	Masterbox_width = 800;
	Masterbox_height = 500;
	if(Masterbox_params != null){
		params = Masterbox_params.split(",");
		for(i=0; i<params.length; i++){
			attr = params[i].split("=");
			for(a in vars = ["title", "width", "height"]) if(attr[0] == vars[a]) eval("Masterbox_" + attr[0] + " = '" + attr[1] + "';");
		}
		Masterbox_width = eval(Masterbox_width);
		Masterbox_height = eval(Masterbox_height);
	}
	$("#Masterbox_overlay").css('opacity',.75).fadeIn(1000);
	$("#Masterbox_title").text(Masterbox_title);
	if($.browser.msie){
		$("#Masterbox_container").animate({
			opacity: 0.5,
			width: (Masterbox_width+40) + 'px',
			marginLeft: '-' + ((Masterbox_width+40)/2) + 'px'
		},500).animate({
			opacity: 1,
			height: (Masterbox_height+80) + 'px',
			marginTop: '-' + ((Masterbox_height+80)/2) + 'px'
		},500);
		$("#Masterbox_body").css({width: (Masterbox_width+40) + 'px', 'height': (Masterbox_height+40) + 'px'})
		.html('<iframe style="width:' + (Masterbox_width+40) + 'px; height:' + (Masterbox_height+40) + 'px;" frameborder="0" src="' + Masterbox_url + '"></iframe>')
		.fadeIn(500);
	}else{
		$("#Masterbox_container").animate({
			opacity: 0.5,
			width: (Masterbox_width+40) + 'px',
			marginLeft: '-' + ((Masterbox_width+40)/2) + 'px'
		},500).animate({
			opacity: 1,
			height: (Masterbox_height+80) + 'px',
			marginTop: '-' + ((Masterbox_height+80)/2) + 'px'
		},500,function(){
			$("#Masterbox_body").css({width: (Masterbox_width+40) + 'px', 'height': (Masterbox_height+40) + 'px'})
			.html('<iframe style="width:' + (Masterbox_width+40) + 'px; height:' + (Masterbox_height+40) + 'px;" frameborder="0" src="' + Masterbox_url + '"></iframe>')
			.fadeIn(500);
		});
	}
}

function Masterbox_close(){
	masterbox = false;
	$("#Masterbox_body").empty();
	$("#Masterbox_overlay").fadeOut(1000);
	$("#Masterbox_container").animate({
		opacity: 0.5,
		height: '4px',
		marginTop: '-2px'
	},500).animate({
		opacity: 0,
		width: '4px',
		marginLeft: '-2px'
	},500);
}

document.onkeypress = function hiddenLightbox(e){
	var kC=(window.event)?event.keyCode:e.keyCode;
	if(kC==27 && masterbox){ Masterbox_close(); return false; }
}