/**
 * Gadget Filter Class
 * @desc Filters the selected gadgets
 * @author Doug Hill doug@zagg.com
 * @since 04.28.2010
 */
var Filter = Class.create({
	initialize: function(des){
		if(des){
			this.des = des;
		}
		else{
			this.des = false;
		}
		this.cat_box = $('zs_category');
		this.brand_box = $('zs_brand');
		this.model_box = $('zs_model');
		this.getInitialData();
		this.setObserves();
	},
	getInitialData: function(){
		this.updateDrop(this.makeRequest('category', false, false, false), this.cat_box);
		this.updateDrop(this.makeRequest('brand', false, false, false), this.brand_box);
		this.updateDrop(this.makeRequest('gadget', false, false, false), this.model_box);
	},
	makeRequest: function( query, category, brand, gadget ){
		var add_string = "?";
		
		if(category != false){
			add_string += "&category="+category;
		}
		if(brand != false){
			add_string += "&brand="+brand;
		}
		if(gadget != false){
			add_string += "&gadget="+gadget;
		}
		if(add_string != '?'){
			var end = '&c=json';
		}
		else{
			var end = '?c=json';
		}
		var url = query+add_string.substring(1)+end;
		new Ajax.Request('/personalize/zaggskins/includes/ajax/gadget_filter.php', {
			method: 'post',
			parameters: {
				query_string: url
			},
		    asynchronous: false,
			onComplete: function(e) {
			   result = e.responseText.evalJSON(true);
			}
		});
		return result;
	},
	updateDrop: function(val, select, sel){
		var def = new Element('option', {value: 'all'}).update('All');
		select.update();
		select.insert({bottom:def});
		val.data.each(function(ele){
			if(sel && sel == ele.id){
				var option = new Element('option', {value: ele.id, selected: 'selected'});
			}
			else{
				var option = new Element('option', {value: ele.id });
			}
			if(ele.displayName){
				option.update(ele.displayName.htmlEntities());
			}
			else{
				option.update(ele.name.htmlEntities());
			}
			select.insert({bottom:option});
		});
	},
	setObserves: function(){
		this.cat_box.observe('change', function(){
			if(this.cat_box.value == 'all' || this.cat_box.value == '0'){
				this.getInitialData();
				this.cat_box.value = '0';
				this.brand_box.value = '0';
				this.model_box.value = '0';
			}
			else{
				this.cat = this.cat_box.value;
				this.updateDrop(this.makeRequest('brand', this.cat, false, false), this.brand_box);
				this.updateDrop(this.makeRequest('gadget', this.cat, false, false), this.model_box);
			}
		}.bind(this));
		
		this.brand_box.observe('change', function(){
			if(this.brand_box.value == 'all' || this.brand_box.value == '0'){
				this.getInitialData();
				this.cat_box.value = '0';
				this.brand_box.value = '0';
				this.model_box.value = '0';
			}
			else{
				this.brand = this.brand_box.value;
				if(this.cat){
					this.updateDrop(this.makeRequest('gadget', this.cat, this.brand, false), this.model_box);
				}
				else{
					this.updateDrop(this.makeRequest('gadget', false, this.brand, false), this.model_box);
				}
				this.updateDrop(this.makeRequest('category', false, this.brand, false), this.cat_box, this.cat);
			}
		}.bind(this));
		
		this.model_box.observe('change', function(){
			if(this.model_box.value == 'all' || this.model_box.value == '0'){
				this.getInitialData();
				this.cat_box.value = '0';
				this.brand_box.value = '0';
				this.model_box.value = '0';
			}
			else{
				//set session
				var json = new SetSession(this.model_box.value);
				this.switchContent(json.json);
			}
		}.bind(this));
	},
	switchContent: function(json){
		buttons = new BuildButtons(this.des, json);
	}
});
