/* Released: 2010-03-31 12:16:36 */

/**
 * @author stec 
 */


var $gJQ = $gJQ || jQuery.noConflict();

/**
 * @class CategoryFilter
 * @example new CategoryFilter();
 */
function CategoryFilter(settings) {
	this.settings = $gJQ.extend(this.settings, settings);
	this.init();
}

CategoryFilter.prototype = {
	/**
	 * private
	 * @config id selector of GO button and SELECT field 
	 */
	settings : {
		goBtn : "#go-category",
		categorySelect : "#filterCategoryValue"
	},
	
	init : function() {
		var me = this;
		$gJQ(this.settings.goBtn).click(function(e){
			e.preventDefault();
			me.doFilter();
		});
	},
	
	doFilter : function() {
		var v = $gJQ(this.settings.categorySelect).val();
		this._toggleCategories(v);
	},
	
	_toggleCategories : function(category) {
		if(category) {
			$gJQ(".comparison-action-comp[id^=category-]").not("#category-" + category).fadeTo(0, 0).hide();
			$gJQ("#category-" + category).fadeTo(250, 1).show();		
			return;
		}
		//otherwise enable all of them
		$gJQ(".comparison-action-comp[id^=category-]:hidden").fadeTo(250, 1).show();
	}
};

/**
 * @class PriceFilter
 */
var PriceFilter = function(settings){
	this.settings = $gJQ.extend(this.settings, settings);
	this.init();
}


PriceFilter.prototype = {
	
	/**
	 * private 
	 */
	settings : {
		btnGo : "#go-pricerange",
		selectMin : "#filterMinValue",
		selectMax : "#filterMaxValue",
		minValue : 0,
		maxValue : 0,
		stepSize : 1,
		onSucces : function(){},
		onError : function(){}
	},
	
	init : function() {
		var me = this;
		
		me._populatePriceBoxes();
		
		//execute callback function when user clicks on GO
		$gJQ(this.settings.btnGo).click(function(e){
			e.preventDefault();
			me.doFilter();
		});
		
		$gJQ(this.settings.selectMin).change(function(){me._checkConflictMinSelect(me)});
		$gJQ(this.settings.selectMax).change(function(){me._checkConflictMaxSelect(me)});

	},
	
	doFilter : function() {
		var me = this;
		if (me.isValid()){
			me.settings.onSucces();
			return;
		}
		me.settings.onError();
	},
	
	reset : function(settings) {
		this.settings = $gJQ.extend(this.settings, settings);
		this._populatePriceBoxes();
	},
	
	isValid : function() {
		return !( parseInt($gJQ(this.settings.selectMax).val()) < parseInt($gJQ(this.settings.selectMin).val()) );
	},
	
	getPriceRange : function() {
		return [parseInt($gJQ(this.settings.selectMin).val()), parseInt($gJQ(this.settings.selectMax).val())];
	},
	
	getPriceLabels : function() {
		return [
			$gJQ.trim( $gJQ(this.settings.selectMin).parent("label").clone().find("select").remove("select").end().text() ),
			$gJQ.trim( $gJQ(this.settings.selectMax).parent("label").clone().find("select").remove("select").end().text() )
		];
	},
	
	_populatePriceBoxes : function() {
		var arrayOptions = [];
		
		if (!(parseInt(this.settings.maxValue) > parseInt(this.settings.minValue))) {
			return;
		}
		
		//get stepSize
		this.settings.stepSize = this._getStepSize(this.settings.maxValue);
		
		//fill selectboxes		
		var i = this.settings.minValue;
		
		while (i <= (this.settings.maxValue+this.settings.stepSize)) {
			arrayOptions.push('<option value="' + i + '">' + i + '</option>');
			i = i + this.settings.stepSize;
		}
		
		$gJQ(this.settings.selectMin).empty().html(arrayOptions.join());
		$gJQ(this.settings.selectMax).empty().html(arrayOptions.reverse().join());
		
		this._setDefaultSelectedState();
	},
	
	//default select loweset/highest price
	_setDefaultSelectedState : function() {
		try {
			this._resetMinSelectBox();
			this._resetMaxSelectBox();
		} catch(e) {
			// Catch and ignore IE6 bug that occurs when selecting an option
		}
	},
	
	_resetMinSelectBox : function() {
		try {
			$gJQ(this.settings.selectMin +' option:first').attr('selected', 'selected');
		} catch(e) {
			// Catch and ignore IE6 bug that occurs when selecting an option
		}
	},
	
	_resetMaxSelectBox : function() {
		try {
			$gJQ(this.settings.selectMax +' option:first').attr('selected', 'selected');
		} catch(e) {
			// Catch and ignore IE6 bug that occurs when selecting an option
		}
	},
	
	_checkConflictMinSelect : function(me) {
		var range = me.getPriceRange();
		if (parseInt(range[0]) > parseInt(range[1])) {
			me._resetMaxSelectBox();
		}
	},
	
	_checkConflictMaxSelect : function(me) {
		var range = me.getPriceRange();
		if (parseInt(range[0]) > parseInt(range[1])) {
			me._resetMinSelectBox();
		}
	},
	
	_getStepSize : function(v) {
		var step = 1;
		var v = parseFloat(v);
		if(v && v > 0.00) {
			if (v < 50.00) {
				step = 5;
				v += step - v%step;
			} else if (v >= 50.00 && v < 200.00) {
				step = 10;
				v += step - v%step;
			} else if (v >= 200.00) {
				step = 50;
				v += step - v%step;
			} else {
				throw 'NotReached';
			}
		}
		return step;
	}
}

/**
 * @class AccessoiresPndFilter
 * @example new AccessoiresPndFilter();
 */
function AccessoiresPndFilter(settings) {
	this.settings = $gJQ.extend(this.settings, settings);
	this.init();
}

AccessoiresPndFilter.prototype = {
	/**
	 * private
	 * @config goBtn selector for GO button 
	 * @config selectBox selector for SELECT field
	 */
	settings : {
		goBtn : "#accessories-pnd-filter button",
		selectBox : "#accessories-pnd-filter select"
	},
	
	_productCache : [],
	
	init : function() {
		var me = this;
		$gJQ(this.settings.goBtn).click(function(e){
			e.preventDefault();
			if (me._productCache.length == 0) {
				me._productCache = $gJQ(".product-comp");
			}
			me.filter();
		});
	},
	
	filter : function() {
		var v = $gJQ(this.settings.selectBox).val();
		this._toggleAccessoires(v);
	},
	
	_toggleAccessoires : function(pndSku) {
		var me = this;
		var lines = $gJQ(".comparison-action-comp .line").not(".dark-header"); 
		
		//look for accessoires to fill
		var compatAcc = (pndSku)
			? $gJQ(me._productCache).filter("[rev~='"+pndSku+"']")
			: $gJQ(me._productCache);
		
		//clear the lines
		$gJQ(lines).empty();
		
		//fill the lines
		$gJQ(compatAcc).each(function(i, n){
			$gJQ(n).appendTo($gJQ(lines[(parseInt(i/4))]))
		});
	}
};
