/**
		LightWindow by Mattias Bohez
		Copyright Walking Men 2010
**/
var LightWindow = Class.create({   
	initialize: function(options) {
		var options = options || {};
		this.params = {
			'draggable': options.draggable || false,
			'width': options.width || 'auto',
			'height': options.height || 'auto',
			'maxWidth': options.maxWidth || 'auto',
			'maxHeight': options.maxHheight || 'auto',
			'url': options.url || false,
			'content': options.content || "",
			'flash': options.flash || {},
			'parameters': options.parameters || {},
			'top': options.top || null,
			'left': options.left || null,
			'remove': options.remove || false,
			'loading': options.loading || "Loading...",
			'modal': options.modal || true
		};
		
		this.create();
		
		// load content
		if(this.params.url) this.ajax(this.params.url, this.params.parameters);
		else if (this.params.content) this.load(this.params.content);
		else if (this.params.flash) this.flash(this.params.flash.swf,this.params.flash.width,this.params.flash.height,this.params.flash.flashvars,this.params.flash.params,this.params.flash.attributes);
	},
	create: function(){
		// 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';
		this.boxObj.insert(this.closeObj); // insert close button
		
		// create Modal overlay
		this.overlay = new Element('div',{'class':'fullOverlay'});
		this.overlay.className = 'fullOverlay';
		var bodyHeight = $(document.body).getHeight();
		var viewportheight = document.viewport.getHeight();
		var modalHeight = (bodyHeight > viewportheight)? bodyHeight : viewportheight;
		this.overlay.setStyle({
			position:'absolute',
			top: 0,
			left:0,
			width: '100%',
			height: modalHeight + 'px',
			opacity: .70,
			display: 'none'
		});
		
		// close handlers
		var oThis = this;
		if(!this.params.remove) {
			this.closeObj.observe("click", function(){oThis.hide();});
			this.overlay.observe("click", function(){oThis.hide();});
		}
		else{ 
			this.closeObj.observe("click", function(){oThis.remove();});
			this.overlay.observe("click", function(){oThis.remove();});
		}
		
		// write to page
		$(document.body).insert(this.overlay);
		$(document.body).insert(this.boxObj);
		
		// store border widths for size calculations
		this.verticalBorder = this.boxObj.down('.t').getHeight() + this.boxObj.down('.b').getHeight();
		this.horizontalBorder = this.boxObj.down('.l').getWidth() + this.boxObj.down('.r').getWidth();
		
		// 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') });
		
		this._inDom = true;
	},
	ajax: function(url,params){
		this.load(this.params.loading);
		var oThis = this;
		//ayax call
		new Ajax.Request(url, {
				method: 'get',
				parameters: params,
				onSuccess: function(transport){
					oThis.load(transport.responseText);
				},
				onFailure: function(transport){
					oThis.load("error: " + transport.status + " - " + transport.statusText);
				}
		});
	},
	load: function(data){
		if(this._inDom == false) this.create();
		this.contentObj.innerHTML = "";
		this.insert(data);
		this.show();
	},
	flash: function(swf,width,height,flashvars,params,attributes){
		var flashvars = flashvars || {};
		var params = params || {};
		var attributes = attributes || {};
		var width = width || 640;
		var height = height || 480;
		var id = "flash_"+Math.random();
		
		this.load("<div id=\""+id+"\" style=\"width:"+width+"px;height:"+height+"px;\"></div>");
		
		swfobject.embedSWF(swf, id, width, height, "9.0.0","expressInstall.swf", flashvars, params, attributes);
	},
	insert: function(data,params){
		this.contentObj.insert(data,params);
	},
	show: function(){
		this.boxObj.show();
		if(this.params.modal) this.overlay.show();
		this.resetSize();
		this.defaultPosition();
	},
	hide: function(){
		this.boxObj.hide();
		this.overlay.hide();
	},
	remove: function(){
		this.boxObj.remove();
		this.overlay.remove();
		this._inDom = false;
	},
	clear: function(){
		this.contentObj.innerHTML = "";
	},
	close: function(){
		if(this.params.remove){
			this.remove();
		}
		this.hide();
	},
	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;
	},
	resetSize: function(){
		this.boxObj.style.height = 'auto';
		this.boxObj.style.width = 'auto';
		
		this.contentObj.setStyle('float:left');
		var contentSize = this.contentObj.getDimensions();
		this.contentObj.setStyle('float:none');
		
		if (contentSize.width > this.params.maxWidth) contentSize.width = this.params.maxWidth;
		if (contentSize.height > this.params.maxHeight) contentSize.height = this.params.maxHeight;
		
		this.boxObj.style.height = contentSize.height + this.verticalBorder + 'px';
		this.boxObj.style.width = contentSize.width + this.horizontalBorder + 'px';
	},
	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)
	},
	setPosition: function(left,top){
		this.boxObj.style.left = left +'px';
		this.boxObj.style.top = top +'px';
	}
	
});
