(function($) {
$.fn.ui_easing = function(options) {

	
	this.each(function() {	
	
	
		return ui_easing(this, options);
	
  	})
  
}

ui_easing = function (element, options) {


		return new ui_fn_effects(element, options);

}
ui_fn_effects = function (element, options) {

		this.element = element;
		
		this.settings= $.extend({}, $.fn.ui_easing.defaults, options);
		
		this[this.settings.type]()

}

ui_fn_effects.prototype = {

	wrapper : function() {
	
		
		$(this.element).css({position:'relative'}).show()
	
		if (!this.settings.wrapper && $(this.element).parent('.ui-wrapper').size() == 0) {
	
			this.top = $(this.element).css('top'); if(isNaN(parseInt(this.top,10))) this.top = 'auto';
			this.left = $(this.element).css('left'); if(isNaN(parseInt(this.left,10))) this.left = 'auto';
			
	
			this.wrapper = jQuery('<div/>', {
							'class':'ui-effect-wrapper',
		    				css: {
   		    			 	overflow:'hidden',
   		    			 	display:'inline-block',
   		    			 	height:$(this.element).outerHeight(),
   		    			 	width:$(this.element).outerWidth(),
   		    			 	float: $(this.element).css('float'),
   		    			 	clear: $(this.element).css('clear'),
   		    			 	position: $(this.element).css('position'),
   		    			 	top: this.top, 
   		    			 	left: this.left,
   		    			 	zIndex: $(this.element).css('z-index') 
   		    			 	}
    	    			})
    	    			
			$(this.element).css({top:0,left:0}).wrap(this.wrapper)   	
			
			this.wrapper=$(this.element).parent('.ui-effect-wrapper')
			
			
		
		} else {
		
			if(this.settings.wrapper) {
			
				
				this.wrapper = $(this.settings.wrapper);
			
				$(this.wrapper).addClass('ui-wrapper');
		
				if (this.settings.wrapper != 'body') {
				
					$(this.element).parent('.ui-wrapper').css({
   		    			 	overflow:'hidden',
   		    			 	display:'inline-block'
   		    			 	});
				} 

			} else {
			
			this.wrapper = $(this.element).parent('.ui-wrapper');
			
			}
			
	
		 } 
		

		return	this.wrapper
	},
	

	
	fade_direction : function () {
	
		if (this.settings.direction) {
		
			if (this.settings.direction == 'in') {
			
				this.opacity = 1;	
			
			} else if (this.settings.direction == 'out'){
			
				this.opacity = 0;				
			}				
		
		} else {
		
			this.opacity = ($(this.element).css('opacity') - 1) * (-1);	
						
		}
		
		return this.opacity;
	},
	
	fade : function () {
			

		this.fade_direction()
		
		$(this.element).animate({opacity:this.opacity}, this.settings.speed, this.settings.callback);			
		
	
	},

	slide_angle_to_coordinates : function () {	
	
		var width = $(this.wrapper).outerWidth(); 
		
		var height = $(this.wrapper).outerHeight()
				
		var cos = Math.cos(Math.PI*this.settings.angle/180).toFixed(3);
		
		var sin = Math.sin(Math.PI*this.settings.angle/180).toFixed(3);

			
		
		if ( cos == 0 || sin == 0 ) {
				
			this.slide_x = width * cos
			this.slide_y = height * sin 
			
		
		} else  {
		
			var x_factor = cos/Math.abs(cos);
			var y_factor = sin/Math.abs(sin);
			
			var cotan = Math.round(Math.abs(cos/sin),2);
			
			if ( Math.abs(cos * width) < Math.abs(sin * height)) {
			
				this.slide_x = width * x_factor * cotan;
				this.slide_y = height * y_factor
			
			} else if ( Math.abs(cos * width) > Math.abs(sin * height)){
			
				this.slide_y = height * y_factor / cotan;
				this.slide_x = width * x_factor 
					
			} else {
			
				this.slide_y = height * y_factor
				this.slide_x = width * x_factor
			
			}						
		
		} 
		
	
	return this.slide_y,this.slide_x;
	
	},
	
	slide_direction : function () {
	
		var self=this;
		
		if (this.settings.direction) {
			
			if (this.settings.direction == 'in') {
			
				this.settings.angle= this.settings.angle+180;

						this.slide_angle_to_coordinates()
					
						$(this.element).css({'left': this.slide_x, 'top':this.slide_y});
						
						this.slide_in_coordinates()	
	
			
			} else if (this.settings.direction == 'out') {
			
				this.slide_angle_to_coordinates()	
				
			}
	
		
		this.in_or_out()
			
			} else {
				
		
		this.in_or_out()	

				if (this.location == 'out' ) {
				
					if (this.settings.direction =='across') {
				
						this.settings.angle= this.settings.angle+180;
				
					} 
					
					
						this.slide_angle_to_coordinates()
					
						$(this.element).css({'left': this.slide_x, 'top':this.slide_y});
						
						this.slide_in_coordinates()	
						
									
				
				} else {
				
					this.slide_angle_to_coordinates()
					
				
				}
				
						
			
		}		

		return this.slide_x,this.slide_y;
	},	

	in_or_out : function () {
	
	
		var wrapper_left = $(this.wrapper).offset().left;
		var wrapper_right = $(this.wrapper).offset().left+$(this.wrapper).outerWidth();
		var wrapper_top = $(this.wrapper).offset().top;
		var wrapper_bottom = $(this.wrapper).offset().top+$(this.wrapper).outerHeight();
		
		var element_left = $(this.element).offset().left;
		var element_right = $(this.element).offset().left+$(this.element).outerWidth();
		var element_top = $(this.element).offset().top;
		var element_bottom = $(this.element).offset().top+$(this.element).outerHeight();
	
			if (wrapper_left <= element_left && wrapper_right >= element_right && wrapper_top <= element_top && wrapper_bottom >= element_bottom) {this.location = 'in'} else {this.location = 'out'}			
	
		 return this.location;
	},
	
	slide_in_coordinates : function () {
	
	
		if (this.settings.x == 'center') { this.slide_x= ($(this.wrapper).outerWidth()-$(this.element).outerWidth())/2;}
		if (this.settings.y == 'center') { this.slide_y= ($(this.wrapper).outerHeight()-$(this.element).outerHeight())/2;}
		if (this.settings.x == 'left') { this.slide_x= 0;}
		if (this.settings.y == 'top') { this.slide_y= 0;}
		if (this.settings.x == 'right') { this.slide_x= $(this.wrapper).outerWidth()+parseInt($(this.wrapper).css("borderLeftWidth"), 10)+parseInt($(this.wrapper).css("borderRightWidth"), 10)-$(this.element).outerWidth();}
		if (this.settings.y == 'bottom') { this.slide_y= $(this.wrapper).outerHeight()+parseInt($(this.wrapper).css("borderTopWidth"), 10)+parseInt($(this.wrapper).css("borderBottomWidth"), 10)-$(this.element).outerHeight();}
		
		
		return this.slide_x,this.slide_y;		
	
	},	
	slide : function () {	
		
		var self = this;	
		
		this.wrapper()
		
		if ($(this.element).css('opacity')==0) {$(this.element).css({opacity:1})}
		
		this.slide_direction();
		
		$(this.element).animate({left: this.slide_x, top:this.slide_y}, this.settings.speed, function() {
					
		 if(self.location=='in') {$(self.element).hide(); }
		
		
			$(self.element).ui_bind_function(self.settings.callback)
		});	
			
	}
}

$.fn.ui_easing.defaults = {
	speed:750,
	angle: 0,
	opacity:0,
	x:'center',
	y:'top',
	callback:jQuery.noop
}; 
 })(jQuery);
