$(document).ready(function(){

    $("table tr:even").addClass("striped");

    $("table tr.striped.genderM").addClass("striped-m");
    $("table tr.striped.genderF").addClass("striped-f");
    
    $("table.results").tableSorter({
      sortClassAsc: 'headerSortUp',
      sortClassDesc: 'headerSortDown',
      headerClass: 'header'
    });

});

function enableLinks() {
    $("table.results a").click(function(){
        window.open(this.href,'runner_detail',"location=no,menubar=no,status=no,width=400,height=300");
        return false;
    });
}

var searchForm = {

    f:null,
    af:null,
    toggle:null,
    modeflag:null,
    
    load:function(){
    
        // do stuff here that should occur before page load is finished:
        //alert(this.urlMode());
        
        // once page loads, initialize the form actions:
        var self = this;
        $(document).ready(function(){self.init();});
    
    },
    
    init:function(){

        var self = this;
        
        this.f = $("form.search.basic");
        this.af = $("form.search.advanced");
        
        if (!this.af.size()) return; // exit early if the advanced form doesn't exist
                
        this.f.append('<input type="hidden" name="mode" value="basic" />');        
        this.af.append('<input type="hidden" name="mode" value="advanced" />');
        this.modeflag = $("input[@name='mode']",this.f);

        this.af.css('display','none');
                
        $($("fieldset").get(0)).append(' <small>( <a href="?mode=advanced">advanced search</a> )</small>');
        this.toggle = $("small a",this.f).get(0);
        $(this.toggle).click(function(){
            self.toggleAdvanced();
            return false;
        });
        
        if (this.urlMode()=='advanced') this.showAdvanced();
        
        this.defaults();
        
    },
    
    defaults:function(){

        var f_name = $("form.basic input[@name=name]");
        var f_location = $("form.basic input[@name=location]");
        
        if (f_name.attr('value') == 'everyone') f_name.css('color','#888888');
        if (f_location.attr('value') == 'everywhere') f_location.css('color','#888888');
        
        f_name.focus(function(){
            if (this.value=='everyone') this.value = '';
            this.style.color = '#000000';
        });
        f_name.blur(function(){
            if (this.value=='') {
                this.value = 'everyone';
                this.style.color = '#888888';
            }
        });
        
        f_location.focus(function(){
            if (this.value=='everywhere') this.value = '';
            this.style.color = '#000000';
        });
        f_location.blur(function(){
            if (this.value=='') {
                this.value = 'everywhere';
                this.style.color = '#888888';
            }
        });
    },
    
    toggleAdvanced:function(){

        var mode = this.urlMode($(this.toggle).attr('href'));
        
        if (mode=='advanced') {
            this.showAdvanced();
        } else {
            this.hideAdvanced();
        }
        
        return false;
    },
    
    showAdvanced:function(){
    
        this.af.slideDown();
        $(this.toggle).attr('href','?mode=basic');
        $(this.toggle).empty().append('basic search');
        $(this.modeflag).attr('value','advanced');
        
        $("select,input",this.f).attr('disabled','disabled');
        $("select,input",this.f).attr('readonly','readonly');
        this.f.css('color','#999');

        return false;
    
    },
    
    hideAdvanced:function(){

        this.af.slideUp();
        $(this.toggle).attr('href','?mode=advanced');
        $(this.toggle).empty().append('advanced search');
        $(this.modeflag).attr('value','basic');

        $("select,input",this.f).removeAttr('disabled');
        $("select,input",this.f).removeAttr('readonly');
        this.f.css('color','#000');
        
        return false;
        
    },
    
    urlMode:function(url){
        var arg = 'mode';
        var regexS  = "[\\?&]"+arg+"=([^&#]*)";
        var regex   = new RegExp(regexS);
        var url     = (url) ? url : window.location.href;
        var results = regex.exec(url);
        return (results==null) ? "" : results[1];
    }

}

searchForm.load();


