var map;

$(document).ready(function(){
	
	//Create our google map based on the id
	map = new GMap2(document.getElementById("map_canvas"));
	map.setUIToDefault();
	
	// MISSION 
	var icon_mission_base = new GIcon(G_DEFAULT_ICON);
	icon_mission_base.iconSize = new GSize(61, 36);
	icon_mission_base.shadow = "";
	icon_mission_base.image = "/images/utility/marker_mission.png";
	icon_mission_base.iconAnchor = new GPoint(30.5, 0);
	icon_mission_base.infoWindowAnchor = new GPoint(30.5, 0);
	icon_mission_base.imageMap = Number(0, 0, 61, 36);

	// EVENT
	var icon_event_base = new GIcon(G_DEFAULT_ICON);
	icon_event_base.iconSize = new GSize(49, 36);
	icon_event_base.shadow = "";
	icon_event_base.image = "/images/utility/marker_event.png";
	icon_event_base.iconAnchor = new GPoint(14.5, 14.5);
	icon_event_base.infoWindowAnchor = new GPoint(14.5, 0);
	icon_event_base.imageMap = Number(0, 0, 29, 29);
	
	// CHECKIN
	var icon_checkin_base = new GIcon(G_DEFAULT_ICON);
	icon_checkin_base.iconSize = new GSize(27, 27);
	icon_checkin_base.shadow = "";
	icon_checkin_base.image = "/images/utility/marker_checkin.png";
	icon_checkin_base.iconAnchor = new GPoint(14.5, 14.5);
	icon_checkin_base.infoWindowAnchor = new GPoint(14.5, 0);
	icon_checkin_base.imageMap = Number(0, 0, 29, 29);

	//bounds
	//var bounds = new GLatLngBounds;
	
	// default item to display
	var defaultData = new Object(); 
	var markerImage = "";

	var cityLong,cityLat,zoomLvl;
	var cityLongLatZoom = new Array();
	var cityID = $("#city-id").html();
	cityLongLatZoom[4] = new Array(33.694638,-84.484863,9); // atlanta
	cityLongLatZoom[5] = new Array(42.371735,-71.148834,10); // boston
	cityLongLatZoom[6] = new Array(41.928847,-87.805481,9); // chicago
	cityLongLatZoom[7] = new Array(32.813247,-96.785431,11); // dallas
	cityLongLatZoom[8] = new Array(42.43562,-83.122559,9); // detroit
	cityLongLatZoom[9] = new Array(34.1209,-118.234863,8); // los angeles
	cityLongLatZoom[10] = new Array(25.855516,-80.212555,10); // miami
	cityLongLatZoom[13] = new Array(33.446339,-112.013855,10); // phoenix
	cityLongLatZoom[14] = new Array(32.709889,-117.182922,11); // san diego
	cityLongLatZoom[15] = new Array(37.72728,-122.30255,9); // sf
	cityLongLatZoom[16] = new Array(47.609635,-122.3283,12); // seattle
	cityLongLatZoom[20] = new Array(29.732185,-95.42038,11); // houston
	cityLongLatZoom[38] = new Array(39.764214,-104.996338,9); // denver
	cityLongLatZoom[39] = new Array(40.718119,-74.11377,9); // nyc
	cityLongLatZoom[40] = new Array(28.585125,-81.350327,11); // orlando
	cityLongLatZoom[41] = new Array(39.964491,-75.157471,11); // philly
	
	function display_info(itemData){
		// display nothing...
		$('#panel_info .location').hide();

		if(itemData.sourceType == "checkin"){
			markerImage = "marker_" + itemData.sourceType + "_drop.png";
		}else{
			markerImage = "marker_" + itemData.sourceType + ".png";
		}
	
		$('#panel_info .location').empty().html('<img src="/images/utility/'+ markerImage +'" alt="Checkin" /><span></span>');

		if(itemData.sourceType == "checkin"){
			$('#panel_info .location span').html('<strong>' + itemData.name + '</strong>');
		}else{
			$('#panel_info .location span').html('<strong><a href="' + itemData.detail + '">' + itemData.name + '</a></strong>');
		}
		
		if(itemData.address1)$('#panel_info .location span').append('<br>' + itemData.address1);	
		if(itemData.city) $('#panel_info .location span').append('<br />'+ itemData.city);
		if(itemData.state) $('#panel_info .location span').append(', '+ itemData.state);
		if(itemData.zip) $('#panel_info .location span').append(' '+ itemData.zip);	
		
		$('#panel_info .agent div#icon').html('<img src="' + itemData.team_photo + '" width="27" />');
		
		$('#panel_info .agent span').html('From: <strong><a href="' + itemData.team_profile + '">' + itemData.team_name + '</a></strong>');

		if (itemData.sourceType == "checkin"){
			$('#panel_info .agent span').prepend('By: <strong><a href="' + itemData.team_profile + '">' + itemData.agent_name + '</a></strong><br/>');
		}
		
		if (itemData.sourceType == "checkin"){
			if($('#panel_info .business').length > 0) $('#panel_info .business').remove();
		
			if(itemData.shout)
			{	
				//If the shout out element is already in the HTML, else we want to add it
				if($('#panel_info .shout_out').length > 0)
				{
					$('#panel_info .shout_out').html("SHOUT: " + itemData.shout);
				}
				else
				{
					$('#panel_info .agent').after('<dd class="shout_out">SHOUT: ' + itemData.shout + '</dd>');
				}
				
				$('#panel_info .shout_out').show();
				
			}
			else
			{
				$('#panel_info .shout_out').hide();
			}
			
			$('#panel_info .business').hide();
		}else{
			$('#panel_info .shout_out').hide();
		}

		if(!itemData.sourceType){
			$('#panel_info .location').empty().append("Stay tuned for Agent activity!");					
		}

			$('#panel_info .location').show("fast");		

	}


	function display_yelp(item, yelpData){
		var business = yelpData.businesses[0];
		//Update info panel
		display_info(item);
		// $('#panel_info .location span').empty().html('<strong>' + item.name + '</strong><br/>' + item.address1 + '<br />'+ item.city + ', ' + item.state + ' ' + item.zip);
		// 	$('#panel_info .agent div#icon').html('<img src="' + item.team_photo + '" width="27" />');
		// 	$('#panel_info .agent span').html('By: <strong><a href="' + item.team_profile + '">' + item.team_name + '</a></strong>');
		
		//If the business element already exists, else we want to add it
		if($('#panel_info .business').length > 0)
		{
			$('#panel_info .business span').show();
			//$('#panel_info .business span').html('<strong>NEARBY:<br /><a href="' + business.url + '" target="_blank">' + business.name + '</a></strong><br/>' + business.categories[0].name + '<br/>' + business.address1 + '<br/><img src="' + business.rating_img_url_small + '" alt="rating"/><br/><a href="http://www.yelp.com" target="_blank" class="poweredByYelp">powered by Yelp!</a>');
		}
		else
		{
			$('#panel_info dl').append(''+
				'<dt class="clearfix business">NEARBY:<br />'+
					'<img src="/images/utility/icon_yelp.gif" alt="Yelp"/>'+
					'<span><strong><a href="' + business.url + '" target="_blank">' + business.name + '</a></strong><br/>' + business.categories[0].name + '<br/>' + business.address1 + '<br/><img src="' + business.rating_img_url_small + '" alt="rating"/><br/><a href="http://www.yelp.com" target="_blank" class="poweredByYelp">powered by Yelp!</a></span>'+
				'</dt>');							
		}
	}

	function get_yelp(item){
		// checks the following localflavor+musicvenues+galleries+bookstores+coffee if not results, don't display
		$.ajax({
			url: "/scripts/proxy.php",
			type: "GET",
			data: ({
				url: "http://api.yelp.com/business_review_search",
				ywsid: "d0ogZdLbBIxdhB_a-f_12Q",
				lat: item.latitude,
				long: item.longitude,
				radius: 5,
				limit: 1,
				category: "localflavor+musicvenues+galleries+bookstores+coffee",
				term : item.name
			}),
			dataType: "json",
			success: function(yelpData){
				var business = yelpData.businesses[0];
				if(business){
					display_yelp(item, yelpData);
				}
			}					
		});
	}
	$.getJSON("http://www.fiestamovement2.com/json/utility/5", function(data){
	
		//Checkins
		$.each(data.checkins, function(i, item){
			var point = new GLatLng(item.latitude, item.longitude);
			var markerOptions = { icon : icon_checkin_base };
			var marker = new GMarker(point, markerOptions);

			// default: place 1st Checkin into Info pane
			if (i == 0){
				defaultData = item;
				defaultData.sourceType = "checkin";
			}
			
			GEvent.addListener(marker, "click", function() {
			    marker.openInfoWindowHtml("Checkin<br /><b>" + item.name + "</b>");
				//Update info panel
				item.sourceType = "checkin";
				display_info(item);		
			});
			map.addOverlay(marker);
		});
		
		
		//Missions
		$.each(data.missions, function(i, item){
			var point = new GLatLng(item.latitude, item.longitude);
			var markerOptions = { icon : icon_mission_base };
			var marker = new GMarker(point, markerOptions);

			// if we don't have a checkin to use, default becomes first mission
			if(!defaultData.sourceType == "checkin"){
				if (i == 0){ 
					defaultData = item;
					defaultData.sourceType= "mission";
				}
			}

			GEvent.addListener(marker, "click", function() {
			    marker.openInfoWindowHtml("Mission<br /><b>" + item.name + "</b>");
				//Call the Yelp API via a proxy script
				item.sourceType = "mission";		
				get_yelp(item);	
			});
			map.addOverlay(marker);
		});
		
		
		//Events
		$.each(data.events, function(i, item){
			var point = new GLatLng(item.latitude, item.longitude);
			var markerOptions = { icon : icon_event_base };
			var marker = new GMarker(point, markerOptions);
			
			// if we don't have a mission to use, default becomes first event
			if(!defaultData.sourceType == "checkin"){
				if (i == 0){ 
					defaultData = item;
					defaultData.sourceType= "event";
				}
			}
			GEvent.addListener(marker, "click", function() {
			    marker.openInfoWindowHtml("Event<br /><b>" + item.name + "</b>");
				item.sourceType = "event";
				get_yelp(item);							
			});
			map.addOverlay(marker);
		});


		if(cityLongLatZoom[cityID]){
			cityLat = cityLongLatZoom[cityID][0]; //lat
			cityLong = cityLongLatZoom[cityID][1]; //long
			zoomLvl = cityLongLatZoom[cityID][2]; // zoom
			map.setCenter(new google.maps.LatLng(cityLat,cityLong), zoomLvl);
		}else{
			// entire US
			map.setCenter(new google.maps.LatLng(27.683528,-84.199219), 3);
		}
		
//		set_map_center($("#city-id").html());
		display_info(defaultData);
		//map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
		//map.zoomOut();	
	});
});