var tabView = null;
var bingMap = null;

$(document).ready(function() {
    $('.d-toolbar-button').mouseenter(function() {
	if ($(this).hasClass("d-toolbar-button-selected"))
	    $(this).addClass("d-toolbar-button-selected-hover");
	else
	    $(this).addClass("d-toolbar-button-hover");
    });
    $('.d-toolbar-button').mouseleave(function() {
	if ($(this).hasClass("d-toolbar-button-selected"))
	    $(this).removeClass("d-toolbar-button-selected-hover");
	else
	    $(this).removeClass("d-toolbar-button-hover");
    });

    bingMap = new Ports.BingMap();
    bingMap.OnAddShapes = function() {

	var i=0;
	var l = mapPoints.length;
	while (i < l) {
	    var x = mapPoints[i];
	    if (x.code == "MASSC" || x.code == "SCHOO" || x.code == "RELIG" || x.code == "INSTN" )
		this.GetLayerById(x.code).AddShape(x.id, x.latitude, x.longitude, '<a href="/directory/organisation/'+x.id+'.htm">'+x.title+'</a>', x.description);
	    i++;
	}

	for(var i=0; i<bingMap.GetLayerById("MASSC").shapes.length; i++)
	    bingMap.GetLayerById("MASSC").shapes[i].SetMinZoomLevel(10);
	for(var i=0; i<bingMap.GetLayerById("SCHOO").shapes.length; i++)
	    bingMap.GetLayerById("SCHOO").shapes[i].SetMinZoomLevel(10);
	for(var i=0; i<bingMap.GetLayerById("RELIG").shapes.length; i++)
	    bingMap.GetLayerById("RELIG").shapes[i].SetMinZoomLevel(10);
	for(var i=0; i<bingMap.GetLayerById("INSTN").shapes.length; i++)
	    bingMap.GetLayerById("INSTN").shapes[i].SetMinZoomLevel(10);
    }

    bingMap.OnPostLoad = function() {
	$("#btnZoomIn").click(function() {
	    bingMap.ZoomIn();
	});
	$("#btnZoomOut").click(function() {
	    bingMap.ZoomOut();
	});
	$("#btnMapReset").click(function() {
	    bingMap.ResetView();
	});

	$("#mapHint").fadeIn();

	bingMap.GetLayerById("MASSC").CreateLayerButton("Churches");
	bingMap.GetLayerById("SCHOO").CreateLayerButton("Schools");
	bingMap.GetLayerById("RELIG").CreateLayerButton("Religious");
	bingMap.GetLayerById("INSTN").CreateLayerButton("Other");

	bingMap.map.AttachEvent("onendzoom", function() {
	    if (bingMap.map.GetZoomLevel() <= 9 && $('#mapHint').is(':hidden'))
		$("#mapHint").fadeIn();
	    else if (bingMap.map.GetZoomLevel() >= 10  && $('#mapHint').is(':visible'))
		$("#mapHint").fadeOut();
	});
	bingMap.SetViewAsDefault();

	bingMap.LoadBoundaries();

	bingMap.map.ZoomOut();
	setTimeout(function() {
	    bingMap.map.ZoomIn();
	}, 1000);

    };

    tabView = new Objective.UI.TabView("t", "tabHeader", "tabBody");
    tabView.AddTab(new Objective.UI.TabView.Tab("browse","People &amp; Places"));
    var tb = new Objective.UI.TabView.Tab("map","Map");
    tb.OnSelect = function() {
	bingMap.Render("map");
    };
    tabView.AddTab(tb);
    tabView.AddTab(new Objective.UI.TabView.Tab("changes","Recent changes"));

    tabView.Render();

    tabView.OnLoad();


});

