// Stores the last room shown.  If it's the default room, dont clear it on hide.
var last_show;

// Gets parameter with the name 'name' from the params array.
// In this application, used to retrieve the default floormap via params['floormap']
// if params is not set, attempts to discover the variables from the query string.

function getURLparams( name ) 
{
  if(params[name]){
		return params[name];
  }
  return Querystring(name);
}

//Primary function for highlighting an image, accepts the image object
function show(img)
{
	//Save the id of this image to last show
	last_show = img.id;
	var room = getURLparams("floormap");
	var default_img;
	if (room!=""){
		var default_img = document.getElementById(room);
	}

	// If the default room is being shown, highlight is with 100%
	// Any other room is going to be highlighted at 70% instead
	if(default_img == img){
		img.style.MozOpacity=.9; img.style.opacity=.9; img.style.filter='alpha(opacity=90)';
	}
	else{
		img.style.MozOpacity=.7; img.style.opacity=.7; img.style.filter='alpha(opacity=70)';
	}

	// Get the table row for the list of rooms on the side that corresponds to this image
    // If any are found, check the type name to decide what class to give it.
	var td = document.getElementById('row_'+img.id);
	if(td){
		if(td.attributes.type_name.value=="Public Service Desks"){
			if(default_img == img){
				td.className = 'default_sp_color';
			}
			else{
				td.className = 'service_point_color';
			}
		}
		else if(td.attributes.type_name.value=="Restrooms"){
			if(default_img == img){
				td.className = 'default_br_color'; 
			}
			else{
				td.className = 'restroom_color';
			}
		}
		else{
			if(default_img == img){
				td.className = 'default_highlight';
			}
			else{
				td.className = 'highlight';
			}
		}
	}
}

//Primary function for unhighlighting an image, accepts the image object
function hide(img){
    // If the shown object was the default object, dont hide it.
	if(last_show != getURLparams("floormap")){
		if(img.group_selected == undefined || img.group_selected != 1){
			img.style.MozOpacity=0;img.style.opacity=0; img.style.filter='alpha(opacity=0)';
		}
		td = document.getElementById('row_'+img.id);
		if(td){
			td.className = 'normal_row';
		}
		if(img.id != getURLparams("floormap")){
			show_default();
		}
	}
}

