$(document).ready(function(){	
	$('#nav li span:first').remove();
	//Check Stock Levels
	$('#product-form').submit(function(event) {
		$('.product-error').remove();
		$success = true;
		$msg = '';
		$color = jQuery.trim($('#c option:selected').attr('class'));
		$color = $color.replace(/\s+/g, '');
		$color = $color.toLowerCase();
		$size = jQuery.trim($('#s option:selected').val().toLowerCase());
		$quantity = parseFloat($('#num').val());
		//Check Quantity is a proper number and is not equal to 0
		if(isNaN($quantity) == true)
		{
			$msg = 'The value is not a number. Please enter proper value';
			$success = false;
		}
		else if($quantity == 0)
		{
				$quantity = 1;
		}
		else
		{
			$stock = $('#' + $color + $size).html();
			//Take 6 away for stock
			$stock = parseInt($stock) - 6;
			//Out of Stock
			if($stock <= 0 || $stock == '' || $stock == 'null' || isNaN($stock))
			{
				$msg = 'We are currently <strong>sold out</strong> on this size and color for this product. If you would still like to order this item please contact us at <a href="mailto:customercare@leevalleyireland.com">customercare@leevalleyireland.com</a> and we will do our best to fulfil your order';
				$success = false;
			}
			
			//Customer has ordered more than the current stock level
			// Add Five
			else if($stock < $quantity)
			{
				$msg = 'We are currently low on stock for this size and color for this product. We currently have <span class="current-stock">' + $stock + '</span> available of this product. If you would still like to order the same amount for this product please contact us at <a href="mailto:customercare@leevalleyireland.com">customercare@leevalleyireland.com</a> and we will do our best to fulfil your order.';
				$success = false;
			}		
		}
		
		
		if($success == false)
		{
			$('.proddesc').append('<div class="product-error" style="display:none;"><span class="warning"></span>' + $msg + '</div>');
			$('.product-error').fadeIn(1000);
			event.preventDefault();
		}
	});
	
	
	//Change In Stock Status
	$('.product-change').change(function() { checkStock();});
	$('#swatches').click(function() { checkStock();});
	
	
	//Check Stock
	function checkStock() {
		$color = jQuery.trim($('#c option:selected').attr('class'));
		$color = $color.replace(/\s+/g, '');
		$color = $color.toLowerCase();
		$size = jQuery.trim($('#s option:selected').val().toLowerCase());
		$stock = $('#' + $color + $size).html();
		$stock = parseInt($stock) - 6;
		$('#current-stock-level').hide();
		if($stock <= 0 || $stock == '' || $stock == 'null' || isNaN($stock)) {
			$('#current-stock-level').addClass('err');
			$('#current-stock-level').html('Out of Stock');
			
		} else {
			$('#current-stock-level').removeClass('err');
			$('#current-stock-level').html('In Stock');
		}
		$('#current-stock-level').fadeIn(1000);
	}
	
	
	//Change Currency text 
	$count =0;
	setInterval(function(){ 
	$count++;
		if($count == 1){ $('.change').html('&pound;');}	
		else if($count == 2){ $('.change').html('$');}	
		else if($count == 3){ $('.change').html('&euro;'); $count = 0;}
	}, 3000);
	
	//products
	$(".home .product-listing:nth-child(4n+1)").addClass('clearRow');
	$(".interior .product-listing:nth-child(3n+1)").next().addClass('clearRow');
	
	//nav bar li's
	$("#nav li ul").each(function(index) {
		$count = $(this).children("li").length;
		$(this).find("li:nth-child("+ $count + ")").addClass('last');
		});
	
	// search box functionality
	var defaultsearchtext = 'Keyword or Product Code';
	$('#searchvalue').attr('value', defaultsearchtext); 
	$('#searchvalue').focus(function(){
		$(this).attr('value', ( 
			($(this).attr('value') == defaultsearchtext) ? '' : $(this).attr('value') ) 
		)
	}); 
	$('#searchvalue').blur(function(){
		$(this).attr('value', ( 
			($(this).attr('value') == '') ? defaultsearchtext : $(this).attr('value') ) 
		)
	});
	
	// subscription box functionality
	var defaultsubscribetext = 'Your Email Address';
	// $('#subscribe').attr('value', defaultsubscribetext); 
	$('#subscribe').focus(function(){
		$(this).attr('value', ( 
			($(this).attr('value') == defaultsubscribetext) ? '' : $(this).attr('value') ) 
		)
	}); 
	$('#subscribe').blur(function(){
		$(this).attr('value', ( 
			($(this).attr('value') == '') ? defaultsubscribetext : $(this).attr('value') ) 
		)
	});
	var defaultnametext = 'Your Name';
	// $('#name').attr('value', defaultnametext); 
	$('#name').focus(function(){
		$(this).attr('value', ( 
			($(this).attr('value') == defaultnametext) ? '' : $(this).attr('value') ) 
		)
	}); 
	$('#name').blur(function(){
		$(this).attr('value', ( 
			($(this).attr('value') == '') ? defaultnametext : $(this).attr('value') ) 
		)
	});
	
// login form
	$('#ctype-ret').click(function() {
		$('#p').focus();
		$('#paneconfirm').hide();
		$('#emailconfirm').hide();
		$('#panereset').show();
    });
	$('#ctype-new').click(function() {
		$('#p').focus();
		$('#paneconfirm').show();
		$('#emailconfirm').show();
		$('#panereset').hide();
    });
	
	if ($('#ctype-ret').attr('checked')) {
		$('#paneconfirm').hide();
		$('#emailconfirm').hide();
		$('#panereset').show();
	}
	if ($('#ctype-new').attr('checked')) {
		$('#paneconfirm').show();
		$('#emailconfirm').show();
		$('#panereset').hide();
	}

	// checkout address form
	$("input[name='atype']").click(function(){
		addressSelect();
	});
	addressSelect();
	
	// checkout payment form
	$("input[name='ptype']").click(function(){
		paymentSelect();
	});
	paymentSelect();
	
	// email a friend form
	$('#emailafriend').hide();
	$('#email-a-friend').click(function(){
		$('#emailafriend').toggle();
		return false; 
	});
	
	// product images (lightbox)
	$('a[rel*=lightbox]').lightBox({
		overlayBgColor: '#666',
		overlayOpacity: 0.6
	});

	// product tabs 
	
	// hide all except first tab content
	$('#tabs div:not(:first)').hide();
	
	// show current tab content, hide others
    $('#tabs ul a').click(function(){
		$('.current').removeClass('current');
		$(this).parent().addClass('current');
		
		showtab = $(this).attr('href');
		// alert( showtab );
		$( showtab ).show();
		$('#tabs div:not( '+showtab+' )').hide();
		
		return false;
    });
	
	// hide submenus by default (unless its current), toggle on click
	$('.setcategories ul li:not(.current) ul').hide();
	$('.setcategories ul li a').click(function() {
		$(this).next().toggle();
		if ($(this).attr('href') == '#') {
			return false;
		}
	});
	
	// new image viewer
	$('.tbhook').click(function(){
		// tb_show('Other Views',"#TB_inline?height=600&width=555&inlineId=tb_target&modal=true",false);
		tb_show('Other Views',"#TB_inline?height=550&width=555&inlineId=tb_target&modal=true",false);
		return false; 
	});
	$('#tbcl').click(function(){
		tb_remove();
		return false; 
	});
	$('#imgcol a').click(function() {
		$('#imgbarmain').attr( 'src', $(this).attr('href') );
		
		return false;
	});
	
	// currency changer
	$('#currchg').click(function() {
		$('#currchg ul').toggle();
	});
	
	// colour swatch stuff
	$('#swatches img').click(function(){
		var swatch = $(this).attr('rel');
		var swatchvalues = swatch.split(' ');
		// swatchvalues[0] contains larger image path
		// swatchvalues[1] contains swatch title
		var swatchimg = swatchvalues.shift();
		var swatchtext = swatchvalues.join(' ');
		$('#pim').attr('src', swatchimg);
		$('#swatchtitle').text(swatchtext);
		$('#c').val(swatchtext);
	});

});

function addressSelect() {
	$('.addressitem').hide();
	$("input[name='atype']:checked").parent().next('div').show(); 
}

function paymentSelect() {
	$('.paymentitem').hide();
	$("input[name='ptype']:checked").parent().next('div').show(); 
}
