$(document).ready(function () {
	$('#cvv2').click(function(e) {
		e.preventDefault();
		window.open('/cvv2.html','cvv2','width=500,height=500');
	});
	$('body#products a.thickbox').fancybox();
	$('#faq div#content div.faq div.grouping').addClass('closed');
	$('#faq div#content div.faq div.grouping h4').click(function(e) {
		$(e.target).parent().toggleClass('closed');
	});
	$('a[href*="http://"]').attr('target','_new_window');
	$('form#ProductAddForm div :input.part').managePartNumber();
	$('form#ProductAddForm div :input.range, form#ProductAddForm div :input.type').blur(function(e) {
		var container = $(e.target).parent().parent();
		if ($(container).find('select.type option:selected').val() != '') {
			$.post('/product_attribute_values',{id: $(container).find('input:hidden').val(),value: $(container).find('input.range').val(),type: $(container).find('select.type option:selected').val()},function(data) {
					if (data.success) {
						$(container).find('div.alert').hide();
					}else{
						$(container).find('div.alert').show()
						$(container).find('div.alert').html(data.message);
					}
			},'json');
		}
	});
	$('form#ProductAddForm').submit(function() {
		var validates = true;
		$(this).find(':input.part').each(function() {
			if ($(this).val() == false) {
				validates = false;
			}
		});
		if (validates == false) {
			if ($('div#flashMessage').length == 0) {
				$('ul.categoryNav').after('<div id="flashMessage" class="error">Required attributes were left blank. Please make sure you have selected all attributes required.</div>');
			}
			return false;
		}else{
			$('div#flashMessage').remove();
		}
	});
	$('input#sameAsBilling').click(function(e) {
		if ($(e.target).is(':checked')) {
			$('#OrderContactName').val($('#OrderCcFirstName').val()+' '+$('#OrderCcLastName').val());
			$('#OrderShippingAddress1').val($('#OrderBillingAddress1').val());
			$('#OrderShippingAddress2').val($('#OrderBillingAddress2').val());
			$('#OrderShippingCity').val($('#OrderBillingCity').val());
			$('#OrderShippingStateId').val($('#OrderBillingStateId').val());
			$('#OrderShippingZip').val($('#OrderBillingZip').val());
			$('#OrderShippingCountryId').val($('#OrderBillingCountryId').val());
			if ($('#OrderShippingCountryId').val() != '23') {
			    $('#OrderShippingProvince').val($('#OrderBillingProvince').val());
			    $('select#OrderShippingStateId').parent().hide();
    			$('input#OrderShippingProvince').parent().show();
		    }
		}
	});
	if ($('select#OrderShippingCountryId :selected').val() == '' || $('select#OrderShippingCountryId :selected').val() == '23') {
		$('select#OrderShippingCountryId option[value="23"]').attr('selected','true');
		$('input#OrderShippingProvince').parent().hide();
	}else{
		$('select#OrderShippingStateId').parent().hide();
	}
	$('select#OrderShippingCountryId').change(function(e) {
		if ($(e.target).find('option:selected').val() == '23') {
			$('input#OrderShippingProvince').parent().hide();
			$('select#OrderShippingStateId').parent().show();
		}else{
			$('select#OrderShippingStateId').parent().hide();
			$('input#OrderShippingProvince').parent().show();
		}
	});
	
	if ($('select#OrderBillingCountryId :selected').val() == '' || $('select#OrderBillingCountryId :selected').val() == '23') {
		$('select#OrderBillingCountryId option[value="23"]').attr('selected','true');
		$('input#OrderBillingProvince').parent().hide();
	}else{
		$('select#OrderBillingStateId').parent().hide();
	}
	$('select#OrderBillingCountryId').change(function(e) {
		if ($(e.target).find('option:selected').val() == '23') {
			$('input#OrderBillingProvince').parent().hide();
			$('select#OrderBillingStateId').parent().show();
		}else{
			$('select#OrderBillingStateId').parent().hide();
			$('input#OrderBillingProvince').parent().show();
		}
	});
	$('form#OrderEditForm input[value="Checkout"]').click(function(e) {
	    //e.stopPropagation();
	    $('form#OrderEditForm').append('<input type="hidden" name="submit" value="Checkout" />');
	});
});

$.fn.extend({
	managePartNumber: function() {
		this.each(function() {
		 	switch($(this).attr('tagName')) {
				case 'INPUT':
					$(this).keyup(function(e) {
						$(e.target).buildPartNumber($(e.target).parent().parent().find('select.type').val());
					});
					break;
				case 'SELECT':
					$(this).change(function(e) {
						$(e.target).buildPartNumber();
					});
					break;
			}
		});
	},
	buildPartNumber: function() {
		var part_number = $(':hidden#ProductName').val() + ' - ';
		var measurement = '';
		$('form#ProductAddForm div :input.part').each(function() {
		    var type = $(this).parent().parent().find('select.type').val()
			if ($(this).val() !== '') {
				switch($(this).attr('tagName')) {
					case 'INPUT':
						part_number += $(this).val() + ((type == 's') ? "\"" : "" ) +' - ';
						break;
					case 'SELECT':
					  if ($(this).find(':selected').val() && (jQuery.inArray(parseInt($(this).find(':selected').val(),10), partNumber) >= 0)) {
							part_number += $(this).find(':selected').text() +' - ';
						}
						break;
				}
			}
		})
		$('div.partNumber span').html(part_number.substring(0,part_number.length - 3));
	}
});