/**
 * Wellemoebel productfinder
 *
 * @author Tim Lochmueller
 * @company HDNET GmbH & Co KG
 */

var filterMaps = [];

/**
 * The jQuery integration for direct call on the dom Element
 */
jQuery.fn.filterImageMap = function(options) {
	
	// Define the settings
	settings = jQuery.extend({
		ajaxData: "",
		prepareFilterFunctions: null,
		data: [],
		filter: []
	}, options);
	
	settings.filterCount = settings.filter.length;
	settings.workingArea = this;

	filterMaps[$(this).attr('id')] = FilterImageMap.initialize(settings);
	return filterMaps[$(this).attr('id')];
}

jQuery.fn.updateView = function() {
	filterMaps[$(this).attr('id')].updateView();
	return true;
}


/**
 * The filterImageMap Object
 */
var FilterImageMap = {
		
	initialize : function (opt) {
		// Set configuration
		this.options = opt;
		
		// Get AJAX data
		if(this.options.ajaxData.length > 0) {
			this.options.data = $.ajax({
				url: this.options.ajaxData,
				dataType: "json"
			}).responseText;
		}
		
		this.updateView();
		return this;
	},

	updateView : function () {
		// Get the Data
		if(this.options.prepareFilterFunctions != null)
			this.options.prepareFilterFunctions();
		
		var showData = this.getCurrentData();
		
		// Remove and show via filter
		var ids = this.data2IdsOnly(showData);
		jQuery.log(FilterImageMap);
		this.options.workingArea.children().each(function(){
			if(FilterImageMap.inArray(this.id, ids))
				$(this).fadeIn(1000);
			else				
				$(this).fadeOut(1000);
		})
		
		// After all effects
		setTimeout(function() { $('#primaryContent .inner').jScrollPane({scrollbarWidth:10, scrollbarMargin:10, showArrows:true}); }, 1400);
	},
	
	inArray : function (item,arr) {
		for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
		return false;
	},

	data2IdsOnly : function (data) {
		var ids = [];
		for(d = 0; d < data.length; d++) {
			ids.push(data[d].id);
		}
		return ids;
	},

	getCurrentData : function () {
		var tempData = [];
		
		for(d = 0; d < this.options.data.length; d++) {
			var add = true;	
			for(i = 0; i < this.options.filterCount; i++){

				if(this.options.filter[i]( this.options.data[d].filter[i] ) == false) {
					add = false;
				}
			}
			
			if(add) {
				tempData.push(this.options.data[d]);
			}
		}
		
		return tempData;
	}
	
	
}

