/****************************
 LightFrame
 - iframe in lightbox
 - by flux 21/12/2009
****************************/

function LightFrame() {
	this.OPACITY = 0.7;
	this.STYLE_BOX = 
		"position:absolute;top:0;left:0;width:100%;display:none;z-index:1001";
	this.STYLE_BG = 
		"position:absolute;top:0;left:0;width:100%;background-color:#000;";
	this.STYLE_PANEL = 
		"position:absolute;top:0;left:0;";
	this.STYLE_FRAME = 
		"position:absolute;top:0;left:0;width:{w}px;height:{h}px;background-color:#FFF;display:none;";
	
	LightFrame.prototype.frame = $("<iframe frameborder='0'></iframe>")
		.attr("scrolling", "no");
	LightFrame.prototype.box = $("<div></div>")
		.attr("style", this.STYLE_BOX);
	LightFrame.prototype.panel = $("<div></div>")
		.attr("style", this.STYLE_PANEL).append($('<a href="javascript:$.LightFrame.close();"><img src="/2010/resource/images/closebtn.gif" width="128" height="32" border="0" /></a>'));
	LightFrame.prototype.bg = $("<div></div>")
		.attr("style", this.STYLE_BG)
		.css("opacity", "0")
		.height( $(document).height() )
		.append(LightFrame.prototype.panel);

	LightFrame.prototype.open = function (url, fWidth, fHeight) {
		this.frame
			.attr("style", this.STYLE_FRAME.replace("{w}", fWidth).replace("{h}", fHeight))
			.attr("src", url);
		this.box.show();
		this.bg.
			height( $(document).height() ).
			fadeTo("normal", this.OPACITY, function () {
			LightFrame.prototype.frame.show();
			LightFrame.prototype.update();
		})
	}

	LightFrame.prototype.close = function () {
		LightFrame.prototype.frame.attr("src", "").hide();
		LightFrame.prototype.bg.fadeTo("normal", "0", function(){
			LightFrame.prototype.box.hide();
		});
	}

	LightFrame.prototype.update = function () {
		if (this.frame.is(":visible"))
		{
			this.frame.css({
				left:($(window).width() - this.frame.width()) / 2 + $(window).scrollLeft(),
				top:($(window).height() - this.frame.height()) / 2 + $(window).scrollTop()
			});

			this.panel.css({
				left:($(window).width() - this.frame.width()) / 2 + $(window).scrollLeft(),
				top:($(window).height() - this.frame.height()) / 2 + $(window).scrollTop() - this.panel.height()			
			});
		}
	}

	this.bg.click(this.close);
	this.box.append(this.bg);
	this.box.append(this.frame);
	$("body").append(this.box);	

	$(window)
		.resize(function (){
			LightFrame.prototype.update();
		})
		.scroll(function (){
			LightFrame.prototype.update();
		});
}

$(window).ready(function (){
	$.LightFrame = new LightFrame();
});
