function register() {
	this.keys = [];
	this.values = [];
}

register.prototype.add = function(key,value) {
	var i = this.keys.length;
	this.keys[i] = key;
	this.values[i] = value;
	return this;
}

register.prototype.findIndexForKey = function(key) {
	var i,item;
	for(i=0;item=this.keys[i];i++) {
		if(key == item) return i;
	}
}

register.prototype.get = function(key) {
	return this.values[this.findIndexForKey(key)];
}


/****************/

function mapHandler() {
//	this.basepath = "/images/map/";
	this.baseImageName = "mapa_1.png";
	this.basepath = "/images/map/";
	this.mapImageId = "map_image";
	this.mapImage;
	/** ids,images,showIds ... order of elementes _MATTERS_ */
	this.ids = [ "map_sz", "map_j", "map_sv", "map_sm", "map_jz" ];
	this.images = [ "mapa_sz.png", "mapa_j.png", "mapa_sv.png", "mapa_sm.png", "mapa_jz.png" ];
	this.showIds = [ "show_sz", "show_j", "show_sv", "show_sm" ,"show_jz" ];
	this.lastShowed;
	this.clicked=this.basepath+this.baseImageName;
}
	
mapHandler.prototype.load = function() {
	var i,id,selector;
	for(i=0;id=this.ids[i];i++) {
		selector = "#"+id;
		$(selector).mouseover(this.__e_mouseover);
		$(selector).mouseout(this.__e_mouseout);
		$(selector).click(this.__e_click);
		$("#print_all").click(this.__e_printAll);
	}

	this.hideAll();
	this.mapImage = $("#"+this.mapImageId);
	return this;
}

mapHandler.prototype.hideAll = function() {
	var i,id;
	for(i=0;id=this.showIds[i];i++) {
		$("#"+id).hide();
	}
}
	
mapHandler.prototype.__e_printAll = function(event) {
	var map = r.get("map");
	for(var i=0;item=map.showIds[i];i++) {
		$("#"+item).show();
	}

	event.preventDefault();
}

mapHandler.prototype.getImagePath = function(attr_id) {
	return this.basepath + this.images[this.findIndexByAreaId(attr_id)];
}

mapHandler.prototype.findIndexByAreaId = function(attr_id) {
	var i,id;
	for(i=0;id=this.ids[i];i++) {
		if(id == attr_id) { return i; }
	}
	throw new Exception("attr_id not found");
}
/*
mapHandler.prototype.getInstance = function() {
	if(!this.instance) {
		this.instance = new mapHandler();
	}
	return this.instance;
}*/

mapHandler.prototype.__e_mouseover = function(event) {
	var map = r.get("map");
	map.mapImage.attr(
		"src",
		map.getImagePath($(this).attr("id"))
	);
	return this;
}

mapHandler.prototype.__e_mouseout = function(event) {
	var map = r.get("map");
//	if(this.clicked != $(this).attr("id")) {
		map.mapImage.attr(
			"src",
			map.clicked
		);
//	}
}

mapHandler.prototype.hideLast = function() {
	if(this.lastShowed) {
		$("#"+this.lastShowed).toggle();
	}

//	var id = this.findIndexByAreaId($(this).attr("id"));


	return this;
}

mapHandler.prototype.show = function(clicked_id) {
	var map,i;
	map = r.get("map");
	i = map.findIndexByAreaId(clicked_id);
	$("#"+this.showIds[i]).toggle();
	this.lastShowed = this.showIds[i];
}

mapHandler.prototype.__e_click = function(event) {
	var map = r.get("map");
	map.hideAll();
	map.show($(this).attr("id"));

	map.mapImage.attr(
		"src",
		map.getImagePath($(this).attr("id"))
	);
	map.clicked = 		map.getImagePath($(this).attr("id"));
	
//	event.preventDefault();
}
	
r = new register();
map = new mapHandler();
r.add("map",map);

$(document).ready(function() {
	map = r.get("map");
	map.load();
});
