var xmlIn;
var numPages ;
var map;
var markersArray;
var blueIcon;



$(function() {
	
	$('#mapBack').click(function() {
		
		map.clearOverlays();
		
		$('#locOutput').html(" ");
		
		var currentPage = $("input#ctl00_Main_CurrentPage").val();
		var downPage = parseInt(currentPage)-1;
		$("input#ctl00_Main_CurrentPage").val(downPage);
		
		var currentPage = $("input#ctl00_Main_CurrentPage").val();
		
		if(currentPage == 1){
			$('#mapBack').css({visibility:'hidden'});
		}else{
			$('#mapBack').css({visibility:'visible'});
		}
		$('#mapFwd').css({visibility:'visible'});
		$('#mapCount').css({visibility:'visible'});
		$('#mapCount').html('&nbsp;'+currentPage+' of '+numPages+'&nbsp;')
		
		var step = 2*(downPage-1);
		
		var counter = 1;
		var bounds = new GLatLngBounds();  
		
		for (var j = step; j <= step+1; j++){
			var nameIn = $('Location:eq('+j+') Name',xmlIn).text();
			var addressIn = $('Location:eq('+j+') Address',xmlIn).text();
			var cityIn = $('Location:eq('+j+') City',xmlIn).text();
			var stateIn = $('Location:eq('+j+') State',xmlIn).text();
			var zipIn = $('Location:eq('+j+') ZIPCode',xmlIn).text();
			var telIn = $('Location:eq('+j+') Phone',xmlIn).text();
			
			$('#locOutput').append(counter+'.&nbsp;'+nameIn+'<br>'+addressIn+'<br>'+cityIn+',&nbsp;'+stateIn+'&nbsp;'+zipIn+'<br>'+telIn+'<br><br>');
			
			markerLatLog = markersArray[j].split(',');

			var point = new GLatLng(markerLatLog[0],markerLatLog[1]);
			blueIcon = new GIcon(G_DEFAULT_ICON);
			if (counter == 1){
				blueIcon.image = "images/blue-dot-1.png";
			}else{
				blueIcon.image = "images/blue-dot-2.png";
			}					
			blueIcon.iconSize = new GSize(32, 32);

			markerOptions = { icon:blueIcon };
			map.addOverlay(new GMarker(point, markerOptions));
			bounds.extend(point);
			
			
			counter++;
		}
		map.setZoom(map.getBoundsZoomLevel(bounds)); 
		
		map.setZoom(map.getZoom()-1); 
		map.setCenter(bounds.getCenter());
		
		
	});
	
	$('#mapFwd').click(function() {
		
		map.clearOverlays();
		$('#locOutput').html(" ");
		
		var currentPage = $("input#ctl00_Main_CurrentPage").val();
		var upPage = parseInt(currentPage)+1;
		$("input#ctl00_Main_CurrentPage").val(upPage);
		
		var currentPage = $("input#ctl00_Main_CurrentPage").val();
		
		if(currentPage == numPages){
			$('#mapFwd').css({visibility:'hidden'});
		}else{
			$('#mapFwd').css({visibility:'visible'});
		}
		$('#mapBack').css({visibility:'visible'});
		$('#mapCount').css({visibility:'visible'});
		$('#mapCount').html('&nbsp;'+currentPage+' of '+numPages+'&nbsp;')
		
		var step = 2*(upPage-1);
		
		var counter = 1;
		var bounds = new GLatLngBounds();  
		
		
		for (var j = step; j <= step+1; j++){
			var nameIn = $('Location:eq('+j+') Name',xmlIn).text();
			var addressIn = $('Location:eq('+j+') Address',xmlIn).text();
			var cityIn = $('Location:eq('+j+') City',xmlIn).text();
			var stateIn = $('Location:eq('+j+') State',xmlIn).text();
			var zipIn = $('Location:eq('+j+') ZIPCode',xmlIn).text();
			var telIn = $('Location:eq('+j+') Phone',xmlIn).text();
			
			if(nameIn != ""){
				$('#locOutput').append(counter+'.&nbsp;'+nameIn+'<br>'+addressIn+'<br>'+cityIn+',&nbsp;'+stateIn+'&nbsp;'+zipIn+'<br>'+telIn+'<br><br>');
				
				markerLatLog = markersArray[j].split(',');
				
				var point = new GLatLng(markerLatLog[0],markerLatLog[1]);
				blueIcon = new GIcon(G_DEFAULT_ICON);
				if (counter == 1){
					blueIcon.image = "images/blue-dot-1.png";
				}else{
					blueIcon.image = "images/blue-dot-2.png";
				}					
				blueIcon.iconSize = new GSize(32, 32);

				markerOptions = { icon:blueIcon };
				map.addOverlay(new GMarker(point, markerOptions));
				bounds.extend(point);
				counter++;
			}
			
		
		}
		map.setZoom(map.getBoundsZoomLevel(bounds)); 
		
		map.setZoom(map.getZoom()-1); 
		map.setCenter(bounds.getCenter());
		

		
	});
	
});

