// JavaScript Document

$.validator.setDefaults({
	errorElement: "em",
	onfocusout: function(element) { $(element).valid(); },
	onclick: function(element) { $(element).valid(); },
	showErrors: function(errorMap, errorList) {
		this.defaultShowErrors();
	},
	errorPlacement: function(error, element) {
		var label = element.prev('label').first();
		
		if (label.length > 0)
			label.append(error);
		else
			element.after(error);
 },
	unhighlight: function(element, errorClass, validClass) {
	}
});

$(function() {
					 
	// Set a corresponding class for the last child
	$('ul li:last-child').addClass('last');
	$('tr td:last-child').addClass('last-child');
	$('table tr:last-child').addClass('last-child');
	$('tr:nth-child(even)').addClass('even');
	$('tr:nth-child(odd)').addClass('odd');
	
	// Validate form when triggered by the corresponding class
	$("form.validate").each(function() {
		$(this).validate();
	});
	
	// Initialize drop down menu
	$('#site-nav > .nav').dropdown();
	
	// Set default values for form elements
	$('#global-search-input').defaultvalue('Search keyword or item #');
	$('#email-subscribe-input').defaultvalue('Email address');
	
	// Submit links
	$('.submit-link').each(function () {
		$(this).click(function () {
			$(this).parents('form').submit();
			return false;
		});
	});
	
	// Initialise homepage carousel
	$('.infinite-carousel').infiniteCarousel({ speed : 7500 });

});

$.fn.filterInput = function (sFilteredValues) {
	return this.each(function () {
		var o = $(this)
		var s = o.val();
		var i;
		var sReturnString = "";
		for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
			var c = s.charAt(i);
			if (sFilteredValues.indexOf(c) == -1)
				sReturnString += c;
		}
		o.val(sReturnString);
	});
}


