$(function(){
	// inspired by http://nettuts.s3.amazonaws.com/011_jQuerySite/sample/portfolio.html#about
	var hash = window.location.hash.substr(1);
	var pipo = function(){
		$("a[href|='/accueil'], a[href|='/biographie'], a[href|='/discographie'], a[href|='/concerts'], a[href|='/media'], a[href|='/boutique'], a[href|='/panier'], a[href|='/news'], a[href|='/espace-pro'], a[href|='/mentions-legales']").each(function(){
			var href = $(this).attr('href');
			if (hash == href){
				var toLoad = hash + ' #swithcontent';
				$('#swithcontent').load(toLoad);
			}
			if (hash != ''){
				if (href == hash){
					$(this).parent("li").addClass('current').focus();
					//window.parent.document.title = href.substr(1,1).toUpper() + href.substr(2);
				} else {
					$(this).parent("li").removeClass('current');
				}
			}
		});
	}();
	$("a[href|='/accueil'], a[href|='/biographie'], a[href|='/discographie'], a[href|='/concerts'], a[href|='/media'], a[href|='/boutique'], a[href|='/panier'], a[href|='/news'], a[href|='/espace-pro'], a[href|='/mentions-legales']").live('click', function(){
		var that = $(this).attr('href');
		var toLoad = that + ' #swithcontent';
		if (window.location.pathname != '/' || window.location.search)
			window.location.replace('/#' + that);
		$('#loading').remove();
		$('#header').prepend('<div id="loading"><img src="/img/ajax-loader.gif" alt="Chargement..." /></div>');
		$('#loading').fadeIn('normal');
		$('#swithcontent').hide('normal', function(){
			$('#swithcontent').load(toLoad, '', function(){
				$('#swithcontent').show('normal', function(){
					$('#loading').fadeOut('normal');
				});
			});
		});
		window.location.hash = that;
		//window.parent.document.title = that.substr(1,1).toUpper() + that.substr(2);
		$('#menu li a').each(function(){
			var href = $(this).attr('href');
			if (href == that){
				$(this).parent("li").addClass('current').focus();
			} else {
				$(this).parent("li").removeClass('current');
			}
		});
		return false;
	});

	// ajouter au panier
	var add2cart = function(){
		//$(".formclass").submit(function(event){
		$(this).submit(function(event){
			event.preventDefault();
			var $form = $(this);
			var ref = $form.find('input[name="ref"]').val();
			var title = $form.find('input[name="title"]').val();
			var price = $form.find('input[name="price"]').val();
			var size = $form.find('select[name="size"]').val();
			var url = $form.attr('action');

			$.post(url, {add2cart: 1, ref: ref, title: title, price: price, size: size},
			function(data){
				var content = $(data).find('#swithcontent');
				$("#swithcontent").empty().append(content);

				$("#menu li a").each(function(){
					$(this).parent("li").removeClass('current');
				});
				$("#menu7").parent("li").addClass('current');
				window.location.hash = '/panier';

			});
		});
	}
	$(".formclass").livequery(add2cart);

	// update qte panier
	var updcartqte = function(){
		$(this).submit(function(event){
			event.preventDefault();
			var $form = $(this);
			var ref = $form.find('input[name="ref"]').val();
			var qte = $form.find('input[name="qte"]').val();
			var url = $form.attr('action');

			$.post(url, {updatecartqte: 1, ref: ref, qte: qte},
			function(data){
				var content = $(data).find('#swithcontent');
				$("#swithcontent").empty().append(content);

				$("#menu li a").each(function(){
					$(this).parent("li").removeClass('current');
				});
				$("#menu7").parent("li").addClass('current');
				window.location.hash = '/panier';

			});
		});
	}
	$(".updcart").livequery(updcartqte);

});

