

var map = null;
var baseIcon = null;
var tinyIcon = null
var geocoder = null;
var resultCount = 0;
var currentIndex = 0;
var points = [];
var currentMarkers = [];
var searchMarkers = [];
var isAuthorized = false;
var noResults = false;

if (window.attachEvent){
	window.attachEvent("onload", function(){initialize(); });
}
else if(window.addEventListener){
	window.addEventListener("load", function(){initialize(); }, false);
}

$(document).ready(function(){
	$("#intl_alumnae").click(function(){
		getIntlMarkers();
		return false;
	});
});


function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(address, function(point){ showInfoSearch(point, address, "C"); });
	} 
}

function showInfoSearch(point, address, letter) {
	if (!point) {
		alert(address + " not found.");
	} else {
		map.setCenter(point, 10);
		
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		
		// Set up our GMarkerOptions object
		var markerOptions = { icon:letteredIcon };
		var marker = new GMarker(point, markerOptions);
		map.addOverlay(marker);
		searchMarkers.push({marker: marker, requested: false});
		
		var displayInfo = "<div class='search_miles'>";
		displayInfo += "<p>Find " + constit + " with in ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=.25' onclick='return getMarkers(this);'>0.25 miles</a>, ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=.5' onclick='return getMarkers(this);'>0.5 miles</a>, ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=1' onclick='return getMarkers(this);'>1 miles</a>, ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=5' onclick='return getMarkers(this);'>5 miles</a>, ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=10' onclick='return getMarkers(this);'>10 miles</a>, ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=15' onclick='return getMarkers(this);'>15 miles</a>, ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=20' onclick='return getMarkers(this);'>20 miles</a>, ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=30' onclick='return getMarkers(this);'>30 miles</a>, or ";
		displayInfo += "<a rel='" + searchMarkers.length + "' target='_blank' href='/ajax/googlemap.aspx?pageaction=viewmap&mid=36&lat=" + point.y + "&long=" + point.x + "&d=50' onclick='return getMarkers(this);'>50 miles</a>";
		displayInfo += " of " + address + ".</p></div>";
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(displayInfo, {maxWidth:300});
		});
		
		marker.openInfoWindowHtml(displayInfo, {maxWidth:300});
	} //if
	
}
	
function getMarkers(elm){
	var href = elm.href;
	var rel = elm.rel;
	$("#loading").show();

	clearMarkers();
	points = null;
	points = [];
	currentIndex = 0;
	//resetSearchMarkers();
	//searchMarkers[rel-1].requested = true;
	
	/*if (searchMarkers[rel-1].requested) {
		href += "&count=" + resultCount;
		currentIndex -= results;
	} else {
	}*/
	
	$.ajax({
		url: href, 
		type: "GET",
		success: function(html){
				$("#search_results").hide().html(html);
				var js = $("#search_results textarea").val();
				eval(js);
				if (isAuthorized && !noResults){
					$("#gpaging").show();
					$("#search_results textarea").remove();
					$("#search_results").show();
					showMarkers();
				} else {
					var msg = noResults?"No results.<br />Please try another distance or location.":"Please login. <a href='/login/?ModuleID=40&Referrer=" + location.pathname + "'>Click here to login.</a>";
					var timeout = noResults?5000:20000;
					
					$("#loading p").html(msg);
					setTimeout(function(){
						$("#loading").hide();
						$("#loading p").html("Loading...");
					},timeout);
				}
				SI_clearFooter();
			},
		error: function(html){
				alert("Error occured while trying to retrieve the data. Please try again.");	
				$("#loading").hide();						
			},
		async: true
	});	
	
	$("#intl_alumnae").show('slow');
	map.addControl(control);
	noResults = false;
	return false;
}

