$(document).ready(function()
{
	initNavigation();
	
	//This code removes the Quality and Quantity breadcrumbs
	//if they appear on the site. Removing them server side would
	//be preferable but Kentico does not easily allow access to this.
	var breadCrumbString = "";
	$(".breadcrumbs a").each(function() {
		
		var url = $(this).attr("href");
		var urlText = $(this).text();
		if(urlText != "Quality" && urlText != "Quantity")
			breadCrumbString += "<a href='" + url + "'>" + urlText + "</a> / ";
									 
	});
	
	$(".breadcrumbs").html(breadCrumbString.substring(0, (breadCrumbString.length - 2)));
	//highlight the current page in the menu
	$("#nav a").each(function() {
		
		var url = document.URL;
		var href = $(this).attr("href");
		
		if(url.indexOf(href) != -1) 
			$(this).parent().addClass("active");
							  
	});
	
	$("#searchbutton").removeAttr("onclick")
	$("#searchbutton").click(function() {
		
		var searchTerm = $("#searchtext").val();
		window.location.href = "/search.aspx?searchtext=" + searchTerm + "&searchmode=anyword"; 
									  
	});
	
	$("#searchtext").keyup(function(event) {
		if (event.keyCode == '13')
			$("#searchbutton").click();						
	});
	
	$(".btn-login").each(function() {
		
		var url = $(this).attr("href");
		url += "?returnurl=" + document.URL.replace('http://sw360.federationmedia.com', '');
		url = url.replace('http://www.stormwater360.co.nz', '');
		$(this).attr("href", url);
								  
	});
	
	//override link which is not directing correctly.
	$("#nav a").click(function(e) {
		var href = $(this).attr("href");
		if(href == "/Products/QUANTITY.aspx") {
			e.preventDefault();
			window.location = "/Products.aspx#quantity";
		}					  
	});
	
	$(".land-list a").each(function() {
		
		var href = $(this).attr("href");
		href = href.substring(1, (href.length));
		
		if($("a#" + href).length == 0)
			$(this).parent().remove();						
	});
	
	$(".register-link").each(function() {
		
		var url = $(this).attr("href");
		url += "?returnurl=" + document.URL.replace('http://sw360.federationmedia.com', '');
		url = url.replace('http://www.stormwater360.co.nz', '');
		$(this).attr("href", url);
		
	});
	
	$('#gallery').cycle({
	 	fx: 'scrollLeft',
	    speed: 1200,
	    timeout: 6000,
	    pager: '#pager'
	 });
	
	//this needs to be delayed to give the 
	//cycle functionality (above) time to get everything into place.
	setTimeout("removeNumbers()", 500);
	
});
function initNavigation()
{
	jQuery('.navigation li').hover(function()
	{
		$(this).addClass('hover');
	},
	function()
	{
		$(this).removeClass('hover');
		setTimeout(function(){Cufon.refresh('ul.nav > li > a')}, 10);
	});
}

function removeNumbers() {
    $("#pager a").each(function () {
        $(this).html("&nbsp;");
    });

    $("#pager").css("display", "block");

}




