/*
Description: WPWORKS JS CORE
Author: wpworks.net
Author URI: http://wpworks.net
*/

var WPW = WPW || {}; 

WPW.dragContainerMoved = false;
WPW.DragStuff = function(the_trigger, to_drag, to_drag_width, drag_width_limit, to_drag_height, drag_height_limit, inner_drag){
	var main = this;
	var handlerPosition_x = 0;
	var handlerPosition_y = 0;
	var oldMx = 0;
	var oldMy = 0;
	var mX = 0;
	var mY = 0;
	
	var dragDistance = 0;
	var dragDistanceV = 0;
	
	var drag_on = false;
	
	main.percent = 0;

	//handles the mouse drag&rotate events
	var touchEventsPreset = function(){
		the_trigger.bind("touchstart", function(e){
			
  			var touch_start = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
			// mX = -oldMx + touch_start.pageX;
			// mY = -oldMy + touch_start.pageY;			
			
			oldMx = touch_start.pageX;
			oldMy = touch_start.pageY;
			drag_on = true;
			
			handlerPosition_x = to_drag.position().left;
			handlerPosition_y = to_drag.position().top;			
			
			dragStart();
			the_trigger.bind("touchmove", function(e){
				e.preventDefault();
      			var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
				mX = -oldMx + touch.pageX;
				mY = -oldMy + touch.pageY;
				oldMx = touch.pageX;
				oldMy = touch.pageY;

				handlerPosition_x = handlerPosition_x + mX;
				handlerPosition_y = handlerPosition_y + mY;
				dragDistance += Math.abs(mX);
				dragDistanceV += Math.abs(mY);
				
				testPosition();
				
				return false;
			});	
			
			the_trigger.bind("touchend", function(e){
				the_trigger.unbind('touchmove');
				the_trigger.unbind('touchend');
				dragEnd();
			});
			the_trigger.bind("touchcancel", function(e){
				the_trigger.unbind('touchmove');
				the_trigger.unbind('touchend');
				dragEnd();
			});
		});
	};
	
	if(WPW.isTouchDevice == true)touchEventsPreset();
	
	the_trigger.mousedown(function(e){
		oldMx = e.pageX;
		oldMy = e.pageY;
		drag_on = true;
		dragStart();
		
		handlerPosition_x = to_drag.position().left;
		handlerPosition_y = to_drag.position().top;		
		
		the_trigger.mousemove(function(e){
			mX = -oldMx + e.pageX;
			mY = -oldMy + e.pageY;
			oldMx = e.pageX;
			oldMy = e.pageY;
			
			handlerPosition_x = handlerPosition_x + mX;
			handlerPosition_y = handlerPosition_y + mY;
			dragDistance += Math.abs(mX);
			dragDistanceV += Math.abs(mY);
			
			testPosition();

			return false;
		});
		
		$('html, body').mouseup(function(){
			the_trigger.unbind('mouseup');
			the_trigger.unbind('mousemove');
			$('html, body').unbind('mouseup');
			dragEnd();
			return false;
		});
		the_trigger.mouseup(function(){
			the_trigger.unbind('mouseup');
			the_trigger.unbind('mousemove');
			$('html, body').unbind('mouseup');
			dragEnd();
			return false;
		});
		return false;
	});	
	
	var moveDragger = function(){
		to_drag.css('left', handlerPosition_x + "px");
		to_drag.css('top', handlerPosition_y + "px");
		

		main.percent = handlerPosition_x/(drag_width_limit() - to_drag_width());
		the_trigger.trigger("DragPercent");
	} 
	
	main.manualRefresh = function(){
		handlerPosition_x = to_drag.position().left;
		handlerPosition_y = to_drag.position().top;
		testPosition();
	}
	
	var testPosition = function(){
		if(inner_drag){
			
			if(handlerPosition_x < 0) handlerPosition_x = 0;
			if(handlerPosition_y < 0) handlerPosition_y = 0;
			if(handlerPosition_x > drag_width_limit() - to_drag_width())handlerPosition_x = drag_width_limit() - to_drag_width();
			if(handlerPosition_y > drag_height_limit() - to_drag_height())handlerPosition_y = drag_height_limit() - to_drag_height();
		} else {
			
			if(handlerPosition_x < drag_width_limit() - to_drag_width()) handlerPosition_x = drag_width_limit() - to_drag_width();
			if(handlerPosition_y < drag_height_limit() - to_drag_height()) handlerPosition_y = drag_height_limit() - to_drag_height();
			if(handlerPosition_x > 0)handlerPosition_x = 0;
			if(handlerPosition_y > 0)handlerPosition_y = 0;				
		}
		
		moveDragger();
	}
	
	var dragStart = function(){
		dragDistance = 0;
		dragDistanceV = 0;
		WPW.dragContainerMoved = false;
	}
	var dragEnd = function(){
		if(dragDistance > 10 || dragDistanceV > 10){
			WPW.dragContainerMoved = true;
		}
		
		setTimeout(function(){WPW.dragContainerMoved = false}, 100);
		dragDistance = 0;
		dragDistanceV = 0;
	}
	
	the_trigger.bind("RefreshDragger", function(){
		main.manualRefresh();
	});
	
}
WPW.AutoMove = function(the_trigger, to_drag, to_drag_width, drag_width_limit, to_drag_height, drag_height_limit){
	var main = this;
	var handlerPosition_x = 0;
	var handlerPosition_y = 0;
	var pX = 0;
	var pY = 0;

	
	if(!WPW.isTouchDevice)the_trigger.mousemove(function(e){

		var pos_x = e.pageX - the_trigger.offset().left;
		pX = pos_x/drag_width_limit();
		var pos_y = e.pageY - the_trigger.offset().top;
		pY = pos_y/drag_height_limit();
		
		var margin = 0.16;
		
		pX = Math.min(Math.max(pX - margin + margin * 2 * pX, 0), 1); 
		pY = Math.min(Math.max(pY - margin + margin * 2 * pY, 0), 1); 
		//pY = pY - margin + margin * 2 * pY; 

		moveDragger();

		return false;
	});	
	
	var moveDragger = function(){
		to_drag.css('left', -(to_drag_width() - drag_width_limit()) * pX + "px");
		to_drag.css('top', -(to_drag_height() - drag_height_limit()) * pY + "px");
	} 
	
	the_trigger.bind("RefreshDragger", function(){
		moveDragger();
	});
	
}

