// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 500;
$(function() {

  // gestion selon le browser du saut entre les images
  if(browser == "firefox")
  {
    stepGallery = 480;
  }
  else if(browser == "chrome")
  {
    stepGallery = 478;
  }
  else
  {
    stepGallery = 482;
  }

  $('#dialog').dialog({
		autoOpen: false,
		show: 'blind',
		hide: 'blind',
		draggable: false,
		resizable: false,
		modal: true,
		width: 500,
		height: 360,
    position: "center"
	});
	
	$('#confirm').dialog({
		autoOpen: false,
		show: 'blind',
		hide: 'blind',
		draggable: false,
		resizable: false,
		width: 150,
		height: 100,
    position: "center"
	});

  $('#errorEnvoi').dialog({
		autoOpen: false,
		show: 'blind',
		hide: 'blind',
		draggable: false,
		resizable: false,
		width: 150,
		height: 100,
    position: "center"
	});
	
	$('#encour').dialog({
		autoOpen: false,
		show: 'blind',
		hide: 'blind',
		draggable: false,
		resizable: false,
		width: 150,
		height: 100,
    position: "center"
	});
	
	$('#openDialog').click(function() {
		$('#dialog').dialog('open');
		return false;
	});

	loadSlideshowDp($("#firstIdTechnique").val(),$("#idDossier").val(),$("#firstIdGroup").val());
});

function submitFormError()
{
	idDossier = $("#idDossierPatient").val();							 
	titre = $("#titreDossierPatient").val();

	nom 	= $("#nomFormError").val();
	email	= $("#emailFormError").val();
	message = $("#messageFormError").val();

	if(message == "" || nom == "" || email == "")
	{
		alert("Veuillez remplir tous les champs");
		return false;
	}
	else
	{
    $('#dialog').dialog('close');
	 	$('#encour').dialog('open');
		$.ajax({
      type: "POST",
      url: "/dossierpatient/mail",
      data: "nom="+nom+"&email="+email+"&message="+message+"&idDossier="+idDossier+"&titre="+titre,
      success: function(msg){
        $('#encour').dialog('close');
        
        $("#nomFormError").val("");
        $("#emailFormError").val("");
        $("#messageFormError").val("");

        if(msg == 1)
          $('#confirm').dialog('open');
        else
          $('#errorEnvoi').dialog('open');
        return false;
		   }
		 });
	}
}

function loadSlideshowDp(idTechnique, idDossier, idGroup)
{
	$.ajax({
    type: "POST",
    url: "/dossierpatient/slideshowdp",
    data: "idTechnique="+idTechnique+"&idDossier="+idDossier+"&idGroup="+idGroup,
    success: function(data) {
      $('#divStack').html(data);
      InitSlideshowDp();
    }
	 });
}

function loadSlideshowDpFilters(idDossier,idDiag,idGamme,idLoca)
{
  var query = "idDossier=" + idDossier + (idDiag != null ? "&idDiag="+idDiag : "") + (idGamme != null ? "&idGamme="+idGamme : "") + (idLoca != null ? "&idLoca="+idLoca : "")
  
  $.ajax({
    type: "POST",
    url: "/dossierpatient/slideshowfilter",
    data: query,
    success: function(data) {
      $('#divStack').html(data);
      InitSlideshowDp();
    }
	 });
}

function InitSlideshowDp()
{
	var stackLength = $(".stack > li").length;
  var imgStack = $(".stack > li > img");

 	imgStack.each(loadImg);
  var images = $("#galleryDP li");

	if(stackLength > 1)
	{
		$("#slider").slider({ 
			orientation: 'vertical',
      animate: 'fast',
			min : 0,
			max : stackLength-1,
			value : stackLength-1,
			slide: function(event, ui) 
      {
				var visualIndex = ui.value;
				jumptoImg(visualIndex,'galleryDP',stepGallery);
			},
      change: function(event, ui) 
      {
				var visualIndex = ui.value;
        
        stackLength = images.length-1;
        position = stackLength-visualIndex;
        
        currentImage('galleryDP').removeClass("current");
        $("#picture_"+position).addClass("current");
			}
		});

		$('#galleryDP').mousewheel(function(event, delta) {
			event.preventDefault();
      if(delta > 0)
			{	
				if($("#slider").slider("option", "value") < stackLength-1)
				{
					changeImageBy(delta,'galleryDP',stepGallery);
				}
        else
        {
          var fullsize = -stepGallery * (images.length-2);
          $("#galleryDP ul").css("top",fullsize);
          $("#slider").slider("option", "value",0);
          $("#galleryDP ul li:last-child").addClass("current");
        }
			}
			else
			{
				if($("#slider").slider("option", "value") > 0)
				{
					changeImageBy(delta,'galleryDP',stepGallery);
				}
        else
        {
          $("#galleryDP ul").css("top","0px");
          $("#slider").slider("option", "value",images.length-1);
          $("#galleryDP ul li:first-child").addClass("current");
        } 
			}
		});
    
    //On arrive sur l'image clé au premier affichage de la page
    changeImg(currentImage('galleryDP'), 'galleryDP',stepGallery);
	}
}

function loadImg(index, objImg) 
{
	var obj = $(objImg);
	var tempImg = $(document.createElement('img'));
  

	tempImg.load(function() 
  {	
		var counter = new Number($('#blocImgRemaining span').text().split(' ')[0]) - 1;
		
		if(counter) 
		{
		  update_counter(counter);
		}
		else 
		{
		  hide_msg_display();
		}
 	});
  
	tempImg.attr('src', obj.attr('__ref'));
  tempImg.attr('alt', obj.attr('alt'));
  
	obj.replaceWith(tempImg);
};

function update_counter(counter) 
{
  if(counter == 1)
  {
	  var phrase = " image";
  }
  else
  {
	  var phrase = " images";
  }
  $('#blocImgRemaining span').text(counter + phrase);
};

function hide_msg_display() 
{
  $('#blocImgRemaining').fadeOut('normal');
};

function showMoreInfo()
{
  $("#voirPlus").show();
}

function showGammes(idDiag)
{
  if($("#listGammes_"+idDiag).css('display') == "none")
  {
    $("#listGammes_"+idDiag).show();
  }
  else
  {
    $("#listGammes_"+idDiag).hide();
  }

}

function showLocalisations(idDiag,idGamme)
{
  if($("#listLocalisations_"+idDiag+"_"+idGamme).css('display') == "none")
  {
    $("#listLocalisations_"+idDiag+"_"+idGamme).show();
  }
  else
  {
    $("#listLocalisations_"+idDiag+"_"+idGamme).hide();
  }

}
