// JavaScript Document
jQuery(function() {
	productTabify();
	
	jQuery('#newsletter-signup input[type=text]').each(function() {
		jQuery(this).val(jQuery(this).attr('rel'));
		jQuery(this).focus(function() {
			if (jQuery(this).val() == jQuery(this).attr('rel') ) {
				jQuery(this).val('');
			}
		}).blur(function() {
			if (jQuery(this).val() == '') {
				jQuery(this).val(jQuery(this).attr('rel'));
			}
		});
	});
	
	jQuery('#newsletter-signup').submit(function() {
		if (jQuery('#ContactName').val() == jQuery('#ContactName').attr('rel')  ||  jQuery('#ContactName').val() == '') {
			alert("Please enter your name");
			jQuery('#ContactName').focus();
			return false;
		}
		if (jQuery('#ContactEmail').val() == jQuery('#ContactEmail').attr('rel')  ||  jQuery('#ContactEmail').val() == '') {
			alert("Please enter your email address");
			jQuery('#ContactEmail').focus();
			return false;
		}
		if (jQuery('#ContactEmail').val().indexOf('@',0)==-1  ||  jQuery('#ContactEmail').val().indexOf('.',0)==-1) {
			alert(jQuery('#ContactEmail').val() + ", " + "is an invalid email address.")
			jQuery('#ContactEmail').focus();
			return false;
		}
		
		alert("Thank you for subscribing to our newsletter");
		return true;
	});
});

function productTabify() {
	var t = jQuery('.product-page-tabs');
	if (!t.length) { return; }
	
	jQuery(t).prepend("<div class='product-page-tab-headings'></div>");
	
	// remove related products if none available
	if (!jQuery('ul.product-related-products').length) {
		jQuery('.product-related-products').remove(); // h2 and div
	}
	
	var h2 = jQuery(t).find('h2').each(function(e) {
		jQuery(this)
			.addClass('inactive')
			.css({cursor:'pointer'})
			.click(function() {
				jQuery('div.product-page-tab-headings h2').addClass('inactive');
				var tabclass = jQuery(this).removeClass('inactive').attr('class');
				productShowTab(tabclass);
			});
		jQuery('.product-page-tab-headings').append(this);
	});
	
	//var first = jQuery(t).get(0);
	//jQuery(first).click();
	
	productShowTab('product-description', true);
}

function productShowTab(tabclass, first) {
	jQuery('div.product-page-tab')
		.stop()
		.hide();
	jQuery('div.'+tabclass).fadeIn('fast');
	jQuery('h2.'+tabclass).removeClass('inactive');
	
	if (!first) {
		jQuery('html,body').animate(
			{ scrollTop: jQuery('.product-page-tab-headings').offset().top },
			{ duration:'fast', easing:'linear' }
		);
	}
}

jQuery(document).ready(function() {
	jQuery("span").each(function(i, e) {
		if ( (jQuery(e).attr('class') == "price") && (jQuery(e).text() == "£0.00") ) {
			jQuery(e).text("Coming Soon");
		}
	})
})

