	
(function(){

	var _googleMap = null;
	var _googleMapDefaultZoomLevel = 14;
	
	var _filterDiff = new Array();

	function Exists(e)
	{
		return (((e) !== undefined) && ((e) !== null));
	}
	
		var GPointWrapper = function(x, y)
	{
		try
		{
			return new GPoint(x, y);
		}
		catch (ex)
		{
			return {X:x, Y:y};
		}
	};
	
	var GSizeWrapper = function(w, h)
	{
		try
		{
			return new GSize(w, h);
		}
		catch (ex)
		{
			return {Width:w, Height:h};
		}
	};
	
	/*
	 *
	 *	Mapping
	 *
	 */
	
	function _initMap() {
		_googleMap = new GMap2(document.getElementById("GoogleMapArea"));
		_googleMap.addControl(new GSmallZoomControl());
		
		if (locationArray.length > 0)
		{
			var initLL = new GLatLng(locationArray[0][0], locationArray[0][1]);
			_googleMap.setCenter(initLL, _googleMapDefaultZoomLevel);
		}
		
		for (var index in locationArray)
		{
			var indexInt = parseInt(index)+1;
			var indexString = "0" + indexInt;
			if (index > 8)
			{
				indexString = indexInt;
			}
		
		
			var gll = new GLatLng(locationArray[index][0], locationArray[index][1]);
			var markerIcon = _createMapMarker(_applicationRootUrl + "i/map/pin-"+indexString+".png",
									  _applicationRootUrl + "i/map/pin-shadow.png",
									  _applicationRootUrl + "i/map/pin-"+indexString+".png",
									  {Width: 18, Height: 42}, {Width: 37, Height: 42},
									  {X:10, Y:0}, {X:17, Y:3});
									  
		
			var siteMarker = new GMarker(gll, {icon:markerIcon});
			siteMarker.IsVisible = true;
			_googleMap.addOverlay(siteMarker);
		}
	} 
	

	
	var _createMapMarker = function(imageUrl, shadowUrl,
		transparentImageUrl, iconSize, shadowSize, iconAnchor,
		infoWindowAnchor)
	{
		var icon;
		try
		{
			icon = new GIcon();
		}
		catch (ex)
		{
			icon = {};
		}
		if (Exists(imageUrl))
		{
			icon.image = imageUrl;
		}
		if (Exists(shadowUrl))
		{
			icon.shadow = shadowUrl;
		}
		if (Exists(transparentImageUrl))
		{
			icon.transparent =  transparentImageUrl;
		}
		if (Exists(iconSize))
		{
			icon.iconSize = GSizeWrapper(iconSize.Width, iconSize.Height);
		}
		if (Exists(shadowSize))
		{
			icon.shadowSize = GSizeWrapper(shadowSize.Width, shadowSize.Height);
		}
		if (Exists(iconAnchor))
		{
			icon.iconAnchor = GPointWrapper(iconAnchor.X, iconAnchor.Y);
		}
		if (Exists(infoWindowAnchor))
		{
			icon.infoWindowAnchor = GPointWrapper(infoWindowAnchor.X, infoWindowAnchor.Y);
		}
		
		return icon;
	};
	
	function setGooglemapFloat()
	{
		$('#GoogleMapArea').stickyfloat($('#GoogleMapArea').position().top, $('#content').height(), 
										20, 400);
	}
	
	
	function clearFilters()
	{
		$("#FilterDropdown input[type='checkbox']").attr('checked', false);
	}
	
	function setInitialState()
	{
		var o = parseUri.options;
		var strictMode = $('strictMode').checked;
			
		o.strictMode = strictMode;
		var items = parseUri(window.location);
		var query = items[o.q.name];
			
		if (Exists(query['what']))
		{
			$('#SearchFormWhat').val(unescape(query['what']));
		}
		
		if (Exists(query['where']))
		{
			$('#SearchFormWhere').val(unescape(query['where']));
		}
		
		if (Exists(query['price']))
		{
			var prices = query['price'].split(',');
			for (var p in prices)
			{
				$('#FilterDropdownPriceCheck'+prices[p]).attr('checked', true);
			}
		}
		
		if (Exists(query['quality']))
		{
			var qualities = query['quality'].split(',');
			for (var q in qualities)
			{
				$('#FilterDropdownStarsCheck'+qualities[q]).attr('checked', true);
			}
		}
	
		if (Exists(query['location']))
		{			
			$('#FilterLocationDropdownLocation').val(query['location']);
		}
	}
	
	
	$(document).ready(function() {
		_initMap();
		setGooglemapFloat();
		setInitialState();
										
		$('#FilterSearchLink').click(function() {
			if ($('#FilterDropdown').is(':visible'))
			{
				$('#FilterDropdown').hide();
			}
			else
			{
				$('#FilterDropdown').show();
			}
		});
									
		$('#FilterDropdownCancel').click(
			function() 
			{
				while(_filterDiff.length > 0)
				{
					var	boxId = _filterDiff.pop();
					$('#'+boxId).attr('checked', !$('#'+boxId).attr('checked'));
				
				}
				$('#FilterDropdown').hide();
			}
		);
		
		$('#FilterDropdownClear').click(function() { clearFilters(); } );	
		
		$("#FilterDropdown input[type='checkbox']").click(
			function() 
			{
				elementId = $(this).context.id;
				if ($.inArray(elementId, _filterDiff) < 0)
				{
					_filterDiff.push(elementId);
				}
				else
				{
					_filterDiff = $.grep(_filterDiff, 
										 function (val)
										 {
											return val != elementId;
										 });
				}
			
			}
		); 		
											
		$('#FilterLocationLink').click(function() {
			if ($('#FilterLocationDropdown').is(':visible'))
			{
				$('#FilterLocationDropdown').hide();
			}
			else
			{
				$('#FilterLocationDropdown').show();
			}
		});
									
		$('#FilterLocationCancel').click(
			function() 
			{
				$('#FilterLocationDropdown').hide();
			}
		);	
									
		$(".FilterLocationDropdownItem").click(function(evt) {
			var loc = $(this).text();
			$('#FilterLocationLinkText').html(loc + " ");
			$('#FilterLocationDropdownLocation').val(loc);
			$('#FilterLocationDropdown').hide();
		});
		
		$(".ResultListItem").mouseover(function(evt) {
			var i = 0;
			for (i = 0; i < evt.currentTarget.attributes.length; ++i)
			{
				if (evt.currentTarget.attributes[i].nodeName == "idx")
				{
					var loc = locationArray[evt.currentTarget.attributes[i].nodeValue];
					_googleMap.setCenter(new GLatLng(loc[0], loc[1]));
					return;
				}
			}
		});
	});
})();
