/**
 * Wheeel Emoticons Picker 0.1
 * © 2010 Giorgio Festi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

jQuery.noConflict();
(function($) {  $(document).ready(function(){

	jQuery.fn.wheeel = function (callback) {
		$.wheeel(this, callback);
		return this;
	};

	jQuery.wheeel = function (container, callback) {
		var container = $(container).get(0);
		var target = callback;
		$(container).html('<div class="wheeel"><div class="overlay"></div><div class="marker"></div></div>');
		var e =  $('.wheeel', container);;
		var ee = this;
		ee.radius = 60;
		ee.square = 70;
		ee.width = 150;
		ee.idx = 0;
		
		var init = function (angle)
		{
			$('.marker', e).css({
			      left: Math.round(Math.sin(angle) * ee.radius + ee.width / 2) + 'px',
			      top: Math.round(-Math.cos(angle) * ee.radius + ee.width / 2) + 'px'
			    });	
			$('.overlay',e).css('background-image', 'url(/bracchettov2/OnionHead/'+onionSmile[ee.idx][0]+')');
		}
		
	
		  /**
		   * Get relative position of element
		   */
		  var mousePosition = function (ev) {
			var x = ev.pageX - container.offsetLeft - ee.width / 2;
			var y = ev.pageY - container.offsetTop - ee.width / 2;

		    return {x:x,y:y};
		  };
		
		 var mousedown = function (event) {
			    // Capture mouse
			 
			    

			    // Check which area is being dragged
			    var pos = mousePosition(event);
			    ee.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > ee.square;
			    if (ee.circleDrag){
			    	
			    	if (!document.dragging) {
						 $(document).bind('mousemove', ee.mousemove).bind('mouseup', ee.mouseup);
				      document.dragging = true;
					 }
			    	
			   // $(target).each(function(){this.value = pos.x+" "+pos.y+" "+ee.circleDrag});
			    // Process
			    ee.mousemove(event);
			    }else{
			    	$(target).each(function(){this.value += onionSmile[ee.idx][1]});
			    //	$(container).animate({ height: 'hide', opacity: 'hide' }, 'slow');
			    }
			    return false;
			  }
		 
		 ee.mousemove = function (event) {
			    // Get coordinates relative to color picker center
			    var pos = mousePosition(event);
			   

			    // Set new HSL parameters
			    if (ee.circleDrag) {
			      var angle = Math.atan2(pos.x, -pos.y);
			      var hue = angle / 6.28;
			      if (hue < 0) hue += 1;
			     //$(target).each(function(){this.value = hue});
			      ee.idx =  Math.round((onionSmile.length * hue));
			      
			      $('.overlay',e).css('background-image', 'url(/OnionHead/'+onionSmile[ee.idx][0]+')');

			  
			    $('.marker', e).css({
			      left: Math.round(Math.sin(angle) * ee.radius + ee.width / 2) + 'px',
			      top: Math.round(-Math.cos(angle) * ee.radius + ee.width / 2) + 'px'
			    });
			    }
			    return false;
			  }

		  ee.mouseup = function () {
			    // Uncapture mouse
			    $(document).unbind('mousemove', ee.mousemove);
			    $(document).unbind('mouseup', ee.mouseup);
			    document.dragging = false;
			  }
		
		init(0);
		$('*', container).mousedown(mousedown);
		
	}

	
	
	

}); })(jQuery)