var ModalWindow = Class.create();

ModalWindowOptions = Object.extend({
	overlayOpacity: 0.6,
	animate: true,
	
	windowWidth: 500,
	windowHeight: 300
}, window.ModalWindowOptions || {});

ModalWindow.prototype = {
	
	overlay: undefined,
	modalWindow: undefined,
	modalWindowInstruction: undefined,
	
	initialize: function() {
		this.overlayDuration = ModalWindowOptions.animate ? 0.2 : 0;
		
		this.locateLinks();
		
		var bodyObj = $$('body')[0];
		this.overlay = Builder.node('div', { id: 'overlay' });
		this.modalWindow = Builder.node('div', { id: 'modalWindow' });
		this.modalWindowInstruction = Builder.node('div', { id: 'modalWindowInstruction' });
		
		// Apply some style to the overlay
		this.overlay.setStyle({ 
			position: 'fixed',
			background: '#fff', 
			
			top: '0px', 
			left: '0px', 
			right: '0px', 
			bottom: '0px', 
			
			'z-index': 90
		});
		
		// Apply some settings to the modal window
		this.modalWindow.setStyle({ 
			width: ModalWindowOptions.windowWidth+'px', 
			height: ModalWindowOptions.windowHeight+'px',
			'-moz-border-radius': '8px',
			'-webkit-border-radius': '8px',
			background: '#fff',
			padding: '4px',
			position: 'fixed',
			'z-index': 90,
			border: '1px solid #999'
		});
		
		// Apply some style to the instructions
		this.modalWindowInstruction.setStyle({
			width: ModalWindowOptions.windowWidth+'px', 
			height: ModalWindowOptions.windowHeight+'px',
			position: 'fixed',
			'z-index': 90,
			'text-align': 'center',
			'font-size': '11px',
			color: '#777'
		});
		this.modalWindowInstruction.innerHTML = 'click outside of the box to close it';
		
		this.overlay.hide().observe('click', (function() { this.end(); }).bind(this));
		this.modalWindow.hide();
		this.modalWindowInstruction.hide();
		
		bodyObj.appendChild(this.overlay);
		bodyObj.appendChild(this.modalWindow);
		bodyObj.appendChild(this.modalWindowInstruction);
	},
	
	start: function(link) {
		
		var contentFile = link.readAttribute('content') || link.readAttribute('href');
		var title = link.readAttribute('title') || link.readAttribute('value');
		new Effect.Appear(this.overlay, {duration: this.overlayDuration, from: 0.0, to: ModalWindowOptions.overlayOpacity});
		
		var pageSize = this.getPageSize();
		var w = link.readAttribute('width');
		var h = link.readAttribute('height');
		
		w = (w == undefined) ? ModalWindowOptions.windowWidth : w;
		h = (h == undefined) ? ModalWindowOptions.windowHeight : h;
		
		var y = Math.round((pageSize[1] - h) / 3);
		var x = Math.round((pageSize[0] / 2)  - (w / 2));
		
		this.modalWindow.setStyle({ top: y+'px', left: x+'px', width: w+'px', height: h+'px' });
		
		var padding = ( this.modalWindow.getStyle('padding').gsub(/px/, '') );
		var instructionY = y + parseInt(h) + (padding*2) + 8;
		this.modalWindowInstruction.setStyle({ top: instructionY+'px', left: x+'px', width: w+'px', height: h+'px'});
		
		var params = {};
		var media = contentFile.include("mov") || 
					contentFile.include("wmv") || 
					contentFile.include("mp3") || 
					contentFile.include("swf") || 
					contentFile.include("m4v") || 
					contentFile.include("mp4") || 
					contentFile.include("avi");
		
		this.modalWindow.innerHTML = '<h4 style="padding: 15px 30px; margin: 0px; text-align: center; font-size: 11px;">'+title+'</h4>';		
		if (media) {
			this.modalWindow.innerHTML += '<embed src="'+contentFile+'" />';
		}else{
			new Ajax.Updater(this.modalWindow, contentFile, { method: 'get', parameters: params, insertion: Insertion.Bottom });
		}
		
		this.modalWindow.show();
		this.modalWindowInstruction.show();
	},
	
	end: function() {
		this.modalWindow.hide();
		this.modalWindowInstruction.hide();
		new Effect.Fade(this.overlay, {duration: this.overlayDuration});
	},
	
	locateLinks: function() {
		this.locateLinks = Prototype.emptyFunction();
		
		document.observe('click', (function(event) {
			var target = event.findElement('a[rel^=modal]') || event.findElement('area[rel^=modal]') || event.findElement('input[rel^=modal]');
			if (target) {
				event.stop();
				this.start(target);
			}
		}).bind(this));
	},
	
	/* Get Page size is taken from lightbox by Lokesh Dhakar */
	getPageSize: 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];
	}
	
}

document.observe('dom:loaded', function() {
	new ModalWindow();
});
