/*!
	// Copyright 2009 Eugene M. Sadhu & Respective library and image creators.
	// All Rights Reserved.
	// This software or parts thereof may not be used or reproduced in any manner without express written permission.
	// For information:
	//   Eugene M. Sadhu (eugene@uicmed.net)
*/
var orgmap = jQuery.csv()($.ajax({ url: "orgmap.csv", async: false }).responseText),geocoder,d=null,zoomloc,map, markers = [];
function sm(id){
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.iconSize = new GSize(25,35);
	icon.shadow = "";
	if(id==16){
		icon.image="gcfd_logo.png";
	}else{
	icon.sprite = {};
	icon.sprite.image = "img/sprite.png";
	icon.sprite.top =  id * 61 -2;
	//
	}
	return icon;
}

$(document).ready(function(){
	var c,seen,smm,orglen,dirlen,i,j,t,tli,temp,cat,point,total;
	$("#ins").corner();
	map = new GMap2(document.getElementById('cmap'));
	c = new GLatLng(41.8,-87.6);



	map.setCenter(c, 11);
	map.setUIToDefault();
	geocoder = new GClientGeocoder();

	map.disableScrollWheelZoom();
	markers[0]=[];
	markers[0][0]=[];
	seen=[];
	smm=[];
	for (i = 1; i <= 16; i++) {
		markers[i]=[];
		seen[i]=[];
		smm[i]=sm(i);
		$("#sel>ul>li:eq(" +(i-1) + ")>div").css("background-position", "0 -" + (59+61*(i-1)) +"px");

	}
	$("#sel>ul>li:eq(15)>div").css("background-position", "0 0").attr("id","gcfd").css("background-image","url(gcfd.ico)");
	
	orglen=orgmap.length;

	for(i=1;i<orglen;i++){
		tli=$("<li>").attr("id","s"+orgmap[i][0]+"-"+orgmap[i][2]).html(orgmap[i][3]).addClass("l");
		$("#sel" + ("0"+orgmap[i][0]).slice(-2)  + " ul").append(tli);
		markers[orgmap[i][0]][orgmap[i][2]]=[];
		seen[orgmap[i][0]][orgmap[i][2]]=[];
	}
	for(i=1;i<=16;i++){
		$("#sel" + ("0"+i).slice(-2)  + " ul").append( $("<li>").attr("id","s"+i).addClass("l").html("-ALL-"));
	}
	dirlen=directory.length;
	d=window.location.toString().split("#")[1];
	for (i = 1; i < dirlen; i++) {
		temp=directory[i]; cat=temp[2]; point = new GLatLng(temp[15],temp[14]);
		marker = new GMarker(point,smm[cat]);
		marker.idx=i;
		markers[cat][temp[3]].push(marker);
	}
	for(i=1;i<=16;i++){
		total=0; temp=markers[i];
		for (j in temp){
			t=parseInt(temp[j].length);
			$("#sel li#s"+i+"-"+j).append(" [" +t +"]");
			total+=t;
		}
		$("#sel>ul>li:eq("+(i-1)+")>div").append(" [" + total +"]");
	}
	function showhide(id,subid,onoff){
		var mset=markers[id][subid], tc=mset.length,i;

		if(onoff){
			if(seen[id][subid]==0){
				$("#loading").show();
				for(i=tc;i--;){
					map.addOverlay(mset[i]);
				}
				$(mset).each(function(i,marker){ GEvent.addListener(marker, "click", function(){ showmsg(marker,id,i);  }); });
				$("#loading").hide();
				seen[id][subid]=1;
			}else{
				for(i=tc;i--;){ mset[i].show(); }
			}
			if(pageTracker){
				pageTracker._trackEvent('Taxonomy Sub', 'Add', id + "-" + subid);
			}
			if(d){dd(id,subid,1);}
		}else{
			for(i=tc;i--;){
				mset[i].hide();
			}
			if(d){dd(id,subid,0);}
			if(pageTracker){
				pageTracker._trackEvent('Taxonomy Sub', 'Remove', id + "-" + subid);
			}

		}
	}
	if(d){ d=d=='debug'?1:0;}
	$("#sel li.l").toggle(function(){
		var idparts=$(this).attr("id").substring(1).split("-"), id=idparts[0], subid;
		if(idparts.length==1){
			for(subid in markers[id]){
				showhide(id,subid,1);
			}
			$(this).siblings().addClass("lshown");
		}else{
			showhide(id,idparts[1],1);
		}
		$(this).addClass("lshown");
		$(this).parent().parent().addClass("shown");

	}, function(){
		var idparts=$(this).attr("id").substring(1).split("-"), id=idparts[0], subid;
		if(idparts.length==1){
			for(subid in markers[id]){
				showhide(id,subid,0);
			}
			$(this).siblings().removeClass("lshown");

		}else{
			showhide(id,idparts[1],0);
		}
		$(this).removeClass("lshown");
		if($(this).siblings(".lshown").length==0){
			$(this).parent().parent().removeClass("shown");
		}
	});

	$("#sel li.b div").toggle(function(){
		$(this).addClass("opened");
		if(pageTracker){
			pageTracker._trackEvent('Category', 'Open', $(this).html());
		}
		$(this).next().show();
	}, function(){
		$(this).removeClass("opened");
		if(pageTracker){
			pageTracker._trackEvent('Category', 'Close', $(this).html());
		}
		$(this).next().hide();
	});
	if(d){
		$("#lic").after($("<ul>").attr("id","d"));
	}
	function dd(i,j,s){
		var m,k,e,t;
		if(s){
			for(m in markers[i][j]){
				k=markers[i][j][m];e=directory[k.idx];t=$("<li>").addClass("d"+i+"-"+j).attr("title",e[0]).html(e[1] + "<em>" + e[12] +"</em>");
				$("#d").append(t);
			}
		}else{
			$("#d li.d"+i+"-"+j).remove();
		}
	}
	$("#dbox").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 350,
		modal: true,
		buttons: {
			'Go': function() {
				sca($('#addr').val());
			},
			Cancel: function() {
				$("#sel #zl").val($("#sel #zl").attr("oldvalue"));
				$(this).dialog('close');
			}
		},
		close: function() {
			$("#dbox p#err").html("");
		}
	});

	$("#sel #zl").change(function(){
		var val=$(this).val();
		if(val=="c"){
			$('#dbox').dialog('open');
		}else{
			map.setCenter(zoomloc[val][0],zoomloc[val][1]);
			$(this).attr("oldvalue",val);
			if(pageTracker){ pageTracker._trackEvent('ZoomTo', "Position: " + val); }
		}
	}).attr("oldvalue",1);
	$(".header_main").before($("<img>").attr("src","img/SSHRM-logo-new.gif").attr("id","egl"));
	//$(window).resize(function(){map.checkResize(); });
	$("#ins a[id]").click(function(){
		var id="#" + this.id.split("-")[1];
		$(id).effect("highlight", {}, 3000);

	});
	$('#cmap').resizable({ autoHide: true, stop: function(event, ui) { map.checkResize(); } });
});


