/*
var selectTexts = { 
	"mySelectText0" : "Hoofdgroep",
	"mySelectText1" : "Groepsnaam",
	"mySelectText2" : "Merk"
}
*/

function selectMeWrapper(selectFieldId, linkNo, selectNo, manual, cascade) {
	selectMe(selectFieldId, linkNo, selectNo)

	if (manual) {
		// filter is immediately placed after the change of a select box
		// uncomment next line if you want the user to explicitly use the 'search' button
		placeFilter(selectNo);
	}
	
	if (cascade) {
		// change other selects based on this selection
		//cascadeChanges(selectFieldId, linkNo, selectNo);
	}
	cascadeChanges(selectFieldId, linkNo, selectNo);
}


Object.prototype.in_array = function(datum, strict) {
	if (strict) function equals(a,b) { return a === b }
	else function equals(a,b) { return a == b }

	for (var i in this) {
		if (equals(this[i], datum)) return true;
	}
	return false;
}


// if something is selected, hide unrelated choices of other selections
// make sure this happens only on manual change
function cascadeChanges(selectFieldId, linkNo, selectNo) {
	var selected = linkNo + "_" + selectNo;
	for (i in option_array) {
		// get the possibly related option (the <p> that holds the option)
		poss_related = document.getElementById("option_" + option_array[i]);
		if (poss_related != null) {
			// only affect select boxes lower down the food chain
			otherSelectNo = option_array[i].charAt(option_array[i].length - 1);
			textVar = document.getElementById("mySelectText"+otherSelectNo);
			if (parseInt(otherSelectNo) > parseInt(selectNo)) {
				if (!associations[selected].in_array(option_array[i])) {
					poss_related.style.display = "none";
					current_option = document.getElementById("option_"+option_array[i]);

					// if necessary deselect this option
					if (textVar.childNodes[0].nodeValue == current_option.childNodes[0].childNodes[0].nodeValue) {
						var newText = document.createTextNode(selectTexts["mySelectText"+otherSelectNo]);
						textVar.replaceChild(newText, textVar.childNodes[0]);
						selectField = document.getElementById(selectTexts["mySelectText"+otherSelectNo]);
						// deselect the actual select box as well
						selectField.selectedIndex = -1;
					}
				} else {
					poss_related.style.display = "block";
				}
			}
		}
	}
}


// return the smallest number
function min(a, b) {
	if (a <= b) {
		return a;
	}
	return b;
}


// submit the filter-form using GET
// also set breadcrumbs and do AJAX calls
function placeFilter(filter_quantity) {
	
	// MSIE doesn't seem to set the base correctly, so we set it explicitly
	base = document.getElementsByTagName("base")[0].href;
	if (base == null) {
		base = "";
	}

	var queryString = "";
	var breadCrumbs = "<span class='active'><a href='" + base 
		+ "'>" + productsName + "</a></span>\n";
	var separator = "&";
	var justCategory = false;
	filter_quantity = min(filter_quantity + 1, selects.length);
	// get filter values
	for (i=0; i<filter_quantity; i++) {
		currentOptions = selects[i].options;
		for (j=0; j<currentOptions.length; j++) {
			if (currentOptions[j].selected 
				&& currentOptions[j].value != "dummy") {
				queryString += separator + "filter_" + escape(selects[i].name)
					+ "=" + escape(currentOptions[j].value);
				if (i + 1 != filter_quantity) {
					if (i < 2) {
						breadCrumbs += " | <span class='active'><a href='" + base 
							+ "index.php?cms_show=module&cms_id=datamodule&module_source=product&action=view&category=main"
							+ "&template=categories_product&refresh=1" + queryString + "'>" + currentOptions[j].value.toLowerCase() + "</a></span>\n";
					} else {
						breadCrumbs += " | <span class='active'><a href='" + base 
							+ "index.php?cms_show=module&cms_id=datamodule&module_source=product&action=view&category=main"
							+ "&max=6&offset=0&refresh=1" + queryString + "'>" + currentOptions[j].value.toLowerCase() + "</a></span>\n";
					}
				} else {
					breadCrumbs += " | <span class='off'>" + currentOptions[j].value.toLowerCase() + " </span>\n";
				}
				if (i == 0) {
					justCategory = true;
				} else {
					justCategory = false;
				}
			}
		}
	}

	$('breadcrumbs').innerHTML = breadCrumbs;

	// if only 'category' (Hoofdgroep) is given, redirect to a page with a
	// different template (module_datamodule_categories_product.tpl.php).
	if (justCategory) {
		url = base + "barebone.php?module=datamodule&module_source=product&action=view&category=main"
		+ queryString + "&template=categories_product";
	} else {
		url = base + "barebone.php?module=datamodule&module_source=product&action=view&category=main"
		+ queryString + "&max=6&offset=0";
	}

	var producten = new AJAXProducten();
	producten.setFilter(url);
}


// wrapper for apply_filters wich is defined in the datamodule template
function update_filters() {
	if (typeof apply_filters == "function") {
		apply_filters();
	}
}


var AJAXProducten = Class.create();

var originalContent;

AJAXProducten.prototype = {
	initialize: function() {
		// pass
	},

	setFilter: function(url) {
		var options = {
			method: 'get',
			onSuccess: this.writeResult.bind(this)
		};
		new Ajax.Request(url, options);
	},

	writeResult: function(r){
		if( originalContent == null ) originalContent = $('div').innerHTML; 
		$('div').innerHTML = r.responseText;
	}
}

/**
 * Reset the content of the main area if it was altered by an AJAX call.
 */
function resetContent() {
	if( originalContent != null )
		$('div').innerHTML = originalContent;
}

/**
 * Reset the search boxes and the content. 
 * Doesn't work yet, unfortunately. The boxes aren't reset and neither is the content.
 */
function resetSearchFields() {
	selectMe('Groep', 0, 0 );
	selectMe('Groep', 0, 1 );
	selectMe('Groep', 0, 2 );
	resetContent();
}

