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

			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;
			}
	});
}
function showCart()
{
	$("#the_cart").fadeIn(800, function () {

	});
	setTimeout(function(){hideCart()},1800)


}

function hideCart()
{
	$("#content ul ul").fadeOut(800, function () {
		$('#the_cart').removeAttr("style");
		animateCart();
	});



}

function animateCart()
{
	var originalColor = $("thead .cart").css("backgroundColor");
	var fadeColor = "#001f40";	

	$(".cart").animate( { backgroundColor: fadeColor }, 1000)
	.animate( { backgroundColor: originalColor }, 1000, 'linear',
	setTimeout(function(){animateCart()},10)
);
};

$('.qte').keyup(function(key)
{
	if (isInt(this.value) )
	{
		tmp=this.id.split("_");
		id=tmp[1];
		total_price = parseInt(this.value) * curToNum($('#price_'+id).text());
		$('#total_'+id).text(numToCur(total_price));
		updateTotaux();
		$(this).removeClass("error");

	}
	else
	{
		$(this).addClass("error");
	}

});


function isInt(x) { 
	var y=parseInt(x); 
	if (isNaN(y)) return false; 
	return x==y && x.toString()==y.toString(); 
} 
function curToNum(cur)
{

	if(global_currency == "USD")
	{
		num = cur.toString().replace(/\$|\,/g,'');

	}
	else
	{
		num = cur.toString().replace(/\€|\s/g,'');
		num = num.replace('\,','.');
	}
	return parseFloat(num);

}


function numToCur(num)
{
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;

	if(global_currency == "USD")
	{
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);

	}
	else
	{
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+' '+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + num + ',' + cents  + ' €');
	}
}

function updateTotaux()
{
	var subtotOffline = 0;
	var subtotOnline =0;

	$(".qte").each(function() {

		tmp=this.id.split("_");
		id=tmp[1];

		var qte = parseInt($(this).val());
		var prix = curToNum($('#price_'+id).text());
		if($('#price_'+id).attr("class") == "price_offline")
		{
			subtotOffline += qte*prix;
		}
		else
		{
			subtotOnline += qte*prix;
		}

	});
	if (subtotOffline!=0)
	{
		$("#sous_tot_off").text(numToCur(subtotOffline));
	}
	else
	{
		$("#sous_tot_off").text("");

	}
	if (subtotOnline!=0)
	{
		$("#sous_tot_on").text(numToCur(subtotOnline));
	}
	else
	{
		$("#sous_tot_on").text("");

	}

	$("#total_commande").text(numToCur(subtotOnline + subtotOffline));
	$("#prix_total").text(numToCur(subtotOnline + subtotOffline));




}


var next_cap = ""

function hideCaption() {
	$('#crossfader').fadeOut(600);

	$.get($('#crossfader').attr('rel')+'?junk='+Math.floor(Math.random()*10000), function(data){
		next_cap = data;
		setTimeout("showCaption()",500);
	});


}


function showCaption() {

	$('#crossfader').html(next_cap);
	$('#crossfader').fadeIn(800);
	setTimeout("hideCaption()",6000);
}



$(document).ready(function() {

	$('#ChatImage').attr("src","/images/btn_chat.png");
	
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).focus(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -100,
          left: -33,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).blur(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });


	initMenus();
	hideCaption();
	if(cart_empty !='1')
	{
		animateCart();
	}

	$('.add_to_cart').click(function()
	{


		$('#cart_display').load($(this).attr("href"),{ query: this.id },function() {
			if(cart_empty == '1')
			{
				var targetOffset = $(".cart").offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 400);
				cart_empty = "0";
				setTimeout(function(){showCart()},500);
			}

		}

	);
	return false;
	}
);

$(".tab_content").hide();
$("ul.tabs li:first").addClass("active").show(); 
$(".tab_content:first").show();

//On Click Event
$("ul.tabs li").click(function() {

	$("ul.tabs li").removeClass("active");
	$(this).addClass("active");
	$(".tab_content").hide();

	var activeTab = $(this).find("a").attr("href");  
	$(activeTab).fadeIn(); 
	return false;
});



$('#search_submit').hide();
$('#search_keywords').keyup(function(key)
{
	if (this.value.length >= 3)
	{
		old = $('.search_bar').css('background-image');
		old = old.split('/');
		new_url = "";
		for (var i=0; i < old.length - 1; i++) {
			new_url += old[i] + '/';
		};

		new_url1 = new_url +  "suggestion-loader.gif)";
		new_url2 = new_url +  "magnifying-glass.gif)";

		$('.search_bar').css('background-image', new_url1);
		$('#suggestions').load(
			$(this).parents('form').attr('action'),
			{ query: this.value + '*'},
			function() { 
				$('#suggestions').fadeIn('400');
				$('.search_bar').css('background-image', new_url2);
				$('#close_link').click(function()
				{
					$('#suggestions').html('');
					$('#suggestions').hide('400');
					$('.search_bar').css('background-image', new_url);
				}
				);
			}

		);
	}
	else if (this.value == "")
	{

		old = $('.search_bar').css('background-image');
		old = old.split('/');
		new_url = "";
		for (var i=0; i < old.length - 1; i++) {
			new_url += old[i] + '/';
		};

		new_url += "magnifying-glass.gif)";
		$('#suggestions').html('');
		$('#suggestions').hide('400');
		$('.search_bar').css('background-image', new_url);
	}
});



$('#search_keywords').focus(function() {
if (this.value == $(this).attr('rel'))
	$(this).val("");
});



$('#confirm_command').click(function()
{

	valid=true;
	$(".qte").each(function()
	{
		if (!isInt(this.value) )
		{
			valid=false;
		}
	});

	if(!valid)
	{
		$('#error_flash').show('600');
	}
	return valid;	
});

$('.more_content').hide();


$('.more_link').click(function(key)
{
	tmp=this.id.split("_");
	id=tmp[1];
	$('#content_'+id).slideToggle(300);
	nouveau_text = $(this).attr("rel");
	ancien_text = $(this).text();
	$(this).attr("rel",  ancien_text);
	$(this).text(nouveau_text);
	return false;

});

$('#club_filters select').change(function() {
	$(this).parents('form:first').submit();
});
$('#club_filters input').hide();


$('#month_selector').change(function() {
		$(this).parents('form:first').submit();
	});

$('#submit_month').hide();

$('#nature').change(function() {
	location.href = $(this).attr("rel")+$(this).val();
});
$('#categorie').change(function() {
	location.href = $(this).attr("rel")+$(this).val();
});
$('#pays').change(function() {
	if($(this).attr("rel") )
	{
		location.href = $(this).attr("rel")+$(this).val();
	}
});


});
