﻿var map;
var mrkrMgr;
var mrkrs = new Object();
var sidebarUrl = new Array();
var geoXml = new Array();

function addMarkers(points) {
	var i;
	mrkrMgr.clearMarkers();
	for (i = 0; i < points.length; i++) {
		mrkrMgr.addMarker(createMarker(points[i]), 0);
	}
	mrkrMgr.refresh();
	$("#loading_indicator").hide();
}

function createMarker(data) {
	var tinyIcon = new GIcon();
	tinyIcon.shadow = kmlLoc + "mm_20_shadow.png";
	tinyIcon.iconSize = new GSize(12, 20);
	tinyIcon.shadowSize = new GSize(22, 20);
	tinyIcon.iconAnchor = new GPoint(6, 20);
	tinyIcon.infoWindowAnchor = new GPoint(5, 1);
	
	switch (data.AYPStatusLevel) {
	    case "1": tinyIcon.image = kmlLoc + "mm_20_green.png";
	        break;
	    case "2": tinyIcon.image = kmlLoc + "mm_20_orange.png";
	        break;
	    case "3": tinyIcon.image = kmlLoc + "mm_20_yellow.png";
			break;
		case "4":
		case "5":
		case "6":
		case "7":
		case "8":
		case "9":
		case "A":
		case "B":
		case "C":
		case "D": tinyIcon.image = kmlLoc + "mm_20_red.png";
			break;
		case "": tinyIcon.image = kmlLoc + "mm_20_blue.png";
			break;
		default: tinyIcon.image = kmlLoc + "mm_20_black.png";
			break;
	}
	var point = new GLatLng(data.Lat, data.Long);
	if (point.equals(new GLatLng(0, 0))) {
		markerOptions = { icon: tinyIcon, hide: true };
	}
	else {
		markerOptions = { icon: tinyIcon};
	}
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click", function() {
		var myHtml = "<b>" + data.Name + "<br/>" + data.Address + "<br/><br/>" +
		             "<a href=\"" + data.Href + "\">School Report</a></b><br/>";
		var tempPx = map.fromLatLngToDivPixel(point);
		tempPx.x = tempPx.x + 140;
		var tempPt = map.fromDivPixelToLatLng(tempPx);
		map.panTo(tempPt);
		map.openInfoWindowHtml(point, myHtml);
	});
	mrkrs[data.Num] = marker;
	return marker;
}

function changeOverlay(kmlUrl) {
	var index = geoXml.length - 1;
	if (kmlUrl == null) {
		if (index > 0) {
			map.removeOverlay(geoXml.pop());
			geoXml[index - 1].show();
		}
		else {
			geoXml[index].show();
		}
		map.returnToSavedPosition();
	}
	else {
		geoXml[index].hide();
		geoXml.push(new GGeoXml(kmlUrl));
		var myListener = GEvent.addListener(geoXml[index+1], 'load', function() {
			if (geoXml[index+1].loadedCorrectly()) {
				$("#loading_indicator").hide();
			}
			GEvent.removeListener(myListener);
		});
		$("#loading_indicator").show();
	    map.addOverlay(geoXml[index + 1]);
	}
	if (geoXml.length > 2) {
		var myListener2 = GEvent.addListener(map, "moveend", function() {
		    GEvent.removeListener(myListener2);
			map.panBy(new GSize(-140, 0));
		});
		geoXml[geoXml.length - 1].gotoDefaultViewport(map);
	}
}

function selectMarker(schoolNum) {
	var marker = mrkrs[schoolNum];
	var loc = marker.getLatLng();
	var undef = new GLatLng(0, 0);
	var url;

	if (loc.equals(undef)) {
		noGeoData(schoolNum);
	}
	else {
		GEvent.trigger(marker, 'click');
	}
}

function updateSidebar(href) {
	if (sidebarUrl.length == 1) {
	    $.get(href, function(data) {
	        $("#sidebarStatus").html(data);
	        $("#sidebarState").hide();
	        $("#footnote").css("visibility", "hidden");
	        $("#sidebarStatus").show();
	        if (Browser.ie6() && $('#sidebarStatus > table').height() > 602) {
	            $('#sidebarStatus').css('width', '282px');
	        }
	        if (href.indexOf("type=district") > -1) {
	            $("#sidebarStatus a").click(function(event) {
	                event.preventDefault();
	                sidebarUrl.push($(this).attr("href"));
	                updateMap($(this).attr("href"));
	                updateSidebar($(this).attr("href"));
	                $("#navControl").show();
	            });
	        }
	    });
	}
	else if (sidebarUrl.length == 2) {
	    $.get(href, function(data) {
	    	$("#sidebarDistrict").html(data);
	    	$("#sidebarStatus").hide();
	    	$("#sidebarDistrict").show();
	    	if (Browser.ie6() && $('#sidebarDistrict > div').height() > 602) {
	    		$('#sidebarDistrict').css('width', '282px');
	    	}
	    });
	}
	$("#navControlTop").show();
}

function updateMap(href) {
		var regex = new RegExp("\\?", "g");
		var type = "";
		var status = "";
		var district = "";
		
		regex.exec(href);
		var query = new String(RegExp.rightContext).toLowerCase();
		var items = query.split("&");
		regex.compile("=", "g");
		for (var i in items) {
			regex.test(items[i]);
			if (RegExp.leftContext == "type") {
				type = RegExp.rightContext;
			}
			else if (RegExp.leftContext == "status") {
				status = RegExp.rightContext;
			}
			else if (RegExp.leftContext == "district") {
				district = RegExp.rightContext;
			}
		}
		if (type == "school") {
			addSchoolsByStatus(status);
		}
		else if (type == "district") {
			changeOverlay(kmlLoc + "status" + status + ".kmz");
		}
		else if (district != "") {
		    changeOverlay(kmlLoc + district + ".kml");
		    addSchoolsByDistrict(district);
		}
	}

