(function($) {
$.fn.ui_modalbox = function(options) {		
		
	options = options || {};	
		
	if ($(this).is('a , img , button') ) {	
	
		this.each(function() {
		
		/* $(this).live("click", function(event){ event.preventDefault();}); */
	
			$(this).bind('click', function(){
	
				if ($(this).is('a')) { 
		 	
		 			var content = options.content ||  $(this).attr('href');
		 			
		 	
		 		} else if ($(this).is('img')) { 
		 			
		 			var content = options.content ||  $(this).attr('src');
		 		
		 		}
		 
					var title = options.title || $(this).attr('title');
				

				 object_options= $.extend({ 'content': content, 'title': title }, options);
				
				return this, ui_modalbox (object_options), false;
				 

  	
			})
	

  		})
  	
  	} else {
 	
		this.each(function() {
			
			$(this).bind('click', function(){
			
				return this, ui_modalbox (options), false;
			}) 	
		})	
  	}


  
};

ui_modalbox  = function (options) { 

		return new ui_fn_modalbox (options) , false ;

};

ui_fn_modalbox = function (options) {  

	this.settings= $.extend({}, $.fn.ui_modalbox.defaults, options);
	
	this.content = this.settings.content;
	
	if (this.settings.filter) { this.filter(); }
		
	this.ui_actions = ['ui_center','ui_click_to_front','ui_draggable']
				
	this.box_properties= ['type','content','slideshow','callback','speed','effect','filter','message','redirect','css']
	
	this.box_constructor = ['wrapper','header','body','footer','closeable']
				
	this.type();

	return false ; 

};
	

ui_fn_modalbox.prototype = {
		
	
	body: function () {
		
		
		return $(this.content).appendTo(this.box);	
	
	},		
	
	buttons: function () {

	
		var self = this;
		
		this.buttonPane = jQuery('<div/>', { 'class': 'ui-modalbox-buttonpane'});			
				
		var buttons = this.settings.buttons;
	
	

		$.each(buttons, function(name, fn) {
		
		/* ie compatibilty issue with button type */
		
		   if($.browser.msie){
			   
			   $('<button/>', {'class':'ui-button ui-corner-all',click: function(){$(self.box).ui_bind_function(fn)}}).text(name).appendTo(self.buttonPane)
      
   			} else {
   
   				$('<button/>', { 
   								type: 'button',
   								'class':'ui-button ui-corner-all',
   								click: function(){$(self.box).ui_bind_function(fn)}
   								 }).text(name).appendTo(self.buttonPane);
   
   			}	
		});
			 
			return $(this.buttonPane).appendTo(self.box);				 	
	},
	

	
	close: function () {

			this.box.ui_hide(this.settings.effect,this.settings.speed,function() {$(this).remove()})		
			if (this.overlay){this.overlay.ui_hide(this.settings.effect,this.settings.speed,function() {$(this).remove();})}

		
	},
	
	closeable: function () {
	
		var self= this;

		this.closeButton = jQuery('<a/>', {
							'class': 'ui-modalbox-close',
							click: function(){
							    self.close()
							 	}
    						}).ui_set_to_front();
    
    		
		if (this.settings.header){$(this.header).append(this.closeButton )} 
		
		else 
		
		if (this.settings.footer){$(this.footer).append(this.closeButton )}
		
		else {$(this.box).prepend(this.closeButton )}
		

		
		return						
	},
	

	createBox: function () {
		
		var self = this;
			
		 this.box.css({opacity:0}).prependTo(document.body).ui_set_to_front();


			$('.ui-modalbox-content', this.box).ui_resize_to_container(window)

		 		
			for (var action in self.ui_actions){ 
				
					self.box[self.ui_actions[action]]();
					
				}
		
		self.resize_to_content()
		self.box.ui_click_to_front()
		return	self.box
	
	},
	
					
	filter: function () {
	
	if (this.content == '#') { this.content = document.body}
	
			this.content = $(this.content).find(this.settings.filter).clone(true).show()
		
		return this.content;			
	},		
					
	footer: function () {
		
		return this.footer = jQuery('<div/>', {'class': 'ui-modalbox-footer ui-draggable'}).append(this.settings.footer).appendTo(this.box)			
	},		


	form : function () {
	
		var self = this
				
		this.settings.buttons={ submit:function () {self.form_submit(); } , cancel:function () { self.close(); }}
		
		return this.buttons()
	
	},
	
	form_submit : function () {
	
		var self =this;
		
		if (this.settings.form === 'html') {
		
			this.form =  $('form' ,this.box)
		
			this.form.submit()
		
		} else if (this.settings.form === 'iframe') {
		
			this.form =  $('iframe' ,this.box).contents().find('form');
	
			
			ui_overlay(this.form);
			
			this.form.submit();
			
		
		} else if (this.settings.form === 'ajax') {
		
			this.form =  $('form' ,this.box)
		
			var post_url = this.form.attr('action')	
			
			var ajax = {
				url: post_url, 
				type: 'POST', 
				data: this.form.serialize(),
				success: function(body) {
				
					 self.close()
					 
					 if (self.settings.modal) {
					 
					 return	new ui_modalbox({type:'message', title:'Message', content:body, modal:true, redirect:self.settings.redirect});	
					 }
					 
					 else {	
					 
					return	new ui_modalbox({type:'message', title:'Message', content:body, modal:false, redirect:self.settings.redirect}); 
					
					 }
   		
   	 				

				}, 
   				error: function (){
   					self.close()
   					
					 if (self.settings.modal) {
					 
					 return	new ui_modalbox({type:'message_alert', title:'Message', content:'Your request was unsuccessful. Please try again', modal:true, redirect:self.settings.redirect});	
					 }
					 
					 else {	
					 
					return	new ui_modalbox({type:'message_alert', title:'Message', content:'Your request was unsuccessful. Please try again', modal:false, redirect:self.settings.redirect}); 
					
					 }   					
   		   		
   				}
   			};
   			
   			this.box.remove()
      	     
			jQuery.ajax(ajax);					
		
		}

	
	},
			
	height: function () {
		
		return $('.ui-modalbox-content', this.box).css({ height:this.settings.height})	
	},	
	
	header: function () {
		
		return this.header = $('<div/>', {'class': 'ui-modalbox-header ui-draggable'}).appendTo(this.box)

	},		
	
	
	modal: function () {
	
		this.ui_actions.splice($.inArray('ui_draggable', this.ui_actions),1) ; 
		 	
		this.box.css({position:'fixed'})
	},

	
	resize_to_content: function ()  {	
		
		 $(this.box).width($(this.content).width());
		 
		 $(this.box).ui_center();	

	},

	resize_to_screen: function ()  {	
		
		this.settings.height || (this.settings.height=$(window).height()) ;
		this.settings.width || (this.settings.width=$(window).width());	
		
	},

		
	setupBox :function () {
	
		var self = this;	
		
		
		$.each(self.settings, function(name, value) {
		
	
		
			if ( $.inArray(name, self.box_constructor) < 0 &&  $.inArray(name, self.box_properties) < 0)  {self.box_constructor.push(name);}

			if (!value && $.inArray(name, self.box_constructor) > 0){ self.box_constructor.splice($.inArray(name, self.box_constructor),1)};
			
		});			



		this.box_constructor.push('createBox');
		
		
		
		for (element in self.box_constructor){ 
		
		
				
					self[self.box_constructor[element]]();
		}
			
		
		/* this for ie7 */
		if($.browser.msie){ this.resize_to_content();}
		
		if( !this.settings.header && !this.settings.footer && !this.settings.buttons) {$(this.content).addClass('ui-corner-all');}
		
		if (!this.settings.header && (this.settings.footer || this.settings.buttons)) {$(this.content).addClass('ui-corner-top');}
		
		if (this.settings.header && !this.settings.footer && !this.settings.buttons) {$(this.content).addClass('ui-corner-bottom');}
		
		
		$(this.box).ui_show(this.settings.effect,this.settings.speed)

		return false ;
	},

	title : function () {
	
		this.title = $('<span/>', {'class': 'ui-modalbox-title'}).prepend(this.settings.title).appendTo(this.box)
		
		if (this.settings.header){$(this.header).append(this.title)} 
		
		else 
		
		if (this.settings.footer){$(this.footer).append(this.title)}
		
		return
	
	},
		
	type : function () {
	
			
		if (this.settings.modal){this.overlay = ui_overlay('body');}
		
		return	this.settings.type && this['type_'+this.settings.type]()	
	
	},

	type_ajax: function ()  {
	
		var self = this;
		
        var ajax = {
            
            url: self.content, 
            
            type: 'GET', 
            
            dataType: 'html', 
            
            cache: false, 
            
            success: function(body) {
            
            if(self.settings.message) {
            	
            	self.content= body;
            	         
            	return  self.type_message() 
            
            }else {
            
            	self.content= body;
            	         
            	return  self.type_html() 
            
            }
            	
            	

   			}, 
   			
   			error: function (){
            				
				self.content= 'Your request was unsuccessful. Please try again';
				
				self.type_message_alert();
            				
   			}
        };
                
        jQuery.ajax(ajax); 
				
	
	},
		

	type_message: function ()  {
		
		var self = this;					
        
        this.settings.title || (this.settings.title='message')
        
        if (this.settings.redirect) { 
        
        	if (this.settings.redirect=='self') {
        	
        		this.settings.buttons={ ok: function () { self.close() ; ui_overlay('body');location.reload();}}
        	
        	} else if (this.settings.redirect=='back') {
        	
        		this.settings.buttons={ ok: function () { self.close() ; ui_overlay('body'); history.back();}}
        	
        	}else {
        	
        		this.settings.buttons={ ok: function () { self.close() ; ui_overlay('body'); window.location = self.settings.redirect;}}
        	
        	}
        
        } else {
        
        
        this.settings.buttons={ ok: function () { self.close()}}	
        
        } 					
		this.settings.type='html';
        return this.type_html();
	
	},
	
		

	type_message_ok: function ()  {		
	
	this.content = jQuery('<div/>', {'class': 'ui-message-ok'}).append(this.content);	
	return this.type_message();
	
	},
	
	type_message_alert: function ()  {		
	
	
	this.content = jQuery('<div/>', {'class': 'ui-message-alert'}).append(this.content);	
	return this.type_message();	
	
	},		
	
	type_html: function ()  {
	
	
    	this.content = jQuery('<div/>', {'class': 'ui-modalbox-content'}).append(this.content);		
    		
		this.setupBox();
		
		
		$(this.box).ui_bind_function(this.settings.callback);
		
	
	},
	
	type_id: function ()  {
		        
		
        this.content = jQuery(this.content).clone(true).show();	
 			
		return this.type_html();
        
	
	},
						
	type_iframe: function ()  {
	
		var self =this;
		
		this.resize_to_screen()

		this.content = jQuery('<iframe/>', {
						'class': 'ui-modalbox-content',
	 					frameborder:0,
	 					src:this.content,
	 					height:this.settings.height,
	 					width:this.settings.width
	 					}
	 				)

		
		this.setupBox();

		$('iframe',this.box).load(function() {$(self.box).ui_bind_function(self.settings.callback)})
	
	},	        


	type_image: function ()  {
		
		
		this.content = jQuery('<img/>', {
										'class': 'ui-modalbox-content',
										src:this.content,
										border:0
										}
					 )								

		this.setupBox();
		
		
		$(this.box).ui_bind_function(this.settings.callback);
	
	},


	type_slideshow: function ()  {
		
		this.content = jQuery(this.content).clone(true).show();	
		
		this.type_html();
		
		
		$(this.content).ui_slideshow( {width:'320',height:'280'});	
		
		
		$(this.box).ui_bind_function(this.settings.callback);
	
	},	
	
	width: function () {
		
		return $('.ui-modalbox-content', this.box).css({ width:this.settings.width})	
	},
		
	wrapper: function () {
	
		if(!this.settings.css) {this.settings.css=this.settings.type}
	
		return this.box = jQuery('<div/>', {
							'class': 'ui-modalbox-wrapper ui-modalbox-'+this.settings.css,
   							 css: {position:'absolute'}

    						});

	}

}

        	

$.fn.ui_modalbox.defaults = {
       	
	effect:'fade',
	modal:true,
	closeable:true,
	header:true,
	footer:false,
	speed:500,
	callback:jQuery.noop

}; 
 })(jQuery);