var colour = 'green';

function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}

function fill_form() {
	$(".form-clear, #ajaxSearch_input").each(function() {
		if(!$(this).val()) {
			$(this).val($(this).attr("rel"));
		}
	});
}

$(document).ready(function() {
	
	$("#header-li-products").hover(function() {
		$("#header-li-products ul").addClass("shown");
	}, function() {
		$("#header-li-products ul").removeClass("shown");
	});
	
	$("#header-li-products ul").hover(function() {
		$("#header-li-products ul").addClass("shown");
	}, function() {
		if(!$("#header-li-products ul").hasClass("shown")) {
			$("#header-li-products ul").removeClass("shown");
		}
	});

	$("#menu ul li a img").hover(function() {
		var src_replace = str_replace($(this).attr("src"), 'menu', 'menu/'+colour+'-over');
		$(this).attr("src", src_replace);
	}, function() {
		var src_replace = str_replace($(this).attr("src"), '/'+colour+'-over', '');
		$(this).attr("src", src_replace);
	});
	
	fill_form();
	
	$(".form-clear, #ajaxSearch_input").focus(function() {
		if($(this).val() != $(this	).attr("rel")) {
		}
		else {
			$(this).val("");
		}
		//$("#ajaxSearch_input").removeClass("form_req");
	});
	
	$(".form-clear, #ajaxSearch_input").blur(function() {
		fill_form();
	});
	
	$("#email-friend").click(function() {
		$("#email-friend-form-container").toggle();
		$(".email-friend-form-arrow").toggle();
		$("#email-friend-your-name").focus();
	});
	
	if($("#EmailFriendForm div").hasClass("errors") || !$("#EmailFriendForm div").hasClass("loading")) {
		$("#email-friend-form-container").show();
		$(".email-friend-form-arrow").show();
	}

	$("#login-form-toggle").click(function() {
		$("#right #login-form").toggle();
		$("#right #login-form #wlpeUsername").focus();
	});
	
	$("#EmailFriendForm").submit(function() {
		$("#EmailFriendForm #email-friend-submit").attr("disabled", "disabled");
		$("#EmailFriendForm .loading").show();
	});
	
	$("#search form").submit(function() {
		if($("#ajaxSearch_input").val() == 'Search') {
			// $("#ajaxSearch_input").addClass("form_req");
			$("#ajaxSearch_input").focus();
			return false;
		}
		else {
			return true;
		}
	});
	
	$("#checkbox-news-article-where").hide();
	$("#checkbox-other-where").hide();
	
	$("#checkbox-news-article").click(function() {
		$("#checkbox-news-article-where").toggle();
		$("#checkbox-news-article-where input").focus();
	});
	
	if($("#checkbox-news-article:checked").val()) {
		$("#checkbox-news-article-where").toggle();
	}
	
	$("#checkbox-other").click(function() {
		$("#checkbox-other-where").toggle();
		$("#checkbox-other-where input").focus();
	});

	if($("#checkbox-other:checked").val()) {
		$("#checkbox-other-where").toggle();
	}
	
	var feature_not_available = 'This feature is currently not available in the beta.';
	
});