var kamAnimationInterval = null;
var kamAnimationTransitionLength = null;

var imageFader = new Class({
	Implements:[Options,Events],
	options:{
		itemIdentifier: '.item',
		navigationItems: null,
		startIndex: 1,
		transitionLength: 750,
		moveTimeSecs: 5000
	},
	baseElm:null,
	items:null,
	numItems:null,
	currentItem:0,
	periodical:null,
	backgroundSet:false,
	index:0,
	prevIndex:0,
	nextFunction:null,
	flag:false,
	fadeIn:null,
	
	initialize: function(baseElm,options){	
		this.setOptions(options);
		// set class properties
		this.baseElm = $(baseElm);	
		this.items = baseElm.getElements(this.options.itemIdentifier);
		this.numItems = this.items.length;
		this.currentItem = 0;

		this.baseElm.setStyle("overflow-y","hidden");
		
		if( this.numItems > 1 ){
			// set background
			this.setBackground(this.items[this.currentItem]);

			this.items.each( function(elm){
				elm.setStyle("display","none");			
				elm.setStyle("opacity",0);
			});
		}	
		
		if(this.options.navigationItems){
			this.options.navigationItems.each( function(navItem, key){
				navItem.addEvent('click', function(evt){
					evt.stop();	
					if(!this.flag){					
						this.periodical = $clear(this.periodical);		
						if(this.nextFunction) this.periodical = this.nextFunction.periodical(this.options.moveTimeSecs);													
						this.prevIndex = this.currentItem;
						
						if(key != 0 && key != this.options.navigationItems.length - 1){												
							var prev = key - 2;
							if(prev < 0) prev = this.options.navigationItems.length - 1;
							this.currentItem = prev;
							this.moveNext();
						}else{
							(key == 0) ? this.movePrev() : this.moveNext();
						}
					}
				}.bind(this));
			}.bind(this));
		}
		
	},
	setBackground: function(elm){	
		if( elm ){
			if( elm.get("tag").toLowerCase()!="img" ){
				var currentImg = elm.getElement("img");
				if(currentImg){
					this.baseElm.setStyle("background-image","url("+currentImg.get("src")+")");
				}
			}
			else{
				this.baseElm.setStyle("background-image","url("+elm.get("src")+")");	
			}
		}
	},
	moveNext: function(){
		this.hideAll();	
		( this.currentItem<(this.numItems-1) ) ? this.currentItem++ : this.currentItem=0;
		this.index = this.currentItem;
		this.showCurrent();
	},
	movePrev: function(){
		this.hideAll();
		(this.currentItem==0 ) ? this.currentItem = (this.numItems-1) : this.currentItem--;
		this.index = this.currentItem;
		this.showCurrent();
	},
	showCurrent: function(){
		this.hideAll();
		//alert(this.currentItem);
		this.items[this.currentItem].setStyle('display','block');

		this.fadeIn = new Fx.Morph( this.items[this.currentItem], { 
			duration: this.options.transitionLength,
			onStart: function(){
				this.flag = true;
				this.fireEvent('fadeStart', this);
			}.bind(this),
			onComplete: function(){
				this.flag = false;
				this.setBackground(this.items[this.currentItem]);
				this.fireEvent('fadeComplete', this);						
			}.bind(this)
		});
		this.fadeIn.start({"opacity":[0,1]});
	},
	hideAll: function(){
		this.items.each( function(elm,index){
			if( index!=this.currentItem ){			
				elm.setStyles({
					"opacity":"0",
					"display": "none"
				});
			}
		});
	},
	start: function(index) {		
		if(typeof(index) == 'undefined') index = this.options.startIndex;		
		if( this.numItems>1 ){
		//	if(index == 0) index = this.numItems - 1;
			this.currentItem = index;
			this.prevIndex = 0;
			this.nextFunction = function(){
				this.prevIndex = this.currentItem;
				this.moveNext();
			}.bind(this);
			this.periodical = this.nextFunction.periodical(this.options.moveTimeSecs);
		}		
	},
	halt: function(){
		$clear(this.periodical);	
		this.periodical = null;
	}
});

