
jQuery.noConflict();
jQuery(document).ready(function($){
    var DOMAIN = '';
    var nbr_loc_main    = $('#nbr_filter_loc');
    var nbr_loc_handler = function () {
        var loc_id = $('#'+this.id+' option:selected').attr('value');
        var index  = parseInt($('#'+this.id).attr('index'));
        var url    = DOMAIN+'index.php?option=com_nbreal&ct=requests&req=LocationList&format=raw';
        
        //posts data to the php script to get location info
        $.getJSON(url,{
            id : loc_id,
            index : index
        },function( data ){

            //removes "this" index number div and all divs with indexes above
            i = index + 1;
            while ( $('#nbr_loc_div_' + i ).length > 0 ) {
                if((index+1) == i) $('#nbr_loc_div_' + i ).remove();
                $('#nbr_loc_div_' + i ).hide('fast', function () {
                    $(this).remove();
                });
                i++;
            }
            //if the previous location add children
            if( data.locations && data.locations.length > 0 ) {
                //generates the select box
                var selectBox = '<div id="nbr_loc_div_' + ( index + 1 ) + '" style="display:none;">';
                if(data.label) selectBox = selectBox + '<label for="nbr_loc_select_' + ( index + 1 ) + '">' + data.label + ':</label><br />'
                selectBox = selectBox + '<select id="nbr_loc_select_' + ( index + 1 ) + '" name="nbr_filter[loc_select][' + ( index + 1 ) + ']" index="' + ( index + 1 ) + '" >';
                selectBox = selectBox + '<option></option>';
                $.each(data.locations, function(i) {
                    if(data.locations[i].id != "undefined") cur_id = data.locations[i].id; else cur_id = data.locations[i].iso;
                    selectBox = selectBox + '<option value="'+cur_id+'">'+data.locations[i].title+'</option>';
                });
                selectBox = selectBox + '</select></div>';
                nbr_loc_main.append( selectBox );
                $('#nbr_loc_select_' + ( index + 1 ) ).change( nbr_loc_handler );
                $('#nbr_loc_div_' + ( index + 1 ) ).show('fast');
            }
        });
    }


    //sets starting index
    if($('#nbr_loc_select_0').length>0) {
        startIndex = 0;
    } else {
        startIndex = 1;
    }

    //add the event function to all combo boxes
    while ( $('#nbr_loc_select_' + startIndex ).length > 0 ) {
        $('#nbr_loc_select_' + startIndex ).change( nbr_loc_handler );
        startIndex++;
    }

    //deletes filter cookie 
    $('.nbr_filter_reset').click(function() {
        document.cookie = 'nbr_filter_cookie'+"="+""+''+"; path=/";
        location.href = DOMAIN+'index.php?option=com_nbreal';
    });


    //sets slider params
    if($(".nbr_slider").length>0) {
        $(".nbr_slider").cycle({
            fx:      "scrollDown",
            speed:    500,
            timeout:  5000
        });
    }

    var nbr_info_handler = function (e) {
        e.preventDefault();
        //ajax request to object info request
        var url        = DOMAIN+'index.php?option=com_nbreal&ct=requests&req=InfoRequest&format=raw';
        var dataString = $("form#nbr_contact").serialize(); 

        //sends form data and gets json back
        $.ajax({
            type: "POST",
            url: url,
            data: dataString,
            dataType: "json",
            success: function(info){
                var contentor = $('#nbr_contact_contentor');
                var form = contentor.html();
                contentor.html(info.msg);
                contentor.append('<p><a href="javascript:;" id="nbr_loadform">Back</a></p>');
                $('#nbr_loadform').click(function () {
                    contentor.html(form);
                    $('#nbr_contact').submit(nbr_info_handler);
                });
            }
        });
        
    }   

    $('#nbr_contact').submit(nbr_info_handler);

});

