// JavaScript Document
$(document).ready(function() {
   
   $(".contact").href = "#";
   
   $(".contact").click(function(){
		$("#infoContact").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});
   
   if ($("#slideshow").length > 0) {
   //This keeps track of the slideshow's current location
	var current_panel = 1;
	//Controlling the duration of animation by variable will simplify changes
	var animation_duration = 5000;
	
	$.timer(6000, function (timer) {
		//Determine the current location, and transition to next panel
		switch(current_panel){
			case 1:
				$("#slideshow").stop().animate({left: "-941px", top: "0px"}, "");
				current_panel = 2;
			break;
			case 2:
				$("#slideshow").stop().animate({left: "-941px", top: "-293px"}, "");
				current_panel = 3;
			break;
			case 3:
				$("#slideshow").stop().animate({left: "0px", top: "-293px"}, "");
				current_panel = 4;
			break;
			case 4:
				$("#slideshow").stop().animate({left: "0px", top: "0px"}, "");
				current_panel = 1;
			break;	
	  		timer.reset(12000);
		}
	});
   }
   
   if ($("#output-voyages").length > 0) {
		$.ajax({
			type: "GET",
			url: "voyages-groupes.xml",
			dataType: "xml",
			success: parseXml
		  });
   }
   
   if ($("#live-voyages").length > 0) {
		
		$.ajax({
			type: "GET",
			url: "voyages-groupes.xml",
			dataType: "xml",
			success: parseXmlLive
		  });
   }
   
});

function parseXml(xml)
{
  var intCompteur = 0;
  var intPage = $("#pagenum").val();
  var intNbPerPage = 6;
  var intCurrentPage = 1;
  
  //find every Tutorial and print the author
  $(xml).find("Groupe").each(function()
  {
	if ((intCompteur >= (intPage-1)*intNbPerPage) && (intCompteur < intPage*intNbPerPage)) {
		strEtat = "";

		if ($(this).attr("state") != undefined) {
			strEtat = "<p id=\"" + $(this).find("Image").text() + "-state" + "\" class='" + $(this).attr("state")  + "'>" + $(this).attr("state") + "</p>";
		}
		strImage = "<img src=\"images/voyages/" + $(this).find("Image").text() + ".jpg\" alt=\"" + $(this).find("Titre").text() + "\" />";
		strTitre = "<h3>" + $(this).find("Titre").text() + "</h3>";
		strDate = "<p class='date'><strong>" + $(this).find("Date").text() + "</strong><span id=\"" + $(this).find("Image").text() + "-dispo" + "\">" + $(this).find("Places").text() + "</span></p>"; 
		strDocument = "<p class=\"documentation\"><a href=\"pdf/" + $(this).find("Pdf").text() + "\">Télécharger la publicité de ce voyage &raquo;</a> (PDF " + $(this).find("Pdf").attr("size") + ")</p>";
		strItineraire = "<div>" + $(this).find("Itineraire").text() + "</div>";
		
		if (strItineraire == "<div></div>") {
			strItineraire = "";
		}
		
    	$("#output-voyages").append("<li id='" + $(this).find("Image").text() +"'>" + strEtat + strImage + strTitre + strDate + $(this).find("Description").text() + strDocument + strItineraire + "</li>");
  	}
	intCompteur++;
  });
  
  while (intCompteur > 0) {
	  	if (intCurrentPage == intPage) {
			$("#pagination").append("<li>Page " + intCurrentPage + "</li>");
		} else {
			if (intCurrentPage == 1) {
				$("#pagination").append("<li><a href='voyages-groupes.html'>Page " + intCurrentPage + "</a></li>");	
			} else {
				$("#pagination").append("<li><a href='voyages-groupes-page-" + intCurrentPage + ".html'>Page " + intCurrentPage + "</a></li>");	
			}
		}
		intCompteur = intCompteur - intNbPerPage;
		intCurrentPage++;
  }

}

function parseXmlLive(xml)
{
  var intCompteur = 0;
  var intPage = $("#pagenum").val();
  var intNbPerPage = 6;

  //find every Tutorial and print the author
  $(xml).find("Groupe").each(function()
  {
	if ((intCompteur >= (intPage-1)*intNbPerPage) && (intCompteur < intPage*intNbPerPage)) {
		if ($("#" + $(this).find("Image").text() + "-dispo").length > 0) {
			$("#" + $(this).find("Image").text() + "-dispo").text($(this).find("Places").text());
		}
		
		if ($(this).attr("state") != undefined) {
			if ($("#" + $(this).find("Image").text() + "-state").length > 0) {
				$("#" + $(this).find("Image").text() + "-state").attr("class", $(this).attr("state"));								
			} else {
				strEtat = "<p id=\"" + $(this).find("Image").text() + "-state" + "\" class='" + $(this).attr("state")  + "'>" + $(this).attr("state") + "</p>";
				$("#" + $(this).find("Image").text()).html(strEtat + $("#" + $(this).find("Image").text()).html());
			}
		}
  	}
	intCompteur++;
  });

}

