var map, windowWidth, windowHeight ;
var centerX        = 1295;
var centerY        = 1100;
var moveAmt        = 250;
var minHeight      = 300;
var currentZoom    = 0;
var showBuildings  = 'Show';


//var sliderKnob  = $('slider-knob');
//var slider;
var path        = 'http://www.williams.edu/home/campusmap/images/';

var images = [
	        path + 'williams_map.jpg',
	        path + 'williams_map_50.jpg', 
	        path + 'williams_map_100.jpg'];




//updateDim --> calls resetDim() [see below] and also updates the dimensions of the 'window' variable (containing the map image)
//This is called if the browser window is resized.
function updateDim()
{
     resetDim();
     map.updateDimensions(windowWidth,windowHeight);
	 map.draggable();
	 updatescroll();
	 

}

//resetDim --> Resets the dimensions of our window containing our map
function resetDim()
{
	windowHeight = window.getHeight()-minHeight; //Allow for spacing at the bottom to show Map Legend.
	if(windowHeight<minHeight) windowHeight=minHeight; //Set min height
	var scrollW  = window.getWidth();
	windowWidth  = scrollW>=800 ? maxWidth : (scrollW-25);
	
	$('window').setStyles({'height':windowHeight+'px', 'width':windowWidth+'px' });
	$('buildingInfo').setStyles({'height':windowHeight+'px'});
	$('scroller').setStyles({'height':windowHeight+'px'});
    $('scrollBar').setStyles({'height':(windowHeight-24)+'px'});

} 

//calculateCoordinates --> calcuates the exact pixel coordinates for a building
// x:  value of x-coordinate of building, as a percentage (0-100)
// y:  value of y-coordinate of building, as a percentage
// id:  building id, if we have it it will produce a popup tooltip.
function calculateCoordinates(x,y,id)
{

   var coordx = (((x/100) * map.orig[0].w) - (windowWidth/2)).toInt();
   var coordy = (((y/100) * map.orig[0].h) - (windowHeight/2)).toInt();
		
	if(!coordx) coordx = centerX; //just in case... reset.
	if(!coordy) coordy = centerY;
			
	map.zoom(0,currentZoom);
	
	map.jump(-coordx,-coordy,id); 
}

function adjustZoom(zoom_in)
{
   //Zoom In
   if(zoom_in=='in') {
	newZoom = (currentZoom-10) >0 ? (currentZoom-10) : 0;
   }
   
   //Zoom Out
   else {
     newZoom = (currentZoom+10) <100 ? (currentZoom+10) : 100;
   }

   if(newZoom!=currentZoom) 
      map.zoom(newZoom,currentZoom);
  
  
   currentZoom=newZoom;
	
}


//adds functionality to the control pad buttons and the slider.
function addControlPadEvents()
{

	$('pan-up').addEvent('click',    function(){	map.move(0, -moveAmt);  });
	$('pan-right').addEvent('click', function(){    map.move(moveAmt, 0);   });
	$('pan-down').addEvent('click',  function(){	map.move(0, moveAmt);   });
    $('pan-left').addEvent('click',  function(){    map.move(-moveAmt, 0);  });
	
	$('reset').addEvent('click', function(){
        //slider.set(0);
	    map.zoom(0,currentZoom);
		map.jump(-centerX,-centerY);
	}); 
		
	$('zoom-in').addEvent('click', function() {  adjustZoom('in'); });
	$('zoom-out').addEvent('click', function() { adjustZoom('out'); });
	
}

/*
function roundNumber(rnum) {
	var rlength = 1; // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}

function setPercentVals(dx,dy)
{
//left    = Math.abs(map.image.getStyle('left').toInt());
//top     = Math.abs(map.image.getStyle('top').toInt());
left=0;
top=0;

xx = ((dx)/(2590-left)) * 100;
yy = ((dy)/(2200-top)) * 100;

x = roundNumber(xx);//Math.round(xx,2);
y = roundNumber(yy);//Math.round(yy,2);
///$('temp').setHTML('Hello x: '+x+' y:'+y);
}
*/