/* Author: 

*/
function checkEmail(email) { 
	var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailVal = $("#" + email).val();
	return pattern.test(emailVal);
}
$(document).ready(function() {
	
	$('.newsletter-sub').focus(function() {
		$(this).addClass('focused');
		$(this).data('origText',$(this).val());
		$(this).val('');
	}).blur(function() {
		$(this).removeClass('focused');
		if($(this).val() == "") {
			$(this).val($(this).data('origText'));
		}
	});
	
	if($('#LeftMenu').length) {
		$('#LeftMenu li a').removeClass('active');
		var loc = window.location.href;
		//console.log(loc);
		var theFile = loc.substring(loc.lastIndexOf('/') + 1, loc.length);
		$('#LeftMenu a').each(function() {
			var theLink = $(this).attr('href');
			var theLinkFile = theLink.substring(theLink.lastIndexOf('/') + 1, theLink.length);
			if(theLinkFile == theFile) {
				//console.log($(this));
				$(this).addClass('active');
			}
		});
	}
	if($('#Menu').length) {
		var loc = window.location.href;
		//console.log(loc);
		var theFile = loc.substring(loc.lastIndexOf('/') + 1, loc.length);
		$('#Menu a').each(function() {
			var theLink = $(this).attr('href');
			var theLinkFile = theLink.substring(theLink.lastIndexOf('/') + 1, theLink.length);
			if(theLinkFile == theFile) {
				$(this).closest('li').addClass('ActivePage');
			}
		});
	}
	if($('#SideAccountMenu').length) {
		$('#SideAccountMenu li a').removeClass('active');
		var loc = window.location.href;
		//console.log(loc);
		var theFile = loc.substring(loc.lastIndexOf('/') + 1, loc.length);
		$('#SideAccountMenu a').each(function() {
			var theLink = $(this).attr('href');
			var theLinkFile = theLink.substring(theLink.lastIndexOf('/') + 1, theLink.length);
			if(theLinkFile == theFile) {
				//console.log($(this));
				$(this).addClass('active');
			}
		});
	}
	if($('.bottomlinks').length) {
		var loc = window.location.href;
		//console.log(loc);
		var theFile = loc.substring(loc.lastIndexOf('/') + 1, loc.length);
		$('.bottomlinks a').each(function() {
			var theLink = $(this).attr('href');
			var theLinkFile = theLink.substring(theLink.lastIndexOf('/') + 1, theLink.length);
			if(theLinkFile == theFile) {
				$(this).closest('li').addClass('active');
			}
		});
	}
	if($('#SideCategoryList').length) {
		var loc = window.location.href;

		$('#SideCategoryList a').each(function() {
			var theLink = $(this).attr('href');
			if(theLink == loc) {
				$(this).closest('li').addClass('active');
			}
		});
	}
	
	$('.product-cat').delegate('li','mouseenter mouseleave click', function(evt) {
		if(evt.type == 'mouseenter') {
			$(this).closest('li').siblings().css('opacity',0.4);
		}
		if(evt.type == 'mouseleave') {
			$(this).closest('li').siblings().css('opacity',1);
		}
		if(evt.type == 'click') {
			window.location.href = $(this).find('.cat-img a').attr('href');
		}
		return false;
	});
	
	/*
	if($('.ProductList').length) {
		$('.ProductList li').each(function(){
			var theText = $(this).find('.ProductDetails a').text();
			var str1 = theText.substr(0,8);
			var str2 = theText.substr(8,theText.length);
			if(str1 == "Crashmat" || str1 == "crashmat") {
				$(this).find('.ProductDetails a').text('');
				$(this).find('.ProductDetails a').html(str1 + '<br />' + str2);
			}
		});
	}
	*/
	$('.ProductList').delegate('li','click',function(evt) {
		if(evt.type == 'click') {
			window.location.href = $(this).find('.ProductImage a').attr('href');
		}
		return false;
	});
	
	
	$('.ViewDetails').delegate('a','hover',function(evt,target){
		evt.preventDefault();
		if(evt.type == 'mouseover') {
			$(this).addClass('hover');
		}
		if(evt.type == 'mouseout') {
			$(this).removeClass('hover');
		}
	});

	$('.ProductThumbImage').delegate('a','mouseenter mouseleave',function(evt,target) {
		if(evt.type == 'mouseenter') {
			$('.EnlargeIcon').show();
		}
		if(evt.type == 'mouseleave') {
			$('.EnlargeIcon').hide();
		}
	});
	$('.ProductTinyImageList li').delegate('a','mouseenter mouseleave',function(evt) {
		if(evt.type == 'mouseenter') {
			$(this).stop().fadeTo('fast',0.5);
		}
		if(evt.type == 'mouseleave') {
			$(this).stop().fadeTo('fast',1);
		}
	});
	
	/* Stockist page functionality */
	// tabs
	$('#stockist-tabs').delegate('a','click',function(evt) {
		var theID = $(this).attr('href');
		$(this).closest('ul').find('li').removeClass('selected');
		$(this).closest('li').addClass('selected');
		$('.stockist').hide();
		$(theID).show();
		
		return false;
	});
	
	// li hover
	$('#PageContent .stockist').delegate('li','mouseenter mouseleave',function(evt) {
		if(evt.type == 'mouseenter') {
			$(this).addClass('hover');
		}
		if(evt.type == 'mouseleave') {
			$(this).removeClass('hover');
		}
	});

	// FAQs accordions
	$('#faqs .question').delegate('h3','click',function(evt) {
		$(this).closest('.question').toggleClass('selected');
	});
	/*
	if($('.ProductDescriptionContainer').length) {
		$('.ProductDescriptionContainer').children().appendTo('.ProductDesc');
	}
	*/
	
	$('#CheckoutStepBillingAddress').delegate('input','keyup',function(evt) {
		var pattern = /\b(?:p\.?\s*o\.?|post\s+office)\s+box\b/i;
		if ($(this).val().match(pattern)) {
			$(this).closest('.ExpressCheckoutContent').find('input[type="submit"]').hide();
			$(this).addClass('error');
		} else {
			$(this).closest('.ExpressCheckoutBlock').find('input[type="submit"]').show();
			$(this).removeClass('error');
		}
	});
	
	$('#CheckoutStepBillingAddress').delegate('#FormField_7','keyup',function() {
		var targ = $(this).val().replace(/[^\d]/g,''); // remove all non-digits
		if(/^[0-9]+$/.test(targ)) {
		} else {
			alert('Please enter a valid telephone number');
			$(this).val('');
		}
	});
	$('#CheckoutStepBillingAddress').delegate('#FormField_19','keyup',function() {
		var targ = $(this).val().replace(/[^\d]/g,''); // remove all non-digits
		if(/^[0-9]+$/.test(targ)) {
		} else {
			alert('Please enter a valid telephone number');
			$(this).val('');
		}
	});
	
});

