// Javascript.

	function initMenus() {
		$('ul.menu ul').hide();
		$.each($('ul.menu'), function(){
			$('#' + this.id + '.expandfirst ul:first').show();
			$('.expanded').show();
		});
			
		$('ul.menu li a').click(	
			function() {
				var checkElement = $(this).next();
				var parent = this.parentNode.parentNode.id;

				if($('#' + parent).hasClass('noaccordion')) {
					// Check for a single link with no children - i.e. no accordion necessary.
					if(!(checkElement.is('ul'))){
						// Let the link execute with no further interruptions!
						return true;
					}
					
					if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
						$(this).next().children().fadeOut("fast");
					}else{
						$(this).next().children().fadeIn("normal");
					}
					$(this).next().slideToggle('normal');

					return false;
				}
				if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
					if($('#' + parent).hasClass('collapsible')) {
						$('#' + parent + ' ul:visible').slideUp('normal');
					}
					return false;
				}
				if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
					$('#' + parent + ' ul:visible').slideUp('normal');
					checkElement.slideDown('normal');
					return false;
				}
			}
		);
	}


	// Initialise observers etc.
	$(document).ready(function(){
		initMenus()
	
		$('#chkDelivery').click(function(){
			if(this.checked == true){
				$('#delivery').show();
			} else {
				$('#delivery').hide();
			}
		});
		strURL = window.location.pathname;
		arrURL = strURL.split('/');
		
		last = arrURL[arrURL.length - 1];
		
		$('#cbocountry').change(function(){
			var flddelivery = $('#delivery_cost');
			var fldsubtotal = $('#subtotal');
			var flddiscount = $('#discount_amount');
			var fldtotal	= $('#final_total');
			var url			= "";
			
			// Decide post call url by current url.
			if(last == 'basket'){
				url = '../shop/basket/ajax_check_delivery';
			} else {
				url = '../../shop/basket/ajax_check_delivery';
			}
			
			$.post(url,{country_id: $('#cbocountry').val()},function(response){
				var subtotal = parseFloat(fldsubtotal.html().substr(1,fldsubtotal.html().length - 1));
				
				var delivery = parseFloat(response.delivery);
				var total = subtotal + delivery;
				
				flddelivery.html('&pound;'+delivery.toFixed(2));
				fldtotal.html('&pound;'+total.toFixed(2));
				
			},'json');			
		});

	});
