/*
	Plugin Modal v2.5.0
	http://imperavi.com/
 
	Copyright 2010, Imperavi Ltd.
	Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function($){
		
	// Initialization
	$.modal = function(options)
	{
		var obj = new Construct(options);
		obj.init();
		
		// Global
		$.modalClose = function() {
			obj.close();
		};	
		
		$.modalContent = function(content) {
			obj.content(content);
		};	
				
		$.modalTitle = function(title) {
			obj.title(title);
		};	
		
		return obj;
	};
	
	// Options and variables
	function Construct(options)
	{
		this.opts = $.extend({
			url: false,	
			start: false,
			end: false,
			callback: false,
			loader: true,		
			title: 'Modal Window',

			width: 500,
			height: 250,
			
			fixed: false,
			position: false,
			top: false,
			left: false,
			right: false,
			
			className: false,	
			
			sizeWidthValue: 'px',
			sizeHeightValue: 'px',				
			
			overlay: true,
			overlayColor: '#000',
			overlayClose: true,
			overlayOpacity: 0.8
						
		}, options);
	
		this.escHandler = function(e) { if( e.keyCode == 27) this.close(); }.bind(this); 	
		this.closeHandler = function() { this.close(); }.bind(this); 	
	};

	// Functionality
	Construct.prototype = 
	{
		init: function() 
		{
			// overlay
			if (this.opts.overlay)
			{
				if ($('#imp_modal_overlay').size() == 0)
				{
					this.buildOverlay();
					this.showOverlay();				
				} 
				else
				{	
					this.overlay = $('#imp_modal_overlay');
					this.showOverlay();
				}
			}
			
			// window
			if ($('#imp_modal').size() == 0)
			{
				this.build();
				this.open();
			}
			else
			{
				this.$el = $('#imp_modal');
				this.open();
			}
		
			if (this.opts.start) this.opts.start();  		
			
			$(document).keyup(this.escHandler);
			
			//if ($.browser.msie) this.fixIE("100%", "hidden");					
		},
		
		// overlay
		buildOverlay: function()
		{
			this.overlay = $('<div>').attr('id', 'imp_modal_overlay').css({ backgroundColor: this.opts.overlayColor, display: 'none', opacity: this.opts.overlayOpacity });
			$(this.overlay).appendTo('body');	
		},
		showOverlay: function()
		{
			$(this.overlay).show().click(this.closeHandler);
		},
		hideOverlay: function()
		{
			if (this.opts.overlay) $(this.overlay).hide().unbind('click', this.closeHandler);	
		},		
		
		// window
		build: function()
		{
			this.modal_header = $('<div>').attr('id', 'imp_modal_header');
			this.modal_inner = $('<div>').attr('id', 'imp_modal_inner');			
			this.modal_title = $('<div>').attr('id', 'imp_modal_title');
			this.modal_close = $('<a>').attr({ id: 'imp_modal_close', href: 'javascript:void(null);' }).html('&nbsp;').click(this.closeHandler);	
			this.modal_content = $('<div>').attr('id', 'imp_modal_content');	
			
			this.$el = $('<div>').attr('id', 'imp_modal');	

			this.modal_header.append(this.modal_title).append(this.modal_close);

			if(this.opts.height === false) this.opts.height = 'auto';
			
			this.modal_inner.append(this.modal_header).append(this.modal_content);
			
			this.$el.append(this.modal_inner).hide();	
			this.$el.appendTo('body');		
			

									
		},
		open: function()
		{

			
			if(this.opts.height === false) this.opts.height = 'auto';
			
			this.$el.css({ width: this.opts.width });			
		

			if (this.opts.fixed) $('#imp_modal').css('position', 'fixed');
			else $('#imp_modal').css('position', 'absolute');		
				
			if (this.opts.className !== false) $('#imp_modal').addClass('modal_order');	
			else $('#imp_modal').removeClass('modal_order');	
			
			if ($.browser.msie) $('#imp_modal').css('border', '1px solid #ccc');
				
			if (this.opts.fixed === false) 
			{
				if (this.opts.position === false) 
				{
					$('#imp_modal').css({ 'top': '50%', 'left': '50%', 'margin-left': '-' + (this.opts.width/2) + 'px', 'margin-top': '-' + ((this.opts.height/2)+80) + 'px' });		
				}
				else 
				{
					$('#imp_modal').css({ 'top': this.opts.top, 'margin-top': 'auto', 'left': '50%', 'margin-left': '-' + (this.opts.width/2) + 'px' });		
				}
			}
			else
			{
				$('#imp_modal').css({ 'top': '50%', 'left': '50%', 'margin-left': '-' + (this.opts.width/2) + 'px', 'margin-top': '-' + ((this.opts.height/2)) + 'px' });						
			}


			var self = this;

			$.ajax({
				url: this.opts.url,
				cache: false, 
				success: function(data)
				{
					self.title(self.opts.title);
					self.content(data);
					
					self.$el.show();		

	        		if (self.opts.end) self.opts.end();      		        	
				}
			});			
		},
		close: function()
		{
			this.$el.hide();
			this.hideOverlay();
			$(document).unbind('keypress', this.escHandler);	
			
			if ($.browser.msie) this.fixIE("", "");		
			if (this.opts.callback) this.opts.callback();			
		},
		
		content: function(content)
		{
			$('#imp_modal_content').html(content);
		},		
		title: function(title)
		{
			$('#imp_modal_title').html(title);		
		},
		
		// utility
		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',''));
		}			
	};
	
	// bind
	Function.prototype.bind = function(object)
	{
	    var method = this; var oldArguments = $.makeArray(arguments).slice(1);
	    return function (argument)
	    {
	        if (argument == new Object) { method = null; oldArguments = null; }
	        else if (method == null) throw "Attempt to invoke destructed method reference.";
	        else { var newArguments = $.makeArray(arguments); return method.apply(object, oldArguments.concat(newArguments)); }
	    };
	}		

})(jQuery);

