/**
 * @author shrestha
 */

PHILIPS.namespace('PHILIPS.ui'); 

PHILIPS.ui.filter = new Class({
    Implements: [Options, Events],
    options: {
		IDS:{
			scrollbar: 'scrollbar_wrapper',
			scrollpane: 'product_content',
			scrollHandle: 'handle2',
			noResult: 'no_result'
		},
		CLASSES:{
			seeAll: 'see_all'
		},
		CONSTS:{
			maxnum: 5
		}
    },
    initialize: function(options) {
        this.setOptions(options);
		this._findElements();
		this._attachListeners();
		this.filterItems();
		this.resizeFilters();
		
    },
	
		//----------------------------------------------------------------------
		// PROPERTIES
		//----------------------------------------------------------------------
		filters : [],
		products: [],
		productSlide: [],
		midFilter:[],
		inProgress:0,
		
		//----------------------------------------------------------------------
		// PRIVATE METHODS
		//----------------------------------------------------------------------
		
	_attachListeners: function() {
		this.filters.each(function(el, index) {
			el.getElements('input').each(function(elm, index){elm.addEvent('click', this.filterItems.bindWithEvent(this, index));}, this);
		}, this);
		$(document.body).getElements('.'+this.options.CLASSES.seeAll).addEvent('click', this.showAllItems.bindWithEvent(this));
	},
		
	_findElements: function() {
		this._populateFilters();
		this.products = $$('#product_list .product-item');
		for(z=0; z<this.products.length; z++){
			this.productSlide[z] = new Fx.Slide(this.products[z], {mode: 'horizontal'});
			this.midFilter[z] = 1;		
		}
		
		
		
	},
	_populateFilters: function() {
		this.filters = $$('#product_filters li.filter_cols');
	},

	_releaseWait: function(){
		this.inProgress = 0;
	},	

	_filterThroughItems: function(item){
		
		var filterItemList = []; 
		var allowedClasses = [];
		
		filterItemList = item.getElements('li.filter_item');
		
		
		// Only filter if at least one of the items is checked
		if(this._atLeastOneChecked(filterItemList)){
			for(j=0; j<filterItemList.length; j++){
				var className = '';
				// If it is checked
				if(filterItemList[j].getElement('input').checked){
					className = filterItemList[j].getElement('input').get('value');
					
					allowedClasses.push(className);
				}
			}	
			this._removeProducts(allowedClasses);
			
		}
	},
	_atLeastOneChecked: function(inputs){
		// Checking if at least one of the checkboxes in this filter category is checked
		var checkForChecked = false;
		for (k = 0; k < inputs.length; k++) {
			if (inputs[k].getElement('input').checked) {
				checkForChecked = true;
			}
		}
		
		return checkForChecked;
	},
	_removeProducts: function(allowedClasses){
		var count = 0;
		for(l = 0; l < this.products.length; l++){
			var checkIfClassExists = false;
			for(m = 0; m < allowedClasses.length; m++){
				if(this.products[l].hasClass(allowedClasses[m])){
					checkIfClassExists = true;
					
				}
			}
			
			if(!checkIfClassExists){
				this.midFilter[l] = 0;
			}else{
				count++;
			}
			
		}
		
		
		
	},
	_showAll: function(){
		for (n = 0; n < this.products.length; n++) {
			//this.products[n].show();
			this.midFilter[n]=1;
			this._hideShowItems();
		}
		if (this.products.length > this.options.CONSTS.maxnum){
			$(this.options.IDS.scrollbar).setStyle('opacity', 1);
		}
	},
	_resetScrollbar: function(){
		$(this.options.IDS.scrollpane).scrollTo(0,0);
		$(this.options.IDS.scrollHandle).setStyle('left',0);
		 
	},
	_hideShowItems: function(){
		
		var count = 0;
		for(p=0; p<this.products.length; p++){
			if(this.midFilter[p]==1){
				this.productSlide[p].slideIn();
				count++;
			}else{
				this.productSlide[p].slideOut();
			}
		}
		if (count < this.options.CONSTS.maxnum){
			

			$(this.options.IDS.scrollbar).setStyle('opacity', 0);
			
		}else{
			
			$(this.options.IDS.scrollbar).setStyle('opacity', 1);
			this._resetScrollbar();
		}
		if(count < 1){
			$(this.options.IDS.noResult).show();
		}else{
			$(this.options.IDS.noResult).hide();
		}
	},
	_seeAllButton:function(check){
		var button = $$('.'+ this.options.CLASSES.seeAll + ' span');
		if(check == 1){
			$$('.'+this.options.CLASSES.seeAll+ ' span').setStyle('background-position', '0pt -50px');
		}else{
			$$('.'+this.options.CLASSES.seeAll+ ' span').setStyle('background-position', '0pt 0px');
		}
	},
	_doFiltering: function(){
		this._seeAllButton(0);
			this._populateFilters();
			
			for (n = 0; n < this.products.length; n++) {
				this.midFilter[n] = 1;
			}
			
			for (i = 0; i < this.filters.length; i++) {
			
				this._filterThroughItems(this.filters[i]);
				
			}
			
			this._hideShowItems();
			setTimeout("makeScrollbar()", 500);
			setTimeout(function() {
				if (window['filter']) {
					window['filter']._releaseWait();
				}
			}, 500);
			
	},
	
	_setSeeAll: function(){
		var seeAllCheck = true; 
		for (i = 0; i < this.filters.length; i++) {
			var filterItemList = [];	
			var item = this.filters[i];
			filterItemList = item.getElements('li.filter_item');
			for(r=0; r<filterItemList.length; r++){
				if(filterItemList[r].getElement('input').checked == true){
					seeAllCheck = false;
				}
				
			}		
		}
		if(seeAllCheck){
			this._seeAllButton(1);
		}
	},
	_checkNoResults: function(){
		
	},
		//----------------------------------------------------------------------
		// EVENT HANDLERS
		//----------------------------------------------------------------------
	filterItems: function() {
		if(this.inProgress == 1){
			
			return false;
		}
		
		this.inProgress = 1;
		this._doFiltering();
		this._setSeeAll();
		this._checkNoResults();
		
	},
	showAllItems: function(){

		this.filters.each(function(el, index){
			el.getElements('input').each(function(elm, index){elm.checked=false;}, this);
		}, this);	
		
		this.filterItems();
		this._seeAllButton(1);
	},
	resizeFilters: function(){
		
		maxHeight = 0;
		for(i=0;i<this.products.length;i++){
			
			//Replaceing BRs with commas
			var summary = this.products[i].getElements('.summary')[0].get('html');
			summary = summary.replace(/<br>/gi, ', ');
			
			this.products[i].getElements('.summary')[0].set('html', summary);
			newHeight = this.products[i].getElements('.product_summary')[0].offsetHeight;
			if(newHeight > maxHeight){
				maxHeight = newHeight;
			}
		}
		for (i = 0; i < this.products.length; i++) {
			this.products[i].getElements('.product_summary')[0].setStyle('height', maxHeight + 'px');
		}
	},
	
		//----------------------------------------------------------------------
		// GETTER SETTERS
		//----------------------------------------------------------------------
	setSomeVariable: function() {
	
	},
	getSomeVariable: function() {
	
	}
	
});