
var splist = ['zoomto','dept','vFilter','fund']; // list of all anim menus
var filterlist = ['dept','vFilter','fund']; // list of related filter menus
var deptfilters = ['vFilter','fund']; 

// close all filterBoxes
function closeFilters(sp){
	if(sp!='all'){
		var filterId = 'sp_'+sp;
		var thisList = document.getElementById(filterId);
		if(thisList.style.display=="block"){
			var arrow =(sp!="zoomto"?true:false);
			//hideSearch(sp,arrow);
			if(browserIE()){
				thisList.style.display="none";
				hideSearch(sp,arrow);
			}else{
				$("#"+filterId).animate({ height: 'hide', opacity: 'hide' }, 125, function(){hideSearch(sp,arrow);});
			}
		}
	}else{
		for(var spn = 0; spn < splist.length; spn++){
			
			var filterId = 'sp_'+splist[spn];
			var thisList = document.getElementById(filterId);
			if(thisList){
				if(thisList.style.display=="block"){
					var arrow =(splist[spn]!="zoomto"?true:false);
					//hideSearch(splist[spn],arrow);
					var SPName = splist[spn];
					if(browserIE()){
						thisList.style.display="none";
						hideSearch(SPName,arrow);
					}else{
						$("#"+filterId).animate({ height: 'hide', opacity: 'hide' }, 125, function(){hideSearch(SPName,arrow);});
					}
				}
			}
		}
	}
}
function resetFilters(deptCode){
	
	for(var spnum = 0; spnum < filterlist.length; spnum++){
		var filterId = "sp_"+filterlist[spnum];
		if(spnum == 0){
			$("div#"+filterId+" div.option").each(function(n){														
				var dept_id = "dept_"+deptCode; 													
				if(this.id==dept_id){
					
				   clickFilter1('dept',this,this.innerHTML,deptCode,true);																					 
				}
			});
		}else{
			$("div#"+filterId+" div.option:first").each(function(n){
				clickFilter1(filterlist[spnum],this,this.innerHTML,0,true);																					 
			});
		}
	}
	resetVFilter();
	getDept();
}
// action to take when filter item is clicked
// clickFilter('vFilter',n,n.innerHTML,n.id,true); 
// state is T/F value to say whether fhtml is recorded on the label.
function clickFilter(sptype,fele,fhtml,fval,state){
	var filterId = 'sp_'+sptype;
	var thisList = document.getElementById(filterId);
		if(thisList.style.display=="block"){
			var arrow =(sptype!="zoomto"?true:false);
			if(browserIE()){
				thisList.style.display="none";
				hideSearch(sptype,arrow);
			}else{
				$("#"+filterId).animate({ height: 'hide', opacity: 'hide' }, 125, function(){hideSearch(sptype,arrow);});
			}
		}	
	if(state){
		var bt_ele = 'bt_'+sptype;
		var btDis_ele = 'btDis_'+sptype;
		document.getElementById(bt_ele).innerHTML = fhtml;
		document.getElementById(btDis_ele).innerHTML = fhtml;
	}
	switch(sptype){
		case 'dept':
		currentDept = fval;
		getDept();
		break;
		case 'vFilter':
		if(deptArray[currentDept][5] == filterFields[0]){ // if the dept over this project recognizes agencies...
			vfCat = fval[1];
			agImgName = fval[2];
			agImgWidth = fval[3];
			agImgHeight = fval[4];
		}else{
			vfCat = fval[1];	
		}
		updateMarkers(true);
		break;
		case 'fund':
		fundCat = fval;
		updateMarkers(true);
		break;
		case 'zoomto':
		zoomToLoc = fval;
		zoomToLocation();
		break;
	}
	var id_ele = "sp_"+sptype;
	removeBG(id_ele);
	if(state){
		fele.style.backgroundColor = "#F2F2F2";
	}	
}			

