//
// iCents.net
//
////////////////////////////////////////////////////////////////////////////////
// INFOBOXES
////////////////////////////////////////////////////////////////////////////////

// ---

function Next()
   {
   var atual = $(this).parent().parent();
   atual.fadeOut();

   var proximo = atual.nextAll("div.info");
   if (proximo.length)
      {
      proximo.eq(0).fadeIn();
      SaveToCookies(proximo);
      };
   };

// ---

function Previous()
   {
   var atual = $(this).parent().parent();
   atual.fadeOut();

   var anterior = atual.prevAll("div.info");
   if (anterior.length)
      {
      anterior.eq(0).fadeIn();
      SaveToCookies(anterior);
      };
   };
   
// ---

function First()
   {
   $("div.info").hide();

   var primeiro = $("div.info:first");
   primeiro.fadeIn("slow");
   
   SaveToCookies(primeiro);
   };

// ---

// If we go back to the same page in less than 10 minutes, it opens the slide we were in before leaving the page.
function SaveToCookies(element)
   {
   if (!$(element).length) return;
   
   // Gets the element id.
   var id = $(element).attr('id');
   
   // Saves the element id plus the page to the cookies. days = 0.00694 = 10 minutes.
   createCookie('slide', id + "|" + location.pathname, 0.00694, '/demo');
   };

// ---

$(function() {

    // If we don't have a current slide, starts with the first one.
    var currentSlide = "info1";

    if (readCookie('slide'))
       {
       // Reads the current page from the cookies.
       var currentPage = readCookie('slide').split('|')[1];

       if (currentPage == location.pathname)
          {
          // Reads the current slide from the cookies.
          currentSlide = readCookie('slide').split('|')[0];
          };
       };

    // Counts slides.
    var slideNum = 1;
    
    // Get all slides.
    var slides = $("div.info")
    
    // For each slides, add buttons Previous and Next.
    slides.each(function()
       {
       texto = '<div class="buttonSpace"><span class="slideNum">slide ' + slideNum + '/' + slides.length + '</span><button class="next">Next&nbsp;&nbsp;&nbsp;>></button><button class="previous"><<&nbsp;&nbsp;&nbsp;Previous</button></div>';
       slideNum++;
       $(this).append(texto);
       });

    // Removes first Previous, and last Next.
    $("button.previous:first").attr("disabled","disabled").css("opacity","0.4");
    $("button.next:last").attr("disabled","disabled").css("opacity","0.4");

    // Adds button events.
    $("button.next").click(Next);
    $("button.previous").click(Previous);

    // Shows the current slide.
    if (!$("#" + currentSlide).length) currentSlide = "info1";
    $("#" + currentSlide).fadeIn(2000);

    SaveToCookies($("#" + currentSlide));
    });

// ---

////////////////////////////////////////////////////////////////////////////////
// COOKIES
////////////////////////////////////////////////////////////////////////////////

testaCookie = function()
   {
   return navigator.cookieEnabled;
   };
   // testaCookie

////////////////////////////////////////////////////////////////

readCookie = function(nome)
   {
  	var i, cookieArray;
  	nome += "=";
  	cookieArray = document.cookie.split(";");
  	for (i=0; i<cookieArray.length; i++)
  	   {
    		var c = cookieArray[i];
    		while (c.charAt(0)==" ") c = c.substring(1, c.length);
    		if (c.indexOf(nome) == 0) return c.substring(nome.length, c.length);
     	}
  	return null;
  	};
  	// readCookie

////////////////////////////////////////////////////////////////

createCookie = function(nome, valor, days, path, domain)
   {
   var data, expira;
  	if (days)
     	{
    	data = new Date();
    	data.setTime(data.getTime()+(days*24*60*60*1000));
    	expira = "; expires=" + data.toGMTString();
     	}
   else expira = "";
   if (!path) path = "/";
   if (!domain) domain = "";
   else domain = "; domain=" + domain;
  	document.cookie = nome + "=" + valor + expira + "; path=" + path + domain;
   };
   // createCookie

////////////////////////////////////////////////////////////////

eraseCookie = function(nome)
   {
   iCents.createCookie(nome, "", -1);
   };
   // eraseCookie

////////////////////////////////////////////////////////////////

// END


