/*
 * 	var modalw = new modal_window({jquery: $j, window: $j("#modal"), content: $j("#modalcontent"), width: "600px", height: "400px"});
 *  then: modalw.show(); or modalw.trigger(); 
 */
function modal_window(config) {
	
	this.config = config;
	
	this.j = config.jquery ? config.jquery : $;
	this.wid = config.window;
	this.content = config.content ? config.content : config.window;
	
	this.original_html = this.content.html();
	this.original_width = null;
	this.original_height = null;
	
        this.closed_callback = function() {};

	var self = this;
	
	this.black_bg = this.j("body").append("<div></div>").children("div:last-child").css("display","none").css("background-color", "#000000");
	this.black_bg.click(function() {
		self.trigger();
	});
	
	this.j(window).scroll(function() {
		if (self.wid.is(":visible"))
			self.center(self.wid); 
	});

	this.j(window).resize(function() {
		if (self.wid.is(":visible"))
			self.center(self.wid); 
	});

	this.trigger = function(url) {
		if (!this.wid.is(":visible"))
		{
			this.show(url);
		}
		else
		{
			this.hide();
		}
	};
	
	this.show_bg = function() {
		this.black_bg.css("top","0px").css("left","0px");
		this.black_bg.css("position", "absolute").css("z-index", "9998");
		this.black_bg.css("width", this.j(document).width()).css("height", this.j(document).height());
		this.black_bg.css("opacity", "0.5").fadeIn();
	};
	
	this.show = function(url, fn) {
		if (this.wid.is(":visible"))
			return;
		
		this.show_bg();
		
		// this.wid.css("position", "absolute").css("z-index", "9999");
		this.wid.css("position", "fixed").css("z-index", "9999");
		this.content.html(this.original_html);
		if (self.config.width)
			self.content.css("width", self.config.width);
		if (self.config.height)
			self.content.css("height", self.config.height);
		this.center(this.wid);
		this.wid.fadeIn(function() {
                    self.center(self.wid);
                    if (jQuery.isFunction(url)) {
                      url(self.content);
                    } else if (url)
                    {
                            jQuery.get(url, function(data) {
                                    self.content.html(data);
                                    if (self.config.width)
                                            self.content.css("width", self.config.width);
                                    if (self.config.height)
                                            self.content.css("height", self.config.height);
                                    if (fn) fn();
                            });
                    } else {
                        if (fn) fn();
                    }
      		});

	};
	
	this.display_html = function(html) {
		if (this.wid.is(":visible"))
			return;
		
		this.show_bg();
		this.wid.css("position", "fixed").css("z-index", "9999");
		this.content.html(html);
		if (self.config.width)
			self.content.css("width", self.config.width);
		if (self.config.height)
			self.content.css("height", self.config.height);
		this.center(this.wid);
		this.wid.fadeIn(function() {
			self.center(self.wid);
		});
		
	};
	this.hide = function(callback) {
		if (!this.wid.is(":visible"))
			return;
		this.wid.stop().fadeOut();
		this.black_bg.fadeOut();
                if (this.close_callback) { this.close_callback(); }
                if (callback) callback();
	};
	
	this.center = function(el) {
		this.wid.css("top", ( $j(window).height()/2 - this.wid.height()/2 )  + "px");
		this.wid.css("left", ( $j(window).width()/2 - this.wid.width()/2 )  + "px");
	};
	
	this.center2 = function() {
		$el = this.wid;
		var frm = $('iframe',top.document.body);
		var iframeXOffset = 0, iframeYOffset = 0, windowHeight = 0, windowWidth = 0;
		var i=frm.length;
		while (i--) {
			if (frm[i].contentDocument) {
				doc = frm[i].contentDocument;
			} else {
				doc = frm[i].contentWindow.document;
			}
			if (doc === document) {
				//located our iframe!
				iframeXOffset = $(frm[i]).offset().left;
				iframeYOffset = $(frm[i]).offset().top;
				break;
			}
		};
		if (jQuery.browser.msie) {
			windowWidth = top.window.document.documentElement.clientWidth;
			windowHeight = top.window.document.documentElement.clientHeight;
		} else {
			windowWidth = top.window.innerWidth;
			windowHeight = top.window.innerHeight;
		}
		var elHeight = $el.height();
		var newTop = ((windowHeight/2) - (elHeight/2)) - iframeYOffset + $(parent.document.documentElement).scrollTop();
		if ((newTop + elHeight) > $(document).height()) {
			newTop = $(document).height() - elHeight;
		}
		$el.css ({
			left: ((windowWidth/2) - ($el.width()/2)) - iframeXOffset + $(parent.document.documentElement).scrollLeft(),
			top: newTop
		});
	}

}


