/******************************/
/*                            */
/*        LightWindow         */
/*                            */
/******************************/
var LightWindow = Class.create({   
	initialize: function(options) {
		var options = options || {};
		this.params = {
			'draggable': options.draggable || false,
			'width': options.width || '800px',
			'height': options.height || 'auto',
			'url': options.url || false,
			'content': options.content || "",
			//'class': options.class || "",
			'parameters': options.parameters || {},
			'top': options.top || null,
			'left': options.left || null,
			'remove': options.remove || false
		}
		
		// create box
		this.boxObj = new Element('div',{'class': 'lightWindow'});
		this.boxObj.className = 'lightWindow';
		this.boxObj.insert('<div class="tl"></div><div class="t"></div><div class="tr"></div><div class="contentContainer"><div class="l"></div><div class="r"></div><div class="content"></div></div><div class="bl"></div><div class="b"></div><div class="br"></div>'); // insert the needed html structure
		// create close element
		this.closeObj = new Element('a',{'class': 'close', 'href': 'javascript:void(0);'}); 
		this.closeObj.className = 'close';
		if(this.params.remove){
			this.closeObj.close = function(){ // extend oject with close function
				this.previous('div.contentContainer').down('div.content').innerHTML = ""; // remove object
				this.up('div.lightWindow').hide();
			}
		}
		else{
			this.closeObj.close = function(){ // extend oject with close function
				this.up('div.lightWindow').hide(); // remove object
			}
		}
		this.boxObj.insert(this.closeObj); // insert close button
		this.closeObj.observe("click", function(){this.close()});
		// write to page
		$(document.body).insert(this.boxObj);
		// make sure the element is hidden
		this.hide();
		// define contentElement
		this.contentObj = this.boxObj.down('div.content');
		// mage draggable
		if(this.params.draggable) new Draggable(this.boxObj, { scroll: window, starteffect: "", endeffect: "", handle: this.boxObj.down('div.t') });
		// set size
		this.setWidth();
		this.setHeight();
		// load ajax
		if(this.params.url) this.ajax(this.params.url, this.params.parameters);
		else if (this.params.content) this.load(this.params.content);
	},
	ajax: function(url,params){
		this.load("<img alt='Please Wait' src='images/icon_wait.gif'/>");
		var obj = this;
		//ayax call
		new Ajax.Request(url, {
				method: 'post',
				parameters: params,
				onSuccess: function(transport){
					obj.load(transport.responseText)
				},
				onFailure: function(transport){
					obj.load("error: " + transport.status + " - " + transport.statusText);
				}
		});
	},
	load: function(data){
		this.contentObj.innerHTML = "";
		this.insert(data);
		this.defaultPosition();
		this.show();
	},
	insert: function(data,params){
		this.contentObj.insert(data,params);
		this.boxObj.style.height = "";
		this.boxObj.style.height = this.boxObj.getHeight()+'px';
	},
	show: function(){
		this.boxObj.show();
	},
	hide: function(){
		this.boxObj.hide();
	},
	remove: function(){
		this.boxObj.remove();
	},
	clear: function(){
		this.contentObj.innerHTML = "";
	},
	getContentObj: function(){
		return this.contentObj;
	},
	setWidth: function(width){
		this.boxObj.style.width = width? width : this.params.width;
	},
	setHeight: function(height){
		//this.boxObj.style.height = height? height : this.params.height;
		//this.contentObj.style.height = height? parseInt(height)-72+"px" : parseInt(this.params.height)-72+"px";
	},
	defaultPosition: function(){
		this.center();
		if (this.params.left != null) this.boxObj.style.left = this.params.left;
		if (this.params.top != null) this.boxObj.style.top = this.params.top;
	},
	center: function(){
		var viewport = document.viewport.getDimensions();
		var scroll = document.viewport.getScrollOffsets();
		var mywindow = this.boxObj.getDimensions();
		this.setPosition((viewport.width - mywindow.width)/2, ((viewport.height - mywindow.height)/2)+scroll.top)
	},
	left: function(left){
		this.boxObj.style.left = left +'px';
		this.params.left=left+"px";
	},
	top: function(top){
		this.boxObj.style.top = top +'px';
		this.params.top=top+"px";
	},
	setPosition: function(left,top){
		this.boxObj.style.left = left +'px';
		this.boxObj.style.top = top +'px';
	}
});