/*
* image fader
*/

var kamAnimationInterval = null;
var kamAnimationTransitionLength = null;

var imageFader = new Class({
	baseElm: null,
	items: null,
	numItems: null,
	currentItem: null,
	periodical: null,
	transitionLength: 2000,
	moveTimeSecs: 5000,
	backgroundSet: false,
	
	initialize: function(baseElm,itemIdentifier){
		// set class properties
		this.baseElm = $(baseElm);
		this.items = baseElm.getElements(itemIdentifier);
		this.numItems = this.items.length;
		this.currentItem = 0;
		
		if( this.numItems > 1 ){
			// set background
			this.setBackground(this.items[this.currentItem]);

			// position all absolute so they sit at the same point
			var baseElmPos = baseElm.getPosition();			
			var counter = 0;
			this.items.each( function(elm){
				elm.setStyle("display","block");	
				elm.setStyle("position","absolute");
				elm.setStyle("left","330px");
				elm.setStyle("top",baseElmPos.y);			
				if( counter!=0 ){
					elm.setStyle("opacity",0);
				}
				counter++;
			});
		}
		
	},
	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.showCurrent();
	},
	movePrev: function(){
		this.hideAll();
		(this.currentItem==0 ) ? this.currentItem = (this.numItems-1) : this.currentItem--;
		this.showCurrent();
	},
	showCurrent: function(){
		this.hideAll();
		var fadeIn = new Fx.Morph( this.items[this.currentItem], { 
			duration: this.transitionLength,
			onComplete: function(){
				this.setBackground(this.items[this.currentItem]);
				var nextFunction = function(){
					this.moveNext();
				}.bind(this);
				this.periodical = nextFunction.delay(this.moveTimeSecs);
			}.bind(this)
		});
		fadeIn.start({"opacity":[0,1]});
	},
	hideAll: function(){
		this.items.each( function(elm,index){
			if( index!=this.currentItem ){
				elm.setStyle("opacity","0");
			}
		});
	},
	start: function(moveTimeSecs,transitionLength) {
		if( this.numItems>1 ){
			this.moveTimeSecs = moveTimeSecs*1000;
			this.transitionLength = transitionLength*1000;
			var nextFunction = function(){
				this.moveNext();
			}.bind(this);
			this.periodical = nextFunction.delay(this.moveTimeSecs);
		}
	}
	
});

/*
* dom ready
*/

var rotators = [];

window.addEvent("domready", function(){

	$(document.body).addClass("hasJS");

	$$("a").each( function(elm){
		// handle videos
		
		// handle popups
		var rel = elm.get("rel");
		if( rel ){
			if( rel.indexOf("kamPopUp")!=-1 ){
				elm.addEvent("click", function(evt){
					evt.preventDefault();
					window.open(elm.href);
				});
			}
		}
	});
	
	$$("#colB div").each( function(elm,index){
		if( elm.hasClass("kamImageFade") ){
			 var newRotator = new imageFader(elm,".item");
			 if( kamAnimationInterval && kamAnimationInterval ){
				 newRotator.start(kamAnimationInterval,kamAnimationInterval);						 
			 }
			 else{
				 newRotator.start(10,1);
			 }
			 rotators[rotators.length] = newRotator;
		}
	});
	
});