function showmsg(marker,id,i){
	var e=directory[marker.idx], str="<div id='pop'><h1>" + e[1] + "</h1>" +e[12] +"<br/>"+e[13]+ "<hr/>" + "contact:<br/>" + e[10] + " " + e[9] + " " + e[8] + "<br/>" + e[5] + "<br/>" + e[6] + (e[4]!="" && e[4]!=" "?"<br/>Entry Notes:" + e[4]:"") + (e[7]!="" && e[7]!=" "?"<br/> <a href='" + e[7] + "'>website</a>":"") +"<a style='float:right; font-size:80%' onClick='removeflag("+id+","+e[3]+","+i + ")'>remove</a></div>";
	if(e[2]=="16"){
		//gcfd
		str="<div id='pop'><h1>" + e[1] + "</h1>" +e[12] +"<br/>"+e[13]+ "<hr/>" +  e[4] + (e[7]!="" && e[7]!=" "?"<br/> <a href='" + e[7] + "'>website</a>":"") +"<a style='float:right; font-size:80%' onClick='removeflag("+id+","+e[3]+","+i + ")'>remove</a></div>";
		str.replace("<br/><br/>","");
	}
	map.openInfoWindowHtml(marker.getLatLng(),str);
	if(pageTracker){ pageTracker._trackEvent('Details', e[0]); }
}
function removeflag(id,subid,i){
	map.closeInfoWindow();
	markers[id][subid][i].hide();
	if(pageTracker){ pageTracker._trackEvent('Removed', markers[id][subid][i][0]); }
}
function sca(a) {
if (geocoder) { geocoder.getLatLng(a,function(p) {if (!p) {$("#dbox p#err").html("Sorry, I'm unable to find a match for " + a); return false;} else {
	$("#dbox").dialog('close');
	zoomloc.push(new Array(p,15));
	$("#zl").append($("<option>").val(zoomloc.length-1).html(a).attr("selected","selected").css("background-color","beige")).change();
	if(pageTracker){ pageTracker._trackEvent('ZoomTo Addition', a); }
}});}
}
zoomloc=[[new GLatLng(41.8,-87.6),10],
[new GLatLng(41.8,-87.6),11],
[new GLatLng(41.794804,-87.589273),14],
[new GLatLng(41.791028,-87.617254),14],
[new GLatLng(41.809452,-87.59378),14],
[new GLatLng(41.812942,-87.617769),14],
[new GLatLng(41.77666,-87.587299),13],
[new GLatLng(41.70455,-87.5348),13]];
