/*
	Modal v2.3 2010-05-03

	Made in Imperavi. All rights reserved.

	http://www.imperavi.com

	Code is released under a GNU General Public Licens http://www.opensource.org/licenses/gpl-license.php
*/
var Modal = Class.extend({
	init: function(options)
	{
		/*
			Options
		*/
		this.options = {
			url: false,
			callback: false,
			start: false,
			end: false,
			cache: false,
			loader: true,
			triggerClose: false,
			title: 'Modal Window',
			drag: false,
			width: 450,
			height: 450,
			sizeWidthValue: 'px',
			sizeHeightValue: 'px',
			overlay: true,
			overlayClose: true,
			fixed: true,
			position: false,
			top: false,
			left: false,
			right: false						
		}
		
		$.extend(this.options, options);
		
		/*
			Handlers
		*/
		this.closeHandler = function() { this.hide(); }.bind(this);
		this.keypressHandler = function( e ) { if( e.keyCode == 27) this.hide(); }.bind(this)

		/*
			Size value
		*/
		if (/px/.test(this.options.width)) this.options.width = new Number((this.options.width.replace('px','')));
		else if (/%/.test(this.options.width))
		{
			this.options.sizeWidthValue = '%';
			this.options.width = new Number((this.options.width.replace('%','')));
		}		
		
		if (/px/.test(this.options.height)) this.options.height = new Number((this.options.height.replace('px','')));
		else if (/%/.test(this.options.height))
		{
			this.options.sizeHeightValue = '%';
			this.options.height = new Number((this.options.height.replace('%','')));
		}		
		
		/*
			Position
		*/
		if (this.options.top && this.options.left)
		{
			this.options.top = this.normalize(this.options.top);
			this.options.left = this.normalize(this.options.left);			
			this.options.position = 'left';
		}
		else if (this.options.top && this.options.right)
		{
			this.options.top = this.normalize(this.options.top);
			this.options.right = this.normalize(this.options.right);						
			this.options.position = 'right';		
		}
		
  
  		/*
  			Build
  		*/  		
		if (this.options.start) this.options.start();  		

  		this.build();

		if ($.browser.msie) this.fixIE("100%", "hidden");
		
	},
	modalCreate: function()
	{
		this.modal = $('<div id="cmts_modal" style="display: none;"><div id="cmts_modal_header"><div id="cmts_modal_title"></div><span id="cmts_modal_close"></span></div><div id="cmts_modal_content"></div></div>');
		$(this.modal).appendTo('body');
		
		if (this.options.fixed) $('#cmts_modal').css('position', 'fixed');
		else $('#cmts_modal').css('position', 'absolute');		


		if (this.options.position === false)
		{
			if (this.options.height !== false) $('#cmts_modal').css({'top': '50%', 'left': '50%', 'margin-top': '-' + (this.options.height/2) + this.options.sizeHeightValue, 'margin-left': '-' + (this.options.width/2) + this.options.sizeWidthValue});		
			else $('#cmts_modal').css({'top': '50px', 'left': '50%', 'margin-left': '-' + (this.options.width/2) + this.options.sizeWidthValue});		
		}
		else if (this.options.position == 'left') $('#cmts_modal').css({'top': this.options.top + 'px', 'left': this.options.left + 'px'});		
		else if (this.options.position == 'right') $('#cmts_modal').css({'top': this.options.top + 'px', 'right': this.options.right + 'px'});		
		
		$('#cmts_modal_close').click(this.closeHandler);	
		
		if (this.options.drag)
		{	
			$('#cmts_modal_title').css('cursor', 'move');
			$('#cmts_modal').draggable({ handle: '#modal_title' });
		}
	},
	overlayCreate: function()
	{
		this.overlay = $('<div id="cmts_modal_overlay" style="display: none;"></div>')
		$(this.overlay).appendTo('body');	
	},	
	load: function()
	{
		this.modal.show();

		$('#cmts_modal_title').text(this.options.title);
		

		if (this.options.height === false) var height = 'auto';
		else var height = this.options.height + 'px';
		
		$('#cmts_modal').css({ height: height, width: this.options.width + 'px' });

		var pbottom = this.normalize($('#cmts_modal_content').css('padding-bottom'))
		var ptop = this.normalize($('#cmts_modal_content').css('padding-top'))

		if (this.options.height === false) var content_height = 'auto';
		else var content_height = this.options.height - ptop - pbottom - $('#cmts_modal_header').get(0).offsetHeight + 'px';

		if (this.options.loader) $('#cmts_modal_content').css('height', content_height).html('<div id="cmts_modal_loader"></div>');
		        
		$.ajax({ url: this.options.url, cache: this.options.cache, success: function(data)
		{
			$('#cmts_modal_content').html(data);
        	if (this.options.triggerClose) 
        	{
        		$.each(this.options.triggerClose, function(i,s)
        		{
	        		$('#' + s).click(this.closeHandler);
        		}.bind(this));
        		
        	}
        	
        	if (this.options.end) this.options.end();      	
        	
		}.bind(this)});	
	    
		$(document).keyup(this.keypressHandler);
	},
	build: function()
	{
		if ($('#cmts_modal').get(0))
		{
			this.modal = $('#cmts_modal');
			if (this.options.overlay) this.overlay = $('#cmts_modal_overlay');
			this.show();
		}
		else
		{
			this.modalCreate();
			if (this.options.overlay) this.overlayCreate();
			this.show();
		}
	},
	show: function()
	{
		if (this.options.overlay && this.options.overlayClose) $(this.overlay).click(this.closeHandler);					  	
	 
	  	if (this.options.overlay) 
	  	{
	  		this.overlay.show();
	  		this.load();
		}
		else this.load();
	},
	hide: function()
	{
		if (this.options.overlay) 
		{
			this.modal.hide();
			this.overlay.hide();
		}
		else 
		{
			this.modal.hide();
		}
		
		if ($.browser.msie) this.fixIE("", "");	
		if (this.options.overlayClose) $(this.overlay).unbind('click', this.closeHandler);

		$(document).unbind('keypress', this.keypressHandler);

		if (this.options.callback) this.options.callback();

		
	},
	fixIE: function(height, overflow)
	{
		$('html, body').css({'width': height, 'height': height, 'overflow': overflow});
		$("select").css('visibility', overflow);
	},	
	normalize: function(str)
	{
		return new Number(new String(str).replace('px',''));
	}
});

