(function($){
	$('document').ready(function(){
		// Popups
		$('[rel=popup]').click(function(e){
			e.preventDefault();
			window.open($(this).attr('href'), '', 'width=660,height=860,scrollbars,resizable');
		});
		// Input values
		$('.add_title').each(function(){
			$(this).val($(this).attr('title'));
			$(this).css('color', '#AAA');
			$(this).blur(function(){
				if(!$(this).val())
				{
					$(this).css('color', '#AAA');
					$(this).val($(this).attr('title'));
				}
			});
			$(this).focus(function(){
				$(this).css('color', '#000');
				if($(this).val() == $(this).attr('title'))
					$(this).val('');
			});
		});
		// Item Select (co page)
		$('.url_select select').change(function(){
			window.location = $(this).val();
		});
		$('.fly').mouseover(function(){
			$(this).addClass('hover');
		});
		$('.fly').mouseout(function(){
			$(this).removeClass('hover');
		});
		try
		{
			$('[rel=lightbox]').lightBox();
		}
		catch(e){}
		// Email signup form validation
		$('.newsletter_signup').submit(function(event){
			if(!($('[name=fullname]').val() && $('[name=email]').val()))
			{
				event.preventDefault();
				alert('Please provide your name and a valid email address.');
			}
		});
	});
	
	
	// Wholesale Page
	window.setTimeout(function(){update_totals();}, 150);
	
	$('.quantity').live('change', function(){
		update_totals();
	});
	
	$(':input.quantity').live('keypress', function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13)
		{
			e.preventDefault();
			update_totals();
		}
	});
	
	$('.quantity').live(':input.change', function(){
		update_totals();
	});
	
	$('.header_row .update').live('click', function(e){
		e.preventDefault();
		update_totals();
		go_to_totals();
	});
	
	function update_totals()
	{
		var subtotal = get_subtotal();
		update_savings_helper_box(subtotal);
		update_percent_columns(subtotal);
		var discount = get_discount(subtotal);
		var discounted = get_discounted_subtotal(subtotal);
		
		$('#subtotal_top').val('$' + subtotal);
		$('#subtotal_bottom').val('$' + subtotal);
		$('#discount_top').val('-$' + discount);
		$('#discount_bottom').val('-$' + discount);
		$('#discounted_top').val('$' + discounted);
		$('#discounted_bottom').val('$' + discounted);
		
		if(is_discounted(subtotal))
		{
			$('#subtotal_top').css('font-weight', 'normal');
			$('#subtotal_bottom').css('font-weight', 'normal');
			$('#subtotal_top').css('color', '#555');
			$('#subtotal_bottom').css('color', '#555');
			$('.discount_only').show();
			if(get_discount_decimal(subtotal) == Number('.10'))
			{
				$('th.col_14_off_price').removeClass('highlight');
				$('th.col_sale').removeClass('highlight');
				$('th.col_wholesale').removeClass('highlight');
				$('th.col_10_off_price').addClass('highlight');
			}
			else if(get_discount_decimal(subtotal) == Number('.14'))
			{
				$('th.col_10_off_price').removeClass('highlight');
				$('th.col_sale').removeClass('highlight');
				$('th.col_wholesale').removeClass('highlight');
				$('th.col_14_off_price').addClass('highlight');
			}
		}
		else
		{
			$('th.col_10_off_price').removeClass('highlight');
			$('th.col_14_off_price').removeClass('highlight');
			$('th.col_sale').addClass('highlight');
			$('th.col_wholesale').addClass('highlight');
			
			$('#subtotal_top').css('font-weight', 'bold');
			$('#subtotal_bottom').css('font-weight', 'bold');
			$('#subtotal_top').css('color', '#000');
			$('#subtotal_bottom').css('color', '#000');
			$('.discount_only').hide();
		}
	}
	
	function go_to_totals()
	{
		$('html,body').animate({scrollTop: $('#subtotal_top').offset().top - 10}, 400);
	}
	
	function is_discounted(subtotal)
	{
		if(get_discount(subtotal) > 0)
			return true;
		return false;
	}
	
	function update_savings_helper_box(subtotal)
	{
		if(subtotal < Number('250.00'))
		{
			$('.amount_to_add').text('$' + (Number('250.00') - subtotal).toFixed(2));
			$('.percent_to_save').text('10');
			$('.savings_help').show();
			return;
		}
		if(subtotal < Number('500.00'))
		{
			$('.amount_to_add').text('$' + (Number('500.00') - subtotal).toFixed(2));
			$('.percent_to_save').text('14');
			$('.savings_help').show();
			
			return;
		}
		$('.savings_help').hide();
	}
	
	function update_percent_columns(subtotal)
	{
		$('td.col_10_off_price').html('&nbsp;');
		$('td.col_14_off_price').html('&nbsp;');
		var discount_decimal = get_discount_decimal(subtotal);
		$('.item_row').each(function(){
			var text_price = $(this).find('.final_price').text();
			var re = /\$+/;
			var price = Number(text_price.replace(re, ''));
			if(discount_decimal == Number('.14'))
			{
				$(this).find('.col_14_off_price').text('$' + (price - price * discount_decimal).toFixed(2));
			}
			else if(discount_decimal == Number('.10'))
			{
				$(this).find('.col_10_off_price').text('$' + (price - price * discount_decimal).toFixed(2));
			}
		});
	}
	
	function get_discounted_subtotal(subtotal)
	{
		return (subtotal - get_discount(subtotal)).toFixed(2);
	}
	
	function get_discount(subtotal)
	{
		return (subtotal * get_discount_decimal(subtotal)).toFixed(2);
	}
	
	function get_discount_decimal(subtotal)
	{
		var discount_decimal =  Number('.0');
		if(subtotal > 250)
			var discount_decimal = Number('.10');
		if(subtotal > 500)
			var discount_decimal = Number('.14');
		return discount_decimal;
	}
	
	function comma_format(value)
	{
		value += '';
		x = value.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
	
	function get_subtotal()
	{
		var subtotal = 0;
		$('.item_row').each(function(){
			var text_price = $(this).find('.final_price').text();
			var re = /\$+/;
			var price = Number(text_price.replace(re, ''));
			var text_qty = $(this).find('.quantity').val();
			var quantity = parseInt('0');
			parsed_qty = parseInt(text_qty);
			if(parsed_qty)
					quantity += parsed_qty;
			subtotal += price * quantity;
		});
		return subtotal.toFixed(2);
	}
})(jQuery);

