var initalLocation;
var browserSupportFlag = new Boolean();
var map ;

function initalizeMap(){

    var latlng = new google.maps.LatLng(51.3314412, -2.2111677);
	var Airsprung = latlng;
    var myOptions = {
        zoom: 14,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    // Try Geolocating The User Before taking in the postcode.
	// disabled  
 /*   if (navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function(position){
            initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            map.setCenter(initialLocation);
        }, function(){
            handleNoGeolocation(browserSupportFlag);
        });
        // Try Google Gears Geolocation
    }
    else 
        if (google.gears) {
            browserSupportFlag = true;
            var geo = google.gears.factory.create('beta.geolocation');
            geo.getCurrentPosition(function(position){
                initialLocation = new google.maps.LatLng(position.latitude, position.longitude);
                map.setCenter(initialLocation);
            }, function(){
                handleNoGeoLocation(browserSupportFlag);
            });
        // Browser doesn't support Geolocation
        }
        else {
            browserSupportFlag = false;
            handleNoGeolocation(browserSupportFlag);
        }
    
    function handleNoGeolocation(errorFlag){
        if (errorFlag == true) {
            //alert("Geolocation service failed.");
            initialLocation = Airsprung;
        }
        else {
       //     alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
            initialLocation = Airsprung;
        } 
        map.setCenter(initialLocation);
    }
    
// */
}

function createMarker(point,options,html) {
    var marker = new GMarker(point, options);
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html, {maxWidth:200});
    });
    return marker;
}



$(function(){
	
	//initalizeMap();

	$('.postCodeSubmit').click(function (event) {
	initalizeMap();

		event.preventDefault();
	agent = jQuery.browser;
	if(agent.msie) {
		event.cancelBubble = true;
	} else {
		event.stopPropagation();
	}
	
		datastring = "postcode=" + $('input.postCode').val();
		
		$.ajax({
	        	type: "GET",
	        	url: "http://www.gainsborough-beds.co.uk/storefinder/stores/findAStore/",
	        	data: datastring,
				dataType: 'jsonp',
	        	jsonp: 'jsonp_callback',
	        	success: function(response, status, instance){
	            	$stores = eval(response);
					var latlng = new google.maps.LatLng($stores['postcode']['lat'],$stores['postcode']['lng']);
	            	// this is customised for gainsborough site. Will need to modify for other sites as necessary
					$store1 = '<div class="name">' + $stores[0]['Store']['name']  + "</div>";
					$store1 += '<div class="address">' + $stores[0]['Store']['address'] + "</div>";
					$store1 += '<div class="postcode">'+ $stores[0]['Store']['postcode'] + "</div>";
					if ($stores[0]['Store']['phone'] != 0 ) {
						$store1 += '<div class="phone">'	+ $stores[0]['Store']['phone'] + '</div>';
					}
					
					$store2 = '<div class="name">' + $stores[1]['Store']['name']  + "</div>";
					$store2 += '<div class="address">' + $stores[1]['Store']['address'] + "</div>";
					$store2 += '<div class="postcode">'+ $stores[1]['Store']['postcode'] + "</div>";
					if ($stores[1]['Store']['phone'] != 0 ) {
						$store2 += '<div class="phone">'	+ $stores[1]['Store']['phone'] + '</div>';
					}
					
					$store3 = '<div class="name">' + $stores[2]['Store']['name']  + "</div>";
					$store3 += '<div class="address">' + $stores[2]['Store']['address'] + "</div>";
					$store3 += '<div class="postcode">'+ $stores[2]['Store']['postcode'] + "</div>";
					if ($stores[2]['Store']['phone'] != 0 ) {
						$store3 += '<div class="phone">'	+ $stores[2]['Store']['phone'] + '</div>';
					}
															
					$('#storeslist .col1').html($store1);
					$('#storeslist .col2').html($store2);
					$('#storeslist .col3').html($store3);					
						               						
					map.setCenter(latlng);
					var marker = new google.maps.Marker( {
						map: map,
						position: latlng,
						icon: "http://www.google.com/intl/en_ALL/mapfiles/marker_green.png"
					});
					
					var	imageIconA = "http://www.google.com/mapfiles/markerA.png";
        			var imageIconB = "http://www.google.com/mapfiles/markerB.png";
        			var	imageIconC = "http://www.google.com/mapfiles/markerC.png";	
					var  latlngA = new google.maps.LatLng($stores[0]['Store']['lat'],$stores[0]['Store']['lng']);
					var  latlngB = new google.maps.LatLng($stores[1]['Store']['lat'],$stores[1]['Store']['lng']);
					var  latlngC = new google.maps.LatLng($stores[2]['Store']['lat'],$stores[2]['Store']['lng']);	
			        
					var markerA = new google.maps.Marker({
		  				position: latlngA, 
						map: map,
						icon: imageIconA,
						title: $stores[0]['Store']['name']
						}); 
			      	var markerB = new google.maps.Marker({
					  	position: latlngB, 
						map: map,
						icon: imageIconB,
						title: $stores[1]['Store']['name']
						}); 
					var markerC = new google.maps.Marker({
					  	position: latlngC, 
						map: map,
						icon: imageIconC,
						title: $stores[2]['Store']['name']
						}); 	

        			var bounds = new google.maps.LatLngBounds();
        
					bounds.extend(map.getCenter());
        			bounds.extend(latlngA);
        			bounds.extend(latlngB);
        			bounds.extend(latlngC);

           map.setCenter(bounds.getCenter());
           map.fitBounds(bounds);
	
				
			
					}
		});
		
	});
	
    //initalizeMap();
	//alert("about to find it")
//	$('#postCodefinder :button').hide();
	
 /*
	
	*/
});	  



	