

var ClassLinker = new Class.create();
ClassLinker.prototype = {
	initialize: function(){
		Element.observe(window, 'load', this.makeLinks.bind(this));
	},
	makeLinks: function() {
		var elements = $$('.make-link');
		elements.each(function(e){
			if (! e.readAttribute('rel')) return;
			Event.observe(e, 'click', this.followLink.bind(this));
		}.bind(this));
	},
	followLink: function(event) {
		var target = $(Event.element(event)).readAttribute('rel');
		if (target) window.location.href = target;
	}
};
classLinker = new ClassLinker();

var FilterButtons = new Class.create();
FilterButtons.prototype = {
	initialize: function(path) {
		this.buttons = [ ];
		this.path = path;
	},
	addButton: function(id, state, hasItems) {
		this.buttons[this.buttons.length] = new FilterButton(id, state, hasItems, this.path);
	}
};

var FilterButton = new Class.create();
FilterButton.prototype = {
	initialize: function(id, state, hasItems, path) {
		this.images = { 'on': new Image(), 'off': new Image(), 'state': state, 'hasItems' : hasItems },
		this.images.on.src = path + '/icon-' + id + '-small-on.gif';
		this.images.off.src = path + '/icon-' + id + '-small-off.gif';
		Element.observe($('search-btn-' + id), 'mouseover', this.over.bind(this));
		Element.observe($('search-btn-' + id), 'mouseout', this.out.bind(this));
		if (state) this.swapon($('search-btn-' + id));
	},
	over: function(event) {
		if (! this.images.hasItems) return;
		this.swapon($(Event.element(event)));
	},
	out: function(event) {
		if (this.images.state) return;
		this.swapoff($(Event.element(event)));
	},
	swapon: function(img) {
		img.src = this.images.on.src;
	},
	swapoff: function(img) {
		img.src = this.images.off.src;
	}
};

var KlabauterSearch = Class.create(Varien.searchForm, {
	// override ajax call to add the category id
    initAutocomplete: function(url, destinationElement, category){
        new Ajax.Autocompleter(
            this.field,
            destinationElement,
            url,
            {
                paramName: this.field.name,
                minChars: 2,
                updateElement: this._selectAutocompleteItem.bind(this),
                parameters: $(category).name + '=' + $(category).value
            }
        );
    }
});


