function initialize() {
    initMap("map");

    $("#submit").bind("click", function() {
	    search($("#keyword").val(), $("#type").val());
	    return false;
        });


    $(window).bind('resize', resizeMap);

    $("#keyword").autocomplete("dict.php", {selectFirst: false});

    $("#dialog").dialog({ autoOpen: false, height: 400, width: 500, draggable: false, modal: true, resizable: false, buttons: { "Close": function() { $(this).dialog("close"); } }  });

    $.getJSON("pop.php", function(json){
	    var pop = new Array();
	    for(var i in json) {
		pop[parseInt(json[i].code)] = parseInt(json[i].number);
	    }
	    tot_pop = pop;
	});
}

function initMap(id) {
    map = new GMap2(document.getElementById(id));

    var location = getLocation();

    map.setMapType(G_PHYSICAL_MAP);
    map.setCenter(new GLatLng(location[0], location[1]), location[2]);
    resizeMap();
}

function updateMap(keyword, type) {
    var i;
    var max = 0;
    var map_data = new Array();
    
    map.clearOverlays();

    for(i = 0; i < data.length; i++) {
	if(type == "population" || type == "density") {
	    if(map_data[data[i].code] == undefined) {
		map_data[data[i].code] = new Array(parseInt(data[i].number), data[i].location, parseInt(data[i].code), data[i]);
	    } else {
		map_data[data[i].code][0] += parseInt(data[i].number);
	    }
	} else if(type == "earnings") {
	    if(map_data[data[i].code] == undefined) {
		map_data[data[i].code] = new Array(parseInt(data[i].annual), data[i].location, 1.0, data[i]);
	    } else {
		map_data[data[i].code][0] += parseInt(data[i].annual);
		map_data[data[i].code][2] += 1;
	    }
	}
    }

    for(var i in map_data) {
	if(type == "earnings") {
	    map_data[i][0] = Math.round(map_data[i][0] / map_data[i][2]);
	} else if(type == "density") {
	    map_data[i][0] = (1.0 * map_data[i][0]) / tot_pop[map_data[i][2]];
	}
	if(map_data[i][0] > max) {
	    max = map_data[i][0];
	}
    }

    for(var i in map_data) {
	displayMarker(map_data[i], max, keyword, type);
    }
}

function displayMarker(info, max, keyword, type) {
    var icon = new GIcon(G_DEFAULT_ICON);

    icon.image = "http://renaudbourassa.com/projects/jobu/marker.png";
    if(type == "population" || type == "density") {
	icon.iconSize = new GSize(20 + Math.round(30.0 * info[0] / max), 20 + Math.round(30.0 * info[0] / max));
	icon.shadowSize = new GSize(30 + Math.round(30.0 * info[0] / max), 20 + Math.round(30.0 * info[0] / max));
    } else if(type == "earnings") {
	icon.iconSize = new GSize(Math.round(50.0 * info[0] / max), Math.round(50.0 * info[0] / max));
	icon.shadowSize = new GSize(10 + Math.round(50.0 * info[0] / max), Math.round(50.0 * info[0] / max));
    }

    markerOptions = { icon:icon, clickable:true };
    var marker = new GMarker(new GLatLng(info[1][1], info[1][0]), markerOptions);

    GEvent.addListener(marker, "click", function() {
	    $.getJSON("list.php?keyword=" + escape(keyword) + "&location=" + escape(info[3].area + ", " + info[3].state), function(json){
		    $("#dialog").dialog('option', 'title', keyword.toUpperCase() + " JOBS IN " + info[3].area.toUpperCase());
		    var str = "<div id=\"data\"><p><b>DATA</b></p><p style=\"margin-bottom:10px;\">";
		    if(type == "population") {
			str += "Number of Workers: " + info[0].toString();
		    } else if(type == "density") {
			var a = info[0] * 100;
			str += "Proportion of Workers: " + a.toString() + "%";
		    } else if(type == "earnings") {
			str += "Annual Salary: $" + info[0].toString();
		    }
		    str += "</p><p><b>JOB OFFERS</b></p><ul>";
		    if(!json) {
			str += "<li>No results found.</li>";
		    } else {
			for(var i in json) {
			    str += "<li><a target=\"_blank\" href=\"" + json[i].link[0] + "\">" + json[i].title[0] + "</a></li>";
			}
		    }
		    str += "</ul></div>";
		    $("#dialog").html(str);
		    $("#dialog").dialog( 'open' );
		});
	});
    map.addOverlay(marker);
}

function jobOffers() {
    
}

function getLocation() {
    var l = google.loader.ClientLocation;
    if(l && l.latitude && l.longitude) {
	return[l.latitude, l.longitude, 7];
    } else {
	return[38, -97, 5];
    }
}

function resizeMap() {
    var h = $(window).height()-150;
    $("#map").css("height",h);
}

function search(keyword, type) {
    $.getJSON("search.php?keyword=" + keyword, function(json){
	    data = json;
	    updateMap(keyword, type);
	});
    return;
}