var SearchForm = Class.extend({
  init:           function(subTypes, features){
                    this.subTypes     = subTypes;
                    this.features     = features;
                    var obj           = this;

                    $('#search-area-wrapper input.tenureType').live('click', function(){ obj.updateSubtypes(); obj.updateFeatures();});
                    $('#search-area-wrapper select.subtypes').live('change', function(){obj.selectSubType(), obj.updateFeatures()});                    
                    $('#search-area-wrapper input.subtype').live('click', this.subtypeControl);
                    $('#search-area-wrapper input.feature').live('click', this.featuresControl);                    
                  },
  getTenure:      function(){
                    return $('#search-area-wrapper input.tenureType:checked').val();
                  },
  getSubType:     function(){
                    return $('#search-area-wrapper select.subtypes').val();
                  },
  getSubtypes:    function(tenureType){
                    if(this.subTypes[tenureType])
                      return this.subTypes[tenureType];
                                        
                    return [];
                  },                                    
  getFeatures:    function(searchType, tenureType, subType){
                    if(typeof subType != 'undefined') {
                      if(this.features.hasOwnProperty(subType))
                        return this.features[subType];                    
                    }
                    
                    if(typeof tenureType != 'undefined' || typeof subType != 'undefined') {
                      if(this.features.hasOwnProperty(tenureType))
                        if(this.features[tenureType].hasOwnProperty(subType))
                          return this.features[tenureType][subType];                        
                      
                    }
                    
                    if(typeof tenureType != 'undefined') {
                      if(this.features.hasOwnProperty(tenureType))
                        return this.features[tenureType];
                    }  
                    return [];
                  },
  selectSubType:  function(){
                    var value = $('#search-area-wrapper select.subtypes').val();
                    if(value == '') {
                      $('#search-area-wrapper ul.subtypes input').attr('checked', false); 
                      $('#search-area-wrapper ul.subtypes input.subtype.all').attr('checked', true);
                    } else {                      
                      $('#search-area-wrapper ul.subtypes input').attr('checked', false); 
                      $('#search-area-wrapper ul.subtypes input#'+ value).attr('checked', true); 
                      $('#search-area-wrapper ul.subtypes input.subtype.all').attr('checked', false);                      
                    }
                    $('#search-area-wrapper ul.subtypes input').uniform();                    
                  },                  
  subtypeControl:  function(){
                      if($(this).hasClass('all')){
                        if(!$(this).attr('checked'))
                          $(this).attr('checked', true);
                        else
                          $('#search-area-wrapper input.subtype[value!=""]').attr('checked', false);
                      } else {
                        if(!$('#search-area-wrapper input.subtype:checked').length)
                          $('#search-area-wrapper input.subtype.all').attr('checked', true);
                        else
                          $('#search-area-wrapper input.subtype.all').attr('checked', false);
                      }
                      
                      var selectedValue = $(this).val();

                      $('#search-area-wrapper .first-row select.subtypes').val(selectedValue);
                      $.uniform.update('#search-area-wrapper select.subtypes');                      
                      $('#search-area-wrapper input.subtype').uniform();
                    },
  featuresControl:  function(){
                      if($(this).hasClass('all')){
                        if(!$(this).attr('checked'))
                          $(this).attr('checked', true);
                        else
                          $('#search-area-wrapper input.feature[value!=""]').attr('checked', false);
                      } else {
                        if(!$('#search-area-wrapper input.feature:checked').length)
                          $('#search-area-wrapper input.feature.all').attr('checked', true);
                        else
                          $('#search-area-wrapper input.feature.all').attr('checked', false);
                      }
                      $('#search-area-wrapper .second-row ul.features input').uniform();
                    },
  updateFeatures: function() {                    
                    var subType     = this.getSubType();          
                    var tenureType  = this.getTenure();                              
                    var features    = this.getFeatures(this.searchType, tenureType, subType);

                    if(!$.isEmptyObject(features)) {
                      var li  = '<li>'+
                                 '<input type="checkbox" class="feature all" value="" checked />All'+
                                '</li>';

                      $.each(features, function(val, i) {
                         li += '<li>'+
                                    '<label for='+ val +'>'+ i +'</label>'+ 
                                    '<input id="'+ val +'"class="feature" type="checkbox" name="' + val +'" value="'+ val +'"/>'+
                                  '</li>';

                      });

                      $('#search-area-wrapper .second-row ul.features').html(li);  
                      $('#search-area-wrapper .second-row ul.features input').uniform();
                      $('#search-area-wrapper .second-row div.property-features').show();                        
                    }
                  },
  updateSubtypes: function() {  
                    var tenureType  = this.getTenure();                                  
                    var subTypes    = this.getSubtypes(tenureType);

                    if(!$.isEmptyObject(subTypes)) {
                      var option  = '<option value="">All</option>';

                      $.each(subTypes, function(val, i) {
                         option += '<option value="'+ val +'">'+ i + '</option>';

                      });
                      
                      $('#search-area-wrapper .first-row select.subtypes').html(option);  

                      var li  = '<li>'+
                                 '<input type="checkbox" class="subtype all" value="" checked />All'+
                                '</li>';

                      $.each(subTypes, function(val, i) {
                         li += '<li>'+
                                    '<label for='+ val +'>'+ i +'</label>'+ 
                                    '<input id="'+ val + '"class="subtype" type="checkbox" name="a5[]" value="'+ val +'"/>'+
                                  '</li>';

                      }); 

                      $('#search-area-wrapper .second-row ul.subtypes').html(li);
                      $('#search-area-wrapper .second-row ul.subtypes input').uniform();
                    }
                  }                                                 
});


