var tabView = null;
var bingMap = null;
var defaultSearch = "e.g. surname, place name, organisation title";


$(document).ready(function() {
    $('.d-large-search-box').corner("round");
    $('.d-large-search-box-field').corner("round");
    $('#mapHint').corner("round");

    $("#q").focus(function() {
	var v = $(this).val();
	if ( v == defaultSearch ) {
	    $(this).css("color", '#000');
	    $(this).val('');
	}
    });
    $("#q").blur(function() {
	if ($(this).val() == '') {
	    $(this).css("color", '#666');
	    $(this).val(defaultSearch);
	}
    });

    $("#q").css("color", '#333');
    $("#q").blur();

    $(".d-search-button").mouseover(function() {
	$(this).addClass("d-search-button-hover");
    });
    $(".d-search-button").mouseout(function() {
	$(this).removeClass("d-search-button-hover");
    });

    $('.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","Quick links"));
    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("facts","Facts and figures"));

    tabView.Render();

    tabView.OnLoad();


});
