document.observe("dom:loaded", function() {
		// bestaat het form: filter-options?
	if( $('filter-options') ) {
		$('filter-options').observe('change', check_filters );
		$('filter-options').observe('custom:change', check_filters );
	};

} );
function autocomplete(ctrlId, acId, pid,organisation) {
	document.observe('dom:loaded', function() {
		new Ajax.Autocompleter(ctrlId, acId, 'index.php', {
			parameters: 'eID=sadc_autocomplete&pid=' + pid + "&organisation="+organisation,
			paramName: 'search',
			afterUpdateElement: autosearch
		});
	});
}
function autosearch(input,item){
	fire_search();
}
//		Filters-dingen klikken.
function logMe(message,comment){
	if(console){
		console.log(comment,message);
	}
}
function check_filters( event ) {

	selectedItem = Event.findElement(event,'input');

	if(!selectedItem || !selectedItem.readAttribute('type') == "checkbox") {
		return;
	}

	filterSet = $(selectedItem).up('fieldset');
	filterParent = $(selectedItem).up();


	if( filterSet ) {

		if( (filterParent.match('legend')) || (filterParent.match('li.has-sub')) ) {
			// Als de checkbox kindjes heeft - dit lijkt goed te gaan
			if( filterParent.match('li.has-sub') ) {
				filterSubs = $(filterParent).select('input');
			};
			if( filterParent.match('legend') ) {
				filterSubs = $(filterSet).select('input');
			};

			if( selectedItem.checked ) {
			filterSubs.each(function(item) {
					item.checked = 1;
					styleFormElements(item);
				});
			} else {
				$(filterSubs).each(function(item) {
					item.checked = 0;
					styleFormElements(item);
				});
			};

		} else {

			// Als de checkbox geen kindjes heeft

		};
		checkSemiFilters(filterSet)


	}; // end: if filterSet

	settings.start=0;
	filter_list();

}; // end: check_filter_set

function checkSemiFilters(fieldset){
if(true){
	// Kijken wat er nu met de header moet gebeuren
	filterHeader = $(fieldset).down('input');

	allInputs = $(fieldset).select('input');
	checked = 0;
	boxes = 0;
	$(allInputs).each(function(item) {
		// De header tellen we niet mee
		if(!$(item).up().match('legend')){
			if(item.checked == 1){
				checked = checked + 1;
			}
			boxes = boxes + 1;
		}
	});

	if( checked > 0) {

		if (checked == boxes){
			filterHeader.checked = 1;
			styleFormElements(filterHeader);
		}
		else {
			// indien de checkboxes lichtblauw moeten worden:
			filterHeader.checked = 0;
			styleFormElements(filterHeader, 'semi');
		}
	} else {
			filterHeader.checked = 0;
			styleFormElements(filterHeader);
	};

	// Kijken of er items met subs zijn die lichtblauw moeten worden
	$(allInputs).each(function(item) {
		enclosingObj = $(item).up();
		if( enclosingObj.match('li.has-sub') ) {
			subs = $(enclosingObj).select('input');
			checked = 0;
			boxes = 0;
			$(subs).each(function(sub) {
				// De parent tellen we niet mee
				if(!$(sub).up().match('li.has-sub')){
					if(sub.checked == 1){
						checked = checked + 1;
					}
				boxes = boxes + 1;
				}
			});
			if( checked > 0) {
				if (checked == boxes){
					item.checked = 1;
					styleFormElements(item);
				}
				else {
					// indien de checkboxes lichtblauw moeten worden:
					item.checked = 0;
					styleFormElements(item, 'semi');
				}
			} else {
				item.checked = 0;
				styleFormElements(item);
			};
		};
	});
}
}

function styleFormElements(item, status) {

	if( status === undefined ) {
		status = "full";
	};

	if( item.type == "checkbox" ) {
		itemHeight = '12';
	};
	if( item.type == "radio" ) {
		itemHeight = '20';
	};

	styledFormElement = $(item).previous('span');

	if( item.checked == true ) {
		styledFormElement.setStyle({ backgroundPosition: '0 -' + itemHeight*2 + 'px' });
	} else {

// indien de checkboxes lichtblauw moeten worden:
		if( status == "semi" ) {
			position = '0 -' + itemHeight + 'px';
			styledFormElement.style.backgroundPosition = position;
		} else {
			styledFormElement.setStyle({ backgroundPosition: '0 0' });
		};
	}

}; //end function styleFormElements

