/*
@version:
..haha, ALPHA!

@TODO:
- ... ;)

@copyright:
deepblue networks AG / Julian Gieseke
*/
var simpleLayer = Class.create({
	initialize: function(url, options) {
		this.setDefaults();
		
		this.url = url;
		if(options) {
			Object.extend(this.options, options);
		}
		
		this.buildLayer();
		
		switch(this.options.contentType) {
			case 'html':
				this.html();
				break;
			case 'image': 
			default:
				this.image();
		}
		
		if(this.options.realign) {
			Event.observe(window, 'scroll', this.position.bind(this));
		}
	},
	setDefaults: function(){
		this.dom = {};
		this.height=0;
		this.width=0;
		this.offset={};
		this.options = {
			animation:false,
			fixed:true,
			overlay:true,
			height:500,
			width:500,
			padding:11,
			parent:$$('body')[0],
			contentType:'image',
			realign:false,
			postBody: {},
			htmlFile: 'show.php'
		}
	},
	image: function(){
		this.dom.layer.addClassName('simpleLayerImageLayer');
		this.dom.image = new Element('img');
		
		var imgPreloader = new Image();
        imgPreloader.onload = (function(){
			this.dom.image.src = this.url;
			this.width = imgPreloader.width;
			this.height = imgPreloader.height;
			this.dom.content = this.dom.image;
			this.display();
        }).bind(this);
        imgPreloader.src = this.url;
	},
	html: function(){
		//console.log('unfinished');
		this.dom.layer.addClassName('simpleLayerHTMLLayer');
		this.options.postBody.image = this.url;
		//console.log(this.options.postBody);
		new Ajax.Request(this.options.htmlFile, {
			method:'post',
			parameters:this.options.postBody,
			onComplete: function(response){
				this.height = this.options.height;
				this.width = this.options.width;
				this.dom.content = response.responseText;
				this.display();
			}.bind(this),
			onFailure: function(){
				this.destroy();
			}.bind(this)
		});
	},
	updateLayerContent: function(){
		//console.log(this.dom.content);
		this.dom.layerContent.update(this.dom.content);
		this.dom.layerContent.fire('simpleLayer:done');
	},
	buildLayer: function(){
		if(this.options.overlay) {
			this.dom.overlay = new Element('div', {'id':'simpleLayerOverlay'});
			this.options.parent.insert({'top':this.dom.overlay});
			this.dom.overlay.setStyle({height: this.getWindowSize()[1]+'px'});
		}
		this.dom.layer = new Element('div', {'id':'simpleLayer'});
		this.options.parent.insert({'top':this.dom.layer});
		
		this.dom.layerWrap = new Element('div', {'id':'simpleLayerWrap'});
		this.dom.layer.insert(this.dom.layerWrap);
		
		this.dom.closeLink = new Element('a', {'id':'simpleLayerCloseLink', 'href':'#close'}).update('close');
		this.dom.layerWrap.insert(this.dom.closeLink);
		this.dom.closeLink.observe('click', function(evt){
			evt.stop();
			this.destroy();
		}.bind(this));
		
		this.dom.layerContent = new Element('div', {'id':'simpleLayerContent'});
		this.dom.layerWrap.insert(this.dom.layerContent);
		
		this.dom.layer.hide();
	},
	position: function(){
		var scrollOffset = document.viewport.getScrollOffsets();
		var leftCenter = scrollOffset[0] + document.viewport.getWidth();
		var topCenter = scrollOffset[1] + document.viewport.getHeight();
		
		this.offset.left = scrollOffset[0] + (document.viewport.getWidth()-(this.width+(this.options.padding*2)))/2;
		this.offset.top = scrollOffset[1]; //+ Math.round((document.viewport.getHeight()-(this.height+(this.options.padding*2)))/2);
		
		this.dom.layer.setStyle({
			'top':this.offset.top+'px',
			'left':this.offset.left+'px'
		});
		this.dom.layerWrap.setStyle({
			'width':this.width+'px',
			'height':this.height+'px'
		});
	},
	display: function() {
		if(this.options.customClass){
			this.dom.layer.addClassName(this.options.customClass);
		}
		this.position();
		this.updateLayerContent();
		if(this.options.animation) {
			this.dom.layer.blindDown({duration:0.5});
		} else {
			this.dom.layer.show();
		}
	},
	destroy: function(){
		if(this.options.animation) {
			this.dom.layer.blindUp({duration:0.3, afterFinish: function(){
				this.dom.overlay.remove();
				this.dom.layer.remove();
				this.dom.image = null;
				this.setDefaults();
			}.bind(this)});
		} else {
			this.dom.overlay.remove();
			this.dom.layer.remove();
			this.setDefaults();
		}
	},
	//took from: no idea.. 
	getWindowSize: function() {
	        
	    var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
});