//site.js
//Created by: Tom jenkins
//Date: 2010 22th Mar
//Contains:
//This contains frontend custom JS functions. N.B. Please launch through JQ onload where you can!!
js_enabled();

$(function (){ 
	//Search validation	
	
	$(".noJS").hide()
	
	$('#SiteSearchForm').submit(function(){	
		if(($('#SiteSearch').val()=='')||($('#SiteSearch').val()==$('#SiteSearch')[0].defaultValue)){jAlert('Please enter something to search on', 'Search alert!'); return false}	
	});	
	
	onLoadBuildEmail=function(){		
		$('.replaceEmail').each(function(){
			$(this).html('<a href="mailto:'+$(this).html().replace("[at]","@")+'?subject=Enquiry%20from%20Website">'+$(this).html().replace("[at]","@")+'</a>' )						 
		});		
	};
	
	$('.wdgtsearchBoxes').change(function(){
		if ($(this).val()!=''){		  
			$(this).parent().submit();
		}
	})
	
	$(".noJS").each(function(i,el){
		var $item=$(el);
		$item.addClass('hidden');
	});

	// Initialize primary nav
	superMenu.init('#TopNav');

	// Initialize clear inputs
	inputDefaults.init('#SiteSearch, .Newsletter');
	
	$('.RSSPagination').each(function(){$(this).addClass('hidden');});
	$('#paginationBottom').removeClass('hidden');
	$('#paginationTop').removeClass('hidden');
});

var inputDefaults = {
	init: function (handle) {
		var that = this,
			initial = false;

		$(handle).focus(function () {
			if (!initial) {
				that.defaultVal = this.value;
				initial = true;
			}
			if (this.value === that.defaultVal) {
				this.value = '';
			}
		}).blur(function () {
			if (this.value === '') {
				this.value = that.defaultVal;
			};
		});
	}
};

/**
 * superMenu object literal
 *
 * You can change speed and delay before init function is run
 *
 * Changing speed:
 * superMenu.speed = 'fast';
 *
 * Changing delay:
 * superMenu.delay = 500;
 *
 */
var superMenu = {
	delay: 800,
	speed: 'normal',

	init: function (handle) {
		var that = this,
			self, timer, prev, menuItem;
		
		$(handle).find('li a.link').each(function () {
			self = $(this).parent();

			self.mouseenter(function () {
				// Checking for previous menu Item if it is available or active
				if (menuItem !== undefined && menuItem.find('a').hasClass('active') && menuItem.find('a').attr('href') !== $(this).find('a').attr('href')) {
					clearTimeout(timer);
					menuItem.find('ul.secondary').hide().prev('a').removeClass('active');
				}
				else {
					clearTimeout(timer);
				}

				$(this).find('ul.secondary').slideDown(that.speed).prev('a').addClass('active');
			}).mouseleave(function () {
				menuItem = $(this);
				timer = setTimeout(function () {
					menuItem.find('ul.secondary').slideUp(that.speed).prev('a').removeClass('active');
				}, that.delay);
			});
		});
	}
};

/**
 * Taken from Modernizer library
 * Replace "no-js" class from <html> element with "js", if it exists
 */
function js_enabled () {
	docElement = document.documentElement;
    docElement.className = docElement.className.replace(/\bno-js\b/, 'js');
}
