jQuery(document).ready(function(){

	if(location.pathname == '/shop/' && document.referrer.match(/products/)){
		window.location = '#gear';	
	}
	jQuery('#sidebar a').attr('title', '');

	jQuery('input[name=phone]').mask("(999) 999-9999");
	//Empty cart popup
	/*jQuery('#cart-link.empty').bind('click', function(){
	
		jQuery('#emptycart').show();
		jQuery('#clickcatch').show().bind('click', function(){
			jQuery('#emptycart').hide();
			jQuery('#clickcatch').hide();
		});
		return false;
	});*/
	
	if(jQuery('#single_product_page_container')[0]){
		jQuery('#thumbnails').jqzoom({
			zoomWidth : 331,
			zoomHeight: 393,
			xOffset: 10
		});
		
		
		var tabs = jQuery('#tab-box .tab-content');
		jQuery(tabs[0]).show();
	
		jQuery(jQuery('#tab-box .tab-headers a')[0]).addClass('active');
		jQuery('#tab-box .tab-headers a').bind('click', function(){
			var link = jQuery(this);
			jQuery('#tab-box .tab-headers a.active').removeClass('active');
			link.addClass('active');
			jQuery('#tab-box .tab-content').hide();
			
			new_tab = link.text().toLowerCase();
			
			if(new_tab == 'gift card submission'){
				new_tab = 'giftcard';
			}
			jQuery('#tab-box .tab-' + new_tab).show();
			
			return false;
		});
		
		
		//Left/right arrows for pictures
		jQuery(document).keydown(function(e){
			var current = jQuery('#thumbnail-list .zoomThumbActive');
			
			if(!current[0]){
				current = jQuery(jQuery('#thumbnail-list a')[0]).addClass('zoomThumbActive');
			}
			
		    if (e.keyCode == 37) { 
		    	if(current.parent().prev()[0]){
		    		current.parent().prev().find('a').click();	
		    	} else {
		    		jQuery('#thumbnail-list a').last().click();
		    	}	    
		    	
		    	zoomActive = false;
		    } else if(e.keyCode == 39){
		    	if(current.parent().next()[0]){
		    		current.parent().next().find('a').click();	
		    	} else {
		    		jQuery('#thumbnail-list a').first().click();
		    	}	
		    	
		    	zoomActive = false;    
		    }
		    
		   });
		   
		//zoomThumbActive
		
		
	}
	
		
	
	jQuery('#sameAsBilling').click(function(){
		var el = jQuery(this);
		
		//if(el.attr('checked')){
			jQuery('#wpsc_checkout_form_2').val(jQuery('#wpsc_checkout_form_11').val());
			jQuery('#wpsc_checkout_form_3').val(jQuery('#wpsc_checkout_form_12').val());
			jQuery('#wpsc_checkout_form_4').val(jQuery('#wpsc_checkout_form_13').val());
			jQuery('#wpsc_checkout_form_5').val(jQuery('#wpsc_checkout_form_14').val());
			jQuery('#wpsc_checkout_form_6').val(jQuery('#wpsc_checkout_form_15').val());
			
			jQuery('#wpsc_checkout_form_8').val(jQuery('#wpsc_checkout_form_17').val());
			
			jQuery('select.country option:selected').removeAttr('selected');
			jQuery('select.country option[value=' + shippingCountry + ']').attr('selected', 'selected');
		//}
		
		//console.log('d');
	});
	
	if(jQuery('select.country')[0]){
		updateCountryList();
		jQuery('select.country').change(updateCountryList);
		
		updateStateList();
		jQuery('#usStates').change(updateStateList);
		jQuery('#caStates').change(updateStateList);
	}
	
	jQuery('#billingForm').submit(function(){

		var good = true;
		jQuery('#billingForm input:visible').each(function(i){
			var el = jQuery(this);
			
			if(el.attr('title') != 'billingaddress' && el.val().length < 1){
				good = false;
			}
		});
		
		/*if(jQuery('#billingForm textarea').val().length < 1){
			good = false;
		}*/
		
		if(!good){
			
			alert('Please fill out all fields before continuing');
			return false;
		}

	});
	
	jQuery('#shippingForm').submit(function(){
		
		var good = true;
		jQuery('#shippingForm input:visible').each(function(i){
			var el = jQuery(this);
			
			if(!el.hasClass('optional') && el.val().length < 1){
				good = false;
			}
		});
		
/*		if(jQuery('#shippingForm textarea').val().length < 1){
			good = false;
		}
*/		
		if(!good){
			
			alert('Please fill out all fields before continuing');
			return false;
		}
	
	
		jQuery('#zipcode').val(jQuery('#wpsc_checkout_form_17').val());
		var params = {
			country	 : jQuery('#wpsc_checkout_form_16').val(),
			region : jQuery('#region').val(),
			wpsc_ajax_actions : 'update_location',
			wpsc_submit_zipcode : 'Calculate',
			wpsc_update_location : true,
			zipcode :	22203
		};
		
		jQuery.post('/index.php', params, function(data){
			ready = true;
			jQuery('#shippingForm').submit();
		});
		
		return ready;
	//	jQuery('#zipcode').val(jQuery('#wpsc_checkout_form_17').val());*/
	
	return true;
	});
	
	jQuery('.comments-link a').bind('click', function(){
		jQuery('#comments').show();
	});
	
	jQuery('#sidebar a.rsswidget').attr('target', '_blank');
	
});

var ready = false;

function updateStateList(){
	var el = jQuery('#usStates:visible, #caStates:visible');
	jQuery('.wpsc_checkout_form_15').val(el.val());
	jQuery('#region').val(el.val());	
}

function updateCountryList(){
	var el = jQuery('select.country');
	
	switch(el.val()){
		case 'US':
			jQuery('tr.state_row').show();
			jQuery('#usStates').show();
			jQuery('#caStates').hide();
			updateStateList();
			break;
		case 'CA':
			jQuery('tr.state_row').show();
			jQuery('#caStates').show();
			jQuery('#usStates').hide();		
			updateStateList();
			break;
		default:
			jQuery('tr.state_row').hide();
			jQuery('.wpsc_checkout_form_15').val('');
			jQuery('#region').val('');
			break;
	}
	

}

//Mailchimp
	
jQuery(document).ready(function(){
	var el = jQuery('input[name=ns_widget_mailchimp_email]');
	el.val('Mailing list').addClass('gray');
	
	el.focus(function(){
		var el = jQuery(this);
		if(el.val() == 'Mailing list'){
			el.val('').removeClass('gray');
		}
	});
	
	el.blur(function(){
		var el = jQuery(this);
		if(el.val() == ''){
			el.val('Mailing list').addClass('gray');
		}	
	});
	
});


function shippingoption(key, key1){
	var params = {
		wpsc_ajax_action : 'update_shipping_price',
		key : key,
		key1 : key1,
		step : 4
	}
	
	jQuery.post( 'index.php', params, function(returned_data) {
	//	window.location = '/products-page/checkout/?step=4';
	
	});
}

