$(document).ready(function(){
	
	//dont do this on the admin page
	if(!$("#adminpage").length)
	{
		//this is for the subscription stuff
		$("#subscription-0").click(function(){
			$("#multizipcode").empty();
			$("#outtermultizip").hide();
			$("#outterweb").hide();
			$("#outtercustomurl").hide();
		});
		
		$("#subscription-1").click(function(){
			
			var zip = $("#zipcode").val();
			
			
			$.getJSON("/signup/?zip=" + zip + "&distance=30", function(data){
				
				var options = new Array();
				var stop = '';
				
				$.each(data, function(){
					options.push('<option value="' + this.zip_id + '">' + this.zip  + '</option>');
				});
				
				options = options.join('');
				$("#multizipcode").html(options);
				
				$("#outtermultizip").show();
				
			});
			
			$("#outtermultizip").show();
			$("#outterweb").show();
			$("#outtercustomurl").hide();
			
		});
		
		$("#subscription-2").click(function(){
			
			var zip = $("#zipcode").val();
			
			
			$.getJSON("/signup/?zip=" + zip + "&distance=45", function(data){
				
				var options = new Array();
				var stop = '';
				
				$.each(data, function(){
					options.push('<option value="' + this.zip_id + '" selected="selected">' + this.zip  + '</option>');
				});
				
				options = options.join('');
				$("#multizipcode").html(options);
				
				$("#outtermultizip").show();
				
			});
			
			$("#outterweb").show();
			$("#outtercustomurl").show();
		});
	}
	
	if($("#map").length)
	{
		//load the map
		var map = new GMap2(document.getElementById("map"));
		map.setMapType(G_NORMAL_MAP);
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
		map.addControl(new GSmallZoomControl3D() );
		
		var box = new GLatLngBounds();
		
		if($("#noagents").length)
		{
			var geoencoder = new GClientGeocoder();
			geoencoder.getLatLng($("#noagents").text(), function(point){
				if (point)
				{
					map.setCenter(point, 13);
				}
			});
			
		}
		else
		{
			//loop the agents
			$(".map_agent").each(function(){
				var lat = $(".a_lat", this).text();
				var lon = $(".a_lon", this).text();
				
				var point = new GLatLng($(".a_lat", this).text(), $(".a_lon", this).text());
				var redIcon = new GIcon(G_DEFAULT_ICON);
				redIcon.iconSize = new GSize(32, 32);
				redIcon.shadowSize = new GSize(37, 34);
				redIcon.iconAnchor = new GPoint(9, 34);
				redIcon.infoWindowAnchor = new GPoint(9, 2);
				redIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
	
			
				var marker = new GMarker(point, { icon:redIcon });
				marker.agent_id = $(".a_id", this).text();
				
				GEvent.addListener(marker,"click", function() {
					
					if(!$("#checkbox_" + this.agent_id).is(":checked"))
					{
						$("#checkbox_" + this.agent_id).attr("checked", "checked");
						this.setImage("http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png");
					}
					else
					{
						$("#checkbox_" + this.agent_id).attr("checked", "");
						this.setImage("http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png");
					}
					
				});
				
				$(this).data("marker", marker);
				
				map.addOverlay(marker);
				box.extend(point);
			});
			
			if($(".map_agent").length <= 1)
                        {
                                map.setCenter(box.getCenter(), map.getBoundsZoomLevel(box) - 5);
                        }
                        else
                        {
                                map.setCenter(box.getCenter(), map.getBoundsZoomLevel(box) - 1);
                        }       
	
			//these are the function for the profile events
			$(".map_agent").mouseover(function(){
				var marker = $(this).data("marker");
				if(marker !== "undefined" && $(":checked", this).length == 0)
				{			
					marker.setImage("http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png");
				}
			});
			
			$(".map_agent").mouseout(function(){
				var marker = $(this).data("marker");
				if(marker !== "undefined" && $(":checked", this).length == 0)
				{			
					marker.setImage("http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png");
				}
			});
			
			$(".map_agent :checkbox").click(function(){
				var marker = $(this).parents(".map_agent").data("marker");
				
				if(marker !== "undefined")
				{
					if($(this).is(":checked"))
					{
						marker.setImage("http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png");
					}
					else
					{
						marker.setImage("http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png");
					}
				}
				
			});
		}
		
	}
	
	//directions map
	$(".direction_input").click(function(){
		$("#directionspopup").show();
		$("#blackout").show();
		
		var dmap = new GMap2(document.getElementById("directionmap"));
		dmap.setCenter(new GLatLng(42.351505,-71.094455), 15);
		directionsPanel = document.getElementById("directionroute");
		directions = new GDirections(dmap, directionsPanel);
		var fromaddress = $(".address_input").val();
		var toaddress =  $("#rdirections").text();
		var directionstr = "from: " + fromaddress + " to: " + toaddress;
		directions.load(directionstr);
		//3702 Eastside, Houston Texas
		//"from: 3702 Eastside St, Houston Texas to: 4433 Timber Hill Sugar Land, Texas 77479"
		//"from: 500 Memorial Drive, Cambridge, MA to: 4 Yawkey Way, Boston, MA 02215 (Fenway Park)"
		//directions.load("from: E Euclid Ave, Cleveland, Cuyahoga, Ohio 44112 to: 4433 Timber Hill Sugar Land, Texas 77479");

		
		return false;
	});
	
	$("#directionspopupclose").click(function(){
		$("#directionspopup").hide();
		$("#blackout").hide();
		
		return false;
	});
	
	if($("#popupmessage").length)
	{
		setTimeout ("$('#popupmessage').slideUp('slow')", 3000);
	}

	$(".question").click(function(){
		var text = $(this).attr("alt");
		var close = $("<a class='close' style='float: right; padding: 5px 5px 0px 0px'>X</a>");
		close.click(function(){
			$(this).parent().remove();
		});
		var textdiv = $("<div style='padding: 10px'>" + text + "</div>");
		var div = $("<div style='border: 1px solid grey; background: white none repeat scroll 0% 0%; position: absolute; width: 300px; margin-left: 40px;'></div>");
		div.prepend(textdiv);
		div.prepend(close);
		$(this).after(div);
	});
	
	$(".sub_list > li > input").click(function(){
		
		$(this).parents("ul").parent("li").children("input:eq(0)").attr("checked", "checked");
		
		var stop;
	});
	
});

