Array.prototype.contains = function(obj) {
 var i, listed = false;
 for (i=0; i<this.length; i++) {
   if (this[i] === obj) {
     listed = true;
     break;
   }
 }
 return listed;
};
   
   
$(document).ready(function() {
    
    /**
     * Picture selection (accommodation)
     */
    $('.picture_selection li').click(function() {
        var imgName = $('a', this).attr('href').substr(1);
        $('#picture_container img').attr('src', 'img/photos/' + imgName + '.jpg');
        
        $('.picture_selection li').each(function() {
            $(this).removeClass('selected');
        })
        
        $(this).addClass('selected');
        return false;
    });
    
    
    /**
     * Map selection (location)
     */
    
    /**
     * Highlight a point on the map by its layer name (layer id)
     * 
     * @param String Layer name
     * @return void
     */
    function selectMapPoint(layerName) {
        $('#map_container div').each(function () {
            $(this).removeClass('selected');
        })
        
        $('.map_selection li').each(function() {
            $(this).removeClass('selected');
        })
        
        $('#' + layerName).addClass('selected');
        $('.map_selection a[@href=#' + layerName + ']').parent('li').addClass('selected');
    }
    
    /**
     * Set hover-events for legend items
     */
    $('.map_selection li').hover(function() {
        var layerName = $(this).find('a').attr('href').substr(1);
        selectMapPoint(layerName);
    }, function() {});
    
    /**
     * Set hover events for map points
     */
    $('#map_container div').each(function() {
        $(this).hover(function() {
            selectMapPoint($(this).attr('id'));
        }, function() {})
    });
    
    
    /**
     * Contact form (contact us)
     */
    if (!$('#enquire_check').attr('checked')) {
        $('#ContactForm .form_enquire').each(function () {
            $(this).attr('disabled', 'disabled');
        });
    } else {
        $('#enquire_check').parent('fieldset').addClass('enabled');
    }
    
    $('#enquire_check').click(function() {
        if (!$('#enquire_check').attr('checked')) {
            $('#ContactForm .form_enquire').each(function () {
                $(this).attr('disabled', 'disabled');
            });
           
            $(this).parent('fieldset').removeClass('enabled');
        } else {
            $('#ContactForm .form_enquire').each(function () {
                $(this).removeAttr('disabled');
            });
            $(this).parent('fieldset').addClass('enabled');
        }
    });
    
    /**
     * Show or hide selectable rooms
     * 
     * @param Object selectElement
     * @param Array options
     */
    function showOptions(selectElement, options) {
    	selectElement.removeOption(/.*/);
    	selectElement.addOption(options);
    	selectElement.val(1);
    }
    
    /**
     * Restrict number of people selection depending on room type 
     */
    $('select[@name^=room_type]').livequery('change', function() {
        if ($(this).val() == 'Single room') {
            showOptions($('select[@name^=people]', $(this).parent()),
                        {1:1});
        }
        
        if ($(this).val() == 'Double room' || $(this).val() == 'Twin room') {
            showOptions($('select[@name^=people]', $(this).parent()),
                        {1:1,2:2});
        }
        
        if ($(this).val() == 'Dorm') {
            showOptions($('select[@name^=people]', $(this).parent()),
                        {1:1,2:2,3:3,4:4,5:5,6:6,7:7});
        }
    });
    
    /**
     * Add + Remove Bookings
     */
    $('.add_another_booking').livequery('click', function(event) {
        var object = $('.booking:last');
        object.after(object.clone());
        $('.remove_booking:last').show();
        return false;
    });
    
    $('.remove_booking').livequery('click', function(event) {
        $(this).parent().hide('slow', function() {$(this).remove()});
        return false;
    });
        
    $('.remove_booking:first').hide();
    
});
