$().ready(function() {
    $('#get_movies').submit(function(e) {
        $('#combobox_patch').addClass('loading');
        $('#movie_search #sbm_movie_search').hide();
        $('#movie_search .loader').show();

        e.preventDefault();
        function parseXMLResults(data) {
            rows = $(data).find('#databaseDiv0 row');
            rdata = $.map(rows, function(row,i) {
                idfilm = $('field[name=T0.IdFilm]', row).text();
                return {
                    'data': row,
                    'id': idfilm,
                    'title': $('field[name=T0.Title]', row).text(),
                    'permalink': 'cinema_list.jsp?idnick='+idfilm,
                    'year': $('field[name=T0.ProductionYear]', row).text(),
                    'country': $('field[name=T0.Country]', row).text(),
                    'theater': $('field[name=T2.Cinema]', row).text(),
                    'genre': $('field[name=T3.genreName]', row).text()
                }
            });
            return rdata;
        }

        hostname = window.location.hostname;
        url = 'http://'+hostname+'/nick/cinema_list.jsp';
        params = { xml: true }
		if ($('#input-film').val() == '') {
            url = 'http://'+hostname+'/nick/cinema_city_ac.jsp';
        }
        if ($('input[name=idFilm]').val()) {
            params.idnick = function() { return $('input[name=idFilm]').val(); }
        }
        if ($('input[name=city]').val()) {
            params.city = function() { return $('input[name=city]').val(); }
        }
        if ($('select[name=slc_theater]').val()) {
            params.cine = function() { return $('select[name=slc_theater]').val(); }
        }
        
        if (params.idnick && params.city && params.cine) {
            link = '/nick/cinema_list.jsp?idnick='+params.idnick()+'&city='+params.city();
            window.location = link;
        } else {
            $.get(url, params, function(data, status) {
                rows = parseXMLResults(data);
                $('#results tbody').html('');
            
                if ($(rows).length > 0) {
                    $(rows).each(function() {
                        link = this.permalink+'&city='+$('input[name=city]').val();
                        tr = $('<tr></tr>');
                        th = $('<th><a href="'+link+'"><span class="title">'+this.title+'</span> <span class="country">&mdash; '+this.country+' '+this.year+'</span></a></th>');
                        theater = $('<td class="theater"><a href="'+link+'">'+this.theater+'</a></td>');
                        genre = $('<td class="genre"><a href="'+link+'">'+this.genre.toUpperCase()+'</a></td>');

                        $(tr).append(th);
                        $(tr).append(theater);
                        $(tr).append(genre);

                        $('#results tbody').append(tr);
                    });
                    $('#results tbody tr').hover(function() {
                        $(this).addClass("highlight");
                    }, function() {
                        $(this).removeClass("highlight");
                    });

                    $('#results tbody tr:last').addClass('last');
                } else {
                    $('#results tbody').append('<tr><th>Nessun risultato trovato.</th></tr>');
                }
            
                $('#movie_search .loader').hide();
                $('#movie_search #sbm_movie_search').show();
                $('#combobox_patch').removeClass('loading');
                $('#results').slideDown(500, function() {
                    $('html').click(function(e) {
                        $('#results').slideUp(500, function() {
                            $('html').unbind('click');
                        });
                    });
                });
            });
        }
    });
});