function clickFilter1(sptype,fele,fhtml,fval,state){
	var filterId = 'sp_'+sptype;
	var thisList = document.getElementById(filterId);
	if(thisList.style.display=="block"){
		var arrow =(sptype!="zoomto"?true:false);
		if(browserIE()){
			thisList.style.display="none";
			hideSearch(sptype,arrow);
		}else{
			$("#"+filterId).animate({ height: 'hide', opacity: 'hide' }, 125, function(){hideSearch(sptype,arrow);});
		}
	}
	
	if(state){
		var bt_ele = 'bt_'+sptype;
		var btDis_ele = 'btDis_'+sptype;
		if(document.getElementById(bt_ele)){
			document.getElementById(bt_ele).innerHTML = fhtml;
			document.getElementById(btDis_ele).innerHTML = fhtml;
		}
	}
	switch(sptype){
		case 'dept':
		currentDept = fval;
		break;
		case 'vFilter':
		vfCat = fval;
		break;
		case 'fund':
		fundCat = fval;
		break;
	}
	var id_ele = "sp_"+sptype;
	removeBG(filterId);
	if(state){
		fele.style.backgroundColor = "#F2F2F2";
	}
	//alert("currentDept = "+currentDept+"; vfCat = "+vfCat+"; fundCat = "+fundCat+";");
}			

// removes the background color from all unselected filter options in the filter pane.
function removeBG(divId){
	var elemArg = 'div#'+divId+' div.option';
	$(elemArg).each(function(n) { 
		this.style.backgroundColor = "#c5c5c5";
	});
}		
// initialize zoomto selector

$(document).ready(function() {

	$("div#sp_zoomto div.option").each(function(n) { 
		var valStr = remove(this.id,'loc_');
		var valNum = parseInt(valStr);
		this.onclick= function(){
			clickFilter('zoomto',this,this.innerHTML,valNum,false);
			this.style.backgroundColor = "#F2F2F2";
		}
		this.onmouseover= function(){
			//if(zoomToLoc!=valNum){
				this.style.backgroundColor = "#D3D3D3";
			//}
		}
		this.onmouseout= function(){
			//if(zoomToLoc!=valNum){
				this.style.backgroundColor = "#c5c5c5";	
			//}
		}
	});
});

function zoomToMenu(){
	showSearch('zoomto',false);	
}

// setup dept filter menu
$(document).ready(function() {
	$("div#sp_dept div.option").each(function(n) { 
		var valStr = remove(this.id,'dept_');
		var valNum = parseInt(valStr);
		if(currentDept == valStr){
			this.style.backgroundColor = "#F2F2F2";	
		}
				
		this.onclick= function(){
			clickFilter('dept',this,this.innerHTML,valStr,true);
		}
		this.onmouseover= function(){
			if(currentDept!=valStr){
				this.style.backgroundColor = "#D3D3D3";
			}
		}
		this.onmouseout= function(){
			if(currentDept!=valStr){
				this.style.backgroundColor = "#c5c5c5";	
			}
		}
	});
});

// setup var filter menu
function setup_var_filters(){
	$("div#sp_vFilter div.option").each(function(n) { 
		var valStr = this.id.split('_');
		if(vfCat == valStr[1]){
			this.style.backgroundColor = "#F2F2F2";	
		}
		this.onclick= function(){
			clickFilter('vFilter',this,this.innerHTML,valStr,true);
		}
		this.onmouseover= function(){
			if(vfCat!=valStr[1]){
				this.style.backgroundColor = "#D3D3D3";
			}
		}
		this.onmouseout= function(){
			if(vfCat!=valStr[1]){
				this.style.backgroundColor = "#c5c5c5";	
			}
		}
	});
}

// setup funding filter menu
$(document).ready(function() {
	$("div#sp_fund div.option").each(function(n) { 
		var valStr = remove(this.id,'fund_');
		var valNum = parseInt(valStr);
		if(fundCat == valNum){
			this.style.backgroundColor = "#F2F2F2";	
		}
		this.onclick= function(){
			clickFilter('fund',this,this.innerHTML,valNum,true);
		}
		this.onmouseover= function(){
			if(fundCat!=valNum){
				this.style.backgroundColor = "#D3D3D3";
			}
		}
		this.onmouseout= function(){
			if(fundCat!=valNum){
				this.style.backgroundColor = "#c5c5c5";	
			}
		}
	});
});
$(document).ready(function() {
	$("#mapControls").hover(
		function(){
			$("a.bt_filter").addClass("bt_filter_hover");
		},
		function(){
			$("a.bt_filter").removeClass("bt_filter_hover");
		}
	);
});

// called when dept radio button is clicked

