jQuery().ready(function(){
	/* Initialize site navigation */
	jQuery('#sidenavigation').accordion({
		active: true,
		header: '.head',
		navigation: true,
		fillSpace: false,
		selectedClass: 'selected',
		autoheight: false, 
		animated: 'easeslide'
	});

	/* Add class to navigation mouse over */
	$(".navitem").mouseover(function() {
		$(this).addClass("open");
	});
	
	$(".navitem").mouseout(function() {
		$(this).removeClass("open");
	});

	/* modal window order */ 
	$('a.order').click(function (e) {
			e.preventDefault();
			var productId = $(this).attr("rel") ;
			$.get("/getProductOrder.res", {productId: productId}, function(data){
				$(data).modal({containerId: 'order-container'});
				initButtons();
				$('a.orderbutton').click(function (e) {
					e.preventDefault();
					$('#basket').triggerHandler("productadd", [{ productId: productId, ammount: $('#order-ammount').attr('value')}]);
					$.modal.close();
				});
			});
	});
	
	/* define the basket events*/
	$("#basket").bind("productchange", function(){
		$.get("/showBasket.res",{rnd: Math.random()}, function(data) {
			$('#basket').hide('slow');
			$('#basket > *').remove('*');
			$('#basket').append($(data));
			$('#basket').show('slow');
			initButtons();			
		});
    });	

	$("#basket").bind("productadd", function(e, postdata){
		$('#basket').hide('slow', function() {
			$.post("/addToBasket.res", postdata, function(data) {
				$('#basket').empty( );
				$('#basket').append($(data));
				$('#basket').show('slow');
				initButtons();
			});
		});
    });	

	$("#checkoutbasket").bind("productchange", function(){
		$.get("/showCheckoutBasket.res",{rnd: Math.random()}, function(data) {
			$('#checkoutbasket').hide('slow');
			$('#checkoutbasket').empty( );
			$('#checkoutbasket').append($(data));
			$('#checkoutbasket').show('slow');
			initButtons();
			$("a.deleteprod").click(function (e) {
				e.preventDefault();
				var lineNumber = $(this).attr("rel");
				$.post("/deleteBasketLine.res", {basketLineNumber: lineNumber}, function(data) {
					$('#checkoutbasket').triggerHandler("productchange");
				});
			});
		});
    });	
	
	
	/* load basket */
	$('#basket').triggerHandler("productchange");
	$('#checkoutbasket').triggerHandler("productchange");
	initButtons();
	initTabs();	
});

function initTabs() {
	$('.tabitem').hide();
	$('.tabitem:first').show('slow');
	$('.tabnavigation a').click(function (e) {
		e.preventDefault();
		var tabId = $(this).attr('href').substring(1);
		$('.tabnavigation li').removeClass('active');
		$(this).parents("li").addClass('active');
		$('.tabitem').hide();
		$("#" + tabId).show('slow');
	});
}

function initButtons(context) {
	/* init buttons */
	$("img.btn").mouseover(function() {
		var src = $(this).attr("src");
		if (src.indexOf("hover")==-1) {
			var base = src.substring(0, src.indexOf(".gif"));
			$(this).attr("src", base + "-hover.gif");
		}
	});
	$("img.btn").mouseout(function() {
		var src = $(this).attr("src");
		if (src.indexOf("hover")!=-1) {
			var base = src.substring(0, src.indexOf(".gif") -6);
			$(this).attr("src", base + ".gif");
		}
	});
	
	$("a.back").click(function (e) {
		e.preventDefault();
		history.back();
	});
	
	$("input.btn").mouseover(function() {
		$(this).addClass("hover");
	});
	
	$("input.btn").mouseout(function() {
		$(this).removeClass("hover");
	});
		
}
