(function() {
	if( typeof jQuery !== 'undefined' ) {
		function imagemapper() {
			this.defaultoptions = {areawidth: 16, areaheight: 16};
		}

		jQuery.imagemap = new imagemapper();
		
		var extensions = {
			mapimage: function(options) {				
				if (!this[0].nodeName.toLowerCase() == "img") { return this; }
				
				var resolvedoptions = {};				
				
				if(options) {
					resolvedoptions.areawidth = (options.areawidth && options.areawidth == 'number') ? options.areawidth : jQuery.imagemap.defaultoptions.areawidth;
					resolvedoptions.areaheight = (options.areaheight && options.areaheight == 'number') ? options.areaheight : jQuery.imagemap.defaultoptions.areaheight;						
				}
				else { resolvedoptions = jQuery.imagemap.defaultoptions; }
				
				var index_areas_x = Math.floor(this[0].width/resolvedoptions.areawidth);
				var index_areas_y = Math.floor(this[0].height/resolvedoptions.areaheight);
				
				if (index_areas_x == 0 && options.imgwidth && options.imgheight)
				{
					index_areas_x = Math.floor(options.imgwidth/resolvedoptions.areawidth);
					index_areas_y = Math.floor(options.imgheight/resolvedoptions.areaheight);
				}
				
				var map = jQuery("<map />").attr("id", this[0].id + "_mapid").attr("name", this[0].id + "_map");
				
				for (var x = 0; x < index_areas_x; x++) {
					for (var y = 0; y < index_areas_y; y++) {
						var coords =  x * resolvedoptions.areawidth + ',' + y * resolvedoptions.areaheight + ',' + (x + 1) * resolvedoptions.areawidth + ',' + (y + 1) * resolvedoptions.areaheight;
						var area = jQuery('<area />').attr('href','#').attr('shape', 'rect').attr('coords', coords).attr('alt', '');
						map.append(area);
					}
				}

				this.after(map).attr("usemap","#" + map.attr("name"))
				
				return map;
			},
			mapimage2: function(options) {				
				if (!this[0].nodeName.toLowerCase() == "img") { return this; }
				
				var resolvedoptions = {};				
				
				if(options) {
					resolvedoptions.areawidth = (options.areawidth && options.areawidth == 'number') ? options.areawidth : jQuery.imagemap.defaultoptions.areawidth;
					resolvedoptions.areaheight = (options.areaheight && options.areaheight == 'number') ? options.areaheight : jQuery.imagemap.defaultoptions.areaheight;						
				}
				else { resolvedoptions = jQuery.imagemap.defaultoptions; }
				
				var index_areas_x = Math.floor(this[0].width/resolvedoptions.areawidth);
				var index_areas_y = Math.floor(this[0].height/resolvedoptions.areaheight);			

				if (index_areas_x == 0 && options.imgwidth && options.imgheight)
				{
					index_areas_x = Math.floor(options.imgwidth/resolvedoptions.areawidth);
					index_areas_y = Math.floor(options.imgheight/resolvedoptions.areaheight);
				}

				for (var x = 0; x < index_areas_x; x++) {
					for (var y = 0; y < index_areas_y; y++) {
						var coords =  x * resolvedoptions.areawidth + ',' + y * resolvedoptions.areaheight + ',' + (x + 1) * resolvedoptions.areawidth + ',' + (y + 1) * resolvedoptions.areaheight;
						var area = jQuery('<area />').attr('href','#').attr('shape', 'rect').attr('coords', coords).attr('alt', '');
						options.mapElement.append(area);
					}
				}

				return options.mapElement;
			}
		}
		
		jQuery.each(extensions, function(i)
		{
			jQuery.fn[i] = this;
		});
	} // end if jQuery exists.
})();

/*
EXAMPLE:

function selected(element, event) {
   var coords = null;
   if( event.target ) { coords = event.target.coords.split(','); }
   else if( event.srcElement ) { coords = event.srcElement.coords.split(','); }
   $('#selected').css("background-position",'-' + coords[0] + 'px' + ' -' + coords[1] + 'px');
}

$(document).ready(function() {
	$("#imagetomap").mapimage({handlername: "selected"});
	$("#newimagetomap").mapimage();
});
*/