function show(_id){
	if(_id == '') return;
	var id = '#'+_id;
	$(id).slideToggle(200);
}

function panier(_id){
	$.post('/produits/panier/',{id:_id},function(data){
		if(data.valide == true)
		{
			$('#panier-liste-content').html(data.html);
		
			$('#panier-prix-total').html(data.total+' $').show();
			if(data.quantite > 0) $('#panier-quantite-total').html(data.quantite);
			
			if($('#all-sel').is(':hidden')) $('#all-sel').show();
			if($('.del-btn').is(':hidden')) $('.del-btn').show();
			if(data.total == 0){
				$('#all-sel,.del-btn,#panier-prix-total').hide();
				
			}
			if($('#all-checked').is(':checked')) $('#all-checked').attr('checked', false);
			$('#panier').show();
			window.frames['printframe'].location.reload(true);
		}
	},'json');
}

function panier_del(_id){
	$.post('/produits/panier_del/',{id:_id},function(data){
		$('#panier-liste-content').html(data.html);
		if(data.total == 0){
			$('#panier-prix-total').html('0 $').hide();
			$('#panier-quantite-total').html('');
			$('#all-sel,.del-btn').hide();
		}else{
			$('#panier-prix-total').html(data.total+' $');
			$('#panier-quantite-total').html(data.quantite);
		}
		$('#panier').show();
		window.frames['printframe'].location.reload(true);
	},'json');
}

function panier_del_sel(){
	var _id = Array();
	
	$('input[type=checkbox][name=panier_checkbox]:checked').each(function(){
		if($(this).val() != 'undefined' && parseInt($(this).val()) > 0){
			_id.push($(this).val());
		}
	});
	
	if(_id.length == 0) return false;
	
	$.post('/produits/panier_del_sel/',{id:_id},function(data){
		$('#panier-liste-content').html(data.html);
		if(data.total == 0){
			$('#panier-prix-total').html('0 $').hide();
			$('#panier-quantite-total').html('');
			$('#all-sel,.del-btn').hide();
		}else{
			$('#panier-prix-total').html(data.total+' $');
			$('#panier-quantite-total').html(data.quantite);
			if($('#all-checked').is(':checked')) $('#all-checked').attr('checked', false);
		}
		$('#panier').show();
		window.frames['printframe'].location.reload(true);
	},'json');
}

function panier_update(){
	var total = $('#panier-liste-content>ul>li').size();
	var num = Array();
	var _id;
	var i;
		
	if(total > 0){
		for(i=0;i<total;i++){
			_id = '#panier-item-'+i+'>input[type=text]';
			num[i] = $(_id).val();
		}
		
		$.post('/produits/panier_update/',{quantite:num},function(data){
			if(data.valide == true)
			{
				$('#panier-liste-content').html(data.html);
				$('#panier-prix-total').html(data.total+' $');
				$('#panier-quantite-total').html(data.quantite);
				if($('#all-sel').is(':hidden')) $('#all-sel').show();
				if($('.del-btn').is(':hidden')) $('.del-btn').show();
				if($('#all-checked').is(':checked')) $('#all-checked').attr('checked', false);
				window.frames['printframe'].location.reload(true);
			}
		},'json');
	}
	
	return false;
}

function panier_vider(){
	$.post('/produits/panier_vider/',{},function(data){
		$('#panier-liste-content').html(data.html);
		$('#panier-prix-total').html('0 $');
		$('#panier').show();
		window.frames['printframe'].location.reload(true);
	},'json');
}

function panier_close(){
	$('#panier').hide();
	$('#panier-envoyer-msg').html('');
}

function panier_print(){
	var total = $('#panier-liste-content>ul>li').size();
	if(total == 0) return false;	
	window.frames['printframe'].focus();
	window.frames['printframe'].print();
}

function envoyer_input(){
	$('.email-input').show();
}

$(function () {
	//panier
	/**********************/
	$("#panier").draggable({
		handle:"div.panier-top"
	});
	
	$('#all-checked').bind('click',function(){
		var checked_status = this.checked; 
		$("input[type=checkbox][name=panier_checkbox]").each(function() 
		{ 
			this.checked = checked_status; 
		}); 
	});
	
	$('#panier-email-send').click(function(){
		panier_envoyer();
	});
	
	$('#panier-email-close').click(function(){
		$('.email-input').hide();
		$('#panier-envoyer-msg').html('');
	});
	
	//quantite update avec ajax
	$('input[name="quantite[]"]').live('keyup',function(){
		panier_update();
	});	

	$('input[name=keyword]').focusin(function(){
		var keyword = $(this).val();
		
		if($.trim(keyword) == 'Recherche' || $.trim(keyword) == 'Search'){
			$(this).val('');
			keyword = '';
		}
	});

	$('input[name=keyword]').focusout(function(){
		var keyword = $(this).val();
		
		if($.trim(keyword) == ''){
			$(this).val('Recherche');
		}
	});
	
	var current = -1;

	$('input[name=keyword]').keyup(function(e){	
		if($('#suggest').is(':visible')){
			if($.browser.msie){
				 var keyStr = e.keyCode;
			}else var keyStr = e.which;

			var _len = $('#suggest>.suggest-content>ul>li').length;
			var len = _len - 1;

			//key down
			if(keyStr == 40){
				if(current >= 0 && current <= len){
					$("#suggest>.suggest-content>ul>li").get(current).style.backgroundColor="";
				}
				current = Number(current)+1;
				if(current > len) current = 0;
				
				$("#suggest>.suggest-content>ul>li").get(current).style.backgroundColor="#fffac4";
				return;	
			//key up
			} else if(keyStr == 38) {
				if(current >= 0 && current <= len){
					$("#suggest>.suggest-content>ul>li").get(current).style.backgroundColor="";
				}
				current = Number(current)-1;
				if( current < 0 ) current = len;
				
				$("#suggest>.suggest-content>ul>li").get(current).style.backgroundColor="#fffac4";
				return;
			} else if (keyStr == 13) {
				console.debug("enter")
				//var url = $("#suggest>.suggest-content>ul>li:eq("+current+")>a").attr('href');
				//window.location.href = url;
				return false;
			}
		}

		var keyword = $.trim($(this).val());

		if(keyword.length > 2 && keyStr != "13"){
			$.post('/search/suggest/',{search:keyword},function(json){
				if(json.html != ''){
					$('#suggest>.suggest-content').html(json.html);
					$('#suggest').show();
				}
			},'json');
		}else{
			$('#suggest>.suggest-content').html('');
			$('#suggest').hide();
		}

	});

	$('input[name=search-btn]').click(function(){
		var s = $('input[name=keyword]').val();
		if(s == '' || s == 'Recherche' || s == 'Search'){
			return false;
		}
		
		$('form[name=search-form]').submit();
	});
	
	$('.view-list').click(function(){
		panier(0);
	});

	$('ul.sub-menu').hover(function(){
		$(this).prev('a').css('color','#a0a0a0');
	},function(){
		$(this).prev('a').css('color','#4E4E4F');
	})

	/*
	$('a').click(function (e) {
		if ($(this).attr('href').match(/\.pdf$/i)) {
				e.preventDefault(); 
			window.open($(this).attr('href'));
		}
	});
	*/
});

// footer
$(function () {
	$(window).resize(function () { adjustFooter(); });
	$('img:last').load(function () { adjustFooter(); });
});

function adjustFooter () {
	if (typeof contentHeight == 'undefined') {
		contentHeight = $('#content').height();
	}
	
	if ($(window).height() > (contentHeight+182)) {
		 $('#content').height($(window).height() - 182);		
	}
}

