﻿// ===========================================
// >>>> NOTE, this file requires jquery
// ===========================================

// ===========================================
// ========     INCLUDE FUNCTIONS     ========
// ===========================================
//$.ajax({url: "js/document/animation.js", type: "GET", dataType: "script", cache: true});
//	$.ajax({url: "js/object/imageloader.js", type: "GET", dataType: "script", cache: true});

// ===========================================
// ========      GLOBAL BOJECTS      =========
// ===========================================

var slider;

var listImagesToLoad;

var listTitleImages;
var listTextImages;

var stopLoaderFunc;

var idHiddenFieldActualImage;

// ===========================================
// ========     PUBLIC FUNCTIONS     =========
// ===========================================
function onPreload_InitPage() {

}

function onEndload_InitPage() {
  // Windows resize event handler registration
  $(window).resize(pageResize);



  // Lista immagini da caricare

  /* Mantenere la coerenza con questo array e quello definito in
  *  "~/Pages/Main.aspx.cs" i due array devono essere coerenti
  *  così da permettere il corretto passaggio del valore indexImage
  */
  listImagesToLoad = new Array();
  listImagesToLoad[0] = "img/default/0001.jpg";
  listImagesToLoad[1] = "img/default/0002.jpg";
  listImagesToLoad[2] = "img/default/0003.jpg";
  listImagesToLoad[3] = "img/default/0004.jpg";
  listImagesToLoad[4] = "img/default/0005.jpg";
  listImagesToLoad[5] = "img/default/0006.jpg";

  listTitleImages = new Array();
  listTitleImages[0] = "The method";
  listTitleImages[1] = "DOM tree";
  listTitleImages[2] = "Secondo la";
  listTitleImages[3] = "By prefixing";
  listTitleImages[4] = "Le informazioni";
  listTitleImages[5] = "Anche il modo";

  listTextImages = new Array();
  listTextImages[0] = "The method optionally accepts a selector expression of the same type that we can";
  listTextImages[1] = "the parents of these elements in the DOM tree and construct";
  listTextImages[2] = "and its properties and methods are declared inside it by prefixing them with the keyword";
  listTextImages[3] = "Secondo la ricerca, pubblicata su Science, il web ha a";
  listTextImages[4] = "memorizzare le informazioni utilizzando nuovi paradigmi";
  listTextImages[5] = "rivoluzionato anche il modo in cui la nostra mente organizza e archivia i ricordi e a forza di rivolgerci ai";

  // Create and start imageloader

  var imageLoader = new ImageLoader(listImagesToLoad);
  imageLoader.setImageLoad_Fun(on_ImageLoad_Fun);
  imageLoader.startLoad();

  slider = new Slider();
  slider.setStartNextDiv_Fun(on_StartNextDiv_Fun);
  slider.setEndNextDiv_Fun(on_EndNextDiv_Fun);

  // Call rezize the first time
  pageResize();
}


// ===========================================
// ========     PRIVATE FUNCTIONS     ========
// ===========================================

//=== Page resize ===
function pageResize() {
  var widthWindow = $("#main").width();
  var heightWindow = $("#main").height();

  //Se si va sotto una certa dimensione
  //  ferma il menÃ¹ a sx
  if (widthWindow < 800) {
    var diff = 800 - widthWindow;
    if (diff > 90) {
      diff = 90;
    }
    $('#menu').css('left', 100 - diff);
  } else {
    $('#menu').css('left', 100);
  }



  //Se si va sotto una certa dimensione
  //  ferma il labelImage
  if (heightWindow < 700 && widthWindow < 800) {
    var diff = 700 - heightWindow;
    if (diff > 50) {
      diff = 50;
    }
//    $('#labelImage').css('bottom', 100 - diff);
  } else {
;//  $('#labelImage').css('bottom', 100);
  }

  //Se non riesco a farci stare dentro il rettangolo
  //  laterale lo tolgo

//  if (heightWindow >= 650 || widthWindow >= 500) {
//    $("#labelImage").show();
//  } else {
//    $("#labelImage").hide();
//  }



  //Se lo schermo Ã¨ abbastanza grande
  //  adatto i due footer in basso ad
  //  un max di 240px

  var sizeDivFooter = Math.max(240, widthWindow - 10) / 2;
  sizeDivFooter = Math.min(sizeDivFooter, 240);
  $("#footerLeft").css("width", sizeDivFooter);
  $("#footerRight").css("width", sizeDivFooter);


  //Se lo schermo Ã¨ orizzontale adatto lo sfondo
  //  orizzontalmente altrimenti lo adatto veritcalmente
  slider.adjustActualImageSize();
}



//=== Set page size to actual size ===

function bloccaDimensioniPagina() {
  $("#main").css("width", ($("#main").width()) + "px");
  $("#main").css("height", ($("#main	").height()) + "px");
}


//=== Callback function OnLoad ===


function on_ImageLoad_Fun(img) {
  var widthWindow = $("#main").width();
  var heightWindow = $("#main").height();

  $(img).addClass("backgroundImg");


  var div = $("<div />");
  $(div).attr("class", "background");
  $(div).hide();
  $(div).append(img);
  $("#main").append(div);

  if (slider.getImage(0) == null) {
    slider.addDivAndImg(div, img);
    slider.nextDiv();
    slider.startSliding(9000);
  } else {
    slider.addDivAndImg(div, img);
  }

  if (stopLoaderFunc != null) {
    stopLoaderFunc();
  }
}

function on_StartNextDiv_Fun(div, img, idx) {
/*
  $("#labelImage").animate({
    right: -300
  },
	100,
	function () {

	});
*/
}

function on_EndNextDiv_Fun(div, img, idx) {
//  $("#labelImageTitle").text(listTitleImages[idx]);
//  $("#labelImageText").text(listTextImages[idx]);
//  $("#labelImage").delay(500).animate(
//    {
//      right: 0
//    },
//	  600,
//	  function () {
//	  }
//  );

  $("#" + idHiddenFieldActualImage).val(idx);

}
