$(function() {


$('input:text[id*="qty"]').bind('keyup',function() {
	$.post("ajax_addToCart.php", { qty: this.id} );
});


function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}


/*
$('#add-one span').bind('click', function() {
	$('html,body').animate({
	    scrollTop: '+=' + $('#respond').offset().top + 'px'
	}, 'fast');
});
*/

$(".scroll").click(function(event){		
	event.preventDefault();
	$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});



/* ============================================
  TROUBLESHOOTING
============================================ */

$('.session-destroy').bind('click', function() {
	
	$.ajax({    
		type: "post",
		url: '/wp-content/themes/omgfriends/scripts/ajax_sessionDestroy.php',
		dataType: "text",
		data: {item_id : this.id},
		success: function(request) {
			alert('session destroyed!');
		} // End success
	}); // End ajax method
	
});



/* ============================================
  HOVERBOXING
============================================ */

if ( $(document).width() > 979 ) {
	$('.hoverbox h2').css({bottom: '260px', display: 'table'});
}

$('.hoverbox div a').hover(
	function() {
		$(this).find('h2').animate({bottom: 0}, 300);
	}, function() {
		$(this).find('h2').animate({bottom: '260px'}, 300);
	}
);





/* ============================================
  CART AJAX
============================================ */

// when adding to cart...

$('.add-to-cart').bind('click', function() {
	
	$.ajax({    
		type: "post",
		url: '/wp-content/themes/omgfriends/scripts/ajax_addToCart.php',
		dataType: "text",
		data: {item_id : this.id},
		success: function(request) {
			$('.cart-empty').removeClass('cart-empty').addClass('cart-full');
			alert('added to cart!');
		} // End success
	}); // End ajax method
	
});



// when updating qty input…

$('input.item-quantity').bind('keyup', function() {
	$item_id = this.id;
	$item_qty = $(this).attr('value');
	
	if ( isNumber($item_qty) ) {
		
		$.post('/wp-content/themes/omgfriends/scripts/ajax_updateItemQty.php', { qty: $item_qty, item: this.id }, function(data) {
			$('#'+$item_id).parent().parent().find('.item-total').html('$'+data.item_total);
			$('#cart-subtotal span').html('$'+data.new_total);
		}, "json");
		
	} else {
		
		$(this).attr('value', 0);
		
	}
});



// when clicking 'remove'...

$('.remove-item').click(function() {
	$the_item = this.id;
	$.post('/wp-content/themes/omgfriends/scripts/ajax_removeFromCart.php', { remove: this.id }, function(data) {
		$('#'+$the_item).parentsUntil('.cart-item').parent().animate({opacity: 0}, 500, function() {
			$(this).remove();
		});
		
		if (data.new_count < 1) {
			$('#cart-status').removeClass().addClass('cart-empty');
		}
		
		if (data.new_items < 1) {
			$('#cart-items').css('display', 'none');
			$('#empty-cart-info').css('display', 'block');
		}
		
		$('#cart-subtotal span').html('$'+data.new_total);
		
	}, "json");
});











/* ============================================
  SLIDESHOWING
============================================ */

function slideSwitch(slide_container, slide_controls, next_id) {
	
	// disable too-fast clicking
	$click_items = $('.clickable');
	$click_items.removeClass('clickable');
	
	// figure out where we are now
	var $current = $(slide_container+' div.current');
	if ($current.length == 0) $current = $(slide_container+' > div:last-child');
	
	// find out where to go next
	$next = $('#'+next_id);
	
	if ($next.length == 0) {
		
		if (next_id == 'prev') {
			$next = $current.prev();
			if ($next.length == 0) {
				$next = $(slide_container+' > div:last');
			}
		} else { // it's just a "next"
			if ( $current.next().length == 0 ) {
				$next = $(slide_container+' > div:first');
			} else {
				$next = $current.next();
			}
		}
		
	}
	
	// prevent z-index weirdness
	$(slide_container+'> div:not(.current)').css({'opacity': 0.0});
	$(slide_container+'> div').css({'z-index': 99}).removeClass('current');
	$(slide_controls).css({'z-index': 101});
	
	// move the dot/marker color
	$(slide_controls+' .current').removeClass('current');
	$('.'+$next.attr('id')).addClass('current');
	
	
	$next.css({'z-index': '100'})
		.addClass('current')
		.animate({opacity: 1.0}, 1000, function() { // the "0" here is what would normally be 1000 for the animation
			$click_items.addClass('clickable'); // reenable clicking
		});
	
}

$(function() {
	
	slideSwitch('.slide-holder', '#slide-controls');
	
	// flip the switch
	var refreshIntervalId = setInterval( function() {
		slideSwitch('.slide-holder', '#slide-controls');
	}, 5000 );
	
	// set the controls
	$(".clickable #slide-controls div:not(.current)").live('click', function() {
		clearInterval(refreshIntervalId);
		slideSwitch('.slide-holder', '#slide-controls', $(this).attr('class'));
	});
	
});




});
