// Custom functions
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;
};

// Custom plugins
(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');
				});											 
			});
		});
	};
	$.fn.swapClass = function(className, addClass) {
		var addClass = addClass || false;
		return this.each(function() {
			if(className) {
				var node = $(this);
				node.data('oClass', node.attr('class'));
				if(addClass)
					className = node.data('oClass') + className;
				node.attr('class', className);
			} else {
				var node = $(this);
				node.attr('class', node.data('oClass'));
			};
		});
	};
})(jQuery);

// Search page
function searchPage() {

	// Global variables
	var amount = $('#search_amount').val();
	var value = $('#search_value').val();
	var output = $('#search_output');
	var navigation = $('#search_page_navigation');

	// Adjust the fallback if there is only one result
	if(amount == 1) {
		output.html('1 result was found');
		
	// Adjust the fallback if no results were found
	} else if(amount == 0) {
		$('#no_results').children('h2').remove();
		output
			.html('No results were found')
			.wrap('<h3 />');
	};

	// Add the value if the search value is anything else than a space
	if(value !== ' ')
		output.html(output.html() + ' for "' + value +'"');
		
	// Hide the navigation if both the previous en the next buttons aren't present
	if(!navigation.find('a').length)
		navigation.remove();
		
	// Add the complete URL to each search result
	$('#search_page .element').each(function() {
		var node = $(this);
		var link = node.children('h2').children('a');
		var address = link.attr('href').substring(1);
		if(address.length > 40)
			address = address.substring(0, 40) + '&hellip;';
		address = $('base').attr('href') + address;
		var title = link.attr('title');
		node.append($('<p class="address"><a href="' + address + '" title="' + title + '">' +  address + '</a></p>'));
	});
};

// News page
function newsPage() {

	// Global variables
	var	container = $('#element_highlight');
	var content = container.find('.content');

	// Global variables
	var oTop = content.css('top');
	var nTop = container.outerHeight() - content.outerHeight();
	
	// Bind events
	content.hover(function() {
		content.stop().animate({
			top : nTop
		}, 250);
	}, function() {
		content.stop().animate({
			top : oTop
		}, 250);
	});
};

// Mailer ext. page
function mailerExtPage() {
	
	var container = $('#contact');

	// Set up the form
	container.find('.message').attr({
		id : 'warning',
		message : 'invalid'
	});
	container.find('.checkboxes').fixCheckBoxes();
	
	// Add controls
	$('#name, #company, #telephone_number').addControls({
		errorClass : 'text_error'													
	});
	$('#e-mail').addControls({
		message : 'E-mail',
		mask : 'email',
		errorClass : 'text_error'
	});
	
	// Toggle
	$('#order_fruit_online_yes').toggleForm('#online_habbits', {
		action : 'show'
	});
	$('#order_fruit_online_no').toggleForm('#online_habbits', {
		action : 'hide'
	});
	
	// Instantly scroll to the error
	container.find('input[type=submit]').click(function() {
		goToAnchor('warning');
	});
};

// Document.ready
$(function() {

	// Search button
	$('form.search input[type=image]').hover(function () {
		var node = $(this);
		var oSource = node.attr('src');
		node.data('oSource', oSource);
		node.attr('src', oSource.substring(0, oSource.length - 4) + '_hover.gif');
	}, function() {
		var node = $(this);
		node.attr('src', node.data('oSource'));
	});
	
	// Add focus events to all selected inputs
	$('input.text, textarea').focus(function() {
		$(this).swapClass('_focus', true);
	}).blur(function() {
		$(this).swapClass();
	});
});
