var map = null;
var geocoder = null;
var strURL = null;
var xPos = null;
var yPos = null;
var address = null;
var searchATM = null;
var searchBranch = null;
var html = null;

var DefaultZoomLevel =13; //11 for whatever
var DefaultLATT = 40.75805 //NY 
var DefaultLONG = -73.98596 //NY




// initialize map
$(document).ready(function() {

	if (GBrowserIsCompatible()) {				
		$("#chkATM").cssCheckbox();
		$("#chkBranch").cssCheckbox();
		$("#AtmBranchFinder").css({display: "block" });
		
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallZoomControl());
		//map.addControl(new GMapTypeControl());
		
	  function gup(name)
        {
          name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]"+name+"=([^&#]*)";
          var regex = new RegExp( regexS );
          var results = regex.exec( window.location.href );
          if( results == null )
            return "";
          else
            return results[1];
        }  		
        
		if (window.location.href.indexOf('DZL') > -1)
		{
		    DefaultZoomLevel = parseInt(gup('DZL'));          
		}

		if (window.location.href.indexOf('LATT') > -1)
		{
		    DefaultLATT = parseFloat(gup('LATT'));          
		}
		
		if (window.location.href.indexOf('LONG') > -1)
		{
		    DefaultLONG = parseFloat(gup('LONG'));          
		}				
		
		map.setCenter(new GLatLng(DefaultLATT, DefaultLONG), DefaultZoomLevel);
		geocoder = new GClientGeocoder();
	}
});



function gup(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function showAddress() {
	var boa_item_type = -1;
	address = document.getElementById("address");
	searchATM = document.getElementById("searchATM");
	searchBranch = document.getElementById("searchBranch");
	
	if(address.value == ""){
		alert("Please provide a valid address");
		return false;
	}
	
	// Clear Markers from map
	map.clearOverlays(); 
	
	if (geocoder) {
		geocoder.getLatLng(address.value, function(point) {			
			if (!point) {
				alert(address.value + " not found");
			} else {
			
				// Fitting the map to the data
				map.setCenter(point, 14);				
				xPos = point.x;
				yPos = point.y;				
				if(searchATM.checked){ boa_item_type = 0; }		
				if(searchBranch.checked){ boa_item_type = 1; }		
				if(searchATM.checked && searchBranch.checked){ boa_item_type = 2; }
				
				if(boa_item_type>=0){
					doSearch(xPos, yPos, boa_item_type);
				}
				//var marker = new GMarker(point);
				//map.addOverlay(marker);
				//marker.openInfoWindowHtml(address);
			}
		});
	} 
}

function doSearch(xPos, yPos, boa_item_type ) {
	var strURL = "getatmlocations.aspx?lat=" + yPos + "&long=" + xPos + "&type=" + boa_item_type + "&rad=3&radtype=1";
	//alert(strURL);
	$(function() {
		$.ajax({
			url: strURL,
			type: 'GET',
			dataType: 'xml',
			timeout: 5000,
			error: function(){
				alert('Error loading XML document');
			},
			success: function(xml) {
				$(xml).find('item').each(function(){
					var lat = $("lat", this).text();	
					var lon = $("lon", this).text();	
					html = "<div id='infoDiv'>" + 
						"<h1>" + normalize($("name", this).text()) + "</h1> " + 
						normalize($("addr_1", this).text()) + "<br /> " + 
						normalize($("city", this).text()) + ", " + $("state", this).text() + " " + $("zip", this).text() + "<br /> " +  normalize($("hours", this).text()) + "</div>";
					
					var point = new GLatLng(lat, lon);
					
					// Create a lettered icon for this point using our icon class
					var icon = new GIcon();
					
					if($(this).attr("type") == 0){
						icon.image =  "images/blue_mrkr.png";
					}
					else{
						icon.image =  "images/red_mrkr.png";
					}
					
					//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
					icon.iconSize = new GSize(28, 28);
					icon.shadowSize = new GSize(37, 34);
					icon.iconAnchor = new GPoint(9, 34);
					icon.infoWindowAnchor = new GPoint(9, 2);
					icon.infoShadowAnchor = new GPoint(18, 25);
					
					//map.addOverlay(new GMarker(point));
					map.addOverlay(createMarker(point, html, icon));
					
				});
			}
		});
	});
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, html, icon) {
		
	var marker = new GMarker(point, icon);
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindow(html);
	});
	
	//GEvent.addListener(marker,"mouseover", function() {
	//		marker.openInfoWindowHtml(html);
	//});  
	
	return marker;
}


function normalize(uppercaseString) {
	var stringSplit = uppercaseString.split(" ");
	var returnedString = "";
	for (var i = 0; i < stringSplit.length; i++) {
		returnedString += stringSplit[ i ].charAt(0) + stringSplit[ i ].substring(1).toLowerCase() + " ";
	}
	return returnedString.substring(0, returnedString.length - 1);
}

function cl(inp, val) {
	if (inp.value == val) {
	    inp.value = "";
	    inp.style.color = "#333333";
	}    
}

function fl(inp, val) {
	if (inp.value == "") {
        inp.value = val;
        inp.style.color = "#999999" ;
    }
}