	//Google Map Code
var map = "";
var geocoder = new GClientGeocoder();
var marker_objects = [];
var marker_htmls = [];
var Athens = new GLatLng(34.423903,-82.824898);

$(document).ready(function(){
	//image rotator code
	//fades out currently visible child, fades in next child, or the first child if you are on the last
	$.fn.fadeToNext = function(speed){
		$(this).children().each(function(){
		
			if($(this).is(":visible")){
				
				
				var callback;
				if(!$(this).is(":last-child")){
					callback = function(){$(this).next().fadeIn(speed);};
				}
				else{
					//no last child, so we fade in the first child of the list
					callback = function(){$(this).parent().children(":first").fadeIn(speed);};
				}
				$(this).fadeOut(speed,callback);
				return false; //stops looping
			}
		
		
		});
		
	}
		
	 $.fn.fadeToPrev = function(speed){
	 
		 $(this).children().each(function(){
		 
		 	if($(this).is(":visible")){
				
				var callback;
				if(!$(this).is(":first-child")){
					callback = function(){$(this).prev().fadeIn(speed);};
				}
					
				else{
					//no previous element, so we fade in the last child
					callback = function(){$(this).parent().children(":last").fadeIn(speed);};
				}
				$(this).fadeOut(speed,callback);
				return false; //stops looping
			}
		 
		 });
	}
		
	//set default displays:
	$("#testRotator").children(":not(:first)").css("display","none");
		
	//previous button
	$("#testPrev").click(function(){
		$("#testRotator").fadeToPrev(300);
		return false;
	});
	
	
	//next button
	$("#testNext").click(function(){
		$("#testRotator").fadeToNext(300);
		return false;
	});
	
	


//Initial map setup
	loadMap();
	
	//Add markers
	readyAllMarkers();

	//Assign onClick to all map marker links
	readyAllLinks();
	
	//Assign an onClick to the seach button
	$(".searchbtn").click(performSearch);	
	
	//Assign enter to submit for the keywords.
	$(document).keypress(function(event){
		if(event.keyCode==13){
			performSearch();
			return false;
		}//end if	
			
	});
		
});

//Grabs search criteria and submits via ajax.
function performSearch(){

	//Grab keywords and category
	var keywords = $('.searchtext').val();
	var category_id = $('#Category_ID').val();

	//purge keywords of the default value
	if(keywords=="Type a name here to search for it..."){
		keywords="";
	}//end if
	keywords = keywords.replace(/ /g,"%20");

	//1. Reload a blank map
	loadMap();
		
	//2. Clear arrays
	marker_objects.length=0;
	marker_htmls.length=0;
		
	//3. Update the side list of categories, callback will load points onto the map
	var url = 'php/run.php?function_name=slBusinessSearch&param[1]='+keywords+'&param[2]='+category_id;
	$('#BusinessesList').load(url,"",reloadMarkers);

	
}//end function

//Mashes two functions together so we can callback from ajax.
function reloadMarkers(){
	
	//fix up the map with new data
	readyAllMarkers();
	readyAllLinks();
	
	//Clear the search terms
	$(".searchtext").val("");
	
}//end function

function loadMap(){
	
	map = new GMap2(document.getElementById('map'));
	map.setCenter(Athens);
	map.setZoom(11);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());	

}//end function


function readyAllMarkers(){

	//Grab all addresses and titles, add markers to map
	$(".MarkerLink").each(function(){
		
		var address 	= $(this).attr('address');
		var address2 	= address.replace(/,/g,"<br/>");
		var title 		= $(this).attr('title');		
		var id 			= parseInt(this.id.replace("Marker",""));

		var marker_html = "<h2>"+title+"</h2><P>"+address2+"</P>";
		addAddress(address,marker_html,id);
		
	});//end MarkerLink
	


}//end function

//Assigns onclicks to map links and changes usable a tags into clickable links.
function readyAllLinks(){

	$(".MarkerLink").each(function(){
		
		$(this).click(function(){
			
			var id = parseInt(this.id.replace("Marker",""));
			
			//Get the marker and its html
			var marker = marker_objects[id];
			var marker_html = marker_htmls[id];
				
			if(marker != null){
				//open the window and pan to the marker
				marker.openInfoWindowHtml(marker_html);
				map.panTo(marker.getLatLng());	
										
			}else{
				alert("Sorry, but this link does not have an address that we can display");
			}//end else
			
		});
		
		//Upgrade the 'look and feel' of the link items as they become ready.		
		$(this).addClass('map_link');
			
	});

}//end function


//Adds one address to the map and the needed arrays.
function addAddress(address,marker_html,ID) {
	
	geocoder.getLatLng(address,function(point) {
	
		if(point){

			//Create a marker and add it to the map. Add an onclick event so its window shows when clicked
			var marker = new GMarker(point);
			map.addOverlay(marker);
			GEvent.addListener(marker,'click',function(){marker.openInfoWindowHtml(marker_html);});

			//Add to Markers to the marker array for later use by the clickable links
			var index = ID;
			marker_objects[index] = marker;
			marker_htmls[index] = marker_html;
			
		}//end else
	
	});//end geocoder
}//end showAddress 