function getIntlMarkers(){
	var href = "/ajax/googlemap.aspx?pageaction=viewcountry&mid=36";
	
	map.setCenter(new GLatLng(20.0993986, -1.09262), 1);
	map.removeControl(control);
	
	$("#loading").show();

	clearMarkers();
	points = null;
	points = [];
	currentIndex = 0;
	
	$.ajax({
		url: href, 
		type: "GET",
		success: function(html){
				$("#search_results").hide().html(html);
				var js = $("#search_results textarea").val();
				eval(js);
				if (isAuthorized){
					$("#gpaging").show();
					$("#search_results textarea").remove();
					$("#search_results").show();
					
					points.push({lat: 36.580585, long: -99.843750, dId: 0, aId: 0, name: '<p>There are too many alumnae in this country to map them all at once.</p><p>Enter a U.S. city or address above and click "Mark it!" to plot your location and see alumnae near you.</p>', address: ''});
					
					showMarkers();

					$("#prevset, #nextset").css("visibility", "hidden");
					$("#gpaging").hide();
					resultCount = 0;
					
				} else {
					var width = $("#loading p:first").css("width");
					$("#loading p").html("Please login again.").css("width", "255px");
					setTimeout(function(){
						$("#loading").hide();
						$("#loading p").html("Loading...").css("width", width);
					},5000);
				}
				SI_clearFooter();
			},
		error: function(html){
				alert("Error occured while trying to retrieve the data. Please try again.");	
				$("#loading").hide();						
			},
		async: true
	});	
	
	$("#intl_alumnae").hide();
	return false;
}

function showMarkers(){
	var i = 0;
	var marker = null;
	var index = 0;
	var tmp = 0;
	// Set up our GMarkerOptions object literal
	var markerOptions = { icon:tinyIcon };

	clearMarkers();
	
	if (points.length && points.length > 0){
		resultCount = points.length;
		index = currentIndex + maxResults;
		
		if (index <= currentIndex){
			//swap them
			tmp = currentIndex;
			currentIndex = index;
			index = tmp - results;
		}
		if ( index < 0 || currentIndex < 0){
			index = results;
			currentIndex = 0;
		}
		if ( index > resultCount ){
			index = resultCount;
		} 
		
		$("#prevset, #nextset").css("visibility", "hidden");
		
		if (0 < currentIndex) {
			$("#prevset").css("visibility", "visible");
		}
		if(currentIndex < resultCount && index < resultCount) {
			$("#nextset").css("visibility", "visible");
		} 
		
	
		for (i = currentIndex; i < index; i++){
			if (points[i].dId === null) {
				markerOptions = { icon:tinyIcon2 };
			}
			marker = new GMarker(new GLatLng(points[i].lat, points[i].long), markerOptions);
			currentMarkers.push(marker);

			map.addOverlay(marker);
			info(marker, points[i]);
		}
		$("#gpaging span.paging").html("<span class='begin'>" + currentIndex + "</span> - <span class='end'>" + index + "</span> of <span class='total'>" + resultCount + "</span>");
		currentIndex = index;
		
		if(currentIndex >= resultCount) {
			$("#nextset").css("visibility", "hidden");
		} 
	}
	maxResults = results;
	$("#loading").hide();
}


function info(marker, point){
	var html = "<p class=\"info\">" + point.name + "<br />" + point.address + "</p>";
	
	GEvent.addListener(marker, "click", function(dId, aId) {
		this.openInfoWindowHtml(html, {maxWidth:300});
	});
}


function clearMarkers(){
	if (currentMarkers.length && currentMarkers.length > 0){
		for (i = 0; i < currentMarkers.length; i++){
			map.removeOverlay(currentMarkers[i]);
			currentMarkers[i] = null;
		}
		currentMarkers = [];
	}
}


function resetSearchMarkers(){
	var i = 0;
	
	for (i = 0; i < searchMarkers.length; i++){
		searchMarkers[i].requested = false;
	}
}

function getProfileInfo(id, aId){
	var name = $("#" + aId + " .name").html();
	var address = $("#" + aId + " .address").html();
	var cityStateZip = $("#" + aId + " .city").html();
	var response = "";
	
	response += "<p class=\"info\">" + name + "<br />" + address + "<br />" + cityStateZip + "</p>";

	return response;
}