function licenseConfig(c){
	for(i in c){
		if(typeof(this[i])!="undefined"){
			this[i]=c[i];
		}
	}
}
licenseConfig.prototype.selected={};
licenseConfig.prototype.inSelected=function(c,v){
	if(this.selected.length > 0){
		for(i=0;i<this.selected.length;i++){
			if(this.selected[i].column==c && this.selected[i].value==v)
				return true; 
		}
	}
	return false;
}

$(function(){
	//var slider=new priceSlider();
	//slider.setAttributeSelector('div.license_options');
	//slider.setProductSelector('#licenses');
	////slider.setFilterSelector('#filter');
	//slider.initialize();
	$('.license_reset').click(function(){
		var oid=$(this).attr('data');
		$('.attribute').each(function(){
			if(this.name=='id['+oid+']' && this.checked==true){
				$(this).attr('checked',false);
				var value=parseFloat(attributes[this.id.substr(9)][this.value]);
				previousPrice=inputGroups.get(this.name,'price');
				if(previousPrice!==false){
					basePrice=parseFloat(basePrice) - parseFloat(previousPrice * document.cart_quantity.cart_quantity.value) ;
				}
				inputGroups.set(this.name,'price',0);
				$("#price").html("&euro; "+CurrencyFormatted(basePrice));
			}
		});
		return false;								
	});
});

