﻿//on dom ready
$(document).ready(function() {

    if (typeof geomarkers != 'undefined')
    {
        $("#map").googleMap(37.1603, -98.8769, 3, {
                controls: ["GLargeMapControl", "GMapTypeControl"],
                markers: geomarkers,
                mapType: G_NORMAL_MAP
            });

        if (geomarkers.length>0)
        {
            Building.CurrentBuilding = geomarkers[0].marker.building;
            Building.Load();
            $('#b-'+Building.CurrentBuilding).addClass('BuildingTableCurrent');
        }
    }
    
    $('div.BuildingTable').hover(
        function(){$(this).addClass('BuildingTableHover')}, 
        function(){$(this).removeClass('BuildingTableHover')}
    );
    
    $('div.BuildingTable').click(function(){
        $('div.BuildingTable').removeClass('BuildingTableCurrent');
        $(this).addClass('BuildingTableCurrent');
        var bid = this.id.replace('b-','');
        if (bid>0)
        {
            Building.CurrentBuilding = bid;
            Building.Load();
        }
    });
    
    $('li.BuildingList').click(function(){
        $('li.BuildingList').removeClass('BuildingListCurrent');
        $(this).addClass('BuildingListCurrent');
        var bid = this.id.replace('l-','');
        if (bid>0)
        {
            Building.CurrentBuilding = bid;
            Building.Load();
        }
    });
    
    $('#switchView').click(function(){
        if ($('#thumbView').is(':visible'))
        {
            $('#thumbView').hide();
            $('#listView').show();
            $(this).html('View Gallery as Thumbnails');
        }
        else
        {
            $('#thumbView').show();
            $('#listView').hide();
            $(this).html('View Gallery as a List');
        }
    });
    
});

var Building = {
    CurrentBuilding: 0,
    CurrentTab: "description",
    Load: function(mode) {
        Building.CurrentTab = mode || "description";
        if (Building.CurrentBuilding>0)
        {
            switch (Building.CurrentTab)
            {
                case "description":
                    Common.LoadContent('#description', 'load.axd?name=buildingxml.ascx&buildingid='+Building.CurrentBuilding);
                    break;
            }
        }
    }
};

var Common = {
    ReplaceContent: function(container, content) {
        $(container).each(function(){ 
                $(this).empty().html(content).show(); 
            });
    },
    LoadContent: function(container, theUrl, onComplete) {  //load url content to the container
        $.ajax({
            url: theUrl,
            success: function(result) { 
                Common.ReplaceContent(container, result);
                if (onComplete && typeof onComplete == 'function') onComplete(result);
            }
        });
    },
    ShowLoading: function(container, paddingTop, paddingBottom) {
        paddingTop = paddingTop || 120;
        paddingBottom = paddingBottom || 0;
        var loading = '<div style="width:100%;text-align:center;padding-top:'+paddingTop+'px;padding-bottom:'+paddingBottom+'px"><img src="images/facebox/loading.gif" alt="" /></div>';
        $(container).each(function(){
            $(this).empty().html(loading).show(); 
        });
    }
};

//icon files are named icons/icon_xx.png
function getIcon (color) {
        var icon = new GIcon();
        icon.image = "img/tack.gif";
        //icon.shadow = "icons/icon_shadow.png";
        icon.iconSize = new GSize(26, 26);
        icon.iconAnchor = new GPoint(10, 20);
        icon.infoWindowAnchor = new GPoint(10, 10);
        icon.infoShadowAnchor = new GPoint(18, 25);
        return icon;
}

/////////////// googleMap class /////////////////

$.googleMap = {
	maps: {},
	marker: function(m, index) {
		if (!m) {
			return null;
		} else {
		    var icon = getIcon(m.color);
			var marker = new GMarker(new GLatLng(m.lat, m.lon), icon);
			if (m.building) {
			    //attach click handlers to buildings on page
		        $('#b-'+m.building).click(function() {
		            GEvent.trigger(marker, "click");
		            $('div.BuildingTable').removeClass('BuildingTableCurrent');
		            $(this).addClass('BuildingTableCurrent');
		            Building.CurrentBuilding = m.building;
		            Building.Load();
		        }).css("cursor","pointer");
			    
		        $('#l-'+m.building).click(function() {
		            GEvent.trigger(marker, "click");
		            Building.CurrentBuilding = m.building;
		            Building.Load();
		        });
		        
			    //attach click handlers to buildings on map
				GEvent.addListener(marker, "click", function() {
    				//call server side to get listing info tip
    				$.ajax({
    				    url: "helper.axd",
    				    data: {q:'buildingxml',buildingid:m.building},
    				    success: function(data) {
    				        marker.openInfoWindowHtml(data);
		                    $('div.BuildingTable').removeClass('BuildingTableCurrent');
        		            $('#b-'+m.building).addClass('BuildingTableCurrent');
		                    Building.CurrentBuilding = m.building;
		                    Building.Load();
    				    }
    				});
  				});
			}
			return marker;
		}
	},
	mapNum: 1
};

$.fn.googleMap = function(lat, lng, zoom, options) {

	// If we aren't supported, we're done
	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;

	// Default to New York
	if (lat == null) lat = 40.781061;
	if (lng == null) lng = -73.97438;
	if (!zoom) zoom = 12;

	// Sanitize options
	if (!options || typeof options != 'object')	options = {};
	options.mapOptions = options.mapOptions || {};
	options.markers = options.markers || [];
	options.controls = options.controls || {};
	options.mapType = options.mapType || G_NORMAL_MAP;

	// Map all our elements
	return this.each(function() {
		// Make sure we have a valid id
		if (!this.id) this.id = "gMap" + $.googleMap.mapNum++;
		// Create a map and a shortcut to it at the same time
		var map = $.googleMap.maps[this.id] = new GMap2(this, options.mapOptions);
		// Center and zoom the map
       	map.setCenter(new GLatLng(lat, lng), zoom);
       	map.setMapType(options.mapType);
       	// Add controls to our map
       	for (var i = 0; i < options.controls.length; i++) {
	       	var c = options.controls[i];
	       	eval("map.addControl(new " + c + "());");
       	}
       	// If we have markers, put them on the map
       	var marker = null;
       	var manager = new GMarkerManager(map);
       	var batch = [];
       	if (options.markers) {
       	    for (var i = 0; i < options.markers.length; i++) {
	       	    if (marker = $.googleMap.marker(options.markers[i].marker, i)) 
	       	    {
	       	        //batch.push(marker);
	       	        map.addOverlay(marker);
                }
       	    }
       	}
       	//manager.addMarkers(batch, zoom);
       	//manager.refresh();
    });
};




