// MA BIBLIO



// OVERLAY

function showOverlay() {
        $('#overlay').fadeIn('fast');
}

function hideOverlay() {
        $('#overlay').fadeOut('fast');
}

// LIGHTBOX

function closeLightbox() {
        hideOverlay();
        $('#lightbox').fadeOut('fast');
        $('#lightbox').remove();
}

// DIALOG

function openDialog(title,content) {
        $('#overlay').fadeIn('fast');
        dialog = '<span id="closeDialog" onClick="closeDialog();"></span>';
        dialog += '   <div id="dialogTitle">';
        dialog += '       '+title;
        dialog += '   </div>';
        dialog += '   <div id="dialogContent">';
        dialog += '       '+content;
        dialog += '   </div>';
        $('#dialog').append(dialog);
        $('#dialog').show();
}

function closeDialog() {
        hideOverlay();
        $('#dialog').html("");
        $('#dialog').hide();
}

// NOTIF

function notif(text) {
    var id = Math.floor(Math.random()*1000)
    $('#notif').append('<div id="notif' + id + '" onClick="notifClose(' + id + ')" class="notif">' + text + '<div class="notifClose"></div></div>');
    $('#notif' + id).slideDown();
    setTimeout('notifClose('+id+')',5000);
}

function notifClose(id) {
    $('#notif' + id).slideUp();
}


	// AUTOCOMPLETE

	function autocomplete(field) {
		$('.autocompleteResults').remove();
		field.addClass('loading');
		field_id = field.attr('id');
		field_value = field.attr('value');
		$.post("/x/"+field_id, { query: ""+field_value+"" }, function(data) {
			field.removeClass('loading');
			if(data.indexOf('ERROR') >= 0) alert(data.split('>')[1]);
			else {
				field.after('<ul class="autocompleteResults">'+data+'</ul>');
			}
		});
	}
	
	function choose(field,id,value) {
		$('.autocompleteResults').hide();
		$('#'+field).val(value).attr('disabled','disabled').after('<input type="button" value="Modifier" onClick="unchoose(\''+field+'\');" id="unchoose-'+field+'" class="unchoose" />');
		$('#'+field+'_id').val(id);
		$('#'+field+'_id').siblings('input[type="submit"]').show();
	}
	
	function unchoose(field) {
		$('#unchoose-'+field).hide();
		$('#'+field).val('').removeAttr('disabled');
		$('#unchoose-'+field).siblings('input[type="submit"]').hide();
	}


// DOCUMENT READY JQUERY

$(document).ready(function() {
  $('#axys-menu').hide();
  $('#axys-up').hide();
  
    var user_key = $("#user_key").val();

    $('#axys-email').click(function () {
        $('#axys-menu').slideToggle('medium');
        $('#axys-up').toggle();
        $('#axys-down').toggle();
        $('#axys-email').toggleClass('open');
    });
  
  //$('body').click(function () {
  //        $('#axys-menu').hide();
  //});
  
  $('#occazAff').click(function () {
          $('.occasion').toggle();
  });
  
  $('#neufAff').click(function () {
          $('.neuf').toggle();
  });
  
  $('#dispoAff').click(function () {
          $('.dispo').toggle();
  });
  
  $('#futurAff').click(function () {
          $('.futur').toggle();
  });
  
  $('#reassortAff').click(function () {
          $('.reassort').toggle();
  });
  
  $('#neutreAff').click(function () {
          $('tbody tr').not('.neutre').toggle();
  });
  
  $('#biblioAff').click(function () {
          $('tbody tr').not('.biblio').toggle();
  });
  
  $('#alerteAff').click(function () {
          $('tbody tr').not('.alerte').toggle();
  });
  
    $('[rel="lightbox"]').click(function() {
        var image = this;
        showOverlay();
        var html = '<div id="lightbox" class="cliquable" onClick="closeLightbox();"><img src="' + image + '" class="couv" /></div>';
        $('body').append(html);
        return false;
    });
    
    $('.confirm').click(function() {
        var answer = confirm('Vraiment ?');
        return answer;
    });
    // Confirmer avec message personnalisé
	$('[data-confirm]').click(function(e) {
		var question = $(this).data('confirm');
		var answer = confirm(question);
		return answer;
	});
			
    $('.autosubmit').change( function () {
        $(this).parent().submit();
    });
    
    $('form.check').submit(function() {
        $('input.required').each(function () {
            var field = $(this).attr('id');
            var fieldlabel = $('label[for="'+field+'"]').html();
            if($(this).val().length == 0) {
                alert('Le champ '+ fieldlabel + ' est obligatoire !');
                result = false;
                return false;
            }
            else result = true;
            return true;
        });
        return result;
    });
    
    $('.sendNotif').each(function() {
        var x = $(this).html();
        $(this).hide();
        notif(x);
    });
    
    $("select.goto").change( function() { // modifier la collection
        valeur = $(this).val();
        document.location.href=valeur;
    });
    
    $(".unfold").click( function() {
        var id = $(this).attr('id');
        var id = id.replace('unfold_','');
        $('#'+id).slideToggle();
    });
    
    // Effacer le texte gris par defaut quand on clique dans un champ
    $('.default').click( function() {
        var defClass = $(this).attr('class');
        if(defClass.indexOf('default') != -1) {
            $(this).val('');
            $(this).removeClass('default');
        }
    });
    
    // Ma biblio
    $(".mabiblio").click( function() {
        var book = $(this).attr('id').split('_')[1];
        span = $(this);
        $.post("/xhr/log_mabiblio", { l: ""+book+"", k: ""+user_key+"" }, function(data) { 
            if(data.length > 0) {
                if(data == 0) {
                    var diode = '<img src="/sites/default/design/square_blue.png" />';
                    notif(diode+" Le livre a bien été retiré de votre bibliothèque !");
                }
                else if(data == 1) {
                    var diode = '<img src="/sites/default/design/square_orange.png" />';
                    notif(diode+" Le livre a bien été ajouté à vos alertes !");
                }
                else if(data == 2) {
                    var diode = '<img src="/sites/default/design/square_green.png" />';
                    notif(diode+" Le livre a bien été ajouté à votre bibliothèque !");
                }
                span.html(diode);
            }
        });
    });
	
	// Autocomplete
	var ac_timer;
	$(".autocomplete").keyup( function() { // rechercher un article
		field = $(this);
		if(field.val().length >= 3) {
			if(ac_timer) clearTimeout(ac_timer);
			ac_timer = setTimeout("autocomplete(field)", 500);
		}
	});
	
	// Fermer autocomplete suggestion
	$("body").click( function() {
		$(".autocompleteResults").hide();
	});
	
	// Deplier une table cachee
	$("table.unfold > thead").click( function() {
		console.log('unfold');
		$(this).siblings('tbody').slideToggle();
	});

});
