	
(function($) {



ui = {
	
	zIndex: 1000,
	speed:500,
	effect:'fade',
	backgroundColor:'black',
	opacity:0.5
	
	};


jQuery.fn.extend({

	ui_wrapper :function (type) {
		
		this.top = $(this).css('top'); if(isNaN(parseInt(this.top,10))) this.top = 'auto';
		this.left = $(this).css('left'); if(isNaN(parseInt(this.left,10))) this.left = 'auto';
		
	
		this.wrapper = jQuery(type, {
		    			'class':'ui-wrapper',
		    			css: {
   		    		 	overflow:'hidden',
   		    		 	display:$(this).css('display'),
   		    		 	height:$(this).outerHeight(),
   		    		 	width:$(this).outerWidth(),
   		    		 	float: $(this).css('float'),
   		    		 	clear: $(this).css('clear'),
   		    		 	position: 'relative',
   		    		 	margin:$(this).css('margin'),
   		    		 	top: this.top, 
   		    		 	left: this.left,
   		    		 	zIndex: $(this).css('z-index') 
   		    		 	}
    	    		})
    	    		
		$(this).css({position:'absolute',top:0,left:0}).wrap(this.wrapper)   	
		
		this.wrapper=$(this).parent('.ui-wrapper');
		
		return this.wrapper;
	
	},

	ui_bind_function : function(fn) {	
	
		return fn.apply(this); 
	
	},

	ui_close_button : function (element) {
	
		return this.live('click', function() { 
		
				$(element).ui_hide( ui.speed, function() {$(element).remove(); })

				return false;
				
				})	
	
	},
	
	ui_set_to_front : function (zIndex) {
	
		var zIndex;
		
		zIndex || (zIndex= ui.zIndex++)
	
		return this.css({'z-index':zIndex})
	
	},
	
	
	ui_click_to_front : function () {
	

		return this.live('click', function() { $(this).ui_set_to_front(); })	
	
	},		
		
	ui_center : function () {
	

		if (this.css('position') == 'fixed') {

			var top_scroll = 0;
	
			var left_scroll = 0;		
		
		} else {
		
			var top_scroll = $(window).scrollTop();
	
			var left_scroll = $(window).scrollLeft();			
		
		}
			
		var top = top_scroll + ($(window).height()- this.height())/2;

		var left = left_scroll +($(window).width()- this.width())/2;

		return this.css({top:top,left:left})	
	
	},
	
	ui_resize : function (max_width, max_height) {
		
		
		var height = this.height();
		var width = this.width();
	
		if (this.is('img'))  {	
		
		if (width != 0 && height!=0) { 
			
			var aspect_ratio = width/height;
			
			if (height > max_height) {
				
				height = max_height;
			
				width = Math.round (height*aspect_ratio);
				
				
				if (width >  max_width) {
				
				width = max_width;
				
				height = Math.round (max_width/aspect_ratio);
				
				}
			
			}

			if (width > max_width) {
				
				
				width = max_width;
				
				height = Math.round (max_width/aspect_ratio);
				
				
				if (height > max_height) {
				
				height = max_height;
			
				width =  Math.round (height*aspect_ratio);
				
				}
			
			}		
		} else {
		
			height = max_height ;
			 width = 'auto' ;
		
		}

		} else {
		
			if (height > max_height || height==0) { height = max_height ;}
			if (width > max_width || width == 0) { width = max_width ;}

	

		}
		
				
		return this.css({width:width, height:height}); 	

	},	
		
	ui_resize_to_container : function (container) {
	
	
		this.ui_resize ($(container).width(),$(container).height())
		 	

	},		


		
	ui_show : function (method,speed,fn) {
	
		method || (method = ui.effect)

		speed || (speed = ui.speed)
		
		fn || (fn=jQuery.noop) 
	
		return this.css({opacity:0}).show().ui_effects({type:method,direction:'in', speed:speed, callback:function() {
				
			$(this).ui_bind_function(fn)
		
			}})
	
	},
		
	
	ui_hide : function (method, speed, fn) {
	
		method || (method = ui.effect)
		
		speed || (speed = ui.speed)
		
		fn || (fn=jQuery.noop)
		
		return this.ui_effects({type:method, direction:'out', speed:speed, callback:function() {
		
			$(this).hide();
			
			$(this).ui_bind_function(fn)
				
			}})	
	

	
	},
	
	ui_toggle : function (method, speed) {
	
		method || (method = ui.effect)
	
		speed || (speed = ui.speed)
	
		if ($(this).is (':hidden')) {$(this).ui_show(method,speed);}
		else if ($(this).is (':visible')) {$(this).ui_hide(method,speed);}
	
	},	
	
	
	ui_drag : function (){
		
				if (!e) var e = window.event;
				
				var e =  jQuery.event.fix(e);

				max_top =$(document.body).height() - this.outerHeight();
				max_left =$(document.body).width() - this.outerWidth();
				
        		
        		element_top = element_intial_y - (mouse_intial_y - e.pageY)	
        			if (element_top < 0) { element_top = 0;}
        			if (element_top > max_top) {element_top = max_top;}
        			
        			
        				
      			element_left = element_intial_x - (mouse_intial_x - e.pageX)
        			if (element_left < 0) { element_left = 0;}
        			if (element_left > max_left) {element_left = max_left;}
     			
     			
     			this.css({top:element_top,left:element_left})
     			
     			
     			return false

	
	},
	
	ui_drag_handle :function () {
	
	 	var dragHandle = $('.ui-draggable',this);
	 
		if(!dragHandle.length) {dragHandle = $(this) }
	 	
	 	return dragHandle
	
	},
	
	ui_draggable : function () {
				
		this.css({position:'absolute'})
		
		var element = $(this); 
		
		var dragHandle = this.ui_drag_handle();
		
		var drag = function () {$(element).ui_drag()};
  
	 	$(dragHandle).bind('mousedown',function(e){
	 	
			mouse_intial_y=e.pageY;
			mouse_intial_x=e.pageX;
        
			element_intial_y=$(element).offset().top;
			element_intial_x=$(element).offset().left; 
        
			$(document).bind('mousemove',drag)
			
 
 		});

		$(document).bind('mouseup',function(){ 
		
			$(document).unbind('mousemove', drag)
			
		})     
	            
	  return this

  	},
  	ui_window_pop : function (width , height) {
				
		var left = (screen.availWidth - width)/2;
		var top =  (screen.availHeight - height)/2;
  	 	
		this.each(function() {
	
			$(this).bind('click', function(){
	 
		 		var href = $(this).attr('href');
		 		var title =  $(this).attr('title');
		 		
		 		var options = 'width='+width+',height='+height+',scrollbars=auto, left='+left+', top='+top+' , resizable=1, status=0 ,toolbars=0,menubar=0,location=0';
		 			
		 		window.open(href, title, options);
	
			return false; 
		 	
		 		 
  		})

  	
  		})
  	}
  	  	
})

 })(jQuery);