$(document).ready(function() {
     if (document.all) {
        $(".nav li").fixHover();
	 }

 	 $('#search_box').hover(function() { $(this).addClass('hovering'); }, function() { $(this).removeClass('hovering'); }); 
});

function cart_add(id) {
	$.ajax({ url: '/ajax/cart_add/'+id+'/' + new Date().getTime(), success: function(msg){ 
		$('span#add_link'+id).html('Added to cart. Continue shopping, <a href="#" onclick="cart_remove('+msg+'); return false;">remove this item</a> <br/>or <a href="/cart/">check out</a>.');
		$('#nav_cart').fadeIn('slow');
	}
	});
}

function cart_remove(id) {
	$.ajax({ url: '/ajax/cart_remove/'+id+'/' + new Date().getTime(), success: function(msg){ 
		$('span#add_link'+id).html('Item removed from cart. <br/><a href="#" onclick="cart_add('+msg+'); return false;">Add to cart</a>.');
	}
	});
}

function subscription_add() {
	var id = $('[name=sub]:checked').val(); 
	if (id == undefined) {
		var id = $('#sub').val();
	}
	$.ajax({ url: '/ajax/cart_add/'+id+'/' + new Date().getTime(), success: function(msg){ 
		$('span#subscription_add_link').html('Added to cart. Continue shopping, <a href="#" onclick="subscription_remove('+msg+'); return false;">remove this item</a> or <a href="/cart/">check out</a>.');
		$('#nav_cart').fadeIn('slow');
	}
	});
}

function subscription_remove(id) {
	$.ajax({ url: '/ajax/cart_remove/'+id+'/' + new Date().getTime(), success: function(msg){ 
		$('span#subscription_add_link').html('Item removed from cart. <a href="#" onclick="subscription_add(); return false;">Add to cart</a>.');
	}
	});
}

function in_cart_remove(id) {
	$.ajax({ url: '/ajax/cart_remove/'+id+'/' + new Date().getTime(), success: function(msg){ 
		$('#row_'+id).fadeOut('slow', function() { $(this).remove(); recalculate_total(id);} );
	}
	});
}

function recalculate_total() {
	var total_us = 0;
	var total_intl = 0;
		
	$('.price').each(function() {
		var totals = $(this).html().replace(/\$/g,'').replace(/,/g,'').replace(/ US/g, '').replace(/ intl\./g, '').split('/');
		total_us += parseInt(totals[0]);
		total_intl += parseInt(totals[1]);
	});

	if (total_us == 0) {
		window.location.href = window.location.href;
	}
		
	$('#cart_total').html('$'+number_format(total_us, 0)+' US/'+number_format(total_intl, 0)+' intl.');
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function checkout() {
		window.location.href = '/cart/checkout/1';
}