var element_class = new Class({
	Implements: [Chain, Options],
	/* standart egenskap */
	options: {
		__produkt_obj					: null,
		__produkt_id					: '',
		__produkt_page_id				: '',
		__status 						: true // hvis element synlig
	},  
	/* -- constructor -- */
	initialize: function($options) {		
		this.setOptions($options);				
	},
	setStatus: function($status){
		if ($status == this.options.__status) return;
		if (this.options.__produkt_obj == null) this.options.__produkt_obj = $('produkt_list_th_'+this.options.__produkt_page_id+'_'+this.options.__produkt_id);
		if ($status) this.options.__produkt_obj.setStyle('display', 'block');
		else this.options.__produkt_obj.setStyle('display', 'none');
		this.options.__status = $status;		
	}
	
	
});

var one_filter_class = new Class({
	Implements: [Chain, Options],
	/* standart egenskap */
	options: {		
		__name					: '',
		__group_id				: 0,
		__group_elem			: null,
		__group_remove_elem		: null,
		__remove_cont_id		: '',
		__remove_callback		: null,
		__filter_elem 			: new Array(),		
		__elements				: new Array(),
		__status				: 0, // 0 - synlig, 1 - usynlig men ikke valgt, 2 - valgt
		__new_status			: 0
	},  
	/* -- constructor -- */
	initialize: function($options) {		
		this.setOptions($options);						
	},
	addElement: function ($params){
		this.options.__elements.push(new element_class($params));
	},
	filter_status: function($status){
		this.options.__new_status = $status;
	},	
	filter_check: function($filter_number){
		/*if ($filter_number > 0 && this.options.__status == this.options.__new_status && this.options.__status == false){
			for (var i = 0; i < this.options.__elements.length; i++){
				this.options.__elements[i].setStatus(false);
			}				
		}
		if ($filter_number == 0){
			for (var i = 0; i < this.options.__elements.length; i++){
				this.options.__elements[i].setStatus(true);
			}				
		} */
		if (this.options.__status == this.options.__new_status) return false;		
		var element_status = true;
		if (this.options.__new_status == 1) element_status = false;		
		for (var i = 0; i < this.options.__elements.length; i++){		
			this.options.__elements[i].setStatus(element_status);
		}					
		if (this.options.__status == 2 || this.options.__new_status == 2) this.fix_html();	
		this.options.__status = this.options.__new_status;			
		return true
	},
	fix_html: function (){	
		if (this.options.__new_status == 2){
			if (!this.options.__group_elem) this.options.__group_elem = $('group_filter_'+this.options.__group_id);
			if (!this.options.__group_remove_elem) {
				this.options.__group_remove_elem = new Element('div', {html:this.options.__name, id:'filter_text_remove_'+this.options.__group_id, styles : {'background-position':'3px 3px', 'padding-left':'15px'}})		
				this.options.__group_remove_elem.inject($(this.options.__remove_cont_id));
				/*this.options.__group_remove_elem.addEvent('click', 
					this.options.__remove_callback.create({
						arguments : [{'obj':null, 'group_id':this.options.__group_id}]					
					})
				);*/
				this.options.__group_remove_elem.addEvent('click', 
					remove_group_filter_clicked.create({
						arguments : [{'obj':null, 'group_id':this.options.__group_id}]					
					})
				);
				this.options.__group_remove_elem.addEvent('mouseover', function(){
					this.elem.setStyles({'color':'#ffffff', 'background-color':'#c76506'});
				}.bind({'elem': this.options.__group_remove_elem}));
				this.options.__group_remove_elem.addEvent('mouseout', function(){
					this.elem.setStyles({'color':'#c76506', 'background-color':'#fcfcfc'});
				}.bind({'elem': this.options.__group_remove_elem}));							
				this.options.__group_remove_elem.addClass('filter_text');
				this.options.__group_remove_elem.addClass('filter_selected');		
			}
			this.options.__group_remove_elem.setStyle('display', 'block');
			this.options.__group_elem.setStyle('display', 'none');			
		} else {			
			if (this.options.__group_remove_elem) {				
				this.options.__group_remove_elem.setStyle('display', 'none');			
			}
			this.options.__group_elem.getElement('input').erase('checked');
			this.options.__group_elem.setStyle('display', 'block');			
		}
	}
});

