var Italjanci = {
  init: function(){
  	this.background_array = ['pano1', 'pano2', 'pano3'];
  	this.bytilka_array = [['images/objects/01-trans.png', 'object1'], ['images/objects/02-trans.png', 'object2'], ['images/objects/03-trans.png', 'object3'], ['images/objects/04-trans.png', 'object4'], ['images/objects/05-trans.png', 'object5'], ['images/objects/06-trans.png', 'object6']];
    if ($('pano_change') && $('panorama')){
      $('pano_change').observe('click', this.changeFignja.bind(this));
    }
    if ($('bkwd') && $('fwd') ){
      $('bkwd').observe('click', this.prevBytilka.bind(this));
      $('fwd').observe('click', this.nextBytilka.bind(this));
    }
	for( var i =0; i< Math.floor((this.background_array.length)*Math.random()); ++i)
		this.changeFignja();
		
	for( var i =0; i< Math.floor((this.bytilka_array.length)*Math.random()); ++i)
		this.nextBytilka();
  },
  changeFignja: function(){
  	
    var className = $('panorama').className;
    if (this.background_array.indexOf(className) != -1){
      var index = this.background_array.indexOf(className);
      if (index == this.background_array.size() - 1){
        index = 0;		
      } else {
        index++;
      }
      $('panorama').removeClassName(className);
      $('panorama').addClassName(this.background_array[index]);
    }
    return false;
  },
  prevBytilka: function(){
    var className = $('object1').className;
    var mainIndex = -1;
    this.bytilka_array.each(function(item, index) {
      if (item[1] == className){
        mainIndex = index;
      }
    });
    if (mainIndex != -1){
      if (mainIndex == 0){
        mainIndex = this.bytilka_array.size() - 1;		
      } else {
        mainIndex--;
      }
      $('object1').removeClassName(className);
      $('object1').addClassName(this.bytilka_array[mainIndex][1]);
      $('object1').src = this.bytilka_array[mainIndex][0];
      return false;
    }
    return false;
  },
  nextBytilka: function(){
    var className = $('object1').className;
    var mainIndex = -1;
    this.bytilka_array.each(function(item, index) {
      if (item[1] == className){
        mainIndex = index;
      }
    });
    if (mainIndex != -1){
      if (mainIndex == this.bytilka_array.size() - 1){
        mainIndex = 0;		
      } else {
        mainIndex++;
      }
      $('object1').removeClassName(className);
      $('object1').addClassName(this.bytilka_array[mainIndex][1]);
      $('object1').src = this.bytilka_array[mainIndex][0];
      return false;
    }
    return false;
  }
};

document.observe("dom:loaded", function() {
	Italjanci.init();
});
