	$(document).ready(function() {

		SetRestaurantName();
        $("select[id$='ddlFromState']").change(function() {
            var country = $("select[id$='ddlFromState'] option:selected")[0].title;
            $("input[id$='txtFromCountry']")[0].value = country;
        });
        $("select[id$='ddlFromStreetConflict']").change(function() {
            var optionValue = $("select[id$='ddlFromStreetConflict'] option:selected")[0].value;
            var addressParts = optionValue.split('|');
            var city = addressParts[0];
            var state = addressParts[4];
            var zipCode = addressParts[5];
            $("input[id$='txtFromCity']")[0].value = city;
            $("input[id$='txtFromZipCode']")[0].value = zipCode;
            var dFromState = $("select[id$='ddlFromState']")[0];
            for (var i = 0; i < dFromState.length; i++) {
                if (dFromState[i].value == state) {
                    dFromState.selectedIndex = i;
                    break;
                }
            }
        });
        
        //Event Tracking : This is kept here for onload tab; another instance will be in RegisterPageEvent method
        GoogleTracking();
        
        Sys.Application.add_load(RegisterPageEvents);
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(OnUpdating);
        //Added an ajax end request event handler to set the page title back to original
    	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() { document.title = 'Location Landing'; OnUpdated(); });
    	
    	
    });

    //This method is used for GoogleTacking
    function GoogleTracking()
    {
        strurl=window.location.href;
        jQuery.each(strurl.split('/'), function()
        {
            if ((this).toUpperCase()== "EN")
	        {
                $("a[id$='lbtnMap']").click(function() {
                try {
           	        pageTracker._trackEvent("Location_Landing_Map_Tab", "Click", "");
           	        }
		        catch (err) {}
                });
                $("a[id$='lbtnDrivingDirections']").click(function() {
                try {
        	        pageTracker._trackEvent("Location_Landing_Driving_Directions_Tab", "Click", "");
        	        }
		        catch (err) {}
                });
                $("a[id$='lbtnNearby']").click(function() {
                try {
        	        pageTracker._trackEvent("Location_Landing_NearBy_Tab", "Click", "");
        	        }
		        catch (err) {}
                });
		        $("a[id$='lbtnGetDirections']").click(function() {
		        try {
        	        pageTracker._trackEvent("Location_Landing_Get_Directions", "Click", "");
        	        }
		        catch (err) {}
                });
            }
        });
    }
    
    function ShowDrivingDirctionsWindow() {
        var mapWindow = window.open("DrivingDirectionsPopup.aspx", null, "height=700,width=770,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,");
        mapWindow.moveTo(50, 50);
        return false;
    }
    
    function DisableSelect()
    {
    	$("select").each(function() {
			$(this).attr("disabled", "true");	
		});
    }
    
    function EnableSelect()
    {
    	$("select").each(function() {
			$(this).attr("disabled", "");	
		});
    }


    function PopulateStartingLocation(street, city, state, zipCode) {
        $("input[id$='txtFromStreet']")[0].value = street;
        $("input[id$='txtFromCity']")[0].value = city;
        var dFromState = $("select[id$='ddlFromState']")[0];
        for (var i = 0; i < dFromState.length; i++) {
            if (dFromState[i].value == state) {
                dFromState.selectedIndex = i;
                break;
            }
        }
        $("input[id$='txtFromZipCode']")[0].value = zipCode;
    }
    
    function ShowDrivingDirections() 
    {
    	var btnDrivingDirections = jQuery("input[id$='btnDrivingDirections']").attr("name");
	    __doPostBack(btnDrivingDirections,"");
    }
    
    function SetRestaurantName()
	{
		$("td[id$='tdRestaurantName']")[0].innerHTML = $("input[id$='hdnRestaurantName']").val();
	}
    
    
    
    function RegisterPageEvents()
    {
    	$("select[id$='ddlFromState']").change(function() {
            var country = $("select[id$='ddlFromState'] option:selected")[0].title;
            $("input[id$='txtFromCountry']")[0].value = country;
        });
        $("select[id$='ddlFromStreetConflict']").change(function() {
            var optionValue = $("select[id$='ddlFromStreetConflict'] option:selected")[0].value;
            var addressParts = optionValue.split('|');
            var city = addressParts[0];
            var state = addressParts[4];
            var zipCode = addressParts[5];
            $("input[id$='txtFromCity']")[0].value = city;
            $("input[id$='txtFromZipCode']")[0].value = zipCode;
            var dFromState = $("select[id$='ddlFromState']")[0];
            for (var i = 0; i < dFromState.length; i++) {
                if (dFromState[i].value == state) {
                    dFromState.selectedIndex = i;
                    break;
                }
            }
        });
        //For GoogleTracking
        GoogleTracking();


    }
    
    function OnUpdating(sender,args){
	   	//get the update progress div
	    var updateProgressDiv = $get('updateProgressDiv'); 
		var updateProgressUploader = $get('updateProgressLoader');
		
	   	//get the update panel
	   	var index  = sender._postBackSettings.panelID.split('|')[0].lastIndexOf('$') + 1;
	   	var updatePanelName = sender._postBackSettings.panelID.split('|')[0].substring(index,sender._postBackSettings.panelID.split('|')[0].length);
		var objUpdatePanel = $get(jQuery("div[id$= " + updatePanelName + "]")[0].id);
	 	DisableSelect();
	    //make the update progress div visible
	    updateProgressDiv.style.display = '';	    
	    
	    //get the bounds of both the update panel and the progress div
	    var updatePanelBounds = Sys.UI.DomElement.getBounds(objUpdatePanel);
	    var updateProgressDivBounds = Sys.UI.DomElement.getBounds(updateProgressDiv);
	
	    updateProgressDiv.style.width = (updatePanelBounds.width - 4) + "px";
		updateProgressDiv.style.height = (updatePanelBounds.height - 5) + "px";
		updateProgressUploader.style.marginTop = Math.round(updatePanelBounds.height / 2) + "px";
		updateProgressUploader.style.marginLeft = Math.round(updatePanelBounds.width / 2) + "px";
			
	    //set the progress element to this position
	    Sys.UI.DomElement.setLocation (updateProgressDiv, updatePanelBounds.x + 4, updatePanelBounds.y );           
	}
	
	function OnUpdated() {
	    // get the update progress div
	    var updateProgressDiv = $get('updateProgressDiv'); 
	    // make it invisible
	    updateProgressDiv.style.display = 'none';
	    EnableSelect();
	}
	
	function SaveCookie(name, value)
	{
		//Set the Restaurant ID cookie from the parameters passed
	    jQuery.cookie(name, value, { expires: 365 });
	    jQuery.cookie("Online_Ordering", "true", { expires: 365 });
	    jQuery.cookie("International", "No", { expires: 365 });
	}
