
$(function (){
    // Update cities based on selected country.

    $("form.search").each(function(index, obj){
        var continent_select = $(obj).find("select.continent");
        var country_select = $(obj).find("select.country");
        var city_select = $(obj).find("select.city");
        var hotel_select = $(obj).find("select.hotel");

        var load_hotels = function (city, cont){
            hotel_select.load("/api/hotels/" + city,
                              function (){
                                  cont();
                              });
        }

        var update_hotels = related_select(city_select, hotel_select, load_hotels,
                                           toggle_select_using_disable([hotel_select]));

        var load_cities = function (country, cont){
            city_select.load("/api/cities/" + country,
                             function (){
                                 cont();
                                 update_hotels();
                             });
        }

        var update_cities = related_select(country_select, city_select, load_cities,
                                           toggle_select_using_disable([city_select, hotel_select]));

        // var load_countries = function (continent, cont){
        //     country_select.load("/api/countries/" + continent,
        //                      function (){
        //                          cont();
        //                          update_cities();
        //                      });
        // }

        // var update_countries = related_select(continent_select, country_select, load_countries,
        //                                       toggle_select_using_disable([country_select, city_select, hotel_select]));

        // update_countries();
        update_cities();
    });

});

function toggle_select_using_disable(children){
    var enable_element = function(element){
        element.attr('disabled', '');
        //element.show();
    }
    var disable_element = function(element){
        element.attr('disabled', 'disabled');
        //element.hide();
    }

    var enable_children = function(){
        $.each(children, function(index, child){
            enable_element(child);
        });
    };

    var disable_children = function(){
        $.each(children, function(index, child){
            disable_element(child);
        });
    };

    var enable_fn = function (parent_val){
        if ("" == parent_val){
            disable_children();
        }
        else {
            enable_children();
        }
    }

    var disable_fn = function (parent_val){
        disable_children();
    }

    return {enable: enable_fn, disable: disable_fn};
}

function related_select(parent, child, child_update_fn, toggle) {
    var parent_update_fn = function(){
        var parent_val = parent.val();
        var child_val = child.val();

        var cont = function(){
            toggle.enable(parent_val);
            child.val(child_val);
        };

        toggle.disable();
        child.val("");

        child_update_fn(parent_val, cont);
    };

    parent.change(parent_update_fn);

    return parent_update_fn;
}


$(function(){
    $('.validate').validate({
        errorClass: 'incorrect',
        errorContainer: "#devnull",
        errorLabelContainer: "#devnull",
		highlight: function( element, errorClass, validClass ) {
			$(element).addClass(errorClass).removeClass(validClass);
			$(element).parent('div').addClass(errorClass).removeClass(validClass);
		},
		unhighlight: function( element, errorClass, validClass ) {
			$(element).removeClass(errorClass).addClass(validClass);
			$(element).parent('div').removeClass(errorClass).addClass(validClass);
		},
		rules: {
  		}
    });
});


// google maps hotels

if (document.getElementById("map_canvas"))
{
    initialize_maps();
}

function initialize_maps() {
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);

    var myOptions = {
        zoom: 16,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var hotel_info = $("#hotel-info");
    var address = hotel_info.find("#hotel-address").text();
    var tooltip_contents = hotel_info.find("#hotel-tooltip").html();
    var title = hotel_info.find("#hotel-title").text();

    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                title: title
            });

            infowindow = new google.maps.InfoWindow({
                content: tooltip_contents
            });

            google.maps.event.addListener(marker, "click", function(){
                infowindow.open(map, marker);
            });
        } else {
            //alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

