/*
 * default.js
 */
 
 /**
<div id="header-image-controls">
  <span class="image-forward"></span>
  <span class="image-number">Bild 0/0</span>
  <span class="image-backward"></span>
<div class="clear"></div>
**/

var mmFX = function(iL) {

  config = {
  
    //autorun für die home
    autorun:false,
    
    //fadein dauer
    duration:1000,
    
    //dauer bis nächstes bild
    interval:5000,
    
    //selector-string für die vor und zurück buttons
    buttonNext:'#header-image-controls .image-forward',
    buttonPrevious:'#header-image-controls .image-backward',
    labelIndex:'#header-image-controls .image-number',
    controls:'#header-image-controls',
    
    //zuerst headerimages, dann home images
    imageSelector:'#header-image div',
    altImageSelector:'#home-image div'
  }

  currentImage = 0;
  lastImage = 0;
  images = new Array();
  autorunInterval = null;
  autorunInited = false;
  locked = false;
  imageList = iL;
  this.init();
}
mmFX.prototype = new Object();

mmFX.prototype.init = function() {
//  $('#header-image').wrap('<div id="header-image-container" style="z-index:-10;" />');

  images = this.getImages();
  images.css('position','absolute');
  
  if(images.length == 0) {
    if($(config.controls).length > 0) {
        $(config.controls).css('display','none');
    }
  } else {
    config.autorun = true;
	if(imageList.length > 0) {
		this.loadContent();
	}
	this.onInitComplete();
  }
}

mmFX.prototype.onInitComplete = function() {
	images = this.getImages();
	this.onFadeInBefore();
	this.initAutorun();
	this.setButtonStates();
}

mmFX.prototype.loadContent = function() {
  if(imageList.length>0) {
	  var cImg = imageList.shift();
	  /*
			<div style="display:none;">
			  <table cellspacing="0" cellpadding="0">
				<tr>
				  <td>
					|
				  </td>
				</tr>
			  </table>
			</div>
		*/
	   
	  var e = $('<div style="display:none;"><table cellspacing="0" cellpadding="0"><tr><td>'+cImg+'</td></tr></table></div>').appendTo(images.parent());
	  var scope = this;
	  e.find('img').bind('load', function() {
		scope.loadContent();
	  });
	  if(!autorunInited)
		this.initAutorun();
  } else {
	this.onInitComplete();
  }
}

mmFX.prototype.getImageSelector = function() {
  var selector = config.imageSelector;
  if($(selector).length == 0)
    selector = config.altImageSelector;	
  return(selector);
}
mmFX.prototype.getImages = function() {
	var selector = this.getImageSelector();
	return($(selector));
}
mmFX.prototype.next = function() {
  if(!locked) {
    ++currentImage;
    this.show();
  }
  return false;
}
mmFX.prototype.previous = function(event) {
  if(!locked) {
    --currentImage;
    this.show();
  }
  return false;
}
mmFX.prototype.show = function() {
  this.lock();
  if(typeof(this.onFadeInBefore) == "function") {
    this.onFadeInBefore();
  }
  /*
  $(images[lastImage]).css('width', $(images[lastImage]).width() + 'px' );
  $(images[lastImage]).css('height', $(images[lastImage]).height() + 'px' );
  alert("lastImageWidth: "+ $(images[lastImage]).width() + " / lastImageHeight: "+ $(images[lastImage]).height());
  */
  $(images[lastImage]).css('z-index',images.length);
  for(var i = 0; i<images.length; i++) {
    if(i == lastImage || i == currentImage) continue;
//    $(images[i]).css('display','none');
    $(images[i]).css('z-index',i);
  }
  $(images[currentImage]).css('display','none');
  $(images[currentImage]).css('z-index',images.length + 1);
  var scope = this;
  $(images[currentImage]).fadeIn(config.duration, function() {
    scope.onFadeInAfter();
  });
  lastImage = currentImage
}

// Hooks
mmFX.prototype.onFadeInBefore = function() {
  if(config.autorun) {
	images = this.getImages();
    window.clearInterval(autorunInterval);
    if(currentImage == images.length) {
      currentImage = 0;
    }
  }
  if($(config.labelIndex).length > 0) {
    $(config.labelIndex).html('Bild ' + (currentImage + 1) + '/' + images.length);
  }
}

mmFX.prototype.onFadeInAfter = function() {
  this.setButtonStates();
  this.initAutorun();
  this.unlock();
}




// Helpers
mmFX.prototype.stopAutorun = function() {
    window.clearInterval(autorunInterval);
    config.autorun = false;
	autorunInited = false;
}
mmFX.prototype.lock = function() {
  locked = true;
}
mmFX.prototype.unlock = function() {
  locked = false;
}
mmFX.prototype.setButtonStates = function() {
  //if(!config.autorun) {
  if( $(config.buttonNext).length > 0 &&  $(config.buttonPrevious).length > 0 ) {
    this.bindButton($(config.buttonNext), (currentImage < images.length - 1), 'click', function(event) {
        event.data.scope.stopAutorun();
        event.data.scope.next();
    });
    this.bindButton($(config.buttonPrevious), (currentImage > 0), 'click', function(event) {
        event.data.scope.stopAutorun();
        event.data.scope.previous();
    });
  }
}
mmFX.prototype.bindButton = function(btn, bind, event, func) {
  if(bind) {
    btn.bind(event, {scope:this}, func);
    btn.css('cursor','pointer');
  } else {
    btn.unbind(event);
    btn.css('cursor','default');
  }
}
mmFX.prototype.initAutorun = function () {
  window.clearInterval(autorunInterval);
  if(config.autorun && images.length > 1) {
    var scope = this;
    autorunInterval = window.setInterval(function() {
      scope.next();
    }, config.interval);
	autorunInited = true;
  }
}



/** STACHT **/
$(document).ready(function() {
  var fx = new mmFX( (typeof mmImageList == "undefined" ?  new Array() : mmImageList) );
  //fx.test('hallo');
});

