﻿function initSliderMapHome() 
{    
    $( document ).ready( function() 
    {
    
        var postcode = "";
        var defaultDistanceIndex;    
        var defaultDistance = $( "#hfDefaultDistance" ).attr("value");


        // insert the slider div
        $(".sliderDist").append( '<div id="sldDistance" class="ui-slider-1"></div>' );
        

        var callPostcodeAnyWhereService = function( address, zoomLevel )
        {
            // invoke the web service
            $.ajax({ type: "POST",  
                url: "/resources/services/locationservices.asmx/GetLatLongFromPostcode",  
                dataType: "xml",  
                data: "postcode=" + address,
                processData: false,  
                error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxErrorPA(XMLHttpRequest, textStatus, errorThrown); },  
                success: function(xml) { showAddressPE(xml, zoomLevel); }  
            }); 
        }

        
        var showAddressPE = function( xml, zoomLevel )
        {
            returnedLat = 0;
            returnedLon = 0;
            
            // Process xml from the webservice
            $( "GeoPoint", xml ).each( function() 
            {
                returnedLat = $( "Latitude", this ).text();            
                returnedLon = $( "Longitude", this ).text();
                
            });
            
            if( returnedLat == 0 && returnedLon == 0 )
            {            
                // invalid postcode so invoke the web service again to get the lat/long for our default postcode
                $.ajax({ type: "POST",  
                    url: "/resources/services/locationservices.asmx/GetLatLongFromPostcode",  
                    dataType: "xml",  
                    data: "postcode=" + $( "#hfDefaultPostcode" ).attr("value"),
                    processData: false,  
                    error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxErrorPA(XMLHttpRequest, textStatus, errorThrown); },  
                    success: function(xml) { showAddressPE(xml, zoomLevel); }   
                });                         
                
                // set the text colour to grey and insert the word postcode in the postcode field
	            $(".txtPostcode").css( { 'color' : '#8c8c8c' } );
	            $(".txtPostcode").attr( "value", "postcode" );
            
                // pop up message
                alert( "Could not find postcode: " + postcode );
            }
                    
            var point = new GLatLng(returnedLat, returnedLon);
            
            map.setCenter( point, zoomLevel );
        }


        // Google map   		
	    var map = new GMap2( document.getElementById( "map" ), { size: new GSize( 511, 377 ) } );	
	    map.disableDragging();
	    map.disableDoubleClickZoom();
	    map.disableInfoWindow();	
    		
    	
	    // first set the default zoom level
	    switch(defaultDistance)
        {
            case "1":	        	             	        	        
                defaultZoom = 15;
                defaultDistanceIndex = 0;
                break;
            case "2":
                defaultZoom = 14;
                defaultDistanceIndex = 1;
                break;
            case "5":
                defaultZoom = 13;
                defaultDistanceIndex = 2;
                break;
            case "10":
                defaultZoom = 12;
                defaultDistanceIndex = 3;
                break;
            case "15":
                defaultZoom = 11;
                defaultDistanceIndex = 4;
                break;
            case "20":
                defaultZoom = 10;
                defaultDistanceIndex = 5;
                break;
            default:
                defaultZoom = 10;	      
                defaultDistanceIndex = 5;      
        }
    		

        // This is the initial map centre		
	    //callPostcodeAnyWhereService($( "#hfDefaultPostcode" ).attr("value"), defaultZoom);    
    	
	    // Distance slider
	    $("#sldDistance").slider(
	    {
	        value: defaultDistanceIndex,
	        step: 1,
	        max: 5,
	        min: 0,
	        range: 'min',
	        change: function(e, ui)
	        {	        	    
	            var sliderValue = ui.value;

	            $(".rbl-distance :input").each(function(counter)
	            {	        
	                if( counter == sliderValue)
	                {
	                    // call the trigger, this will also check it
	                    $(this).trigger("click");                	                	                
	                }
	                else
	                {
	                    this.checked = false;
	                }
	            });	        	  
    	             
    	        
	            // set zoom level  	        
	            switch(sliderValue)
	            {
	                case 0:	        	             	        	        
	                    map.setZoom(15)
	                    break;
	                case 1:
	                    map.setZoom(14)
	                    break;
	                case 2:
	                    map.setZoom(13)
	                    break;
	                case 3:
	                    map.setZoom(12)
	                    break;
	                case 4:
	                    map.setZoom(11)
	                    break;
	                case 5:
	                    map.setZoom(10)
	                    break;
	                default:
	                    map.setZoom(10);	            
	            }	              	        
	        }
	    });
    	
    		
	    // check the radio button (refineDistance) according to the defaultDistance value
	    $(".rbl-distance :input").each(function(counter)
        {	        
            if( counter == defaultDistanceIndex )
            {
                // call the trigger, this will also check it
                $(this).trigger("click");                	                	                
            }
            else
            {
                this.checked = false;
            }
        });
    	
    			
	    // insert the overlay after the range	
	    $("#sldDistance .ui-slider-range").after('<div class = "distance-overlay"></div>');
    	
    	
	    // set the text colour to grey and insert the word postcode in the postcode field
	    $(".txtPostcode").css( { 'color' : '#8c8c8c' } );
	    $(".txtPostcode").attr( "value", "postcode" );
    	
	    $(".txtPostcode").focus( function()
	    {
	        if( $(".txtPostcode").attr( "value" ) == "postcode" )
	        {
	            // sset text colour to black
	            $(".txtPostcode").css( { 'color' : 'black' } );
    	    
	            // clear the postcode
	            $(".txtPostcode").attr( "value", "" );
	        }
	    });


	    var btnPostcode = $("<span id=\"btnPostcode\" class=\"postcode-submit\"></span>").click( function() {
            
            postcode = $(".txtPostcode").attr("value");	    
    	    
	        if ( postcode.length > 0 && postcode != "postcode" )
	        {	    
	            // call showAddress with postcode and zoom level
	            var zoomLevel = map.getZoom();
        	    
	            //showAddress(postcode + ", UK", zoomLevel);
	            callPostcodeAnyWhereService( postcode, zoomLevel );
	        } 
    	    
	    } );
    	
    	
	    $( ".postcode-submit" ).append( btnPostcode );	
    	
    	
	    callPostcodeAnyWhereService($( "#hfDefaultPostcode" ).attr("value"), defaultZoom);
	
	} );
}