(function($){

$(document).ready(function(){
   
    $.tablesorter.addParser({ 
        id: 'waittime', 
        is: function(s) { 
            return false; 
        }, 
        format: function(s) {
            var pos = s.search(/\d/);
            if (pos == -1) {
                return 999; //empty last
            }
            return parseInt(s.charAt(pos)+s.charAt(pos+1));
        }, 
        type: 'numeric' 
    }); 

    //the radios
    $("input[name='questionary']").click(function(){
        $('#service').change();
    });
    

    //the drop downs
    $('#survey,#service').selectchain({
        data_func: function(data){  
            var options = '<option id="NOCHOICE">Välj...</option>';
            data = eval('('+data+')');
            data.services.pop();
            //a bit of an ugly hack to escape html entites
            for (var i=0; i<data.services.length; i++) {
                data.services[i].name = ''+$('<span>'+data.services[i].name.replace('&amp;', '&')+'</span>').text();
            }
            data.services.sort(function(a,b){
                if (a.name < b.name) {
                    return -1;
                }
                if (a.name > b.name) {
                    return 1;
                }
                return 0;
            });
            
            $.each(data.services,function(){
                options += '<option value="'+this.id+'">'+this.name+'</option>\n';
            });
            return options;
        
        }
    });
    $('#service').change(function(){
        var val = $(this).val();
        var survey = $('#survey').val();
        survey = survey.slice(survey.length-1);
		if (val && val != '' && val != 'NOCHOICE') {
           
            $.getJSON('/analys/json-spec.seam?services='+val,callback=function(data){
                $('#capacity-initial').css('display','none');
                    
                data.table.pop(); //last element is empty padding
                
                //check radiobutton
                var questionary = $("input[name='questionary']:checked").attr('id');
                if (questionary == 'questionary1') { //aktuellt vantetidslage
                    //hide the other table if visible
                    $('#capacity-result').css('display','none');
                    $('#capacity-nounits').css('display','none');
                    
                    //remove old table to clear things a bit
                    $('#queue-result').empty();
                    $('#queue-result').append('<table id="queue"><thead><tr><th class="header1"></th><th class="header2"></th></tr></thead><tbody></tbody></table>');
                    
                    //filter
                    var row;
                    var filtered = [];
                    for (var i=0; i<data.table.length; i++) {
                        row = data.table[i];
                        if (row[6] != '' && row[6] != 'Ingen uppgift') {
                            var url = '/se-vantetider/aktuellt-vaentetidslaege/?questionary=a&survey='+survey+'&utbud='+val+'&region='+encodeURIComponent(row[0])+'&enhet='+encodeURIComponent(row[2]);
                            filtered.push(['<a href="'+url+'">'+row[2]+'</a>','<a href="'+url+'">'+row[6]+'</a>']);
                        }      
                    }
                    if (filtered.length > 0) {
                        if (filtered.length > 10) {
                            $('#queue-result').addClass('large');
                        } else {
                            $('#queue-result').removeClass('large');
                        }
                        $('#queue-nounits').css('display','none');
                        $('#queue-result').css('display','block');
                        $('#queue').css('display','block').buildtable(filtered).tablesorter({
                            sortList: [[1,0],[0,0]],
                            headers: { 1: { sorter: 'waittime' } }
                        });
                        
                    } else {
                        $('#queue').css('display','none');
                        $('#queue-result').css('display','none');
                        $('#queue-nounits').css('display','block');
                    }
                } else {
                    //hide the other table
                    $('#queue-result').css('display','none');
                    $('#queue-nounits').css('display','none');

                    //remove old table to clear things a bit
                    $('#capacity-result').empty();
                    $('#capacity-result').append('<table id="capacity"><thead><tr><th class="header1"></th><th class="header2"></th></tr></thead><tbody></tbody></table>');

                    //filter, ledig kapacitet, 
                    var row;
                    var filtered = [];
                    for (var i=0; i<data.table.length; i++) {
                        row = data.table[i];
                        if (row[9] == 'Ja') {
                            var url = '/se-vantetider/aktuellt-vaentetidslaege/?questionary=b&survey='+survey+'&utbud='+val+'&region='+encodeURIComponent(row[0])+'&enhet='+encodeURIComponent(row[2]);    
                            filtered.push([row[0],'<a href="'+url+'">'+row[2]+'</a>']);
                        }      
                    }
                    if (filtered.length > 0) {
                        if (filtered.length > 10) {
                            $('#capacity-result').addClass('large');
                        } else {
                            $('#capacity-result').removeClass('large');
                        }
                        $('#capacity-nounits').css('display','none');
                        $('#capacity-result').css('display','block');
                        $('#capacity').css('display','block').buildtable(filtered).tablesorter({sortList:[[0,0]]}); 
                        
                    } else {
                        $('#capacity').css('display','none');
                        $('#capacity-result').css('display','none');
                        $('#capacity-nounits').css('display','block');
                    }
               }
            });
         }               
    });

});

})(jQuery)
