﻿
Behaviors.register("Locations", "#locations", {
	current: {}
	
	,init: function()
	{
		this.map = $("#map").behavior("Map");
		var Locations = this;
		var locList = $("#tab-Locations li");
		var columns = 3; //static #
		maxHeight = Math.ceil(locList.length / columns); //how long each list can be, or how long it would be if enough were added to it for equal columns
		for (a = 0; a <= columns-1; a++){
			newUL = document.createElement("ul"); //create a columnar UL
			$(newUL).attr("id","locCol" + a); //ID it uniquely
			$("#tab-Locations").append(newUL); //append it to the containing div, after the existing UL
			floor = a * maxHeight //figure out which LI in the existing UL will be first
			ceiling = floor + maxHeight - 1; //figure out which LI will be last
			for (b = floor; b <= ceiling; b++){
				if (locList[b]) newLI = $(locList[b]).clone().appendTo(newUL);
			}
		}
		$("#tab-Locations ul:first").hide();
		this.find('#offices .offices ul li a').bind('click', function() {
			Locations.updateMapNodes( $(this).parent().parents('li') );
			$(window).scrollTop(200);
			return(false);
		});
		
		this.map.addEvent('initialized', function() {
			Locations.updateMapNodes( $('#offices .offices div li') );
		})
	}
	
	,updateMapNodes: function($collection)
	{
		var addresses = [];

		$collection.each( function() {
			var addressNode = {
				el: $('<div></div>').addClass('office-map-html').append($(this).html()).get(0),
				location: '',
				title: $(this).find('ul:first li:first').html()
			};
			$(this).find('ul:first li:gt(0)').each( function() {
				addressNode.location += $(this).text() + ' ';
			} );
			addresses.push(addressNode);
		} );

		this.map.loadAddresses(addresses, true, null);
	}
});
