/*JavaScript include file for CC campus map application*/
function init(){
	var campusmap=new Image()
	campusmap.src='images/map.gif';
	var img=new Array(arrdata.length);
	var tmp;
	for(var i=0;i<arrdata.length;i++){
		if(arrdata[i]!=undefined){
			img[i]=new Image();
			if(arrdata[i][8]==undefined){
				tmp='big';
			}else{
				tmp='small';
			}
			img[i].src='images/buildings/'+tmp+'/'+i+'.jpg';
		}
	}
}

if (selected!=-2)
{
	selected=-1;
}
current=-1
function on(id){
	if(selected==-1){
		changeclass(id,'selected');
		showblurb(id);
		current=id;
	}
}
function off(id){
	if(selected==-1 && current!=-1){
		changeclass(id,'box');
		hideblurb(id);
	}
}
function showbuilding(id){
	if(selected!=-1) 
	{
		changeclass(selected,'box');
		hideblurb(selected);
	}
	selected=id;
	changeclass(id,'current');
	showblurb(id);
	document.getElementById('blurbclose').style.visibility='visible';
}
function hidebuilding(id){
	if(id!=-1){
		if(selected!=-2){
			selected=-1;
		}
		off(id);
		document.getElementById('blurbclose').style.visibility='hidden';
	}
}
function togglebuilding(id){
	if(selected==id)
		hidebuilding(id);
	else
		showbuilding(id);
}

function setMouseOver(){
	if(selected==-1)
	{
		selected=-2;	
	}
	else
	{
		selected=-1;
	}
}

function showblurb(id){
	writeblurb(id);
	blurbsizeposition(id);
	document.getElementById('blurb').style.visibility='visible';
}
function hideblurb(id){
	document.getElementById('blurb').style.visibility='hidden';
}
function changeclass(id,classname){
	document.getElementById('box'+id).className=classname;
}

function writeblurb(id){
	var html;
	var strerrors='';
	if(arrdata[id][2]==0 && arrdata[id][3]==0) strerrors+='Not on the map. ';
	html=arrdata[id][6];
	if(arrdata[id][12]!=undefined) html+=' ('+arrdata[id][12]+')';
	writetodiv('blurbtitle',html);
	html='';
	if(arrdata[id][7]!=undefined) html+='<p><strong>Departments</strong>:  '+arrdata[id][7]+'</p>';
	if(arrdata[id][9]!=undefined) html+='<p><strong>Address</strong>:  '+arrdata[id][9]+'</p>';
	if(arrdata[id][14]==1){
		html+='<img src="images/buildings/';
		if(arrdata[id][8]==undefined){
			html+='big';
		}else{
			html+='small';
		}
		html+='/'+id+'.jpg" alt="'+arrdata[id][6]+'" name="blurbimage" ';
		if(arrdata[id][8]!=undefined){
			html+='onmouseover="changepic()" onmouseout="changepic()" ';
		}
		html+='/>';
	}else{
		strerrors+='No picture available. ';
	}
	if(arrdata[id][8]!=undefined) html+=arrdata[id][8];
	if(strerrors!='') html+='<p><b>'+strerrors+'</b></p>';
	writetodiv('blurbcontent',html);
}

function blurbsizeposition(id){
	document.getElementById("blurb").style.width='294px';
	if(document.getElementById('blurb').offsetHeight>360){
		var width=Math.round(document.getElementById('blurb').offsetHeight/1.2);
		if(width>400)
			width=400;
		document.getElementById('blurb').style.width=width+'px';
	}
	if(arrdata[id][2]==0){
		document.getElementById('blurb').style.top=Math.round((mapheight-document.getElementById('blurb').offsetHeight)/2)+'px';
		document.getElementById('blurb').style.left=Math.round((mapwidth-document.getElementById('blurb').offsetWidth)/2)+'px';
	}else{
		var margintop=0;
		if(arrdata[id][4]+document.getElementById('blurb').offsetHeight>mapheight){
			margintop=mapheight-(arrdata[id][4]+document.getElementById('blurb').offsetHeight)-5;
		}
		var marginleft=arrdata[id][2]+8;
		if(arrdata[id][5]+marginleft+document.getElementById('blurb').offsetWidth>mapwidth)
			marginleft=-document.getElementById('blurb').offsetWidth;
		document.getElementById('blurb').style.top=(arrdata[id][4]+margintop)+headerheight+'px';
		document.getElementById('blurb').style.left=(arrdata[id][5]+marginleft)+'px';
	}
}

function writetodiv(layername,html)
{
	if(document.all)
	{
		document.all[layername].innerHTML=html;
  }	
  else if (document.layers)
	{
		document.layers[layername].document.open();
		document.layers[layername].document.write(html);
		document.layers[layername].document.close();

	}
	else if (document.getElementById)
	{
		document.getElementById(layername).innerHTML=html;
	}
}

function changepic(){
	var imgsrc=document.blurbimage.src;
	if(imgsrc.indexOf('/small/')==-1){
		imgsrc=imgsrc.replace(/big/,'small')
	}else{
		imgsrc=imgsrc.replace(/small/,'big')
	}
	document.blurbimage.src=imgsrc;
}
