img_extensions = new Array('jpg', 'jpeg', 'png', 'gif', 'bmp');
content_types  = new Array("html", "image", "url");

var Lightwindow = Class.create({
	initialize: function(content, style) {
		this.content     = content;
		this.style       = style || null;
		
		this.display();
	},
	getType: function() {
		var url_regexp = /^\/[\w\-\/]+(\.[a-zA-Z]+)?(\?.+)?$/;
		var img_regexp = /\.([a-zA-Z]+)$/;
		
		// Check if url
		if (url_regexp.test(this.content)) {
			// Check if image :
			var extensions = this.content.match(img_regexp);
			if (extensions != null) {
				for (ext=0; ext<extensions.length; ext++) {
				    for(i=0; i<img_extensions.length; i++) {
				    	if (extensions[ext] == img_extensions[i]) {
				    		return "image";
				    	}
				    }
				}
			}
			
			return "url";
		}
		
		return "html";
	},
	
	display: function() {
		// Erase existing elements
		if ($('lightwindow_mask')) document.body.removeChild($('lightwindow_mask'));
		if ($('lightwindow_body')) document.body.removeChild($('lightwindow_body'));
		
		// create/append element
	    document.body.appendChild(this._getMask());
	    document.body.appendChild(this._getBody());
	
	    //display all
	    this._loadContent();
	    return false;
	},
	_getMask: function() {
		var mask = document.createElement('div');
		mask.setAttribute("id", "lightwindow_mask");
		return mask;
	},
	_getBody: function() {
		var lightbody = document.createElement('div');
		lightbody.setAttribute("id", "lightwindow_body");

	    lightbody.appendChild(this._getWindow());
		
		return lightbody;
	},
	_getWindow: function() {
		var lightcontent = document.createElement('div');
		lightcontent.setAttribute("id", "lightwindow_content");
		
		var lightwindow = document.createElement('div');
		lightwindow.setAttribute("id", "lightwindow_window");
		lightwindow.style.display = "none";
		if (this.style != null) Element.setStyle(lightwindow, this.style);

	    lightwindow.appendChild(lightcontent);
	    lightwindow.appendChild(this._getCloseButton());
	    
		return lightwindow;
	},
	_getCloseButton: function() {
		var lightclose = document.createElement('input');
		lightclose.setAttribute("id", "lightwindow_close");
		lightclose.setAttribute("type", "button");
		lightclose.setAttribute("value", ' ');
		lightclose.onclick = this._close;
		
		return lightclose;
	},
	_loadContent: function() {
		var contentType = this.getType();
		
	    document.body.style.overflow = "hidden";
	    if (contentType == "image") {
	    	$('lightwindow_content').innerHTML = '<img src="'+this.content+'" alt=""/>';
	    } else if (contentType == "url") {
	    	new Ajax.Updater('lightwindow_content', this.content, { method: 'get', evalScripts: true, onComplete: Lightwindow._postinit });
	    } else {
	    	$('lightwindow_content').innerHTML = this.content;
	    }

	    new Effect.Appear('lightwindow_window', {duration: 0.8, queue: 'end'});
	    //$('lightwindow_window').style.display = "block";
	
	    $('lightwindow_window').scrollTo();
	},
	_close: function() {
		// Erase existing elements
		if ($('lightwindow_mask')) document.body.removeChild($('lightwindow_mask'));
		if ($('lightwindow_body')) document.body.removeChild($('lightwindow_body'));
	    document.body.style.overflow = "auto";
	}
});

Lightwindow._close = function() {
	// Erase existing elements
	if ($('lightwindow_mask')) document.body.removeChild($('lightwindow_mask'));
	if ($('lightwindow_body')) document.body.removeChild($('lightwindow_body'));
    document.body.style.overflow = "auto";
}

Lightwindow._postinit = function() {
	//if (typeof(Tabs) != "undefined") { Tabs.initTabs(); }
}
