﻿/// <reference path="Js/jquery-1.3.2-vsdoc.js" />
/// <reference path="Js/jquery.cookie.js" />
var geocoder = null;

// initialize the geocoder if supported
$(document).ready(function() {
    if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
    }
});

function rcbCity_OnClientBlur(sender) {
    //get value
    var cityValue = sender.get_text();
    
    // set cookie
    setCityCookie(cityValue);

    // geocode city
    setCityLatLng(cityValue);
}

function rcbCity_OnClientLoad(sender) {
    var combo = sender;
    if (combo.get_text().length < 1) {
        // check for cookie
        var cookieValue = $.cookie('city');
        if (cookieValue && cookieValue.length > 0) {
            combo.set_text(cookieValue);
        }
        else {
            //  combo.set_emptyMessage();
        }
    }
    // set hidden values here
    hdnFldCityLat_onload();
    hdnFldCityLng_onload();
}
function rcbCity2_OnClientLoad(sender) {
    var combo = sender;
    if (combo.get_text().length < 1) {
        // check for cookie
        var cookieValue = $.cookie('restaurant');
        if (cookieValue && cookieValue.length > 0) {
            combo.set_text(cookieValue);
        }
        else {
            //  combo.set_emptyMessage();
        }
    }
}
function rcbRestaurantName_OnClientLoad(sender) {

    var combo = sender;
    if (combo.get_text().length < 1) {
        // check for cookie
        var cookieValue = $.cookie('restaurant');
        if (cookieValue && cookieValue.length > 0) {
            combo.set_text(cookieValue);
        }
        else {
            //  combo.set_emptyMessage();
        }
    }
}
// only try to set if it wasn't set at server
function hdnFldCityLat_onload() {
    if (getLatValue().length < 2) {
        var cookieValue = $.cookie('cityLat');
        if (cookieValue && cookieValue.length > 0) {
            setLatValue(cookieValue);
        }
    }
}
// only try to set if it wasn't set at server
function hdnFldCityLng_onload() {
    if (getLngValue().length < 2) {
        var cookieValue = $.cookie('cityLng');
        if (cookieValue && cookieValue.length > 0) {
            setLngValue(cookieValue);
        }
    }
}
function rcbRestaurantName_OnClientItemsRequesting(sender, e) {
    var context = e.get_context();
    context["lat"] = getLatValue();
    context["lng"] = getLngValue();
}

function rcbCity2_OnClientBlur(sender) {
    //get value
    var cityValue = sender.get_text();

    // set cookie
    setCityCookie(cityValue);

    // geocode city
    setCityLatLng(cityValue);
}

function rcbRestaurant_OnClientBur(sender) {
    setRestaurantCookie(sender.get_text());
}

function setCityCookie(value) {
    $.cookie('city', value);
}
function setRestaurantCookie(value) {
    $.cookie('restaurant', value);
}
function setLatLngCookie(latValue, lngValue) {
    $.cookie('cityLat', latValue);
    $.cookie('cityLng', lngValue);
}

// uses google's geocoder to get lat and lng
function setCityLatLng(address) {
    if (geocoder) {
        if (address.length > 0) {
            geocoder.getLatLng(
                address,
                function(point) {
                    if (point) {
                        setLatValue(point.lat());
                        setLngValue(point.lng());
                        setLatLngCookie(point.lat(), point.lng());
                    }
                });
        }
    }
}

function setLatValue(numLat) {
    // select id that ends with hdnFldCityLat
    $("input[id$=hdnFldCityLat]").val(numLat);
}

function getLatValue() {
    return $("input[id$=hdnFldCityLat]").val();
}

function setLngValue(numLng) {
    // select id that ends with hdnFldCityLng
    $("input[id$=hdnFldCityLng]").val(numLng);
}

function getLngValue() {
    return $("input[id$=hdnFldCityLng]").val();
}
