var cow = new GIcon();
cow.image = "img/cow_icon.png";
cow.iconSize = new GSize(42, 38);
cow.shadow = 0;
cow.iconAnchor = new GPoint(21,31);
var map;
var tooltip = document.createElement("div");
function addMap() {
    if(GBrowserIsCompatible) {
        map = new GMap2(document.getElementById("map"));
        map.setMapType(G_NORMAL_MAP);
        map.setUIToDefault();
        map.setCenter(new GLatLng(centre[0],centre[1]),centre[2]);
        map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
        for(i=0; i<restaurants.length; i++) {
            map.addOverlay(createMarker(new GLatLng(restaurants[i][0],restaurants[i][1]),restaurants[i][2],restaurants[i][3]));
        }
    }
}
function createMarker(latLang,url,text) {
    var marker = new GMarker(latLang,{icon:cow});
    marker.tooltip = '<div class="tooltip"><nobr>'+text+'<\/nobr><\/div>';
    if(url != null) {
        GEvent.addListener(marker, "click", function() {
            window.location = url;
        });
    }
    GEvent.addListener(marker, "mouseover", function() {
        showTooltip(marker);
        $(".tooltip").css({display:"none"}).fadeIn("fast");
    });
    GEvent.addListener(marker, "mouseout", function() {
        $(".tooltip").fadeOut("fast");
    });

    return marker;
}
function showTooltip(marker) {
    tooltip.innerHTML = marker.tooltip;
    var point = map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
    var offset = map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
    var anchor = marker.getIcon().iconAnchor;
    var width = marker.getIcon().iconSize.width/2;
    var height = tooltip.clientHeight;
    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
    tooltip.style.marginLeft= "-" + tooltip.clientWidth/2 + "px";
    pos.apply(tooltip);
}
function addStreetview() {
		var myPano = new GStreetviewPanorama(document.getElementById("map"));
		fenwayPark = new GLatLng(restaurants[0][0],restaurants[0][1]);
		myPOV = {yaw:parseFloat(newPOV['yaw']),pitch:parseFloat(newPOV['pitch']),zoom:parseFloat(newPOV['zoom'])};
		myPano.setLocationAndPOV(fenwayPark, myPOV);

 /* panoramaOptions = {
        latlng : new GLatLng(restaurants[0][0],restaurants[0][1]),
        features : { userPhotos : false },
        pov: newPOV
    };
    var myPano = new GStreetviewPanorama(document.getElementById("map"), panoramaOptions);*/
    GEvent.addListener(myPano, "error", handleNoFlash);
    if($("#mapSwitch").size()<1) {
        $("#map").after("<div id=\"controls\"><a href=\"#\" class=\"active\" id=\"streetSwitch\">Street View</a> :: <a href=\"#\" id=\"mapSwitch\">Map</a></div>");
        $("#streetSwitch").click(function(){
            $("#streetSwitch, #mapSwitch").toggleClass("active");
            $("#map").empty();
            addStreetview();
            return false;
        });
        $("#mapSwitch").click(function(){
            $("#streetSwitch, #mapSwitch").toggleClass("active");
            $("#map").empty();
            addMap();
            return false;
        });
    }
}

function handleNoFlash(errorCode) {
    if (errorCode == 603) {
        alert("Sorry, Google Street View requires flash.");
        $("#map").empty();
        addMap();
        return;
    }
}