Interface = {

	selection: '',
		
	add_class:	function(element, c) {
						var cur_class = $w(element.getAttributeNode('class').nodeValue); // Get the current classes from the element into an Array
						var index = cur_class.indexOf(c); // See if the class we're adding is already there,
						if(index < 0) { // If it's not there,
							cur_class.push(c); // Add it to the array
							element.setAttribute('class', cur_class.join(' ')); // Set the element's class
							return true;
						} else  {
							return false;
						}
					
					},
		
	remove_class: 	function(element, c) {
							var cur_class = $w(element.getAttributeNode('class').nodeValue); // Get the current classes from the element into an Array
							var index = cur_class.indexOf(c); // See if the class we're removing is there,
							if(index >= 0) { // If it's there,
								cur_class[index] = null; // Remove it from the array
								cur_class = cur_class.compact(); // Remove the null value from the array
								element.setAttribute('class', cur_class.join(' ')); // Set the element's class
								return true;
							} else {
								return false;
							}		
						},					
						
	select:	function(id) {
				
				if (id != this.selection) { // If the room isn't selected
					if(this.selection != '') {
					    Room.unload(this.selection);
					    this.remove_class($(this.selection), 'selected');
					}
					Room.load(id);
					this.add_class($(id), 'selected'); // Use setAttribute to add the "selected" class to the room
					this.selection = id; // add the number to the array

				} else { // If the room's already selected

					Room.unload('#'+id);
					this.selection = '';
					this.remove_class($(id), 'selected'); // Use setAttribute to remove the "selected" class.
					Calendar.clear();
						  
				}

			},
			
	clear_selection:	function() {
							Interface.selection.each(function(id) {
								if($('r'+id)) {
									Interface.remove_class($('r'+id), 'selected');
								}
							});
							Interface.selection.clear();
							$('selected').value = Interface.selection;
						},
						
	highlight:	function() {
					Room.cache.each(function(r) {
						var clas = null;
						ms = Maintenance.cache.findAll(function(m) {
							return r.get('id') == m.get('room_id');
						});
						ms.each(function(m){
							if (m.get('class') == 'routine' && clas == null) {
								clas = 'routine';
							} else if (m.get('class') == 'urgent' && (clas == null || clas == 'routine')) {
								clas = 'urgent';
							} else if (m.get('class') == 'emergency' && (clas == null || clas == 'routine' || clas == 'urgent')) {
								clas = 'emergency';
							}
						});
						Interface.add_class($('r'+r.get('id').to_int().toPaddedString(3)), clas);
					});
				},
				
	create_throbber: function() {
	    var r = Raphael("throbber", 700, 100),
	    sectorsCount = 12,
	    color = "#000",
	    width = 15,
	    r1 = 15,
	    r2 = 30,
	    cx = 650,
	    cy = 50,

	    sectors = [],
	    opacity = [],
	    beta = 2 * Math.PI / sectorsCount,

	    pathParams = {stroke: color, "stroke-width": width, "stroke-linecap": "round"};
	    for (var i = 0; i < sectorsCount; i++) {
		var alpha = beta * i - Math.PI / 2,
		cos = Math.cos(alpha),
		sin = Math.sin(alpha);
		opacity[i] = 1 / sectorsCount * i;
		sectors[i] = r.path(pathParams).attr("stroke", Raphael.getColor())
		.moveTo(cx + r1 * cos, cy + r1 * sin)
		.lineTo(cx + r2 * cos, cy + r2 * sin);
	    }
	    (function ticker() {
		opacity.unshift(opacity.pop());
		for (var i = 0; i < sectorsCount; i++) {
		sectors[i].attr("opacity", opacity[i]);
		}
		r.safari();
		setTimeout(ticker, 1000 / sectorsCount);
	    })();  
	},	
				
	show_throbber:	function (x,y) {
	    var throbber = $('throbber');
	    throbber.setAttribute('style', 'display:inline');
	    throbber.setAttribute('cx', x);
	    throbber.setAttribute('cy', y);
	    
	},
	
	hide_throbber: function () {
		$('throbber').setAttribute('style', 'display:none');    
	},
	
	throbber_coords: {'loc-basecamp': [288, 390],
				'loc-expert': [270,205],
				'loc-project': [475,200],
				'loc-workshop': [360,200],
				'loc-lab': [475,200]}
			
};


String.prototype.to_int = function() {
	return parseInt(this);
	};

Number.prototype.ordinal = function() {
	return this + (
			(this % 10 == 1 && this % 100 != 11) ? 'st' :
			(this % 10 == 2 && this % 100 != 12) ? 'nd' :
			(this % 10 == 3 && this % 100 != 13) ? 'rd' : 'th'
		);
	};



