var page = "";
if (window.location.pathname.indexOf("/search/results") != -1) {
	page = "results";
}
else {
	page = "search";
};

$(document).ready(function() {
	if (page == "search") {
		var map_instructions = $("#map_instructions").html();
		$('a.expand').click(function() {
			if (! $(this).hasClass('closer')) {
		    	$(this).text('Click to Close');
				$(this).addClass('closer');
			}
			else {
				$(this).text('Click here for more options');
				$(this).removeClass('closer');
			};
			$(this).parent().find('.expand_it').toggle('medium');
	    	return false;
	    });
		$(".map-expand a.open-close").click(function() {
			if ($(this).hasClass("closer")) {
				map.removeControl(controlLarge);
				$("#map_instructions_top").hide("medium");
				$(this).html("Click Here").addClass("opener").removeClass("closer");
				GUnload();
				$("#map").animate({'width': '220px', 'height': '157px'}, 500);
				$("#neighborhoodSelect").val("").attr("disabled",false);
				$("div.left p:first").show("1000");
				reset_coords();
				timer1 = setTimeout(function() {
					$("#map_instructions_top").remove();
					$("#map").hide();
					$("#map").remove();
					$("#map-mini").show();
					$("#map-mini").after("<div id='map'></div>");
					$("#map").css({'width':'138px','height':'100px','display':'none','background-color':'#e4e4e4'});}, 550);
			}
			else {$("#map-mini").click();};
			return false;
		});
		$("#map-mini").click(function() {
			$(this).hide();
			$("div.create-neighborhood").prepend("<p id='map_instructions_top' style='display:none;'>" + map_instructions + "</p>");
			$("#map_instructions_top").css({'width': '600px', 'padding-bottom': '10px'}).show("medium");
			$("#map").show().animate({'height':'434px','width':'600px'}, 500);
			$("#neighborhoodSelect").val("").attr("disabled",true);
			$("div.left p:first").hide("1000");
			$(".map-expand a.open-close").html("[x] Close Map").addClass("closer").removeClass("opener");
			timer = setTimeout('load()', 600);
		});
		$("#map_activate").click(function() {
			$("#map-mini").click();
			return false;
		});
		if (window.location.hash == "#expand") {$("#map-mini").click();};
		var search_terms_default = "Enter restaurant name or search terms";
		$("#search_form_main input[name=keywords]").val(search_terms_default);
		clearAndDefault($("#search_form_main input[name=keywords]"), search_terms_default);
	};
	if (page == "results") {
		load();
		$('ul.refineries li .options input').click(function() {
			if( $(this).attr('checked') ) {$(this).parent().addClass('selected');} else {$(this).parent().removeClass('selected');}
		});
		$("a.expand").click(function() {
			return false;
		});
		$.preloadImages("/site_images/refine_expand_hover.jpg", "/site_images/refine_collapse.jpg", "/site_images/refine_expand.jpg");
		$("a.click-to-expand").each(function(index) {
			$(this).toggle(function() {
				$(this).html('Click to hide');
				$(this).css('background-image', 'url(/site_images/refine_collapse.jpg)');
				if ($.browser.msie && parseInt($.browser.version) == "6") {$(this).parent().find(".expanding").show();}
				else {$(this).parent().find(".expanding").toggle('medium');};				
				return false;
			}, function() {
				$(this).html('Click Here to Expand');
				$(this).css('background-image', 'url(/site_images/refine_expand.jpg)');
				if ($.browser.msie && parseInt($.browser.version) == "6") {$(this).parent().find(".expanding").hide();}
				else {$(this).parent().find(".expanding").toggle('medium');};				
				return false;
			});
			
		});
		if ($('#top-right-latitude').val() != "") {
			$("#cat_group_6").parent('li').find("div.options input").click(function() {reset_coords();});
		};
	};
	Rectangle.prototype = new GOverlay();
	Rectangle.prototype.initialize = function(map) 
	{		
		var div = document.createElement( "div" );
		div.style.border = this.weight_ + "px solid " + this.color_;
		div.style.position = "absolute";
		map.getPane(G_MAP_MAP_PANE).appendChild( div );
		this.map_ = map;
		this.div_ = div;
	}
	Rectangle.prototype.remove = function()
	{this.div_.parentNode.removeChild(this.div_);}
	Rectangle.prototype.copy = function() 
	{
		return new Rectangle( this.bounds_ , this.weight_ , this.color_ , this.backgroundColor_ , this.opacity_ );
	}
	Rectangle.prototype.redraw = function(force)
	{
		if (! force) return;
		var c1 = this.map_.fromLatLngToDivPixel( this.bounds_.getSouthWest() );
		var c2 = this.map_.fromLatLngToDivPixel( this.bounds_.getNorthEast() );
		this.div_.style.width = Math.abs(c2.x - c1.x) + "px";
		this.div_.style.height = Math.abs(c2.y - c1.y) + "px";
		this.div_.style.left = (Math.min(c2.x, c1.x) - this.weight_) + "px";
		this.div_.style.top = (Math.min(c2.y, c1.y) - this.weight_) + "px";
	}
});


