var get = {
	id : function(i) {
		return document.getElementById(i)
	},
	tag : function(t, p) {
		p = p || document;
		return p.getElementsByTagName(t);
	}
};

var news = {
	hide : function(c) {
		var l = get.tag('p', get.id(c));
		for(var i = 0, j = l.length; i < j; i++) {
			if(!get.tag('strong', l[i])[0].hasChildNodes()) {
				l[i].style.display = 'none';
			};
		};
	}
};

var hide = {
	universal : function(s, t) {
		var s = get.id(s);
		var t = t ? get.id(t) : s;
		if(s.hasChildNodes() == false) {
			t.style.display = 'none';
		};
	}
};

var shorten = {
	byClass : function(t, c, l, e) {
		
		// Handle old shorten calls and pass them to jQuery.shorten
		$(t + '.' + c).shorten({
			length : l,
			closure : e
		});
	}
};

function imageFormatSwap(object,format) {
	var images=null;	
	if(!object)
		return;
	if(!object.tagName)
		return;
	if(object.tagName=='image') {
		images=Array();
		images.push(object);
	}
	else {
		images=object.getElementsByTagName('img');
	}
	for(var i=0; i<images.length;i++) {
		images[i].src=images[i].src.replace(/&format=[a-zA-Z0-9\-_^&]+/,'&format='+format);		
	}
};

function goToAnchor(id) {
	var location = String(window.location.href);
	var position = location.indexOf('#');
	window.location.href = (position > 0) ? location.substring(0, position) + '#' + id : location + '#' + id;
};

function advancedSearchHandler(selector) {
	this.node = $(selector);
	this.html = null;
	this.setData = function(data) {
		var counter = 0;
		for(var i in data) {
			var section = createSection(i, counter);
			for(var j in data[i]) {
				var rule = createRule(data[i][j]);
				section.append(rule);
			};
			this.node.append(section);
			counter++;
		};
	};
	this.addEventHandler = function(selector, event, handler) {
		$(selector).bind(event, handler);
	};
	function createSection(title, i) {
		if(!title.length)
			return $('<div class="section" id="section_' + i + '" />');
		else
			return $('<div class="section" id="section_' + i + '"><h2>' + title + '</h2></div>');
	};
	function createRule(field) {
		var rule = $('<div class="rule" />');
		
		var input = createInput(field);
		rule.append(input);
		
		var label = createLabel(field);
		rule.append(label);
		
		return rule;
	};
	function createLabel(field) {
		return '<label for="' + field + '">' + field + '</label>';
	};
	function createInput(field) {
		return '<input type="checkbox" name="' + field + '" id="' + field + '" />';
	};
	this.toggle = function() {
		this.node.toggle();
		
		if(this.html == null)
			this.html = this.node.html();

		if(this.node.is(':visible'))
			this.restoreContent();
		else
			this.empty();
	};
	this.show = function() {
		this.node.show();
	};
	this.hide = function() {
		this.node.hide();
	};
	this.empty = function() {
		this.node.empty();
	};
	this.restoreContent = function() {
		this.node.html(this.html);
	};
};

(function($) {
	$.fn.fixCheckBoxes = function() {
		return this.each(function() {
			$(this).find('input[type=checkbox]').each(function() {
				var node = $(this);
				var check = $('<input type="hidden" />').attr({
					'id' : node.attr('id') + '_hidden',
					'name' : node.attr('name')
				});
				node.attr('checked') ? check.val('Ja') : check.val('Nee');
				node.removeAttr('name').after(check).click(function() {
					var t = $('#' + node.attr('id') + '_hidden');
					t.val() == 'Nee' ? t.val('Ja') : t.val('Nee');
				});											 
			});
		});
	};
})(jQuery);

$(function() {
	$('form#contact').fixCheckBoxes();
	
	// Hide the facilities container if there are none
	var container = $('#kavel_faciliteiten2');
	if(!container.find('li').length)
		container.hide();
	
	// Move the park properties on the plot page into tabs-container
	$('#park_properties').insertAfter('#tabs-kavel .tabs-pages:first');

	// Set up tabs-containers
	$('.tabs-container').tabs();
	
	// Back link on plot page
	$('<a href="verkoop/aanbod/kavels" title="Terug naar de zoekresultaten">Terug naar de zoekresultaten</a>').click(function(event) {
		history.back();
		event.preventDefault();
	}).wrap('<p />').appendTo(jQuery('#kavel'));
});