var cmap = { 
	currentX: 0, currentY: 0,
	portX: 15, portY: 15,
	centerX: [ 0, -725, -900 ], centerY: [ 0, -145, -500], // these are absolute values, not relative to map origin
	src: [ 'assets/map-62.png', 'assets/map-100.png', 	'assets/map-150.png' ], 
	portwidth: 896, portheight: 500,
	mapwidth: [ 1015, 1650, 2485 ], mapheight: [ 713, 1150, 1725],  // pixel widths
	imagesuffix: [62,100,150],
	zoomlevel: 1,
	bldgstring: '',
	zoomstate: 'open',
	inner: '',
	parking: '',
	pixelValue: function(val) {
		return val + 'px';
	},
	getXOffset: function() {
		return this.portwidth - this.mapwidth[this.zoomlevel-1];
	},
	getYOffset: function() {
		return this.portheight - this.mapheight[this.zoomlevel-1];
	},
	stripPx: function(val) {
		return val.substr(0, val.length-2);
	},
	translateX: function(X) { // this translates coordinates relative to map origin into absolute values.
	
		var position = (+X) + (+this.currentX);
		
		return this.pixelValue( position );
		
	},
	translateY: function(Y) {
		return this.pixelValue( (+Y) + (+this.currentY)  );
	},
	getBuildingString: function() {
		
		this.bldgstring = '';
			
		this.active = bldg.findAll(function(b) {
			return $(b.id.toLowerCase()).style.display == '';
		});
		
				
		this.active.each(function(b,index) {
			if (index>0) this.bldgstring += '&';
			this.bldgstring += 'bd='+b.customNum;	
		}.bind(this)); // bind(this) allows us to use 'this' to refer to the cmap obj within the internal function.  
		
	},
	setLinks: function() {
		
		if (!this.active ) { this.getBuildingString(); }
		
		if ($('printlink')) this.setPrintLink();
		if ($('zoomcontrol')) this.setZoomLink();
		
	},
	setPrintLink: function() {
		var href = "campusmap.htm?zoom="+this.zoomlevel+"&x=" + this.currentX + "&y=" + this.currentY + "&printable=1&"+this.bldgstring;
		if ( this.parking != '' ) href = href + '&pk='+this.parking;
		$('printlink').href = href;
		
	},
	setZoomLink: function() {
		
		var parkingstr = '';
		var getvars = '';
		var bldgs = '';
		
		if ( this.bldgstring != '') { getvars = '&' + this.bldgstring } 
		if (this.parking != '') { getvars += '&pk='+this.parking; }
		 		
		[1,2,3].each(function(num) {
			$('zoom' + num).href='?zoom='+ num + getvars;
		}.bind(this));
	},
	setzoomlevel: function() {
			if ( getVar('zoom') ) {	this.zoomlevel = getVar('zoom'); }

	},
	changezoomlevel: function(newz) {
		this.zoomlevel = newz;
		$('mapimage').src = this.src[this.zoomlevel-1];
		if (newz == 1) this.position(this.currentX*.58,this.currentY*.58);
		if ( newz == 2) this.position(this.currentX*.66,this.currentY*.66);
		cmap.draw();
		cmap.setlinks();
	},
	draw: function() {
		// ensure the currentX,Y vars are in sync with #inner div
		
		if (! this.checkPosition(this.currentX, this.currentY)  ) {  
			this.currentX = this.stripPx(this.inner.style.left);
			this.currentY = this.stripPx(this.inner.style.top);
		}
		
		cmap.checkBoundaries(); // edge detection
		
		// track buildings with movement of map image
		
		for( var i = 0; i < bldg.length; i++) {
			var id = bldg[i].id.toLowerCase();
			var f = $(id);
			var xpos = (+this.currentX) + (+bldg[i].X[this.zoomlevel-1]);
			// if (document.all) { xpos += 11; } 
			f.style.left = xpos + 'px';
		
			var ypos = (+this.currentY) + (+bldg[i].Y[this.zoomlevel-1]);
			
			f.style.top = ypos + 'px';
			
			var xoffsets = {sphs: -65, pdi: -100, rows: -65};
			var yoffsets = {sphs: -85, pdi: -72, rows: -50};
			
			if ( (id == 'rows' || id == 'pdi' || id == 'sphs' )  ) {
					$(id + '_label').style.left = xoffsets[id] + 'px';
					$(id + '_label').style.top = yoffsets[id] + 'px';
				//console.log($(id + '_label').style.left, $(id + '_label').style.width)
			}
			//console.log(f.id + ": " + f.style.left + "," + f.style.top);
			
		}
			
	},
	checkPosition: function(X,Y) {
		// console.log(this.stripPx(this.inner.style.left) + " : " + X);
		if (this.stripPx(this.inner.style.left) == X && this.stripPx(this.inner.style.top) == Y) { return true }
		return false;
	},
	tobldg: function(b) {	
		this.moveTo( -(b.X[this.zoomlevel-1] - this.portwidth/2), -(b.Y[this.zoomlevel-1] - this.portheight/2) );
	},
	bldgClick: function(b){
		$(b.id.toLowerCase()).toggle();
		
		if ($(b.id.toLowerCase()+'_eyeball').src.match(/closed/) ) {
			$(b.id.toLowerCase()+'_eyeball').src = 'images/eyeball_open.gif';	
		}else{
			$(b.id.toLowerCase()+'_eyeball').src = 'images/eyeball_closed.gif';
		}
		this.getBuildingString();
		this.tobldg(b);
				
	},
	drawBldgList: function() {
		new Insertion.After('pretabhead', '<a id="buildinglist" /><table ><thead><tr class="headrow"><th class="first">Building Name</th><th class="first">Building Name</th></tr></thead><tbody id="bldglist"></tbody></table>');
		var rows = Math.round(bldg.length/2); 
		for (i=0; i<rows; i++) {
			
			var bldgid = bldg[i].id.toLowerCase();
			var linkid = bldgid + '_link';
			
			if ( i == rows-1 && bldg.length % 2 == 1) {
				var emptyneighbor = true;
			}else{
				var emptyneighbor = false;
				var bldgid2 = bldg[i+rows].id.toLowerCase();
				var linkid2 = bldgid2 + '_link';	
			}
		
			  
				
			var row = '<tr>';
			row += '<td class="first">' + /* setting up the eyeball open or closed */
					  '<img src="images/eyeball_closed.gif" alt="building off" id="' + bldg[i].id.toLowerCase() + '_eyeball" /> ' +
					  '<a id="' + linkid + '" href="#"  >' + bldg[i].label + ' ('+ bldg[i].id +  ')</a>' + 
					  '</td>';
			if ( emptyneighbor ) {
				row +='<td class="first">' + 
					  '</td>';
			}else{
				row +='<td class="first">' + 
					  '<img src="images/eyeball_closed.gif" alt="building off" id="' + bldg[i+rows].id.toLowerCase() + '_eyeball" /> ' +
					  '<a id="' + linkid2 + '" href="#"  >' + bldg[i+rows].label + ' ('+ bldg[i+rows].id +  ')</a>' + 
					  '</td>';
			}
			row += '</tr>';
			
			new Insertion.Bottom('bldglist', row);
												   
			Event.observe(linkid, 'click', function() {
				cmap.bldgClick(this);
			}.bind( bldg[i]  )
			); 	// bind() makes our building available as 'this' inside the function
			
			if ( ! emptyneighbor ) {
				Event.observe(linkid2, 'click', function() {
					cmap.bldgClick(this);
				}.bind( bldg[i+rows]  )
				); 	// bind(b) makes our building available as 'this' inside the function
			} 	
		}
	},
	loadBldgs: function(){ /* preloading all the required building images */
		new Insertion.After('zoomcontrol', '<div id="buildings" ></div>');
		new Insertion.After('zoomcontrol', '<div id="parking" ></div>');
		
		bldg.each( function(b) {
			var bldglabel = b.id.toLowerCase();
			var imgstr = '<div id="bldglabel" style="display: none;" class="mappy">'+
						 '<img style="position: relative; top: 0px; left: 0px; " id="' + b.id.toLowerCase() + '_pin" src="images/pin.gif">' +
						 '<div id="bldglabel_label"  class="buildinglabel" ><span class="nowrap">' + b.label + ' ('+ b.id + ')</span> '+
						 '</div>'+ 
						 '</div>';
			var re = /bldglabel/g;
			imgstr = imgstr.replace(re, bldglabel);				 
			new Insertion.Bottom( 'buildings', imgstr);
		});
	},
	bldgLabelX: function(b) {
		return this.pixelValue(b.X[this.zoomlevel-1]);
	},
	bldgLabelY: function(b) {
		return this.pixelValue(b.X[this.zoomlevel-1]);
	},
	center: function() {
		var x = this.pixelValue(this.centerX[this.zoomlevel-1]);
		var y = this.pixelValue(this.centerY[this.zoomlevel-1]);
		this.inner.style.left = x;
		this.inner.style.top = y
	},
	position: function(x,y) {
		this.inner.style.left = x + 'px';
		this.inner.style.top = y + 'px';
	},
	setImage: function() {
			$('mapimage').src = this.src[this.zoomlevel-1];
	},
	checkBoundaries: function() {
		
		this.currentX = this.checkBoundX(this.currentX);
		
		this.inner.style.left = this.pixelValue(this.currentX);
		
		
		this.currentY = this.checkBoundY(this.currentY);
		this.inner.style.top = this.pixelValue(this.currentY);
		
	},
	checkBoundX: function(val) {
		
		var ret = val;
		if (val > 0 ) {
			ret = 0;
		}
		if (val < ((+this.portwidth) - (+this.mapwidth[this.zoomlevel-1]) ) ) {
			ret = (+this.portwidth) - (+this.mapwidth[this.zoomlevel-1]);
		}
		
		return ret;
		
	},
	checkBoundY: function(val) {
		
		var ret = val;
		if ( this.currentY > 0 ) {
			ret = 0;
		}
		if ( this.currentY < (this.portheight - this.mapheight[this.zoomlevel-1]) ) {
			ret = this.portheight - this.mapheight[this.zoomlevel-1];
		}
		
		return ret;
	},
	toggleZoomCtrl: function() {
		
		
		var distance = $('zoomcontrol').clientHeight-45;
		
		// console.log(distance);
		if (this.zoomstate == 'closed') {
			//if (document.all) distance += 55;
			this.zoomstate = 'open';
			$('zctrlLink').update( 'Hide Toolbar' );
		}else{
			distance = ( -distance ); 
			// if (document.all) distance -=55;
			this.zoomstate = 'closed';	
			$('zctrlLink').update( 'Show Toolbar' );
		}
		
		Effect.MoveBy('zoomcontrol',distance , 0 );
		
		return false;
	},
	initialize: function() {
		this.inner = $('inner');
		this.setzoomlevel();
		this.setImage();
		this.center();
			
		this.loadBldgs();
			
		// custom map url handling: bd = building code or number, pk = parking lot code or 'all'
		
		var url = location.search.substring(1,location.search.length);
		var wts = url.split("&");
		
		wts.each( function(getvar){
			var nameval = getvar.split('=');
			
			if ( nameval[0] == 'pk' ) {
				
				if ( nameval[1] != '' ) {
					var match = ['a','d','g','p','r'].find( function(type) {
								return nameval[1] == type;
					});
					if ( match ) $('mapimage').src = 'assets/'+match.toLowerCase()+'-'+cmap.imagesuffix[cmap.zoomlevel-1]+'.png';
				}
				cmap.parking = nameval[1];
				// console.log('this.parking: '+this.parking);
				// console.log('parking: '+parking);
			}else if ( nameval[0] == 'bd') {
				current = bldg.find( function(b) {
					if ( nameval[1] == b.customNum || nameval[1] == b.id.toLowerCase() ) { return true; }  
				});
				
				
				
				
				if ( current ) {  // highlight buildings selected on url and indicate their selection in the building list
					current = $(current.id.toLowerCase());
					current.toggle();
					
					var eyeball = $(current.id.toLowerCase()+'_eyeball');
					
					if (eyeball) {
						if (eyeball.src.match(/closed/) ) {
							eyeball.src = 'images/eyeball_open.gif';	
						}else{
							eyeball.src = 'images/eyeball_closed.gif';
						}
					}
				// cmap.bldgClick(b);
			
			
				}
			}
			});
		
		if ( getVar('x') && getVar('y') ) {
			this.position( getVar('x'), getVar('y') );	
		}
		
		this.draw();
		/*		
		$('outer').style.width = this.pixelValue(this.portwidth);
		$('outer').style.height = this.pixelValue(this.portheight);
		$('outer').style.top = this.pixelValue(this.portY);
		$('outer').style.left = this.pixelValue(this.portX);
		*/
	},
	bldgpos: function(id){
		// detect is a prototype enumerable mix-in for selecting an array slot based on some criteria
		// here, we return the element that matches id
			var current = bldg.detect(function(b) { 
	  		return b.id.toLowerCase() == id;
		});
		
		var driftX = this.stripPx( this.translateX(current.X[this.zoomlevel - 1]) ) - this.stripPx( $(current.id.toLowerCase()).style.left );
		var driftY = this.stripPx( this.translateY(current.Y[this.zoomlevel - 1]) ) - this.stripPx($(current.id.toLowerCase()).style.top );
		
		current.X[this.zoomlevel-1] = (current.X[this.zoomlevel-1] - driftX);
		current.Y[this.zoomlevel-1] = ( current.Y[this.zoomlevel - 1] - driftY );
		
		return current.X[this.zoomlevel-1] + ',' + current.Y[this.zoomlevel-1]
	},
	moveTo: function(destX,destY) {
		var newX = false,newY = false,proceed = false;
		
		
		this.destX = this.checkBoundX(destX);
		this.destY = this.checkBoundY(destY); 
		
		
		framerate = 25;
		if (this.moveToHere) { clearTimeout(this.moveToHere)}
		if (this.currentX < this.destX) {
			newX = (+this.currentX + framerate);
			if (newX > this.destX) {newX = this.destX;}
			proceed = true;
		}
		if (this.currentY < this.destY) {
			newY = (+this.currentY + framerate);
			if (newY > this.destY) {newY = this.destY;}
			proceed = true;
		}
		if (this.currentX > this.destX) {
			newX = (+this.currentX - framerate);
			if (newX < this.destX) {newX = this.destX;}
			proceed = true;
		}
		if (this.currentY > this.destY) {
			newY = (+this.currentY - framerate);
			if (newY < this.destY) {newY = this.destY;}
			proceed = true;
		}
		// debugger;
		if ( newX == false  ) {newX = this.currentX;}
		if ( newY == false  ) {newY = this.currentY;}
		if (proceed){
			this.currentX = newX;
			this.currentY = newY;
			this.inner.style.left = this.currentX + 'px';
			this.inner.style.top = this.currentY + 'px';
			
			this.draw();
			
			if (this.currentX != newX) { this.destX = this.currentX; }
			if (this.currentY != newY) { this.destY = this.currentY; }
			this.moveToHere = setTimeout('cmap.moveTo('+this.destX+','+this.destY+')', 30);
		}else{
			this.setLinks();
		}
		
		
	},
	hideActive: function() {
		this.active.each(function(b){
			$(b.id.toLowerCase() ).hide();
		});
	},
	showActive: function() {
		this.active.each(function(b){
			$(b.id.toLowerCase() ).show();
		}); 
	}
}
var pin = {
	computeX: function(b) {
		var x = (+cmap.currentX) + (+b.X[cmap.zoomlevel-1]);
		var printable = getVar('printable');
		if ( ! printable  ) {
			x += 34;
		} 
		return x;
	},
	computeY: function(b) {
		var y = (+cmap.currentY) + (+b.Y[cmap.zoomlevel-1]);
		var printable = getVar('printable');
		if ( ! printable  ) {
			y += 34;
		} 
		return y;
	}
}