$('document').ready(function(){
	
	//	ALL PAGES
	
	//header stores dropdown
	$('DIV#dl_header SELECT').change(function(){
		if ($(this).val()) {
			window.location = '/' + $(this).val();
		}
	});
	
	//autocomplete
	if ($('#search INPUT').length) {
		$('#search INPUT').autocomplete('/autocomplete.php', {
			minChars: 2,
			delay: 40,
			maxItemsToShow: 10
		});
	}
	
	
	
	//	CONTENT PAGES
	
	//content page stores dropdown
	$('DIV#stores SELECT').change(function(){
		if ($(this).val()) {
			window.location = '/' + $(this).val();
		}
	});
	
	//search form
	$('DIV#search FORM').submit(function(){
		window.location = '/all/search/' + $('DIV#search FORM INPUT[@name=search]').val().replace(/ /g,'+');
		return false;
	});

		//custom search form	
		$('DIV.search FORM').submit(function(){
			var cleantitle = $(this).attr('id').split('genre_').join('');
			window.location = '/' + cleantitle + '/search/' + $('DIV.search FORM INPUT[@name=search]').val().replace(/ /g,'+');
			return false;
		});
	
	$('DIV#search INPUT').focus(function(){
		if ($(this).val() == 'Search for a Product!') {
			$(this).val('');
		}
	});
	
	$('DIV.search INPUT').focus(function(){
		if ($(this).val() == 'Search for a Product!') {
			$(this).val('');
		}
	});
	
	$('DIV#search INPUT').blur(function(){
		if ($(this).val() == '') {
			$(this).val('Search for a Product!');
		}
	});
	
	
	
	//	BROWSE PAGES
	
	//browse form
	$('.browse_price FORM').submit(function(){
		if ($(this).children('select').length) {
			select = $(this).children('select')[0];
			
			if (select.options[select.selectedIndex].value == '') {
				alert('Please select a size first.');
				return false;
			}
		}
		
		return true;
	});
	
	//label vendor
	$('SELECT[@name=children]').change(function(){
		var main = $(this).attr('id').replace(/children_/,'');
		
		if ($(this).val() == 'all') {
			window.location = '/' + main;
		} else {
			window.location = '/' + main + '/' + $(this).val();
		}
	});
	
	//label child
	$('SELECT[@name=sibling]').change(function(){
		var main = $(this).attr('id').replace(/sibling_/,'');
		
		if ($(this).val() == 'all') {
			window.location = '/' + main;
		} else {
			window.location = '/' + $(this).val();
		}
	});
	
	
	
	//	FAQ PAGES
	
	//faq toggle
	$('DIV.faq_question').click(function(){
		var id = $(this).attr('id').replace('question_','');
		var answer = $('DIV#answer_' + id);
		
		if (answer.css('display') == 'none') {
			$.ajax({
				type: "GET",
				url: "faq_record",
				data: "faq_id=" + id
			});
		}
		answer.toggle();
	});
	
	
	
	//	DETAIL PAGE
	
	//sizes
	if ($('#product_cart')) {
		function check_sizes() {
			var val = '';
			
			$('#product_cart input[@type=radio]').each(function(){
				if ($(this).attr('checked')) {
					val = $(this).val();
					$(this).parent().addClass('selected');
				} else {
					$(this).parent().removeClass('selected');
				}
			});
			
			if (!val) {
				$('#product_cart input[@type=radio]').each(function(){
					$(this).attr('checked','checked');
				});
			}
		}

		check_sizes();
		
		$('#product_cart input[@type=radio]').click(function(){
			check_sizes();
		});
		$('#product_cart input[@type=radio]').change(function(){
			check_sizes();
		});
	}
	
	
	
	
	//	CHECKOUT PAGE
	
	//cc / paypal option
	if ($('SELECT#field_payment_method')) {
		var info_arr = new Array(
			'first_name',
			'last_name',
			'address',
			'city',
			'state',
			'zip',
			'province',
			'country'
		);
		
		var cc_arr = new Array(
			'card_type',
			'card_num',
			'card_code',
			'exp_date'
		);
		

		function switchPayPal(obj) {
			for (i=0;i<cc_arr.length;i++) {
				if (obj.val() == 'PayPal') {
					$('DIV#container_payment_information_' + cc_arr[i]).hide();
				} else {
					$('DIV#container_payment_information_' + cc_arr[i]).show();
				}
			}
		
			if (obj.val() == 'PayPal') {
				$('SPAN#paypal_instructions').show();
			} else {
				$('SPAN#paypal_instructions').hide();
			}
		}
		
		switchPayPal($('SELECT#field_payment_method'));
		
		$('SELECT#field_payment_method').change(function(){
			//console.log($(this).val());
			switchPayPal($(this));
		});
		
		
		//use billing for shipping
		$('INPUT#field_use_billing').change(function(){
			if ($(this).attr('checked')) {
				for (i = 0; i <= info_arr.length; i++) {
					if ($('INPUT[@name=shipping_information_' + info_arr[i] + ']').length) {
						$('INPUT[@name=shipping_information_' + info_arr[i] + ']').val($('INPUT[@name=billing_information_' + info_arr[i] + ']').val());
					} else {
						$('SELECT[@name=shipping_information_' + info_arr[i] + ']').val($('SELECT[@name=billing_information_' + info_arr[i] + ']').val());
					}
				}
			}
		});
		
		
		//countries
		function switch_international(type) {
			var country = $('SELECT#field_' + type + '_information_country').val();
			
			if (country == 'United States' || country == '') {
				$('INPUT#field_' + type + '_information_state').removeAttr('disabled');
				$('INPUT#field_' + type + '_information_province').attr('disabled','disabled');
			} else {
				$('INPUT#field_' + type + '_information_state').attr('disabled','disabled');
				$('INPUT#field_' + type + '_information_province').removeAttr('disabled');
			}
		}
		
		switch_international('billing');
		switch_international('shipping');
		
		$('SELECT#field_billing_information_country,SELECT#field_shipping_information_country').change(function(){
			var type = $(this).attr('id').replace(/field_/,'').replace(/_information_country/,'');
			switch_international(type);
		});
	}
});



















