/*
Script:
	NFCarousel
	
Author:
	js:
	Richard Hoogstad, <http://www.hoogstadsoftware.nl/>
	
	css:
	Jelmer Janssen, 
 
License:
	MIT License
*/
function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
  return myArray;
}

var NFCarousel = new Class({
	
	options: {
		container:"carousel",
		slideContainer:"slide_container",
		slides: [],
        startIndex: 0,
		nextButton:"next",
		previousButton:"previous",
		autoRotate:false,
		shuffle:false
		
	},
	
	initialize: function(options){
		this.setOptions(options);
		this.slides=this.options.slides;
		this.container=$(this.options.container);
		this.autoSlide=this.options.autoSlide;
		this.setSlideContainer();
		this.slides=this.slideContainer.getChildren('div');
		
		this.index=this.options.startIndex;
		this.autoRotate=this.options.autoRotate;
		//this.setButtons();
		this.shuffle();
		
		for(var i=0;i<this.slides.length;i++){
			//console.log(this.slides[i]);
			if(i==0){
				this.slides[i].setStyle('opacity', '1');
			}else{
				this.slides[i].setStyle('opacity', '0');		
			}
		}
		this.setMaskElement();
		//automatic slide
		if(this.autoRotate && this.slides.length>1){
			this.startAutoRotate();
		}
		this.setMaskEvent();
		
	},
	setSlideContainer : function(){
		var childElements=$$('#'+this.options.container+' div.'+this.options.slideContainer);
		this.slideContainer=childElements[0];
		//console.log(this.slideContainer);
	},
	setButtons : function(){
		var childElements;
		//set the next button
		childElements=$$('#'+this.options.container+' a.'+this.options.nextButton);
		this.nextButton=childElements[0];
		
		//console.log(this.nextButton);
		//set the previous button
		childElements=$$('#'+this.options.container+' div.'+this.options.previousButton);
		this.previousButton=childElements[0];
	},
	nextSlide : function(){
		var previousIndex=this.index;
		//get the new this.index
		if(this.index<this.slides.length-1){
			this.index++;
		}else{
			this.index=0;
		}
		var newIndex=this.index;

	
		this.slides[newIndex].fx 		= this.slides[newIndex].effect('opacity', {duration: 1000}).start(1);
		this.slides[previousIndex].fx 	= this.slides[previousIndex].effect('opacity', {duration: 1000}).start(0)
		
		//console.log(this.slides[previousIndex].id);
		//console.log(this.slides[newIndex].id);
		this.setMaskEvent();
	
	},
	previousSlide:function(){
		//set the previous slide
		var previousIndex=this.index;
		if(this.index>0){
			this.index--;
		}else{
			this.index=this.slides.length-1;
		}
		var newIndex=this.index;
		
		this.slides[previousIndex].effect('opacity', {duration: 1000}).start(0);
		this.slides[newIndex].effect('opacity', {duration: 1000}).start(1);
		this.setMaskEvent();
	
	},
	startAutoRotate : function(){
		//this.nextSlide.delay(1000);
		this.nextSlide.periodical(5000,this);
		
	},
	setMaskElement : function(){

		var childElements=$$('#'+this.options.container+' span.mask');
		//alert('#'+this.options.container+' span.mask');
		this.maskElement=childElements[0];	
		//this.maskElement.addEvent('click',alert('aap'));


		
	},
	/**
	 *ie z-index workaround
	 */
	setMaskEvent : function(){
		
		var currentSlide=this.slides[this.index];
		var childElements=currentSlide.getChildren();

		var currentLink;
		
		//get the text box container
		for(var i=0;i<childElements.length;i++){
			if(childElements[i].getTag()=="div" && childElements[i].hasClass('link')){
				currentLink=childElements[i];
			}
		}

		/*
		 *needs to be some kind of event trigger
		 */
	
		this.maskElement.onclick=function(){
			//alert(thisObj.index);
			window.location=currentLink.innerHTML;
		}
	},
	shuffle : function(){
		var slides=fisherYates(this.slides);
		//console.log(slides);
	}
});
NFCarousel.implement(new Events, new Options);