$(window).unload(function () {GUnload();} );

function Rectangle(bounds, opt_weight, opt_color)
{
	this.bounds_ = bounds;
	this.weight_ = opt_weight || 5;
	this.color_ = opt_color || "#006699";
	this.backgroundColor = opt_color || "#888888";
}

function clickMap(marker, point) // step #1
{		
	if (clickOne == null)
	{
		clickOne = point;
		var rectangle = new GLatLngBounds(clickOne , clickOne);
		if (clickBox != null)
		{map.removeOverlay(clickBox);}
		clickBox = new Rectangle(rectangle);
		map.addOverlay(clickBox);
	}
	else
	{
		coordinateOne = clickOne;
		clickOne = null;
		zoomToBox(clickOne, point, coordinateOne);
	}
}

function mouseMove(point)
{		
	if ( clickOne != null )
	{
		clickBox.bounds_ = new GLatLngBounds( clickOne , point );
		clickBox.redraw(true);
	}
}

function zoomToBox(pointOne, pointTwo, coordinateOne)
{
	map.enableDragging();
	var bounds = map.getBounds();
	var coordinatesSplit = new Array();
	var coordinatesASplit = new Array();
	var coordinatesBSplit = new Array();
	var coordinatesBlongtitudeArray = new Array();
	coordinates = coordinateOne + ", " + pointTwo;
	coordinatesSplit = coordinates.split('), (');
	coordinatesA = coordinatesSplit[0];
	coordinatesB = coordinatesSplit[1];
	coordinatesASplit = coordinatesA.split(', ');
	coordinatesAlatitude = coordinatesASplit[0].slice(1);
	coordinatesAlongtitude = coordinatesASplit[1];
	coordinatesBSplit = coordinatesB.split(', ');
	coordinatesBlatitude = coordinatesBSplit[0];
	coordinatesBlongtitude = coordinatesBSplit[1];
	coordinatesBlongtitudeArray = coordinatesBlongtitude.split(')');
	coordinatesBlongtitude = coordinatesBlongtitudeArray[0];
	coordinatesAlatitude = parseFloat(coordinatesAlatitude);
	coordinatesAlongitude = parseFloat(coordinatesAlongtitude);
	coordinatesBlatitude = parseFloat(coordinatesBlatitude);
	coordinatesBlongtitude = parseFloat(coordinatesBlongtitude);
	if (coordinatesBlatitude > coordinatesAlatitude)
	{
		// alert(coordinatesBlatitude + ' is apparently bigger than ' + coordinatesAlatitude);
		temp = coordinatesAlatitude;
		coordinatesAlatitude = coordinatesBlatitude;
		coordinatesBlatitude = temp;
	}
	if (coordinatesBlongtitude > coordinatesAlongtitude)
	{	
		// alert(coordinatesBlongtitude + ' is apparently bigger than ' + coordinatesAlongtitude);
		temp = coordinatesAlongtitude;
		coordinatesAlongtitude = coordinatesBlongtitude;
		coordinatesBlongtitude = temp;

	}
	document.getElementById("top-right-latitude").value = coordinatesAlatitude;
	document.getElementById("top-right-longtitude").value = coordinatesAlongtitude;
	document.getElementById("bottom-left-latitude").value = coordinatesBlatitude;
	document.getElementById("bottom-left-longtitude").value = coordinatesBlongtitude;

}

function reset_coords () {
	$("#top-right-latitude").val("");
	$("#top-right-longtitude").val("");
	$("#bottom-left-latitude").val("");
	$("#bottom-left-longtitude").val("");
}