var index_currentPhoto = -1;
var index_lastPhoto = -1;
var transition_delay = 7500;
var transition_time = 1000;
var index_backgroundTimer = null;
var rotater_nodes = new Array();
var sidemenu_indexes = new Array();
var index_nodes = new Array();

var index_preload_backgrounds = new Array();
var index_preload_hbackgrounds = new Array();

window.addEvent('domready', function(){
  $('background').setStyle('background-image', 'none');
//  $('index-background').setAttribute('src', base_url+'blank.gif');
  var i = 0;
  var j = 0;
  $$('.csm_content').each(function(el) {
    if (el.getAttribute('background').length > 0) {
      rotater_nodes[i] = el;
      sidemenu_indexes[i] = j
      i++;
    }
    j++;
  });
  var i = 0;
  rotater_nodes.each(function(el) {
    var divStyle = {'position':'absolute',
                   'left':'0px',
                   'top':'85px',
          				 'margin':'0px',
          				 'padding':'0px',
//          				 'background-position':'0px 85px',
//          				 'background-repeat': 'repeat',
          				 'opacity':'0'
//                   'background-image': 'url(\''+base_url+'images/exhibitions/'+el.getAttribute('background')+'\')'
                   };
    index_preload_backgrounds[i] = new Image();
    index_preload_backgrounds[i].src = base_url+'images/exhibitions/'+el.getAttribute('background');
	  index_nodes[i] = new Element('div', {'styles': divStyle});
	  index_nodes[i].setHTML('<img src="'+base_url+'images/exhibitions/'+el.getAttribute('background')+'">');
    $('background').appendChild(index_nodes[i]);
    index_preload_hbackgrounds[i] = new Image();
    index_preload_hbackgrounds[i].src = base_url+'images/exhibitions/'+el.getAttribute('header');
    i++;
  });
  showBackground(0);
});

function showNextBackground() {
  var new_background = (index_currentPhoto+1 == index_preload_backgrounds.length) ? 0 : (index_currentPhoto+1);
  showBackground(new_background);
}

function showBackground(index) {
  if (!index_preload_backgrounds[index].complete) {
    index_backgroundTimer = setTimeout('showNextBackground()', 250);
  }
  else {
    clearTimeout(index_backgroundTimer);
    index_backgroundTimer = null;
    changeBackground(index);
    if (rotater_nodes.length > 1) {
      index_backgroundTimer = setTimeout('showNextBackground()', transition_delay);
    }
  }
}


function changeBackground(index) {
	if (index_currentPhoto != index || index_lastPhoto == -1)
	{
    var i = 0;
    index_nodes.each(function(el) {
      if (i != index_currentPhoto) {
        el.setOpacity(0);
      }
      i++;
    });
		var effect1 = null;
    var effect2 = null;
		if (index >= index_currentPhoto) {
		  if (index_lastPhoto >= 0) {
	      effect2 = index_nodes[index_currentPhoto].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
	    }
	    effect1 = index_nodes[index].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
     }
		else {
	    effect1 = index_nodes[index].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
	    effect2 = index_nodes[index_currentPhoto].effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
		}
    if (effect1 != null) {
      effect1.start(0,1);
    }
    if (effect2 != null) {
      effect2.start(1,0);
    }

    $('photocredit').innerHTML = rotater_nodes[index].getAttribute('credit');
    $('section_header').setAttribute('src', base_url+'images/exhibitions/'+rotater_nodes[index].getAttribute('header'));

    index_lastPhoto = index_currentPhoto;
		index_currentPhoto = index;

    var el = rotater_nodes[index_currentPhoto];
    effect3 = el.effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
    effect3.start(0.5,1);
    el.addClass('sm_mouseover');
    if (index_lastPhoto != -1) {
      el = rotater_nodes[index_lastPhoto];
	    effect4 = el.effect('opacity', {duration: transition_time, transition:Fx.Transitions.linear});
      effect4.start(1,0.5);
      el.removeClass('sm_mouseover');
    }
	}
}
