/**
 * JQuery noconflict and start show onload
 */
var $j = jQuery.noConflict();
$j(document).ready(function() {
	$j('.input_aantaal').numeric();
	loadShoppingCart();
});

/**
 * load the shopping cart through ajax and replace the #cart div content
 */
function loadShoppingCart() {
	// only load shopping cart if it is not empty
	if (checkShoppingCart()) {
		// request new cart-div with correct cart-contents
		div = '#cart';
		//url = '/pdtworkspace/guppynl/public_html/guppy/frontend/shop/cart-div/language/nl';
		url = '/guppy/frontend/shop/cart-div/language/nl';
		$j(div).load(url);
	}
}

/**
 * check if there are elements in the shopping cart display cart if products are
 * available, hide if it is empty
 */
function checkShoppingCart() {
	// show cart if the number of products is higher then 0
	if (node = $('cart')) {
		node.hide();

		// check if there is a cookie with the name 'cart_', ugly, prone to
		// problems
		var _s = document.cookie; // haystack
		var _m = 'cart_'; // needle
		var _c = 0;
		for ( var i = 0; i < _s.length; i++) {
			if (_m == _s.substr(i, _m.length))
				_c++;
		}

		if (_c > 0) {
			node.show();
			return true;
		}
	}
	return false;
}

/**
 * IE waits until another event to send the 'change' events on radios and
 * checkboxes This bind a trigger for those events on click.
 */
function trig_bind() {
	// unbind old events
	$j("input[type='checkbox']").unbind('click')
	$j("input[type='radio']").unbind('click')
	// bind the events
	$j("input[type='checkbox']").bind('click', function() {
		$(this).trigger('change')
	})
	$j("input[type='radio']").bind('click', function() {
		$(this).trigger('change')
	})
}
if (jQuery.browser.msie) {
	trig_bind()
}

/**
 * modify cart javascripts
 */
function addToCart(productId, baseUrl, language, amount, name, cost) {
	// modify cart contents
	product = $j.cookie('cart_' + productId);

	if(!amount){
		amount = 1;
	}
	
	if (product) {
		product = parseInt(product) + parseInt(amount);
	} else {
		product = parseInt(amount);
	}
	$j.cookie('cart_' + productId, product, {
		expires : 365,
		path : '/'
	});

	// edit default 'add to cart' link to 'add more to cart' link (if available in HTML template)
	if (node = $('add-more-to-cart-button')) {
		node.show();
		if (node2=$('add-to-cart-button')){
			node2.hide();
		}
	}
	
	// google analytics
	try{
		_gaq.push(['_trackEvent', 'webshop', 'add to cart', ''+name, Math.round(cost)]);
	}catch(err){
		alert(err);
	}

	// reload shopping cart
	loadShoppingCart();
}

/**
 * remove element from shoppin cart and reload cart element
 */
function removeFromCart(productId, baseUrl, language) {
	// modify cart contents
	product = $j.cookie('cart_' + productId);
	if (product) {
		$j.cookie('cart_' + productId, null, {
			path : '/'
		});

		// reload shopping cart
		loadShoppingCart();
	}
}

/**
 * update cart, recalculate values (fully clientside), then reload shopping cart
 * element
 */
function update_Cart(productId, baseUrl, language) {

	var price_product = document.getElementById('price_' + productId);
	var aantal_product = document.getElementById('aantal_product_' + productId);

	var SubtotaalPrice = document.getElementById('Subtotaal_' + productId);
	var totaalPrice = document.getElementById('totaal_hidden');
	var newSubToaal = 0;
	// modify cart contents
	product = $j.cookie('cart_' + productId);

	if (product) {
		if (aantal_product.value != "") {
			newSubToaal = parseFloat(price_product.value)
					* parseFloat(aantal_product.value);
		}
		var totaal = (parseFloat(totaalPrice.value) - parseFloat(SubtotaalPrice.value))
				+ parseFloat(newSubToaal);
		document.getElementById('Subtotaal_' + productId + '_field').innerHTML = newSubToaal
				.toFixed(2);
		document.getElementById('totaal').innerHTML = totaal.toFixed(2);
		document.getElementById('Subtotaal_' + productId).value = newSubToaal
				.toFixed(2);
		document.getElementById('totaal_hidden').value = totaal.toFixed(2);
		if (aantal_product.value != "") {
			document.getElementById('aantal_product_' + productId).value = aantal_product.value;
		} else {
			document.getElementById('aantal_product_' + productId).value = 0;
		}
		$j.cookie('cart_' + productId, aantal_product.value, {
			expires : 365,
			path : '/'
		});
	}
	// reload the shopping cart element
	loadShoppingCart();
}

/**
 * jQuery address details form div toggles
 */
// TODO: something goes wrong with the hide(400) fixed it at 0
function updateDeliveryAddressDetailsContainer(speed) {
	if ($j('#deliveryAddressSelect').val() == 0) {
		$j('#deliveryAddressDetails-div').show(speed);
	} else {
		speed = 0;
		$j('#deliveryAddressDetails-div').hide(speed);
	}
}

function updateBillingAddressContainer(speed) {
	if ($j('input[name=billingAddressRadio]:checked').val() == 1) {
		$j('#billingAddressContainer').show(speed);
	} else {
		speed = 0;
		$j('#billingAddressContainer').hide(speed);
	}
}

function updateBillingAddressDetailsContainer(speed) {
	if ($j('#billingAddressSelect').val() == 0) {
		$j('#billingAddressDetails-div').show(speed);
	} else {
		speed = 0;
		$j('#billingAddressDetails-div').hide(speed);
	}
}

/**
 * jQuery, show correct paymentMethodDiv
 */
function showPaymentMethod(method) {
	var contentDiv = '#method_' + method + '_contents';
	$j(contentDiv).show(400);

	var splitStr, matchStr = 'method_' + method;
	var paymentMethodDivs = $$('.betalings-methode');
	$j.each(paymentMethodDivs, function(index, obj) {
		if (obj.id != matchStr) {
			splitStr = obj.id.split('_');
			hidePaymentMethod(splitStr[1]);
		}
	});
}

function hidePaymentMethod(method) {
	var contentDiv = '#method_' + method + '_contents';
	$j(contentDiv).hide(400);
}