priceSlider=function(){
	//$ isn't working sometimes so use 'j'
	var j=jQuery;
	//to use when we are in jQuery mode
	var _this=this;
	//push the product object to this array
	this.products=new Array();
	//maps the product ID's
	this.productsIDs=new Array();
	//cache active filter based on their html ID
	this.filters=new Array();
	//contains all showed products based on the 'show' attribute 
	this.activeProducts=new Array();
	//the selector which contains all attributes (without the price slider)
	this.attributeSelector='';
	//the selector which contains all products
	this.productSelector='';
	//the filter selector
	this.filterSelector='';
	this.subtype=0;
	this.config=false;
	this.total=0;

	this.setAttributeSelector=function(selector){
		this.attributeSelector=selector;
	}
	this.setProductSelector=function(selector){
		this.productSelector=selector;
	}
	this.setFilterSelector=function(selector){
		this.filterSelector=selector;
	}
	//append all products based on a JSON object
	this.appendProduct=function(product){
		if(typeof(product.id)=='undefined')
			return;
		product.index=this.products.length;
		product.filters=new Array();
		this.total++;
		this.products[product.id]=product;
		this.productsIDs.push(product.id);
	}
	
	//return all products and cache them
	this.getProducts=function(){
		_array=new Array();
		for(pIndex=0;pIndex<this.productsIDs.length;pIndex++){
			_array.push(this.products[this.productsIDs[pIndex]]);
		}	
		return _array;
	}
	
	//return all active products based on the 'show' attribute and cache them
	this.getActiveProducts=function(){
		this.activeProducts=new Array();
		j(this.productSelector+' tr.license').each(function(){
			if(j(this).attr('show')=='true'){
				product=j(this).attr('data');
				_this.activeProducts.push(product);
			}
		});
		return this.activeProducts;
	}

	//loop through all products and try to show them
	this.showProducts=function(filter){
		products=this.getProducts();
		for(i=0;i<products.length;i++){
			this.showProduct(products[i].id,this.filters[filter]);
		}
	}
	
	//show a product based on its ID and optionally we can remove an active filter
	//filter may be false in case of the price slider
	this.showProduct=function(id,filter){
		if(this.products[id]){
			filterIndex=this.inArray(filter,this.products[id].filters);
			if(filterIndex!==false){
				this.products[id].filters.splice(filterIndex[0],1);
			}
			if(this.products[id].filters.length==0){
				j('#licenseId'+id).attr('show','true').show();
			}
		}
	}
	
	//hide a product based on its ID an push a filter to the product array so we know why its hidden
	//filter may be false in case of the price slider
	this.hideProduct=function(id,filter){
		if(this.products[id]){
			j('#licenseId'+id).attr('show','false').hide();
			if(filter!==false){
				this.products[id].filters.push(filter);
			}
		}
	}
	
	// the initial setup for all attributes (including models, manufacturers, etc..)
	this.attributesSetup=function(){
		//loop through all links within the attribute selector
		j(this.attributeSelector+' ul li a').each(function(){
			var columnName=j(this).parent().parent().parent().children('h3').text();
			var rowValue=this.childNodes[0].textContent;
			j(this).click(function(){
				//retrieve current Id of the attribute container								   
				attributeID=j(this).parent().parent().attr('id');
			   	//try to find the filter
			   	if(_this.inArray(attributeID,_this.filters)===false){
					j(this).parent().parent().attr('filter',j(this).parent().attr('id'));
					//add the attribute html element ID to the active filters
					_this.filters.push(attributeID); 
					//get the JSON product object
					productData=j(this).parent().attr('data').split(','); 
					products=_this.getProducts();
					for(i=0;i<products.length;i++){
						//should we hide the product because the product id isn't appearing in the attributes stack
						if(products[i].id!='undefined'){
							if(_this.inArray(products[i].id,productData)===false){
								_this.hideProduct(products[i].id,attributeID);
							}
						}
					}
					//just create a little bit overhead otherwise it would be very difficult to read
					prefix=j(this).parent().parent().attr('title');
					name=j(this).html();
					//caption=prefix+": "+name;
					caption="<img src='style/images/shop/buttons/dutch/btn_filter_delete_trans.png' title='Verwijder filter' />";
					//create a filter button
					j('<div class="license_filter_remove"></div>').click(function(){
						//onclick: retrieve the filter to be removed
						key2=_this.inArray(j(this).attr('filter'),_this.filters);
						//show all products based on the filter
						_this.showProducts(key2[0]);
						//remove the filter from the active filters
						_this.filters.splice(key2[0],1);
						//reset the attributes
						_this.setAttributes();
						//and remove the filter button
						j(this).remove();
						_this.callback();
					}).attr({'filter':attributeID}).html(caption).css('cursor','pointer').appendTo(j(this).parent());
					//css({'float':'left','border':'1px solid red','padding':'5px'}).attr({'filter':attributeID}).html(caption).css('cursor','pointer').appendTo(this_this.filterSelector);
				}
				_this.setAttributes();
				_this.callback();
			});
			// AF20100915: based on Get parameter filter=[subtype]{[field_id]|[attribute_id]}
			if(_this.inArray(j(this).parent().attr('aid'),filterParameters)){
				j(this).click();
			}
			// from the link to the attribute selector and get the first h3 text 
			var columnName=j(this).parent().parent().parent().children('h3').text();
			// only the text, not the counter (occurrence) 
			var rowValue=this.childNodes[0].data;
			if(_this.config!==false && _this.config.inSelected(columnName,rowValue)){
				j(this).click();
			}
		});
	}
	
	this.setAttributes=function(){
		this.getActiveProducts();
		j(this.attributeSelector+' ul li').each(function(){
			attributeID=j(this).parent().attr('id');
			Search=_this.inArray(attributeID,_this.filters);											 
			products=j(this).attr('data').split(',');
			
			count=0;
			//loop through elements which isn't set as active filter
			if(Search===false){
				for(x=0;x<products.length;x++){
					if(_this.inArray(products[x],_this.activeProducts)){
						count++;
					}
				}
				if(count==0){
					j(this).slideUp('fast');
				}else{
					j(this).slideDown('fast').find('span').text("("+count+")");
					//j(this).children().css({'color':'#474135','cursor':'pointer','font-weight':'normal'});
					j(this).children().attr('class','license_filter_default');
				}
			//some kind of filter is active
			}else{
				//lets make all elements gray except the one thats is the filter
				for(x=0;x<products.length;x++){
					if(_this.inArray(products[x],_this.activeProducts)){
						count++;
					}
				}
				if(count==0){
					//j(this).children().css({'color':'#dddddd','cursor':'default','font-weight':'normal'});
					j(this).children().attr('class','license_filter_inactive');
				}else{
					j(this).find('span').text("("+count+")").slideDown('fast');
					//j(this).children().css({'color':'#666666','cursor':'pointer','font-weight':'bold'});
					j(this).children().attr('class','license_filter_active');
				}
			}
		});
	}
	this.callback=function(){
		var count=0;								
		$("#licenses tr").each(function(){
			if(!$(this).is(":hidden")){
				count++;
				if(count % 2){
					var color={
						standard:"#fff",
						hover:"#ddd"
					}
				}else{
					var color={
						standard:"#ebf5fc",
						hover:"#c9d3da"
					}
				}
				//$(this).css("background-color",color.standard).hover(
				//	function(){
				//		$(this).css("background-color",color.hover);
				//	},
				//	function(){
						$(this).css("background-color",color.standard);
				//	}
				//);						
			}
		});
	}
	
	this.inArray=function (s,a){
		_f=false;
		_k=new Array(false,0);
		//loop through the array
		for(_i=0;_i<a.length;_i++){
			//convert array value to a string
			if(a[_i]==undefined)
				return false;
			_v=a[_i].toString();
			//compare given string and array value
			if(_v==s){
				//we've found the given string
				_f=true;
				_k[1]++; //How may times does the string appears in the array
				if(_k[0]===false)
					_k[0]=_i; //what's the first index
			}
		}
		if(_f===true)
			return _k;
		return false;
	}
	
	this.initialize=function(subtype){
		this.subtype=subtype;
		if(licensesConfig[this.subtype]){
			this.config=licensesConfig[this.subtype];
		}
		j(this.productSelector+' tr').each(function(){	
			var product=new Object();
			product.id=j(this).attr('data');
			j(this).attr('show','true');
			_this.appendProduct(product);
		});
		this.attributesSetup();
		var me=this;
		j('div.license_options'+subtype).each(function(){
			var size=j(this).children('ul.license_option').children().size();
			if(size==0 || (size==1 && me.total == j(this).children('ul.license_option').children().attr('data').split(',').length)){
				j(this).hide();
				var h3=j(this).children('h3');
				if(size==0){
					j("div#licenses_container"+subtype+" table th").each(function(){
						if( j(this).text()==h3.text() ){
							 j(this).html('');
						}
					})
				}
			}
		})
	}
}
