/*
  Desk/Office Locator v2.0
  
  Written by Mike Thomas in Fall of 2007
*/

/*
  Constructor
*/
function Locator() { };

/*
  Highlight an individual desk when you mouseover its hotspot and show the name and
  picture of the associated user.
*/
Locator.prototype.HighlightDesk = function(coords, imageId, opt_toggleDiv) {
  var coords = coords.split(',');
  var clipCoords = [ coords[1], coords[2], coords[3], coords[0] ];
  $(imageId).style.clip = 'rect(' + clipCoords.join('px ') + 'px)';
  if (opt_toggleDiv) $(opt_toggleDiv).style.display = 'block';
};

/*
  Clear all desk highlights when you mouseout from a hotspot and hide the name and
  picture of the associated user.
*/
Locator.prototype.UnHighlightDesk = function(imageId, opt_toggleDiv) {
  $(imageId).style.clip = 'rect(0 0 0 0)';
  if (opt_toggleDiv) $(opt_toggleDiv).style.display = 'none';
};

/*
  If the minimap is present, highlight the specified room. Note the special-casing of
  rooms 228 and 238C which have irregular shapes and require the use of the second
  orange image.
*/
Locator.prototype.HighlightMinimap = function(room) {
  var minimap = $('locator-minimap');
  if (minimap == null) return;
  for (var i = 0; i < minimap.childNodes.length; i++) {
    var child = minimap.childNodes[i];
    if ((child.tagName == 'AREA') && (child.alt == room)) {
      var coords = child.coords.split(',');
      var orangeImage = ((room == '228') || (room == '238C')) ?
          $('locator-minimap-orange2') : $('locator-minimap-orange1');
      orangeImage.style.clip = 'rect(' + coords[1] + 'px ' + coords[2] + 'px ' + 
          coords[3] + 'px ' + coords[0] + 'px)';
    }
  }
};

/*
  Email a user given their name and user id.
*/
Locator.prototype.EmailUser = function(userName, userId) {
	document.location = ['mailto:', userName, ' <', userId, '@cs.virginia.edu>'].join('');
};

/*
  Send an email to everyone in a room given a list of user names.
*/
Locator.prototype.EmailRoom = function(userIds) {
	var addresses = userIds.split(',').join('@cs.virginia.edu,');
	document.location = 'mailto:' + addresses;
};

var locator = new Locator();

/*
  Makes '$()' shorthand for 'document.getElementById()'.
*/
function $(id) {
  if (document.getElementById(id)) {
    return document.getElementById(id);
  } else {
    return null;
  }
};












/*
  -----------------------------------------------------------
  Old Stuff
  -----------------------------------------------------------
*/
function mapover(that,div) {
  var myString = new String(that.coords);
  var myArray = myString.split(',');
  if ((that.alt == '228') || (that.alt == '238C')) {
    document.getElementById('altpic2').style.clip='rect('+myArray[1]+'px '+myArray[2]+'px '+myArray[3]+'px '+myArray[0]+'px)';
  } else {
    if (document.getElementById(div)) {
      document.getElementById(div).style.clip='rect('+myArray[1]+'px '+myArray[2]+'px '+myArray[3]+'px '+myArray[0]+'px)';
    } else {
      document.getElementById('orangepic').style.clip='rect('+myArray[1]+'px '+myArray[2]+'px '+myArray[3]+'px '+myArray[0]+'px)';
    }
  }
  if (document.getElementById('seat' + that.alt)) document.getElementById('seat' + that.alt).style.display = 'block';
}
function mapout(that,div) {
  if (document.getElementById(div)) {
    document.getElementById(div).style.clip='rect(0px 0px 0px 0px)';
  } else {
    document.getElementById('orangepic').style.clip='rect(0px 0px 0px 0px)';
  }
  if (document.getElementById('altpic2')) {
    document.getElementById('altpic2').style.clip='rect(0px 0px 0px 0px)';
  }
  if (that.alt) {
    if (document.getElementById('seat' + that.alt)) document.getElementById('seat' + that.alt).style.display = 'none';
  }
}
function minimap(room) {
  tmp = document.getElementsByTagName('AREA');
  for (i = 0; i < tmp.length; i++) {
    if (tmp[i].alt == room) {
      myString = tmp[i].coords;
      var myArray = myString.split(',');
      if ((room == '228') || (room == '238C')) {
        document.getElementById('minialtpic').style.clip='rect('+myArray[1]+'px '+myArray[2]+'px '+myArray[3]+'px '+myArray[0]+'px)';
      } else {
        document.getElementById('miniorangepic').style.clip='rect('+myArray[1]+'px '+myArray[2]+'px '+myArray[3]+'px '+myArray[0]+'px)';
      }
    }
  }
}
function showAddress(userid) {
  window.status = 'mailto:' + userid + '@cs.virginia.edu';
}
function clearStatus () {
  window.status = '';
}
function nameover(alt) {
	areaTags = document.getElementsByTagName('area');
	for (i = 0; i < areaTags.length; i++) {
		if (areaTags[i].alt == alt) {
			mapover(areaTags[i]);
			break;
		}
	}
}
function nameout(alt) {
	areaTags = document.getElementsByTagName('area');
	for (i = 0; i < areaTags.length; i++) {
		if (areaTags[i].alt == alt) {
			mapout(areaTags[i]);
			break;
		}
	}
}
function massEmail(users) {
	user_array = users.split(',');
	email_list = new Array();
	for (i = 0; i < user_array.length; i++) {
		email_list.push(user_array[i] + '@cs.virginia.edu');
	}
	document.location = 'mailto:' + email_list.join(',');
}