function navBack() {
	sidebarUrl.pop();
	map.closeInfoWindow();
	$("#sidebarDistrict").hide();
	$("#sidebarStatus").show();
	$("#navControl").hide();
	changeOverlay(null);
	mrkrMgr.clearMarkers();
}

function navTop() {
    sidebarUrl.pop();
    map.closeInfoWindow();
    if (sidebarUrl.length == 1) {
        sidebarUrl.pop();
        geoXml.pop().hide();
        $("navControl").hide();
    }
    $("#sidebarStatus").hide();
    $("#sidebarDistrict").hide();
    $("#sidebarState").show();
    $("#footnote").css("visibility", "visible");
    $("a[id^='navControl']").hide();
    changeOverlay(null);
    mrkrMgr.clearMarkers();
}

function makePie(type, data) {
	var chartSrc, percent, labels, colors, rot, color, label;
	chartSrc = "http://chart.apis.google.com/chart?cht=p&chs=290x250&chf=bg,s,fafafa";
	percent = "&chd=t:";
	labels = "&chl=";
	colors = "&chco=";
	if (type == "school" || type =="district") {
		for (i = 0; i < data.length; i++) {
			switch (data[i].AYPStatusLevel) {
				case "1": color = "28953b,";
					      label = data[i].AYPStatusLevel + "|";
					break;
	            case "2": color = "ef922d,";
	                      label = data[i].AYPStatusLevel + "|";
                          break;
                case "3": color = "f8cf23,";
                          label = data[i].AYPStatusLevel + "|";
					      break;
			    case "4":
			    case "5":
			    case "6":
			    case "7":
			    case "8":
			    case "9": color = "cd3429,";
			    	      label = data[i].AYPStatusLevel + "|";
			    	break;
			    case "A": color = "cd3429,";
			    	      label = "10" + "|";
			    	break;
			    case "B": color = "cd3429,";
			    	      label = "11" + "|";
			    	break;
			    case "C": color = "cd3429,";
			    	      label = "12" + "|";
			    	      break;
			    case "D": color = "cd3429,";
			    	      label = "13" + "|";
			    	      break;
			    case "": color = "3030FF";
			    	     label = "14";
			    	break;
			    default: color = "000000,";
			    	     label = "99,";
			}
			if (type == "school" && data[i].schools > 0) {
				rot = "&chp=.50";
				percent += data[i].schoolPer + ",";
				colors += color;
				labels += label;
			}
			else if (type == "district" && data[i].districts > 0) {
			    rot = "&chp=.25";
			    percent += data[i].districtPer + ",";
			    colors += color;
			    labels += label;
			}
		}
		percent = percent.substring(0, percent.length - 1);
		if (labels.substring(labels.length-1) == "|") {
			labels = labels.substring(0, labels.length - 1);
			colors = colors.substring(0, colors.length - 1);
		}
	}
	else {
		chartSrc = "UNRECOGNIZED TYPE";
		rot = "";
		colors = "";
		labels = "";
		data = "";
	}
	
	return chartSrc + rot + colors + labels + percent;
}

function fillPieLegend(data) {
	var schoolPer, districtPer, accessor;
	for (i = 0; i < data.length; i++) {
		if(data[i].AYPStatusLevel > 0 && data[i].AYPStatusLevel <= 9){
			accessor = i+1;
		}
		else if(data[i].AYPStatusLevel >= 'A' && data[i].AYPStatusLevel <= 'D') {
			switch(data[i].AYPStatusLevel) {
			case 'A': accessor = 10;
				break;
			case 'B': accessor = 11;
				break;
			case 'C': accessor = 12;
			    break;
			case 'D': accessor = 13;
			    break;
			}
		}
		else if(data[i].AYPStatusLevel == "") {
			accessor = 14;
		}
		else {
			alert("ACCESSOR NOT FOUND");
		}
		if (data[i].schools == -1 && data[i].districts == -1) {
			$("#Schools" + accessor).parent().parent().hide();
		}
		else {
		    $("#Schools" + accessor).parent().parent().show();

		    if (data[i].schools != -1) {
		    	schoolPer = parseFloat(data[i].schoolPer).toFixed(1);
		    	$("#Schools" + accessor).html(data[i].schools + "&#160;/&#160;" + schoolPer + "%");
		    }
		    else {
		    	$("#Schools" + accessor).text("");
		    }
		    if (data[i].districts != -1) {
			    districtPer = parseFloat(data[i].districtPer).toFixed(1);
			    $("#Districts" + accessor).html(data[i].districts + "&#160;/&#160;" + districtPer + "%");
			   }
			else {
				$("#Districts" + accessor).text("");
			}
		}
	}
}

function hideDistrictPie() {
	$("#districtPie").parent().hide();
	$("#districtPie").parent().prev().attr("colspan", 2);
	$("#districtHeader").parent().hide();
	$("span[id^=Districts]").parent().hide();
	$("#districtColumnHead").hide();
}

function showDistrictPie() {
	$("#districtPie").parent().show();
	$("#districtPie").parent().prev().attr("colspan", 1);
	$("#districtHeader").parent().show();
	$("span[id^=Districts]").parent().show();
	$("#districtColumnHead").show();
}