function resetVFilter(){
	if(currentDept=='0'){
		document.getElementById('vFilterBlock').style.display="none";
		document.getElementById('vFilterDis').style.display="block";
		document.getElementById('vFilterDisLabel').innerHTML="To filter select a dept.";
		document.getElementById('vFilterDisLabel').style.color="#515259";
	}else{
		document.getElementById('vFilterBlock').style.display="block"
		document.getElementById('vFilterDis').style.display="none";
	}	
}

var filtCount = 0;

function getDept(){
	vfCat = '0';
	updateMarkers(true);
	//resetVFilter();
	if(currentDept!='0'){
		// disable vfilter temporarily until content is loaded
		document.getElementById('vFilterBlock').style.display="none";
		document.getElementById('vFilterDisLabel').innerHTML="<img src='images/Loading_menu.gif'/>";
		document.getElementById('vFilterDis').style.display="block";
		
		
		get_var_filter();
		
	}else{
		vfType = '0';
		document.getElementById('vFilterBlock').style.display="none";
		document.getElementById('vFilterDisLabel').innerHTML="To filter select a dept.";
		document.getElementById('vFilterDis').style.display="block";
		
	}
}

function updatefilterLabels(){
	if(currentDept!='0'){
		vfType = deptArray[currentDept][5];
		if(deptArray[currentDept][5] == filterFields[0]){ // if the dept over this project recognizes agencies...
			document.getElementById("vFilter_label").innerHTML = "Agency<br/>filter";
			document.getElementById("vFBB").style.left="48px";
			document.getElementById("bt_vFilter").style.width="130px";
			document.getElementById("btDis_vFilter").style.width="130px";
		}else{
			document.getElementById("vFilter_label").innerHTML = "Program<br/>filter";
			document.getElementById("vFBB").style.left="56px";
			document.getElementById("bt_vFilter").style.width = "123px";
			document.getElementById("btDis_vFilter").style.width = "123px";
		}	
	}
}

function get_var_filter(){
	document.getElementById('loadingImg').style.visibility="visible";			
	var values = "dept="+currentDept+"&field="+deptArray[currentDept][5];
	makePOSTReqAJAX('module/filterarray.jsp', values,refresh_var_filter);
}

function refresh_var_filter(){
	if (req.readyState == 4) {
	    if (req.status == 200) {          
			if(currentDept!='0'){
				 var strTemp=req.responseText.toString();
				 var divEle = document.getElementById('sp_vFilter_options');
				 divEle.innerHTML = '';
				 divEle.innerHTML = strTemp;
				 
					if(deptArray[currentDept][5] == filterFields[0]){ // this is an agency filtered department
						document.getElementById("vf_head").innerHTML = "Select an agency to filter by:";
						document.getElementById("vf_0").innerHTML = "All agencies";
					}else{ // this is a program filtered department
						document.getElementById("vf_0").innerHTML = "All programs";
						document.getElementById("vf_head").innerHTML = "Select a program to filter by:";
					}
				 
				 document.getElementById('loadingImg').style.visibility="hidden";	     	
				 setup_var_filters();
				 $("div#sp_vFilter div.option:first").each(function(n){
				 clickFilter1('vFilter',this,this.innerHTML,0,true);																					 
				 });
				 document.getElementById('vFilterDis').style.display="none";
				 document.getElementById('vFilterBlock').style.display="block"
				 updatefilterLabels();
				 vfCat = '0';
			 }		 
	    }
	}
}
/*********************************************************/

	
function makePOSTReqAJAX(url, parameters,fnname) {

	//document.getElementById('output').innerHTML="";  
	//document.getElementById('output').innerHTML="<h1 style='color: red;'>I can not send a request for this length "+ strlen+"</h1>";  

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
         req = new XMLHttpRequest();
         if (req.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            req.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!req) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }      
      req.onreadystatechange = fnname;      
      req.open('POST', url, true);      
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");      
      req.setRequestHeader("Content-length", parameters.length);      
      req.setRequestHeader("Connection", "close");      
      req.send(parameters); 
	  //alert(parameters);
   }
   

/*function Populate_NewString(){
	if (req.readyState == 4) {	
		if (req.status == 200) {		
		  var strTemp=req.responseText;	
		   if(strTemp != 0){		   		
		  		document.getElementById('output').innerHTML="";          	
		  		document.getElementById('output').innerHTML="<h1>"+strlen+":-"+strTemp+"</h1>";
		  	}
		}
	}	
}*/
