/**
* jQuery.zLayer.full v0.3-BETA plugin
* Copyright (c) 2010 Devin R. Olsen
* Date: 01/09/2011
*
* Project Home: 
* http://www.devinrolsen.com/jquery-zlayers-plugin/
*/
$.fn.extend({   
	zlayer: function(options){ 
		var defaults = {  
			 canvas: document,
			 confine:'',
			 force:'push',
			 mass:20,
			 ease:{
				 rate:1500,
				 method:'easeOut'
			 },
			 rest:{
				 rate:1500,
				 method:null
			 }
		},
		option =  $.extend(defaults, options), 
		o = option, 
		obj = $(this),		
		r = true;
		
		return this.each(function() {
			$(o.canvas).bind({
				mousemove: function(e){
					if(r === true){
					    obj.stop().animate({
							top:cal(e.pageY - obj.offset().top - obj.height() / 2,'y'),
							left:cal(e.pageX - obj.offset().left - obj.width() / 2,'x')					
						},{
							queue:false,
							duration:o.ease.rate,
							easing:o.ease.method
						},0);
					}
				},
			//	mouseleave:function(){
			//		if(o.rest.method || r !== false){
			//			r = false;
			//			obj.stop().animate({
			//				top:0,
			//				left:0
			//			},{ 
			//				duration:o.rest.rate,
			//				easing:o.rest.method,
			//				complete:function(){
			//					r = true;
			//				}
			//			},0);
			//		}			
			//	},
				touchmove: function(e){
				    var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
					x = touch.pageX - $(this).offset().left,
					y = touch.pageY - $(this).offset().top;
					if(x > 0 && y > 0 && (x < $(this).width()) && (y < $(this).height())){
						obj.css({
							left:cal(x - $(this).width() / 2,'x'),
							top:cal(y - $(this).height() / 2,'y')
						});
						r = false;
					}			
					e.preventDefault();
				}
	           
			});
		});




		function cal(v,a){
		v = (o.confine === a)? 0 : (o.force === 'pull')? 
			v / o.mass :  -v / o.mass ;
		return v+'px';		
		}
	}
});