VYRE.BrandFilter = function ( settings ) {
    this.ajaxSearch = null;
    this.brandParent = null;
    this.brandChild = null;
    this.jsonId = null;
    if( typeof settings != "undefined" ) {
        for( var property in this) {
            if(typeof settings[property] != "undefined") {
                this[property] = settings[property];
            }
        }
    }   
    this.savedBrand = "";
    this.callBackFunctions = new VYRE.Utils.ArrayList();
    this.init();
    
};


  VYRE.BrandFilter.addEvent = function( obj, type, fn ) {
    if ( obj.attachEvent ) {
      obj['e'+type+fn] = fn;
      obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
      obj.attachEvent( 'on'+type, obj[type+fn] );
    } else
      obj.addEventListener( type, fn, false );
  }


VYRE.BrandFilter.prototype = {
    init : function () {
        var self = this;
        
        /*this.brandParent.onchange = function () {
            self.onchange();
        }*/
        
        VYRE.BrandFilter.addEvent(self.brandParent,"change",function () {
            self.onchange();
        });
        this.ajaxSearch.callBackFunctions.add(function () {
            self.dataRecieved();
        });
        this.savedBrand = this.brandChild[this.brandChild.selectedIndex].value;
        this.ajaxSearch.setResultCount(-1);
        this.ajaxSearch.loader = {
            start : function () {
                self.brandChild.innerHTML = "";
                self.brandChild.appendChild(VYRE.Utils.createElement({
                    tag:"option",
                    innerText:"Please wait..."
                }));
                self.brandChild.disabled = true;
            },
            stop: function () {
                self.brandChild.disabled = false;
                self.brandChild.innerHTML = "";
            }
        };
        if(this.savedBrand != "") {
            this.callBackFunctions.add(function () {
                self.brandChild.value = self.savedBrand;
            });
        }
        this.searchChildren();
    },
    dataRecieved : function () {
        var childBrands = JSONstring.toObject(VYRE.Utils.gE(this.jsonId).value);
        this.populateChildren(childBrands);
    },
    populateChildren : function (brands){
        var self = this;
        this.brandChild.innerHTML = "<option value = ''>Please select...</option>";
        for(var i = 0; i< brands.length; i++) {
            var option = VYRE.Utils.createElement({
                tag :"option",
                innerText:brands[i].name
            });
            option.value = brands[i].value;
            this.brandChild.appendChild(option);
        }
        self.brandChild.value= "";
        this.callBackFunctions.each(function(func, index){
           if(typeof func == "function") {  func(); }
        });
       
    },
    onchange : function (){
        this.savedBrand = "";
        this.searchChildren();
    },
    searchChildren : function () {
        var brandId = this.brandParent[this.brandParent.selectedIndex].value;
        this.ajaxSearch.attributes.set(new VYRE.Attribute("ITEM_LINK_DEF20",brandId));
        this.ajaxSearch.search();
    }
};


