/**
 * Mosaic Object
 * (c) Copyright 2008 Torsten Walter. All Rights Reserved.
 * 
 * This source code might be used and distributed free of charge
 * as long as this copyright notice stays intact.
 * 
 * Usage:
 * new Mosaic(imgObject, idArray, coordinaterray);
 * new Mosaic(imgObject, idArray, coordinateArray, forceDom);
 * new Mosaic(imgObject, idArray, coordinateArray, className, forceDom);
 */
var Mosaic = function (file, idArr, sliceArr) {
	var className, // only for non canvas implementation
		id_map = {},
		file = file,
		slices = [],
		useCanvas = true;

		/**
		 * variable argument lngth to provide classname for dom use
		 * and force usage without canvas
		 */
		if (arguments.length > 3) {
			if (arguments.length == 5) {
				className = arguments[arguments.length-2];
			}
			if (typeof arguments[arguments.length-1] == "boolean")
				useCanvas = arguments[arguments.length-1];
			else
				className = arguments[arguments.length-1];
		}

	this.setSlice = function (id, coords) {
		id_map[id] = slices.length;
		slices.push(coords);
	}

	if(document.createElement("canvas").getContext && useCanvas) {
		this.getSlice = function (id) {
			var coords = [],
				cv = null,
				ctx = null;

			coords = slices[id_map[id]];
			cv = document.createElement("canvas");
			ctx = cv.getContext("2d");

			cv.setAttribute("width", coords[2]);
			cv.setAttribute("height", coords[3]);
			ctx.drawImage(file,
				coords[0], coords[1], coords[2], coords[3],
				0, 0, coords[2], coords[3]);
			return cv;
		};
	} else {
		this.getSlice = function (id) {
			var coords = [],
				cv = null;

			coords = slices[id_map[id]];
			cv = document.createElement("div");
	    if (typeof className != "undefined") {
	      cv.className = className;
	    }
			cv.style.width  = coords[2]+"px";
			cv.style.height = coords[3]+"px";
			cv.style.backgroundImage = "url("+file.src+")";
			cv.style.backgroundPosition = (-coords[0])+"px "+(-coords[1])+"px"
			return cv;
		};
	}
	

	// init slices
	for (var i = 0; i < sliceArr.length; i++) {
		this.setSlice(idArr[i], sliceArr[i]);
	}
};

Mosaic.prototype = {};

var AMosaic = new AJS.Class({
  css_name:  undefined, // only for non canvas implementation
	id_map:     {},
	file:       new Image(),
	slices:     [],
	use_canvas:  true,
	
  init: function (file, idArr, sliceArr) {
    /**
		 * variable argument length to provide classname for dom use
		 * and force usage without canvas
		 */
		if (arguments.length > 3) {
			if (arguments.length == 5) {
				this.css_name = arguments[arguments.length-2];
			}
			if (typeof arguments[arguments.length-1] == "boolean")
				this.use_canvas = arguments[arguments.length-1];
			else
				this.css_name = arguments[arguments.length-1];
		}
		
		// init browser dependent function
		if(document.createElement("canvas").getContext && this.use_canvas) {
		  
  		this.getSlice = function (id) {
  			var coords = [],
  				cv = null,
  				ctx = null;
  			coords = this.slices[this.id_map[id]];
  			
  			cv = document.createElement("canvas");
  			ctx = cv.getContext("2d");

  			cv.setAttribute("width", coords[2]);
  			cv.setAttribute("height", coords[3]);
  			// draw image to returned canvas
  			ctx.drawImage(file,
  				coords[0], coords[1], coords[2], coords[3],
  				0, 0, coords[2], coords[3]);
  			return cv;
  		};
  		
  	} else {
  	  
  		this.getSlice = function (id) {
  			var coords = [],
  				cv = null;

  			coords = this.slices[this.id_map[id]];
  			cv = document.createElement("div");
  	    if (typeof this.css_name != "undefined") {
  	      cv.className = css_name;
  	    }
  			cv.style.width  = coords[2]+"px";
  			cv.style.height = coords[3]+"px";
  			cv.style.backgroundImage = "url("+file.src+")";
  			cv.style.backgroundPosition = (-coords[0])+"px "+(-coords[1])+"px"
  			return cv;
  		};
  		
		}
		// init slices
  	for (var i = 0; i < sliceArr.length; i++) {
  		this.setSlice(idArr[i], sliceArr[i]);
  	}
  },

  setSlice: function (id, coords) {
    this.id_map[id] = this.slices.length;
		this.slices.push(coords);
  },
});