/*
	Class:    	MooDrop
	Author:   	Crispijn Verkade
	Website:    http://crispijnverkade.nl
	Version:  	1.0
	Date:     	22/09/2009
	Built For:  MooTools 1.2.0
*/

var moodrop = new Class({
	Implements: [Options,Events],

	options: {
        dropsize: 25,
		prev_size: 150,
		prev_id: 'prev',
		url: 'eyedropper.php',
		map: 'http://youngdutchdesign.com/examples/'
	},
	
	initialize: function(img,options) {
		this.img = $(img);
		this.setOptions(options);
		this.hex = '';
		var self = this;
		
		this.imgwidth = this.img.getSize().x;
		this.imgheight = this.img.getSize().y;
		this.imgsrc = this.img.getStyle('background-image').replace('url(', '').replace(')', '').replace(this.options.map,'');
		
		//create the selector
		var dropper = new Element('div',{		'id': 'dropper',
												'class': 'dropper',
												'styles': {
														'width': this.options.dropsize,
														'height': this.options.dropsize
														}
											}).inject(img);
		
		//make the selector draggable
		var myDrag = new Drag.Move(dropper, {
			container: self.img,
			onDrag: function(el){
				el.addClass('dragging');
			},
			onComplete: function(el){
				el.removeClass('dragging');
				self.cropImage(el.getPosition(self.img));
			}
		});
	},
	
	cropImage: function(position){
		var self = this;
		var n = position.y;
		var w = position.x;
		
		//crop the image
		var req = new Request({
			method: 'get',
			url: self.options.url,
			data: { 'image' : self.imgsrc, 'n' : n + (self.options.dropsize - 1), 'w' : w },
			onComplete: function(c) { 
				self.hex = c;
				$(self.options.prev_id).setStyle('background', self.hex);
				}
		}).send();
	}
});