berichten = new Array();
function melding(bericht){
	berichten.push(bericht);
}
/*


CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)

The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked.

You may need to adjust your images a bit if there is a slight vertical
movement during the different stages of the button activation.

The value of selectWidth should be the width of your select list image.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "12";
var radioHeight = "20";
var selectWidth = "140px";

document.write('<style type="text/css">input.fe { display: none; } select.fe { position: relative; width: ' + selectWidth + '; margin-left:8px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "fe") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.checkinput;
				if(!inputs[a].getAttribute("disabled")) {
					// Let's not do this.
					//span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} else {
					span[a].className = span[a].className += " disabled";
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "fe") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].id + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
		// wtf is dit? :
		//document.onmouseup = Custom.clear;
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;

		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
		Event.fire($(element), 'custom:change');
	},
	checkinput: function() {
		element = this;
		span = element.previousSibling;
		if(element.checked == true && element.type == "checkbox") {
			span.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
		} else {
			if(element.type == "checkbox") {
				span.style.backgroundPosition = "0 0";
			}
		}

		if(element.checked == true && element.type == "radio") {
			span.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			group = span.nextSibling.name;
			inputs = document.getElementsByTagName("input");
			for(a = 0; a < inputs.length; a++) {
				if(inputs[a].name == group && inputs[a] != span.nextSibling) {
					inputs[a].previousSibling.style.backgroundPosition = "0 0";
				}
			}
		}
		Event.fire($(element), 'custom:change');
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "fe") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "fe") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "fe") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "fe") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.id + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
var elements = new Array();
function showCards(){
	elements.each(function(card){
		new Effect.Opacity(card.id, { from: 0, to: 1, duration: 0.1, queue:'end' });
	});
}


function tx_sadc_pi7_init(ctrlId, waitId, count, pid, singleid, uid, imagepath, imagewidth, imageheight, view, startingpoint, disciplines, sorting, sword, advancedfilter, sectors, companysizes, organizations, user, zipcode, max_distance) {
	melding('starting ajaj request');
	new Ajax.Request('index.php', {
			method: 'get',
			//OMG why not just send every parameter of the form?? Took me 15 minutes to find out why my new form-field didn't hold any value
			parameters: {
				eID: 'sadc',
				pid: pid,
				singleid: singleid,
				uid: uid,
				count: count,
				imagepath: imagepath,
				imagewidth: imagewidth,
				imageheight: imageheight,
				view: view,
				startingpoint:startingpoint,
				disciplines:disciplines,
				sorting:sorting,
				sword:sword,
				sectors:sectors,
				companysizes:companysizes,
				organizations:organizations,
				user:user,
				zipcode:zipcode,
				max_distance:max_distance
			},
			onFailure: function(transport){melding(transport.status)},
			onLoaded: function(){melding('request sent')},
			onSuccess: function(transport){
					var data = transport.responseText.evalJSON(true);
					melding('data received');
				// Fade in cards
				//$('list-browser').style.display='none';

					$('view-results').innerHTML = '<p><strong>' + data['totalcount'] + '</strong> resultaten gevonden</p>';
					if (view == "projects"){
						var oldcount = elements.length;
					for (var i = 0; i < data['count']; i++) {
						var item = data['content'][i];
						var newdiv = new Element('div');
						var divIdName = "card_"+i;
						newdiv.id = divIdName
						newdiv.setStyle({ opacity: 0 });
						newdiv.addClassName('card');
						newdiv.addClassName('card-project');
						newdiv.innerHTML = '<a href="'+item['link']+'"><img src="' + item['thumbnail'] + '"><h4>'+item['title']+'</h4><p class="bureaunaam">'+item['company']+'</p><p class="disciplines">'+item['disciplines']+'</p><p class="date">'+item['crdate']+'</p><p class="likes"><span>Likes: </span>'+item['likes']+'</p></a>';
						elements[i] = newdiv;
						if(!$(divIdName)){
							$(ctrlId).appendChild(elements[i].wrap('div', { 'class': 'card-loader' }).wrap('div', { 'class': 'card-wrapper' }));
						}
						else{
							$(divIdName).replace(elements[i]);
						}
					}
					// Remove unused cards
					while (i<oldcount){
						var divIdName = "card_"+i;
						$(divIdName).up().up().remove();
						// We need to remove the last item each time
						elements.splice((elements.length-1),1);
						i++;
					}
					if($('header-bureaus') && $('header-projecten')){
						$('header-bureaus').className = '';
						$('header-projecten').className = 'current';
					}
					}
					else if(view == "users"){
							var oldcount = elements.length;
					for (var i = 0; i < data['count']; i++) {
						var item = data['content'][i];
						var newdiv = new Element('div');
						var divIdName = "card_"+i;
						newdiv.id = divIdName
						newdiv.setStyle({ opacity: 0 });
						newdiv.addClassName('card');
						newdiv.addClassName('card-bureau');
						newdiv.innerHTML = '<a href="'+item['link']+'"><img src="' + item['thumbnail'] + '"><p class="bureaunaam">'+item['user_company']+'</p><p class="disciplines">'+item['disciplines']+'</p></a>';
						elements[i] = newdiv;
						if(!$(divIdName)){
							$(ctrlId).appendChild(elements[i].wrap('div', { 'class': 'card-loader' }).wrap('div', { 'class': 'card-wrapper' }));
							}
						else{
							$(divIdName).replace(elements[i]);
						}
					}

					// Remove unused cards
					while (i<oldcount){
						var divIdName = "card_"+i;
						$(divIdName).up().up().remove();
						// We need to remove the last item each time
						elements.splice((elements.length-1),1);
						i++;
					}

					if($('header-bureaus') && $('header-projecten')){
						$('header-bureaus').className = 'current';
						$('header-projecten').className = '';
					}
					}


					melding('deactivating wait');
					// hide progress animation gif
				$(waitId).style.display = 'none'

				melding('unchecking checkboxes');
				$$('input.fe').each(function(checkbox){
					checkbox.checked = false;
					checkbox.previousSibling.style.backgroundPosition = "0 0";
				});

				melding('activating filtering checkboxes');
				if(disciplines != ""){
				disciplines = disciplines + "";
					discipline_array = disciplines.split(",");
					discipline_array.each(function(discipline) {
						$('disc-' + discipline).checked = true;
						$('disc-' + discipline).previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
						if($('disc-' + discipline).nextSibling.nextSibling){
							var children = $('disc-' + discipline).nextSibling.nextSibling;
							$(children).childElements().each(function (child) {child.select('input').each(function (input) {input.checked = true;});});
							$(children).childElements().each(function (child) {child.select('span').each(function (span) {span.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";});});
						}
					});
					checkSemiFilters($('filter-discipline'));
				}
				if(sectors != ""){
				sectors = sectors + "";
					sector_array = sectors.split(",");
					sector_array.each(function(sector) {
						$('branche-' + sector).checked = true;
						$('branche-' + sector).previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
					});
					checkSemiFilters($('filter-branches'));
				}
				if(organizations != ""){
				organizations = organizations + "";
					organization_array = organizations.split(",");
					organization_array.each(function(organization) {
						$('lidmaatschappen-' + organization).checked = true;
						$('lidmaatschappen-' + organization).previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
					});
					checkSemiFilters($('filter-lidmaatschappen'));
				}
				if(companysizes != ""){
				companysizes = companysizes + "";
					companysize_array = companysizes.split(",");
					companysize_array.each(function(companysize) {
						$('bureaugrootte-' + companysize).checked = true;
						$('bureaugrootte-' + companysize).previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
					});
					checkSemiFilters($('filter-bureaugrootte'));
				}
				melding('create pagebrowser');
				// Show pagebrowser if needed
				if($('list-browser')){
						$('list-browser').remove();
				}
				if(data['totalcount'] > count){
					// Build pagebrowser
					var counter = count;
					var countdown = data['totalcount'];
					var pages = "";
					var backbutton = "";
					var forwardbutton = "";
					var userprefix = '';
					if(user>0){
						userprefix = 'user';
					}
					// If the current startingpoint is higher than count then we need a back button
					if(startingpoint >= count){
						backbutton += '<li><a href="javascript:settings.' + userprefix + 'start=' + (startingpoint-count) + ';fire_list()" onclick="" class="button button-back"><span>terug</span></a></li>';
					}
					// If we have more than 'counter' items left we need to be able to go forward
					if((startingpoint + count) < data['totalcount']){
						forwardbutton += '<li><a href="javascript:settings.' + userprefix + 'start=' + (startingpoint+count) +';fire_list()" onclick="" class="button button-forward"><span>vooruit</span></a></li>';
					}

					// Created needed amount of page shortcuts
					var i = 0;
					var current = "";
					var backspacer="";
					var forwardspacer="";
					istart = startingpoint - (6*count);
					iend = startingpoint + (7*count);

					if(istart < 0) {
						istart = 0;
					}
					if(iend > data['totalcount']) {
						iend = data['totalcount'];
					}

					if (istart > count) {
						backspacer = '<li>'
							+'<a href="javascript:settings.' + userprefix + 'start=0;fire_list();" onclick="">'
							+ '1'
							+ '</a>'
							+'</li>'
							+'<li>...</li>';
					}

					if (iend < (data['totalcount'] - count)) {
						lastPage = Math.floor(data['totalcount'] / count);
						last = lastPage * count;
						forwardspacer = '<li>...</li>'
							+'<li>'
							+'<a href="javascript:settings.' + userprefix + 'start='+last+';fire_list();" onclick="">'
							+ (lastPage+1)
							+ '</a>'
							+'</li>';
					}



					for(i=istart; i<iend; i = i+count) {
						current = '';
						if(startingpoint == (i)){
							current = 'current';
						}
						pages += '<li class=' + current + '><a href="javascript:settings.' + userprefix + 'start=' + (i) + ';fire_list();" onclick="">' + ((i/count)+1) + '</a></li>';

					}


					$(ctrlId).innerHTML += '<div id="list-browser"><ol>' + backbutton + backspacer + pages + forwardspacer + forwardbutton + '</ol></div>';
				}
			// Show the correct menu and content width
			melding('showing menu');
			// Only if filter plugin is present
			if($('branche-menu')){
				if(advancedfilter){
					$('branche-menu').style.display = 'none';
					$('filters').style.display = 'block';
					$('content-list').className = '';
					$('content-header').className = '';
				}
				else {
					$('branche-menu').style.display = 'block';
					$('filters').style.display = 'none';
					$('content-list').className = 'front';
					$('content-header').className = 'front';
				}
			}
			// After everything is ready we show the cards, defer is used to make sure the dom has been updated
			melding('showing cards');
			showCards().defer();

			}
		});
}


