function Pixelize(image, container, options) {

	var type_sold = 1;
	var type_inCart = 2;
	var type_sold_not_paid = 3;

	this.image = image;
	this.container = container;
	this.options = options;
	
	this.container_pos = $(container).position();
	
	this.cart = [];
	
	this.num_x = Math.floor(this.options.width / this.options.pixel_size);
	this.num_y = Math.floor(this.options.height / this.options.pixel_size)
	
	this.draw = function() {
		var result = '';
		var pixelsize = this.options.pixel_size;
		
		$(this.container).attr('onmouseout', 'pixelize.reset_info_label()');
		$(this.container).css({
								'background-image' : 'url(' + this.image +')',
								'position': 'relative',
								'width': this.options.width,
								'height': this.options.height
								});
		
		
		for(col = 0; col < this.num_x; col++) {
			for(row = 0; row < this.num_y; row++) {
				result += "<a href='#' "
								+ "onclick='return pixelize.click(this, " + row + ", " + col + ");' "
								+ "onmouseover='pixelize.check_status(this);' onmouseout='pixelize.reset_info_label();' "
				 				+ "style='width: " + pixelsize + "px; height: " + pixelsize + "px;left: " + (col*pixelsize) + "px; top: " + (row*pixelsize) +"px'> </a>";
			}
		}
		
		
		$(this.container).html(result);
		
		if(this.options.sold_pixels.length > 0) {
			this.displaySoldPixels();
		}
		
		if(this.options.my_pixels.length > 0) {
			this.displayMyPixels();
		}
	}
	
	this.click = function(pixel, x, y) {
		
		if(!pixel.owner) {
			pixel.owner = new Owner('Anonym', type_inCart, x, y);
			$(pixel).css('background-color', '#FFFF00');
			this.reset_info_label();
			this.cart.push(pixel);
		}
		else if(pixel.owner.type == type_inCart) {
			pixel.owner = undefined;
			$(pixel).css('background-color', '#000');
			this.reset_info_label();
			this.cart_remove(pixel);
		}
		
		if(this.cart.length == 0) {
			$(this.options.cart_label).html('<i>Tryck på de mörka pixlarna för att lägga till dem i kundvagnen</i>');
			$(this.options.buy_form).css('visibility', 'hidden');
		}
		else {
			$(this.options.buy_form).css('visibility', 'visible');
			$(this.options.cart_label).html('Antal pixlar: ' + this.cart.length + ' Summa: '
											+ (this.cart.length * this.options.price) + ' kr'
											);
											
			this.update_form();
							
		}
	
		return false;
	}
	
	this.displayMyPixels = function() {
		var pixel;
		var children = $(this.container).children();
		var myPixels = this.options.my_pixels;
		var name;
		
		for (i = 0; i < myPixels.length; i++) {
			pixel = myPixels[i];
			children[pixel.y+(pixel.x*this.num_y)].owner = new Owner('Anonym', type_inCart, pixel.y, pixel.x);
			$(children[pixel.y+(pixel.x*this.num_y)]).css('background-color', '#FFFF00');
			this.cart.push(children[pixel.y+(pixel.x*this.num_y)]);
		}
		
		this.reset_info_label();
		this.update_form();
		
		$(this.options.buy_form).css('visibility', 'visible');
		$(this.options.cart_label).html('Antal pixlar: ' + this.cart.length + ' Summa: '
										+ (this.cart.length * this.options.price) + ' kr'
										);
	}
	
	this.displaySoldPixels = function() {

		var pixel;
		var children = $(this.container).children();
		var soldPixels = this.options.sold_pixels;
		var name;
		var classname;
		
		for (i = 0; i < soldPixels.length; i++) {
			pixel = soldPixels[i];
			if(pixel.paid) {
				name = 'Sponsor: ' + pixel.name;
				classname = 'sold';
			} else {
				name = 'Reserverad';
				classname = 'sold_npaid';
			}
			children[pixel.y+(pixel.x*this.num_y)].owner = new Owner(name, type_sold_not_paid, pixel.x, pixel.y);
			$(children[pixel.y+(pixel.x*this.num_y)]).addClass(classname);
		}
	}
	
	
	this.check_status = function(pixel) {
		
		if(pixel.owner) {
		
			var pixel_pos = $(pixel).position();
			var text;
		
			if(pixel.owner.type == type_inCart) {
				text = 'Pixel i varukorg';
			}
			else {
				text = pixel.owner.name;
			}
			
			$(this.options.info_label).text(text).css({
										'display': 'block',
										'left': pixel_pos.left+this.container_pos.left,
										'top': pixel_pos.top+this.container_pos.top-25
									  });
		}
		
	}
	
	this.cart_remove = function(pixel) {
	
		var index = jQuery.inArray(pixel, this.cart);
		var part1 = this.cart.slice(0, index);
		var part2 = this.cart.slice(index+1, this.cart.length);
		this.cart = part1.concat(part2);
		
	}
	
	this.update_form = function() {
		  
		var result = '';
		var cart = this.cart;
		
		jQuery.each(cart, function(i, val) {
			result += cart[i].owner.x + ',' +  cart[i].owner.y + ':';
	    });
	    
	    // Remove last :
	    result = result.substring(0,result.length-1);
	    
		$(this.options.buy_output).attr('value', result);
	}
	
	
	this.reset_info_label = function() {
		$(this.options.info_label).css('display', 'none');
	}
	
	function Owner(name, type, x, y) {
		this.name = name;
		this.type = type;
		this.x = x;
		this.y = y;
	}
}
