// ===========================================
// >>>> NOTE, this file requires jquery
// >>>> NOTE, this component is strictly connected with html anc css
// ===========================================

// ===========================================
// ========       Slider OBJECT      =========
// ===========================================

function Slider() {

  // Fields           ======================================================
  var listDiv = new Array();

  var idxActualDiv = -1;

  var listImage = new Array();

  var timer = null;

  var intervalSliding = 3000;

  // Attributes       ======================================================

  this.getImage = function (idx) {
    if (idx >= 0 || idx < listImage.length) {
      return listImage[idx];
    } else {
      return null;
    }
  }

  var startNextDiv_Fun_ = function (div, img, idx) { };
  this.setStartNextDiv_Fun = function (startNextDiv_Fun) {
    if ($.isFunction(startNextDiv_Fun)) {
      startNextDiv_Fun_ = startNextDiv_Fun;
    } else {
      startNextDiv_Fun = function (div, img) { };
    }
  }

  var endNextDiv_Fun_ = function (div, img, idx) { };
  this.setEndNextDiv_Fun = function (endNextDiv_Fun) {
    if ($.isFunction(endNextDiv_Fun)) {
      endNextDiv_Fun_ = endNextDiv_Fun;
    } else {
      endNextDiv_Fun = function (div, img) { };
    }
  }

  // Public methods   ======================================================

  var restartSliding = this.restartSliding = function () {
    startSliding(intervalSliding);
  }

  var startSliding = this.startSliding = function (intervall) {
    if (intervall == null) {
      return;
    }

    if (timer != null) {
      clearTimeout(timer);
    } else {
      nextDiv();
    }
    intervalSliding = intervall;
    timer = setTimeout(nextSliding, intervall);
  }

  var stopSliding = this.stopSliding = function () {
    if (timer != null) {
      clearTimeout(timer);
    }
    timer = null;
  }

  var addDivAndImg = this.addDivAndImg = function (div, img) {
    addDiv(div);
    addImage(img);
  }

  var nextDiv = this.nextDiv = function () {
    if (listDiv.length <= 0) {
      return;
    }
    if (listDiv.length == 1 || idxActualDiv < 0) {
      idxActualDiv = 0;
      $(listDiv[idxActualDiv]).show();
      $(listDiv[idxActualDiv]).css('z-index', getZLevel(listDiv[idxActualDiv]) + 1);

      adjustActualImageSize();

      endNextDiv_Fun_(listDiv[idxActualDiv], listImage[idxActualDiv], idxActualDiv);
      return;
    }

    var nextDiv = nextDivIdx(idxActualDiv);



    var actualDiv = $(listDiv[idxActualDiv]);
    $(listDiv[idxActualDiv]).css('z-index', getZLevel(listDiv[idxActualDiv]) - 1);
    $(listDiv[nextDiv]).show();
    $(listDiv[nextDiv]).css('z-index', getZLevel(listDiv[idxActualDiv]) + 1);
    $(listDiv[nextDiv]).offset({ top: $("#main").height() });

    startNextDiv_Fun_(listDiv[idxActualDiv], listImage[idxActualDiv], idxActualDiv);
    $(listDiv[nextDiv]).animate({
      top: 0
    },
		1000,
		function () {
		  endNextDiv_Fun_(listDiv[idxActualDiv], listImage[idxActualDiv], idxActualDiv);
		  actualDiv.hide();
		});

    idxActualDiv = nextDiv;

    adjustActualImageSize();
  }

  var adjustActualImageSize = this.adjustActualImageSize = function () {
    if (idxActualDiv >= 0) {
      adjustImageSize(listImage[idxActualDiv]);
    }
  }

  var adjustImageSize = this.adjustImageSize = function (img) {

    var rapMain = ($("#main").width() / $("#main").height());
    var rapImg = ($(img).width() / $(img).height());
    if (rapMain > rapImg) {
      $(img).css("width", "100%");
      $(img).css("height", "auto");
    } else {
      $(img).css("width", "auto");
      $(img).css("height", "100%");
    }

    var top = ($(img).parent().height() - $(img).height()) / 2;
    $(img).css("margin-top", parseInt(top) + "px");
    var left = ($(img).parent().width() - $(img).width()) / 2;
    $(img).css("margin-left", parseInt(left) + "px");
  }

  // Private methods  ======================================================
  var nextSliding = function () {
    if (timer != null) {
      nextDiv();
      timer = setTimeout(nextSliding, intervalSliding);
    }
  }

  var nextDivIdx = function (index) {
    if (index < 0 || index >= listDiv.length) {
      return 0;
    }
    if (index + 1 >= listDiv.length) {
      return 0;
    }

    return index + 1;
  }

  var getZLevel = function (div) {
    var zidx = 0;
    if ((zidx = parseInt($(div).css('z-index'))) == "NaN") {
      return 0;
    } else {
      return zidx;
    }
  }

  var addDiv = function (div) {
    $(div).attr("id", "backgroundImg" + listDiv.length);

    listDiv[listDiv.length] = div;
  }

  var addImage = function (img) {
    listImage[listImage.length] = img;
  }


  // Constructor      ======================================================
  var constructor = function () { //Constructor definition

  }
  constructor(); //Constructor call

}
