function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

$(function() {
	$.swapImage(".swapImage");
	
	$('ul.sf-menu').superfish();

	$(".colorbox").colorbox({width:"800px", height:"80%", inline:true, href:$(this).attr("href"), current:"{current} of {total}"});
	
	$(".lightbox").colorbox();
	
	$('form#productsfilter select').change(function() {
		$('form#productsfilter').submit();
	});
	
	$('dl#orderform dd').slice(1).hide();
	var current = 0;
	$('.nxtbtn').click(function() {
		
		if ($(this).hasClass("details")) {
			if ($('#name').val() == '') {
				$(this).next(".error").text("Name is required");
				return false;
			} else if ($('#phone').val() == '') {
				$(this).next(".error").text("Phone is required");
				return false;
			} else if ($('#email').val() == '') {
				$(this).next(".error").text("E-mail is required");
				return false;
			} else if (!isValidEmailAddress($('#email').val())) {
				$(this).next(".error").text("E-mail is not valid");
				return false;
			} else if ($('#save:checked').size() == 1 && $('#newpassword').val() == '' && $('#newpasswordconfirm').val() == '') {
				$(this).next(".error").text("Password and confirm password are missing");
				return false;
			} else if ($('#save:checked').size() == 1 && $('#newpassword').val() != $('#newpasswordconfirm').val()) {
				$(this).next(".error").text("Password and confirm password don't match");
				return false;
			} else if ($('#currentpassword').size() == 1 && $('#currentpassword').val() != '' && $('#newpassword').val() == '' && $('#newpasswordconfirm').val() == '') {
				$(this).next(".error").text("Password and confirm password are missing");
				return false;
			} else if ($('#currentpassword').size() == 1 && $('#currentpassword').val() != '' && $('#newpassword').val() != $('#newpasswordconfirm').val()) {
				$(this).next(".error").text("Password and confirm password don't match");
				return false;
			}
		} else if ($(this).hasClass("market")) {
			if ($('input[name="market"]:checked').size() == 0) {
				$(this).next(".error").text("Please select a market");
				return false;
			}
		}
		current++;
		$(this).closest("dd").slideUp(500, function() {
			$('dl#orderform dd:eq('+current+')').slideDown(500, function() {
				$('html, body').animate({
					scrollTop: $('dl#orderform dd:eq('+current+')').prev().offset().top
				}, 500);
			});										
		});
		return false;
	});
	
	$('.prevbtn').click(function() {
		current--;
		$(this).closest("dd").slideUp(500, function() {
			$('dl#orderform dd:eq('+current+')').slideDown(500, function() {
				$('html, body').animate({
					scrollTop: $('dl#orderform dd:eq('+current+')').prev().offset().top
				}, 500);
			});										
		});
		return false;
	});
	
	$('input.products').click(function() {
		var ordered = 0;
		$('input[name^="quantity"]').each(function() {
			ordered += $(this).val();						   
		});
		if (ordered == 0) {
			$(this).next(".error").text("You must order at least one product");
			return false;
		}
	});
	
	var searchBoxVal = "Search";
	
	if ($("#search_input").val() == '') {
		$("#search_input").val(searchBoxVal);
	}
	
	$("#search_input").focus(function() {
		if ($(this).val() == searchBoxVal) {
			$(this).val("");
		}
	});
	
	$("#search_input").blur(function() {
		if ($(this).val() == '') {
			$(this).val(searchBoxVal);
		}
	});
	
	$('#menu ul li:last-child').addClass("last");
	
	// ie6
	if ($.browser.msie && $.browser.version.substr(0,1)==6) {
		$("body").addClass("ie6");
		$("body").addClass("ie7");
		DD_belatedPNG.fix('.logo,.facebook,.twitter,.youtube,.textclub,.loveit,#leftMenu,#rightLogos,#footer span,.fred,.streamline,.box,.submit,.nivo-directionNav a');
	} else if ($.browser.msie && $.browser.version.substr(0,1)==7) {
		$("body").addClass("ie7");
	}
	
	$('.forgot').hide();
	$('a.forgotpass').click(function() {
		$('.login').slideUp();
		$('.forgot').slideDown();
		return false;								 
	});	
});
