/**
 * @title       Travel guide results scripts Touring Nature 
 */
$(document).ready(function(){

    
    
    //initializeGmap();

    var changeSelect = function(sTargetSelectId, iId) {
    	iEid = getUrlParameter('eid');
    	if(iEid == ''){
    		iEid = $('#searchTravelForm input[name="eid"]').val();
    	}
        $.get(l10n.url.selectOptions + eval("l10n.url." + sTargetSelectId + "Select"), {id:iId, eid:iEid}, 
            function(data){
               var newOptions = new Array();
               newOptions[0] = {value:'',text:eval("l10n."+sTargetSelectId+"Choice"),selected:true}; 
                xmlDoc = (data).getElementsByTagName(sTargetSelectId); 
                xmlDocLen = xmlDoc.length;
                if (xmlDocLen) {
                    $('#' + sTargetSelectId).parent(".select").removeClass('disabled');
                    for (var i=0; i<xmlDocLen; i++) {
                        var j = i+1;
                        newOptions[j] = {
                            value: xmlDoc[i].getAttribute("id"),
                            text: xmlDoc[i].firstChild.nodeValue,
                            selected:false
                        };
                    } 
                } else if (sTargetSelectId == 'region') {
                    $('#' + sTargetSelectId).parent(".select").addClass('disabled');
                }
               box.ui('form.searchTravelForm').field(sTargetSelectId).setOptions(newOptions, true); 
               box.ui('form.searchTravelForm').field(sTargetSelectId).setIndex(box.ui('form.searchTravelForm').field(sTargetSelectId).getIndex()); 
            });
    }

    box.bind({
        'change.field.select.location' : function(e) {
            if (e.source.getValue() != null) {
                changeSelect('country', this.getValue());
            }
        },
        'change.field.select.country' : function(e) {
            if (e.source.getValue() != null) {
                changeSelect('region', this.getValue());
            }
        },
        'change.field.select.nbResultPerPage' : function() {
            $('#nbResults').submit();
        }
    });
    
    
});