var filters_class = new Class({
	Implements: [Chain, Options],
	/* standart egenskap */
	options: {
		__type					: 1, // 1- same som finn.no has, 2- not implemented yet
		__filters_array 	: {},		
		__filters_array_n	: 0,
		__applied_filters	: {},
		__applied_filters_n : 0,
		__remove_cont_id	: 'group_filter_selected',
		__remove_cont_elem	: null,
		__remove_cont_lab	: null
	},  
	/* -- constructor -- */
	initialize: function($options, $raw_filter) {		
		this.setOptions($options);						
		for (var i = 0; i < $raw_filter.length; i ++){
			var produkt_id = $raw_filter[i][0];
			var produkt_page_id = $raw_filter[i][1];
			//for (var j = 0; j < $raw_filter[i][2].length; j ++){
				/*var group_id = $raw_filter[i][2][j][0];				
				var group_name = $raw_filter[i][2][j][1];*/
				var group_id = $raw_filter[i][2][0];				
				var group_name = $raw_filter[i][2][1];				
				if (!this.options.__filters_array[group_id]) {
					this.options.__filters_array[group_id] = new one_filter_class({'__name':group_name, '__group_id':group_id, '__remove_cont_id':this.options.__remove_cont_id});
					this.options.__filters_array_n ++;
				}
				this.options.__filters_array[group_id].addElement({'__produkt_id': produkt_id, '__produkt_page_id': produkt_page_id})	
			//}								
		}							
		if (this.options.__remove_cont_elem == null) this.options.__remove_cont_elem = $(this.options.__remove_cont_id);		
		this.options.__remove_cont_lab = $('your_search_text'); // could be put to parameters
	},
	aplly_filter : function($filter_obj){
		if (this.options.__applied_filters_n == 0){			
			for (key in this.options.__filters_array){			
			//this.options.__filters_array.each(function (item, index, array){
				//alert(item+ ' ' +index);				
				this.options.__filters_array[key].filter_status(1);
			};
		}
		this.options.__applied_filters[$filter_obj.group_id] = $filter_obj.group_id;
		this.options.__applied_filters_n ++;
		this.options.__filters_array[$filter_obj.group_id].filter_status(2);				
	}, 
	undo_filter : function($filter_obj){				
		this.options.__filters_array[$filter_obj.group_id].filter_status(1);		
		/*var remove_index = this.options.__applied_filters.indexOf($filter_obj.group_id);		
		if (remove_index != -1){				
			this.options.__applied_filters = this.options.__applied_filters.slice(0, remove_index).concat(this.options.__applied_filters.slice(remove_index + 1));
			this.options.__applied_filters_n --;
		}		*/
		this.options.__applied_filters[$filter_obj.group_id] = null;
		delete this.options.__applied_filters[$filter_obj.group_id];
		this.options.__applied_filters_n --;
		if (this.options.__applied_filters_n == 0){
			this.options.__remove_cont_lab.setStyle('display', 'none');			
			for (key in this.options.__filters_array){
			//this.options.__filters_array.each(function (item, index){
				this.options.__filters_array[key].filter_status(0);				
			};
		}
		else this.options.__remove_cont_lab.setStyle('display', 'block');		
	}, 
	calibrate_filters : function(){
		/*if (this.options.__applied_filters.length == 1){
			this.options.__filters_array.each(function(item, index){   
				item.filter_check(1);
			});
		} else {  */
			//for (var i = 0; i < this.options.__applied_filters.length; i ++){
				//this.options.__filters_array[this.options.__applied_filters[i]].filter_check(this.options.__applied_filters.length);
			//}
		//}	
		for (key in this.options.__filters_array){
		//this.options.__filters_array.each(function (item, index){
			this.options.__filters_array[key].filter_check(this.options.__applied_filters_n);
		};
		//this.options.__remove_cont_elem.setStyle('display', 'block');
		if (this.options.__applied_filters_n == 0) this.options.__remove_cont_lab.setStyle('display', 'none');
		else this.options.__remove_cont_lab.setStyle('display', 'block');		
	}
	
});
