/* ------------------------------------------------------------------------
 * simplemodal.js
 * Copyright (c) 2007-2008 Deckmatt, SARL. All rights reserved.
 *
 * Author: Matthieu Fauveau - mfauveau(at)deckmatt(dot)com
 * ------------------------------------------------------------------------ */
var Window = {
	// returns correct dimensions for window has there is sometimes issues with prototype.
	size: function() 
	{
    	var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
    	var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
    	var x = window.pageXOffset || (window.document.documentElement.scrollLeft || window.document.body.scrollLeft);
    	var y = window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop);
    	return {'width': width, 'height': height, 'x': x, 'y': y}
  	}
};

var Simplemodal = {
	IEVersion: function() {
		var regIE = new RegExp("MSIE ([\\d.]+)").exec(navigator.userAgent);
		return regIE?parseFloat(regIE[1]):-1;
	}
}; 

Object.extend(Prototype.Browser, {
	IE6: Prototype.Browser.IE&&(Simplemodal.IEVersion()>=6&&Simplemodal.IEVersion()<7),
	WebKit419: (Prototype.Browser.WebKit&&!document.evaluate)
}); 

Object.extend(Simplemodal, {
	modalId: '',
	
	load: function() {
		if($('simplemodal_overlay')) $('simplemodal_overlay').remove();
		
		var body = $(document.body);
		
		body.insert({ top: (new Element('div', { id: 'simplemodal_overlay' } ).hide()) });		
		var simplemodal_overlay = $('simplemodal_overlay');
		simplemodal_overlay.setStyle({ 			
			position: 'fixed',
			top: '0px',
			left: '0px',
			zIndex: this.options.zIndex,
			backgroundColor: this.options.overlayBackgroundColor,
			height: '100%',
			width: '100%'			
		});	

		if(Prototype.Browser.IE6) {
			simplemodal_overlay.setStyle({
				position: 'absolute',
				height: document.body.clientHeight + "px"
			});
		}
		
		if(!this.options.effects) {
			Element.setOpacity(simplemodal_overlay, (this.options.overlayOpacity/100));
		}
	},
	
	show: function(el, options) {
		try {
			el = $(el);
		} catch(e) {
			return;
		}
		
		this.options = {
			zIndex: 10000,
			overlayOpacity: 25,
			overlayBackgroundColor: '#fff',
			overlayDisplay: true,
			effects: false,
			modalFixed: true
		}
		Object.extend(this.options, options || {});
		
		this.load();
				
		this.modalId = el.id;
		$(el).setStyle({
			position: 'absolute',
			zIndex: (this.options.zIndex+1)
		});
		
		this.center();
		
		if(Prototype.Browser.IE6) {
			this.handleSelectElements('hidden');
		}
		
		if(this.options.effects) {
			var effects = new Array();		
			if(this.options.overlayDisplay) {
				effects.push(new Effect.Appear($('simplemodal_overlay'), { from: 0, to: (this.options.overlayOpacity/100) }));
			}
			effects.push(new Effect.Appear($(this.modalId)));
			new Effect.Parallel(effects);
		}
		else
		{
			if(this.options.overlayDisplay) {
				$('simplemodal_overlay').setStyle({ display: 'block' });
			}
			$(this.modalId).setStyle({ display: 'block' });	
		}	
		
		if(this.options.modalFixed)
		{
			Event.observe(window, 'scroll', this.center);
			Event.observe(window, 'resize', this.center);	
		}		
	},
	
	hide: function() {		
		if(this.options.effects) {
			var effects = new Array();		
			if(this.options.overlayDisplay) {
				effects.push(new Effect.Fade($('simplemodal_overlay')));
			}
			effects.push(new Effect.Fade($(this.modalId)));		
			new Effect.Parallel(effects);
		}
		else
		{
			if(this.options.overlayDisplay) {
				$('simplemodal_overlay').setStyle({ display: 'none' });
			}
			$(this.modalId).setStyle({ display: 'none' });			
		}
		
		if(Prototype.Browser.IE6) {
			this.handleSelectElements('visible');
		}			
		
		if(this.options.modalFixed)
		{	
			Event.stopObserving(window, 'scroll', this.center);
			Event.stopObserving(window, 'resize', this.center);
		}	
	},
	
	center: function() {
		var windowDimensions = Window.size();
		var simplemodalDimensions = $(Simplemodal.modalId).getDimensions();
		//var modalHeight = 660;
		
		var newTop = Math.max((windowDimensions.height/2) - (simplemodalDimensions.height/2) + windowDimensions.y, 0);
		//var newTop = Math.max((windowDimensions.height/2) - (modalHeight/2) + windowDimensions.y, 0);
		
		if (newTop < 10) newTop = 10;
		newTop += "px";
		
		var newLeft = (windowDimensions.width/2) - (simplemodalDimensions.width/2) + "px";
		$(Simplemodal.modalId).setStyle({ top: newTop, left: newLeft });
	},
	
	handleSelectElements: function(visibility) {		
		$$('SELECT').invoke('setStyle', { visibility: visibility });
	}
});