function supports_canvas() {
  return !!document.createElement('canvas').getContext;
}

WPW.ResizeImage = function(cfg){
	 //img, t_w, t_h, scale_mode, position_mode, force_fit, check_canvas
		
	//t_w, t_h ... target width and height of the container to fit in 
	// scale_mode: fit_h, fit_w, fill
	// position_mode: c, tl, tc, tr, cl, cr, bl, bc, br
	
	if(cfg.img){
		if(cfg.img.data('preloader')) return;
	}
			
	var defaults = {
		scale_mode: "fit_h",
		position_mode: "c",
		t_w: function(){},
		t_h: function(){},
		force_fit: false,
		check_canvas: false,
		width:0,
		height:0
	}
	
	if(cfg.img[0].complete || cfg.img.data('canvas_support') || cfg.img.data('loaded')){
		cfg = $.extend({},  defaults, cfg);
		cfg.img.data('cfg', cfg);		
		
		var img = cfg.img;
		
        if(cfg.height == 0){
            cfg.width = img.width(); 
            cfg.height = img.height(); 
        }
		
		//canvas support
		if(!img.data('canvas_check') && cfg.check_canvas){
			img.data('canvas_check', true);
			img.data('canvas_support', supports_canvas());
			
			if(img.data('canvas_support')){
				var canvas = $('<canvas class="image-canvas" width="500" height="500"></canvas>');
				img.data('canvas', canvas);
				img.data('img', canvas);
				img.parent().append(canvas);
				img.data('canvas_context', canvas[0].getContext("2d"));
				img.detach();
			}
		}
		
        var w = cfg.width;
        var h = cfg.height;

        var ratio = cfg.width/cfg.height;
		
		switch(cfg.scale_mode){
			case "fit_w":
		        w = cfg.t_w();
        		h = w/ratio;					
				break;
			case "fit_h":
		        h = cfg.t_h();
        		w = h*ratio;					
				break;
			case "fill":
				if(w < cfg.t_w() || cfg.force_fit){
			        w = cfg.t_w();
	        		h = w / ratio;
        		}
         		     	
		        if(h < cfg.t_h()){
		            h = cfg.t_h();
		            w = h * ratio;
		        }  
			break;
		}
		
		//if there is no canvas... use the img to resize
		if(!img.data('img'))img.data('img', img);
		var trans_img = img.data('img');
		
		if(img.data('canvas_support')){
			var context = img.data('canvas_context');
			img.data('canvas').attr('width', w);
			img.data('canvas').attr('height', h);
			context.clearRect(0, 0, w, h);
			context.drawImage(img[0], 0, 0, w, h);
		} else {
	        trans_img.attr('width', parseInt(w));
        	trans_img.attr('height', parseInt(h));
		}
		
		var size = cfg.position_mode.split("");
		
		switch(size[0]){
			case "c":
        		trans_img.css("top", parseInt(( cfg.t_h() - h )/2 ) + "px");
				break;
			case "b":
				trans_img.css("top", parseInt(( cfg.t_h() - h )) + "px");
				break;
		}
		
		if(size.length < 2)size.push('c');
		switch(size[1]){
			case "c":
		        trans_img.css("left", parseInt(( cfg.t_w() - w )/2 ) + "px");
				break;
			case "r":
		        trans_img.css("left", parseInt(( cfg.t_w() - w )) + "px");
				break;
		}
		
		img.data("w", w);
		img.data("h", h);
		
		
		
	} else {
		cfg.img.bind("load", function(){
			cfg.img.data('loaded', true);
			WPW.ResizeImage(cfg);
		});
		//cfg.img.attr('src', cfg.img.attr('src'));
	}
}

