var clicked = new Array(); // remember which label has been clicked so to keep it stay on the map
var blocks = new Array("npc_block", "shop_block", "warp_block", "mob_block");
var tabs = new Array("npc_tab", "shop_tab", "warp_tab", "mob_tab");

// show label whose id is label_id
function front(label_id){

	if(!document.getElementById(label_id)) return;

	label = document.getElementById(label_id);
	label.style.zIndex = 100;
	label.style.display = "inline"; // default
}

// hide label whose id is label_id 
function back(label_id){

	if(!document.getElementById(label_id)) return;

	if(in_array(label_id, clicked) < 0){
		label = document.getElementById(label_id);
		label.style.zIndex = -1;
		label.style.display = "none";
	}
}

// increase z-index to bring it in front of others
function inc_z(label_id, amount){	
	if(!document.getElementById(label_id)) return;

	label = document.getElementById(label_id);
	label.style.zIndex+=amount;
}

// keep the label stay on the map, because it is clicked
function tag_this(label_id){

	if(!document.getElementById(label_id)) return return_false();

	i = in_array(label_id, clicked);
	
	if(i < 0){ // add to clicked array to keep it showing
		clicked.push(label_id);
		front(label_id);
	}else{	
		clicked.splice(i, 1);	// remove the clicked from array
		back(label_id);
	}
	return return_false();
}

// show all labels whose ID is in the array arr_to_show
// (usually include all npc id or all shop id or all warp id or all mob id)
function show_all(arr_to_show){

	clicked.splice(0, clicked.length); // reset what has been clicked

	for(i = 0; i < arr_to_show.length; i++){
		clicked.push(arr_to_show[i]);	// add to clicked array
		front(arr_to_show[i]);
	}
	return return_false();
}

// clear all labels whose ID is in the array arr_to_clear 
// (usually include all npc id or all shop id or all warp id or all mob id)
function clear_all(arr_to_clear){
	
	clicked.splice(0, clicked.length); // reset what has been clicked
	
	for(i = 0; i < arr_to_clear.length; i++){
		back(arr_to_clear[i]);
	}
	return return_false();
}

// show the div with id given by block
function show_block(block){
	// hide the others
	for(i = 0; i < blocks.length; i++){
		if(block != blocks[i]){
			hide_block(blocks[i]);
			// change tab of originally selected
			tab = document.getElementById(tabs[i]);
			tab.className = "nsw_tab"; // temp
		}else{
			// show and change the selected one
			show_this = document.getElementById(blocks[i]);
			show_this.style.display = "block";
			tab = document.getElementById(tabs[i]);
			tab.className = "cur_tab"; // temp
		}
	}
	return return_false();
}

// hide the div with id given by block
function hide_block(block){
	hide_this = document.getElementById(block);
	hide_this.style.display = "none";
	return return_false();
}

// functions to show a div and change the img_obj's src to new_img_src 
function show_div(div_id, img_obj, new_img_src, cur_img_src){
	
	show = document.getElementById(div_id);
	show.style.display = "block";

	if(img_obj != null){
		img_obj.onclick = function () { hide_div(div_id, img_obj, cur_img_src, new_img_src); }
		img_obj.src = new_img_src; //"images/up_arrow.gif";
	}
	return return_false();
}

function hide_div(div_id, img_obj, new_img_src, cur_img_src){

	hide = document.getElementById(div_id);
	hide.style.display = "none";
	
	if(img_obj != null){
		img_obj.onclick =  function () { show_div(div_id, img_obj, cur_img_src, new_img_src); }
		img_obj.src = new_img_src; //"images/tridown.gif";
	}
	return return_false();
}


 //  ex: in_array('v', ['K', 'v', 'Z']); 
 // return index if found, otherwise returns -1
function in_array(findme, arr){
 
	if (arr == null || arr.length <= 0){
		return -1;
	}
    for(i = 0; i < arr.length; i++){
		if(arr[i] == findme){
			return i;
		}
	}
	return -1;
}

function chg_txt_class(txt_id, txt_class){
	
	if(!document.getElementById(txt_id)) return return_false();

	document.getElementById(txt_id).className = txt_class;

	return return_false();
}

// IE 7 needs special treatment with return false;
function return_false(){

	try{	
		event.returnValue=false; // for IE7 
	} catch(exception) {}
	
	return false;
}