//Show the default image
function show_default(){
	var room = getURLparams("floormap");
	if (room!=""){
		var img = document.getElementById(room);
		if(img){
			show(img);
			img.style.MozOpacity=.9; img.style.opacity=.9; img.style.filter='alpha(opacity=90)'; // 100% as opposed to 70 of normal highlight
		}
	}
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

// Legend function to toggle selecting when checkbox is selected.
function toggle_group(box, group, hide_errors){
	if(box.checked){
		show_group(box, group, hide_errors);
		setCookie("legend["+group+"]",group,30);
	}
	else{
		hide_group(box, group);
		setCookie("legend["+group+"]",group,-1);
	}
}

function show_group(box, group, hide_errors){
	var arr = document.images;
	//var arr= document.getElementById("map").childNodes;
	var img;
	var pattern;
	var td = box.parentNode;

	if(group == "stairs"){
		td.className = 'stairs_color';
	}
	else if(group == "elevators"){
		td.className = 'elevator_color';
	}
	else if (group == "restrooms"){
		td.className = 'restroom_color';
	}
	else if(group == "service_points"){
		td.className = 'service_point_color';
	}
	else if(group == "fire_stairs"){
		td.className = 'fire_stairs_color';
	}
	else if(group == "wifi"){
		td.className = 'wifi_color';
	}
	var counter = 0;
	for(var i=0;i<arr.length;i++){

		img = arr[i];

		if(img.id != undefined){
			if(group == "stairs"){
				pattern = /^st_.*/gi;
				if(img.id.match(pattern)){
					counter++;
					img.group_selected = 1;
					img.style.MozOpacity=1; img.style.opacity=1; img.style.filter='alpha(opacity=100)';
				}
			}
			else if(group == "elevators"){
				pattern = /^el_.*/gi;
				if(img.id.match(pattern)){
					counter++;
					img.group_selected = 1;
					img.style.MozOpacity=1; img.style.opacity=1; img.style.filter='alpha(opacity=100)';
				}
			}
			else if (group == "restrooms"){
				pattern = /^b_.*/gi;
				if(img.id.match(pattern)){
					counter++;
					img.group_selected = 1;
					img.style.MozOpacity=1; img.style.opacity=1; img.style.filter='alpha(opacity=100)';
				}
			}
			else if(group == "service_points"){
				pattern = /^sp_.*/gi;
				if(img.id.match(pattern)){
					counter++;
					img.group_selected = 1;
					img.style.MozOpacity=1; img.style.opacity=1; img.style.filter='alpha(opacity=100)';
				}
			}
			else if(group == "fire_stairs"){
				pattern = /^fire_.*/gi;
				if(img.id.match(pattern)){
					counter++;
					img.group_selected = 1;
					img.style.MozOpacity=1; img.style.opacity=1; img.style.filter='alpha(opacity=100)';
				}
			}
			else if(group == "wifi"){
				pattern = /^wifi_.*/gi;
				if(img.id.match(pattern)){
					counter++;
					img.group_selected = 1;
					img.style.MozOpacity=.5; img.style.opacity=.5; img.style.filter='alpha(opacity=50)';
					//img.style.className="wifi_color semi-transparent";
				}
			}
		}
	}
	if (counter == 0 && typeof hide_errors == "undefined")
	{
		alert("No Rooms of this type found on this floor");
	}
}
function hide_group(box, group){
	var arr= document.images;
	//var arr= document.getElementById("map").childNodes;
	var img;
	var pattern;
	var room = getURLparams("floormap");
	var td = box.parentNode;

	td.className = "normal_row";

	var default_id = "";
	if (room!="" && room != undefined){
		var default_element = document.getElementById(room);
		default_id = default_element.id;
	}

	for(var i=0;i<arr.length;i++){

		img = arr[i];

		if(img.id != undefined && img.id != default_id){

			if(group == "stairs"){
				pattern = /^st_.*/gi;
				if(img.id.match(pattern)){
					img.group_selected = 0;
					img.style.MozOpacity=0; img.style.opacity=0; img.style.filter='alpha(opacity=0)';
				}
			}
			else if(group == "elevators"){
				pattern = /^el_.*/gi;
				if(img.id.match(pattern)){
					img.group_selected = 0;
					img.style.MozOpacity=0; img.style.opacity=0; img.style.filter='alpha(opacity=0)';
				}
			}
			else if (group == "restrooms"){
				pattern = /^b_.*/gi;
				if(img.id.match(pattern)){
					img.group_selected = 0;
					img.style.MozOpacity=0; img.style.opacity=0; img.style.filter='alpha(opacity=0)';
				}
			}
			else if(group == "service_points"){
				pattern = /^sp_.*/gi;
				if(img.id.match(pattern)){
					img.group_selected = 0;
					img.style.MozOpacity=0; img.style.opacity=0; img.style.filter='alpha(opacity=0)';
				}
			}
			else if(group == "fire_stairs"){
				pattern = /^fire_.*/gi;
				if(img.id.match(pattern)){
					img.group_selected = 0;
					img.style.MozOpacity=0; img.style.opacity=0; img.style.filter='alpha(opacity=0)';
				}
			}
			else if(group == "wifi"){
				pattern = /^wifi_.*/gi;
				if(img.id.match(pattern)){
					img.group_selected = 0;
					img.style.MozOpacity=0; img.style.opacity=0; img.style.filter='alpha(opacity=0)';
				}
			}
		}
	}
}


// On click, redirects to another room
function gotoRoom(img){
	var td = document.getElementById('row_'+img.id);
	if(td){
		window.location='index.php?view=show_rooms&room_id='+td.attributes.room_id.value;
	}
}