function animate(currentPage){

	var step = 2*(currentPage-1);
	
	markerLatLog = markersArray[step].split(',');
	map.panTo(new GLatLng(markerLatLog[0],markerLatLog[1]));
}

function getMap() {
	
	var zipInput = $("input#ctl00_Main_zipInput").val();
	var currentPage = $("input#ctl00_Main_CurrentPage").val();
	$('#map-image').html('<h3>Loading Results, Please Wait...</h3>');
	$('#locOutput').html('');
	
	//alert("here");
	
	$.ajax({
		type: "GET",
		url : "pharmacy-locator.ashx?zip="+zipInput,
		dataType: "xml",
		
		success : function (xml) {
			xmlIn = $(xml);

			var mapURLIn = $(xml).find('Locations').attr("Map");
			// find and replace amp;
			
			var mapURLOut = mapURLIn.replace('amp;','');
			var mapURLKey = mapURLOut.replace('client=gme-draftfcb','client=gme-draftfcb');
			
			var markersValue = $.getURLParam("markers",mapURLKey);
			
			if (markersValue == null){
				$('#map-image').html('<h2>Sorry, no results were found</h2>');
			}else{
				
				markersArray = markersValue.split('|');
				var currentPage = $("input#ctl00_Main_CurrentPage").val();
				
				map = new GMap2(document.getElementById("map-image"));
				markerLatLog = markersArray[0].split(',');
				map.setCenter(new GLatLng(markerLatLog[0],markerLatLog[1]), 14);
				map.setUIToDefault();
			}
			// output navigation
			numPages = Math.ceil(markersArray.length/2);
			
			if(currentPage == 1){
				$('#mapFwd').css({visibility:'visible'});
				$('#mapBack').css({visibility:'hidden'});
				$('#mapCount').css({visibility:'visible'});
				$('#mapCount').html('&nbsp;'+currentPage+' of '+numPages+'&nbsp;');
				
				var bounds = new GLatLngBounds();  
				
				for (var i = 0; i <= 1; i++) {
					
					markerLatLog = markersArray[i].split(',');
					
					var point = new GLatLng(markerLatLog[0],markerLatLog[1]);
					blueIcon = new GIcon(G_DEFAULT_ICON);
					if (i == 0){
						blueIcon.image = "images/blue-dot-1.png";
					}else{
						blueIcon.image = "images/blue-dot-2.png";
					}					
					blueIcon.iconSize = new GSize(32, 32);

					markerOptions = { icon:blueIcon };
					map.addOverlay(new GMarker(point, markerOptions));
					bounds.extend(point);
					
				}
				
				
				
				map.setZoom(map.getBoundsZoomLevel(bounds)); 
				
				map.setZoom(map.getZoom()-1); 
				map.setCenter(bounds.getCenter());
				

			}
			
			
			var counter = 1;
			// output addresses
			for (var j = currentPage-1; j <= currentPage; j++){
				var nameIn = $('Location:eq('+j+') Name',xml).text();
				var addressIn = $('Location:eq('+j+') Address',xml).text();
				var cityIn = $('Location:eq('+j+') City',xml).text();
				var stateIn = $('Location:eq('+j+') State',xml).text();
				var zipIn = $('Location:eq('+j+') ZIPCode',xml).text();
				var telIn = $('Location:eq('+j+') Phone',xml).text();
				
				$('#locOutput').append(counter+'.&nbsp;'+nameIn+'<br>'+addressIn+'<br>'+cityIn+',&nbsp;'+stateIn+'&nbsp;'+zipIn+'<br>'+telIn+'<br><br>');
			
				counter++;
			}
			return false;                    
		}
	});
	
}

