/*
* 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,startItem){
		// set class properties
		this.baseElm = $(baseElm);
		this.items = baseElm.getElements(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]);

			// position all absolute so they sit at the same point
			var baseElmPos = baseElm.getPosition();			
			this.items.each( function(elm){
				elm.setStyle("display","block");	
				elm.setStyle("position","absolute");
				elm.setStyle("left",baseElmPos.x);
				elm.setStyle("top",baseElmPos.y);			
				elm.setStyle("opacity",0);
			});
		}
		
	},
	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 = [];
var pageTracker = null;

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

	$("body").addClass("hasJS");
	
	if( _gaq ){
		setupCustomTracking()
	}
	else{
		setupCustomTracking.delay(250);
	}

	var kamCrossFade = $("kamCrossfade");
	if( kamCrossFade ){
		 var newRotator = new imageFader(kamCrossFade,"img");
		 newRotator.start(3.5,1);
		 rotators[rotators.length] = newRotator;
	}	
});

function setupCustomTracking(){
	$$("a.trackClickAsEvent").each( function(elm){
		elm.addEvent("click", function(evt){
			_gaq.push(['_trackEvent',"Links","Link click", elm.get("id")]);
		});
	});	
}
