// js for floorplan app
// v. 1.1 : ML Nov 2006

if (window.wdc){
	window.wdc.floorplans = new Object();
	window.wdc.floorplans.bldgSelected = false;
	window.wdc.floorplans.shown = 5
	window.wdc.floorplans.itemCount = 0
	window.wdc.floorplans.topItemIndex = 0
	window.wdc.floorplans.scrollSpeed = 500
//	window.wdc.floorplans.focus
//	addEvent(window,'load',init);
}

var secs
var timerID = null
var timerRunning = false
var delay = 1000

/*--------------------------------------------  config */
window.wdc.floorplans.rollover_dir = 'http://'+window.location.hostname+'/assets/maps/floorplans/gui/rollovers/'
window.wdc.floorplans.baseCampusImage = 'http://'+window.location.hostname+'/assets/maps/floorplans/gui/2006.gif';
/* ------------------- end config -------------------- */

/*function init(){
	if(document.getElementById){
		if(document.getElementById("floors")!=null){ 
			window.wdc.floorplans.bldgSelected = true; 
			var buildings = document.getElementById('buildings')
			if(buildings){
				reduceList(buildings)
				// add the up/down buttons
				var target = getElementsByClassName(buildings,'div','scroll')[0]
				var header = buildings.getElementsByTagName('h3')[0]
				if(!header.id){ header.id = 'buildingHeader' }
				addUpDown(buildings,buildings.id)
			}
		}
	}
}

function reduceListById(theID){
	cancelAutoScroll()
	var x = document.getElementById(theID)
	if(x){reduceList(x)}
}

function reduceList(container){
	// hide all
	var node = null
	var items = container.getElementsByTagName('li')
	for(var i=0;i<items.length;i++){
		var itemX = items[i]
		// see if we can spot the selected index
		if( hasClass(itemX,'selected') ){
			window.wdc.floorplans.topItemIndex = i
			node = itemX
		}
		appendClass(itemX,'hidden')
//		showIndex(itemX,i); // debugging...
	}
	window.wdc.floorplans.itemCount = items.length
	// set the selected
	var found = 0
	while(true){
		if(found>=window.wdc.floorplans.shown){ break }
		if(node==null) { break }
		if(node.tagName=='LI'){ 
			removeClass(node,'hidden')
			found += 1
		}
		node = node.nextSibling
	}
}

function showIndex(node,index){
	var s = node.getElementsByTagName('span')
	s[0].innerHTML = index
}

function addUpDown(wrapper,containerID){
	// add the scroll controls
	var ctrls = document.createElement('div')
	ctrls.className = 'wdc_scrollcontrols';
	// up
	var upbtn = document.createElement('span')
	upbtn.className = 'wdc_btn'
	upbtn.id = containerID+'_up';
	var sp = document.createElement('span')
	sp.appendChild(document.createTextNode('scroll up'))
	var a = document.createElement('a')
	a.href = "javascript: void scrollList('"+containerID+"',-1); ";
	a.appendChild(sp)
	a.setAttribute('title','Scroll up in this list')
	// continuous scrolling while mouse is down
	addEvent(a,'mousedown',autoScrollUp)
	addEventJK(a,'mouseup',cancelAutoScroll,true,true); // mouseup/out do better with this version of addEvent
	addEventJK(a,'mouseout',cancelAutoScroll,true,true)
	// no autoscrolling with keyboard
	addEvent(a,'keyup',cancelAutoScroll)
	addEventJK(a,'mouseover',cancelAutoScroll,true,true)

	upbtn.appendChild(a)
	// down
	var dnbtn = document.createElement('span')
	dnbtn.className = 'wdc_btn'
	dnbtn.id = containerID+'_dn';
	var sp = document.createElement('span')
	sp.appendChild(document.createTextNode('scroll down'))
	var a = document.createElement('a')
	a.href = "javascript: void scrollList('"+containerID+"',1); ";
	a.appendChild(sp)
	a.setAttribute('title','Scroll down in this list')
	// continuous scrolling while mouse/key is down
	addEvent(a,'mousedown',autoScrollDown)
	addEventJK(a,'mouseover',cancelAutoScroll,true,true)
	addEventJK(a,'mouseup',cancelAutoScroll,true,true)
	addEventJK(a,'mouseout',cancelAutoScroll,true,true)
	addEvent(a,'keyup',cancelAutoScroll)
	dnbtn.appendChild(a)
	// reset
	var rset = document.createElement('span')
	rset.className = 'wdc_btn'
	rset.id = containerID+'_reset';
	var sp = document.createElement('span')
	sp.appendChild(document.createTextNode('reset'))
	var a = document.createElement('a')
	a.href = "javascript: void reduceListById('"+containerID+"')"
	a.appendChild(sp)
	a.setAttribute('title','Reset this list to show your selection')
	rset.appendChild(a)
	// place them in the wrapper in source order:
	ctrls.appendChild(rset)
	ctrls.appendChild(dnbtn)
	ctrls.appendChild(upbtn)
	// place the whole bundle into the wrapper
	var firstChild = wrapper.childNodes[0]
	firstChild.appendChild(ctrls)
//	wrapper.insertBefore(ctrls,wrapper.childNodes[2])
}


function scrollList(cid,direction){
	if(document.getElementById){
		var container = document.getElementById(cid)
		var selected = getElementsByClassName(container,'li','selected')
		if(selected.length>0){
			// run through the whole list
			var items = container.getElementsByTagName('li')
			var newTopIndex = window.wdc.floorplans.topItemIndex + direction
			var z = 'newTopIndex='+newTopIndex
			var doIt = false; 
			if(direction==-1 && newTopIndex >= 0){
				// scroll up
				hideIndex = newTopIndex+window.wdc.floorplans.shown
				showIndex = newTopIndex
				doIt = true;
				z += '\nscrolled up'
			}
			if( direction==1 && newTopIndex <= window.wdc.floorplans.itemCount-window.wdc.floorplans.shown){
				// scroll down
				hideIndex = newTopIndex-1
				showIndex = newTopIndex+window.wdc.floorplans.shown-1
				doIt = true;
				z += '\nscrolled down'
			}
			if(doIt){
				var h = items[hideIndex]
				var s = items[showIndex]
				if(h){ appendClass(h,'hidden') }
				if(s){ removeClass(s,'hidden') }
				window.wdc.floorplans.topItemIndex = newTopIndex
				z+= '\nshowIndex='+showIndex+'\nhideIndex='+hideIndex
//				document.body.style.backgroundColor = 'green'
			} else {
				// we're at the top or bottom of the list
				cancelAutoScroll()
			}
		}
	}
}

*/
/*  ---------------------------------------------------------------- continuous scrolling */
/*function autoScrollUp(){
	if(timerRunning){
		cancelAutoScroll();
	} else {
		window.setTimeout('autoScroll(-1)',750) 
	}
}

function autoScrollDown(){
	if(timerRunning){
		cancelAutoScroll();
	} else {
		window.setTimeout('autoScroll(1)',750) 
	}
}

function autoScroll(dir){
	if (!timerRunning){
		timerID = window.setInterval("scrollList('buildings',"+dir+")",window.wdc.floorplans.scrollSpeed)
		timerRunning = true
	}
}

function cancelAutoScroll(){
	if(timerRunning){
		clearInterval(timerID); 
		timerRunning = false
		//alert('autoscroll cancelled')	
	}
}
*/
/* -------------------------------------------------- campus view rollover functions */
function bldg_over(b){
	if(window.wdc.floorplans.bldgSelected==false){
		var b2 = b.toLowerCase()
		var img = document.getElementById('mapIMG')
		if(img){
			img.src = window.wdc.floorplans.rollover_dir+b2+'.gif'
			highlightList(b,'on')
		}
	}
}

function bldg_out(b){
	if(window.wdc.floorplans.bldgSelected==false){
		var img = document.getElementById('mapIMG')
		if(img){
			img.src = window.wdc.floorplans.baseCampusImage
			highlightList(b,'off')
		}
	}
}

function highlightList(b,mode){
	var targetid = 'b-'+b;
	var li = document.getElementById(targetid)
	if(li){
		if(mode=='on'){
			appendClass(li,'over')	
		} else {
			removeClass(li,'over')	
		}
	}
}