/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function Rotator(path) {
  var obj = this;
  this.loopCount = 2; /* numer przebiegu */
  this.stateColumn1 = false; /* stan grafiki w poszczególnych kolumnach */
  this.stateColumn2 = false;
  this.stateColumn3 = false;
  this.path = path;          /* ścieżka do katalogu w którym znajduje się plik banner.php */
  this.idsColumn1 = new Array();
  this.linksColumn1 = new Array();
  this.lastColumn1Index = 2;
  this.idsColumn2 = new Array();
  this.linksColumn2 = new Array();
  this.lastColumn2Index = 2;
  this.idsColumn3 = new Array();
  this.linksColumn3 = new Array();
  this.lastColumn3Index = 2;
  var jsonString = $.ajax({
      type: "GET",
      url: path + "ajaxInterface.php",
      data: "action=getColumns",
      async: false
    }).responseText;

  var json = eval("(" + jsonString + ")");
  this.idsColumn1 = json.values[0];         /* id grafiki bannera */
  this.linksColumn1 = json.links[0];        /* link, po kliknięciu w banner prowadzi na stronę */
  this.loopCountAll = json.links[0].length; /* ile przebiegów */
  //console.log('loopCountAll: '+this.loopCountAll);
  this.idsColumn2 = json.values[1];
  this.linksColumn2 = json.links[1];
  this.idsColumn3 = json.values[2];
  this.linksColumn3 = json.links[2];
  this.durationRow = new Array();
  $.each(json.duration, function(i, val) { /* czas wyświetlania całego rzędu banerów */
    obj.durationRow[i] = val;
  });
  //console.log('duration: 1-'+this.durationRow[0]+' 2-'+this.durationRow[1]+' 3-'+this.durationRow[2]);
  this.displayRow = new Array(); /* rodzaj wyświetlania poszczególnych przebiegów - testowo 3 przebiegi */
  $.each(json.display, function(i, val) {
    obj.displayRow[i] = val;
  });
  //console.log('display: 1-'+this.displayRow[0]+' 2-'+this.displayRow[1]+' 3-'+this.displayRow[2]);
}

Rotator.prototype.clearState = function() {
  this.stateColumn1 = false;
  this.stateColumn2 = false;
  this.stateColumn3 = false;
}

Rotator.prototype.checkState = function(state) {
  if(!this.stateColumn1 || !this.stateColumn2 || !this.stateColumn3) {
    //console.log("Zeruje timer");
    clearTimeout(timer);
  }
  //console.log('przebieg: '+this.loopCount);
  //console.log('state: 1-'+this.stateColumn1+' 2-'+this.stateColumn2+ ' 3-'+this.stateColumn3);
  if(this.displayRow[this.loopCount-1] == 'standalone') { /* wyświetlane osobno */
    $('#column'+state+'Image').fadeIn("slow", function() {
      //console.log('image '+state+': ok [standalone]');
    });
  } else if(this.displayRow[this.loopCount-1] == 'connected') { /* wyswietlane jako 1 cały baner */
    $("#TripleRotator").css('visibility','hidden');
    if(this.stateColumn1 && this.stateColumn2 && this.stateColumn3) {
      //console.log('display: ['+this.displayRow[this.loopCount-1]+']');
      for(var ii=1; ii<=3; ii++) {
        $('#column'+ii+'Image').fadeIn("slow");
        //console.log('image '+ii+': ok');
      }
    }
  }

  if(this.stateColumn1 && this.stateColumn2 && this.stateColumn3) {
    //console.log("in/out");
    timer = setTimeout("timerEvent()", rotator.durationRow[rotator.loopCount-1]);
    if(this.displayRow[this.loopCount-1] == 'connected') {
      $("#TripleRotator").css('visibility', 'visible');
    }
    if(this.loopCount-1 < this.loopCountAll-1) { /* this.loopCount-1 < this.loopCountAll-1 */
      this.loopCount++; /* zaczynamy kolejny przebieg */
    } else {
      this.loopCount=1; /* zaczynamy od początku */
    }
    this.clearState();
  }
}

Rotator.prototype.rotate = function() {
  //console.log('przebieg:'+this.loopCount+' | length: '+this.idsColumn1.length+' | index:'+this.lastColumn1Index);

  if(init) {
    //this.loopCount = 0;
    //console.log("inicjalizacja loopCount");
    //init = 0;
  }
  //console.log('Przebieg: '+this.loopCount);
  var obj = this;
  var image1 = new Image();
  var image2 = new Image();
  var image3 = new Image();
  $(image1).attr('class', 'rotatorImage');
  $(image1).attr('id', 'column1Image');
  $(image2).attr('class', 'rotatorImage');
  $(image2).attr('id', 'column2Image');
  $(image3).attr('class', 'rotatorImage');
  $(image3).attr('id', 'column3Image');

  // grafika banera 1
  $(image1).hide()
  .load(
    function () {
      $("#column1").html(this);
      //$(this).wrap('<a href="'+obj.linksColumn1[obj.lastColumn1Index]+'"></a>');
      $(this).wrap('<a href="'+obj.path+'redirect.php?id='+(obj.loopCount)+'&type=tv&s='+ssid+'"></a>');
      obj.stateColumn1 = true;
      obj.checkState(1);
    })
  .error(function () {
    $("#column1").text('Wystąpił błąd!');
  }).attr('src',obj.path + "banner.php?id=" + obj.idsColumn1[obj.lastColumn1Index-1] + "&type=tv");

  // grafika banera 2
  $(image2).hide()
    .load(function () {
      $("#column2").html(this);
      //$(this).wrap('<a href="'+obj.linksColumn2[obj.lastColumn2Index]+'"></a>');
      $(this).wrap('<a href="'+obj.path+'redirect.php?id='+(obj.loopCount)+'&type=net&s='+ssid+'"></a>');
      obj.stateColumn2 = true;
      obj.checkState(2);
    })
    .error(function () {
      $("#column2").text('Wystąpił błąd!');
    })
    .attr('src', obj.path + "banner.php?id=" + obj.idsColumn2[obj.lastColumn2Index-1] + "&type=net");

  // grafika banera 3
  $(image3).hide()
    .load(function () {
      $("#column3").html(this);
      //$(this).wrap('<a href="'+obj.linksColumn3[obj.lastColumn3Index]+'"></a>');
      $(this).wrap('<a href="'+obj.path+'redirect.php?id='+(obj.loopCount)+'&type=voip&s='+ssid+'"></a>');
      obj.stateColumn3 = true;
      obj.checkState(3);
    })
    .error(function () {
      $("#column3").text('Wystąpił błąd!');
    })
    .attr('src',obj.path + "banner.php?id=" + obj.idsColumn3[obj.lastColumn3Index-1] + "&type=voip");

   // console.log('length: '+this.idsColumn1.length);
   // console.log('index: '+this.lastColumn1Index);
  if (this.lastColumn1Index >= this.idsColumn1.length) {
    this.lastColumn1Index = 1;
  } else {
    this.lastColumn1Index++;
  }
  if (this.lastColumn2Index >= this.idsColumn2.length) {
    this.lastColumn2Index = 1;
  } else {
    this.lastColumn2Index++;
  }
  if (this.lastColumn3Index >= this.idsColumn3.length) {
    this.lastColumn3Index = 1;
  } else {
    this.lastColumn3Index++;
  }
}