/*
	jquery extensions
*/
(function($)
{	
	$.extend({
		locator:{
			/*
			--------------------------------
			VARIABLES
			--------------------------------			
			*/
			phase:2,
			markers:[],
			locations:[],
			form:"",
			record:"",
			
			/*
			Google Maps reference object
			*/
			MAP:{
				view:{},
				geocoder:new google.maps.Geocoder(), // initiate and assign Geocoder
				details:new google.maps.InfoWindow(), // initiate and assign InfoWindow
				infoBox:new InfoBox(
					{
						content:"<div></div>",
						disableAutoPan: false,
						maxWidth: 0,
						pixelOffset: new google.maps.Size(-3, -113),
						zIndex: null,
						closeBoxMargin: "8px 10px",
						closeBoxURL: "/wp-content/themes/twentyten/assets/images/close.png",
						infoBoxClearance: new google.maps.Size(1, 1),
						isHidden: false,
						pane: "floatPane",
						enableEventPropagation: false				
					}				
				),
				config:{
					zoom:13, // zoom level
					mapTypeId:google.maps.MapTypeId.ROADMAP, // map type (ROADMAP, SATELITE, HYBRID)
					mapTypeControl:false, // User interface boolean,
					keyboardShortcuts:false
				},
				markers:{
					home:{
						icon:"/wp-content/themes/twentyten/assets/images/home.png",
						shadow:"/wp-content/themes/twentyten/assets/images/shadow.png"
					},
					branch:{
						icon:"/wp-content/themes/twentyten/assets/images/store.png",
						shadow:"/wp-content/themes/twentyten/assets/images/shadow.png"
					}					
				},
				default_address:"Toronto, Ontario, Canada",
				bounds:{}
			},
			
			/*
			dom containers reference by selector
			*/
			containers:{
				map:"modal-map",
				buttons:{
					geolocation:"#geolocate"
				},
				form:{
					button:"#search",
					field:"#address"
				}
			},			
			
			/*
			--------------------------------			
			METHODS
			--------------------------------
			init
			geolocation
			--------------------------------			
			*/
			init:function(query)
			{
				this.MAP.default_address = query;
				this.MAP.view = new google.maps.Map(document.getElementById(this.containers.map), this.MAP.config);
				this.MAP.bounds = new google.maps.LatLngBounds();
				this.search.latlng(
					{
						address:this.MAP.default_address + ", Ontario", 
						center:true,					
						callback:this.results.center						
					}
				);
				this.search.stores();
			},
			
			/*
				geolocation
			*/			
			geolocation:{				
				/*
				--------------------------------
				VARIABLES
				--------------------------------			
				*/
				available:false,
				
				/*
				--------------------------------
				METHODS
				--------------------------------
				detect - browser geolocation feature detection
				locate - 
				success - 
				error -		
				--------------------------------			
				*/
				detect:function()
				{
					$($.locator.containers.buttons.geolocation).hide();
					this.available = navigator.geolocation ? true : false;
					if(this.available){$($.locator.containers.buttons.geolocation).show();}
				},

				locate:function()
				{
					navigator.geolocation.getCurrentPosition($.locator.geolocation.success, $.locator.geolocation.error);
				},
				
				success:function(position)
				{
					$.locator.marker.remove();			
					var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
					$.locator.marker.add({center:true, latlng:latlng, type:"home"});
					$.locator.search.stores(latlng);
				},
				
				error:function(msg)
				{					

				}
			},
			
			/*
				marker
			*/
			marker:{
				/*
				--------------------------------
				VARIABLES
				--------------------------------			
				*/

				
				/*
				--------------------------------
				METHODS
				--------------------------------

				--------------------------------			
				*/
				add:function(options)
				{
					if(options.center){$.locator.results.center(options);}
					var style = !options.type ? $.locator.MAP.markers.home : $.locator.MAP.markers[options.type];
					var title = options.STREET_NO + " " + options.STREET_NAME + " - ";
					var dim = {
						icon:{w:32,h:37},
						shadow:{w:50,h:37}
					}	
					
					// phase 2 volunteer count into marker 				
					if($.locator.phase == 2 && options.type)
					{
						//options.VOLUNTEERS = Math.floor(Math.random()*999); // testing purposes only
						title += options.VOLUNTEERS == 0 ? "Volunteers Needed" : options.VOLUNTEERS + " Volunteers";
						style.icon = "/wp-content/themes/twentyten/assets/services/marker.service.php?VOLUNTEERS=" + options.VOLUNTEERS
						if(options.VOLUNTEERS != 0)
						{
							dim.icon.w = 47;
							dim.icon.h = 51;
						}
						/*
						switch(true)
						{
							case 0:
							case (options.VOLUNTEERS < 10):
								dim.icon.w = 32;
							break;
							case (options.VOLUNTEERS >= 10 && options.VOLUNTEERS < 100):
								dim.icon.w = 42;							
							break;
							case (options.VOLUNTEERS >= 100 && options.VOLUNTEERS < 1000):
								dim.icon.w = 53;							
							break;
							case (options.VOLUNTEERS >= 1000):
								dim.icon.w = 64;							
							break;														
						}
						*/
					}
					var icon = new google.maps.MarkerImage(style.icon,new google.maps.Size(dim.icon.w, dim.icon.h),new google.maps.Point(0,0),new google.maps.Point(0, 32));
					var shadow = new google.maps.MarkerImage(style.shadow,new google.maps.Size(dim.shadow.w, dim.shadow.h),new google.maps.Point(0,0),new google.maps.Point(0, 32));
					
					var marker = new google.maps.Marker({
						map: $.locator.MAP.view, 
						position: options.latlng,
						icon:icon,
						shadow:shadow,
						details:options.marker,
						title:title
					});

					google.maps.event.addListener(marker, 'click', function(){$.locator.marker.details(this);});
					$.locator.markers.push(marker);
				},
				
				remove:function()
				{
				    for(var i in $.locator.markers){$.locator.markers[i].setMap(null);}
				    $.locator.markers = Array();
					$.locator.locations = Array();
				},
				
				details:function(marker)
				{
					
					if(marker.details)
					{
						var content;
						switch($.locator.phase)
						{
							case 2:
								// 250 : 335
								/* ruiyang dH = 335, pY = 336*/
								/* rburgess dH = 373, dY = -243, pY = 374 */
								var dH = 405;
								var dY = -410;
								var pY = 406;
								if($.locator.record == "false")
								{
									/* ruiyang dH = 335, pY = 336*/
									/* rburgess dH = 373, dy = -328, pY = 374 */
									dH = 405;
									dY = -410;
									pY = 406;
								}
								content = "<div class=\"marker signup\" style=\"height:" + dH + "px;\"><h3>THE <em></em>EER STORE</h3>" + marker.details.details +  $.locator.form + "<div class=\"pointer\" style=\"top:" + pY + "px;\"></div></div>";
								$.locator.MAP.infoBox.setOptions({pixelOffset:new google.maps.Size(-3, dY)});
							break;
							default:
								content = "<div class=\"marker\"><h3>THE <em></em>EER STORE</h3>" + marker.details.details + "<div class=\"pointer\" style=\"top:111px;\"></div></div>";
							break;
						}

						$.locator.MAP.infoBox.content_ = content;
						$.locator.MAP.infoBox.open($.locator.MAP.view, marker);	
						/*
							fix for form field input focus
						*/
						var loading = setInterval(function(){
							if($(".infoBox"))
							{
								if($(".infoBox").parent().css("z-index"))
								{
									$(".infoBox").parent().css({"z-index":9999})									
									$("input").placeholder();
									clearInterval(loading);									
								}
								
							}
							
						},1);
						
						var storeInterval = setInterval(function(){
							if($("#STORE").length > 0)
							{
								$("#STORE").val(marker.details.STREET_NO + " " + marker.details.STREET_NAME + ", " + marker.details.CITY + " " + marker.details.POSTAL_CODE);								
								$("#STORE_ID").val(marker.details.LOCATION_NO);
								clearInterval(storeInterval);
							}
						},1);
						
					}
				}				
			},			
			
			/*
				search
			*/
			search:{
				/*
				--------------------------------
				VARIABLES
				--------------------------------			
				*/

				
				/*
				--------------------------------
				METHODS
				--------------------------------

				--------------------------------			
				*/
				process:function(address)
				{
					if(address)
					{
						$.locator.MAP.default_address = address;
						$.locator.marker.remove();
						this.latlng(
							{
								address:address, 
								center:true,					
								callback:$.locator.results.mark
							}
						);
					}
				},
				
				latlng:function(options)
				{
					if($.locator.MAP.geocoder){$.locator.MAP.geocoder.geocode(
						{'address': options.address}, 
						function(results, status)
						{
							if (status == google.maps.GeocoderStatus.OK)
							{
								options.latlng = results[0].geometry.location;
								$.locator.MAP.latlng = options.latlng;
								// execute callback and pass location from geocoder
								if(options.callback){(function(callback, options){callback(options);})(options.callback, options);}
								
								$.locator.MAP.bounds.extend(options.latlng);
								
								$.locator.marker.add(options);
							}
						});return false;}					
				},
				stores:function()
				{
					var wait = setInterval(function(){
						if($.locator.MAP.latlng)
						{
							clearInterval(wait);
							
														
							var post = {
								lat:!$.locator.MAP.latlng.Aa ? $.locator.MAP.latlng.Ca : $.locator.MAP.latlng.Aa,
								lng:!$.locator.MAP.latlng.Aa ? $.locator.MAP.latlng.Ea : $.locator.MAP.latlng.Ca
							}
							
							$.ajax({
								url:"/wp-content/themes/twentyten/assets/modals/results.service.php",
								//url:"modals/results.service.php",
								cache:false,
								data:post,
								dataType:"JSON",
								type:"POST",
								success:function(json){
									$.locator.search.results(json.locations);
									$.locator.form = json.form;
									$.locator.record = json.record;
								},
								error:function(){}
							});							
						}
					},1)
				},
				
				results:function(marker)
				{
					for(var i in marker)
					{
						marker[i].marker = marker[i];
						marker[i].type = "branch";
						marker[i].index = i;
						marker[i].latlng = new google.maps.LatLng(marker[i].LATITUDE, marker[i].LONGITUDE);				
						$.locator.marker.add(marker[i]);
					}
				}
			},
			
			/*
				results
			*/
			results:{
				/*
				--------------------------------
				VARIABLES
				--------------------------------			
				*/

				
				/*
				--------------------------------
				METHODS
				--------------------------------
				*/
				
				/*
					
				*/
				mark:function(options)
				{
					$.locator.marker.add({center:true, latlng:options.latlng, type:"home"});
				},
				

				/*
				center the map on the provide latitude and longitude
				*/
				center:function(options){if(options.latlng){$.locator.MAP.latlng = options.latlng;};if(options.center){$.locator.MAP.view.setCenter(options.latlng);}}
			},
			
			functionality:function()
			{
				/*
					bind click functionality to buttons for location
					but not the geoloaction button
				*/
				$(this.containers.buttons.geolocation).live('click', function(){$.locator.geolocation.locate();return false;});			
				
				
				/*
					submit form
				*/
				$(this.containers.form.button).live('click', function(){
					$($.locator.containers.form.field).val() ? $.locator.search.process($($.locator.containers.form.field).val()) : alert("no address") ; 
					return false;
				});
			}
		}
	});
})(jQuery);

