



window.animation = window.animation || {};

animation = function() {}

animation.prototype = {
	iFadeSpeed : 2000,

	iShowMS : 3000,
	iCurrentImg : 0,
	aImages : new Array(),
	aPreloadImages : new Array(),
	iImageCnt : 0,
	
	addImage : function(imagePath) {
		this.aImages[this.iImageCnt] = new Object();
		this.aImages[this.iImageCnt].src = imagePath;
		this.iImageCnt++;
	},
	
	initAnimation : function() {

		for( i=0; i<this.aImages.length; i++ ){
			this.aPreloadImages[i] = this.aImages[i].src;
		}
		
		$("#imageheader").append('<span id="headerImgAnim"></span>')
		/*$("#headerImgAnim").append('<img src="' + this.aPreloadImages[0] + '" />');
		$("#headerImgAnim").removeClass('loading');
		$("#headerImgAnim img:eq(0)").addClass('active');*/
		
		$.preload( this.aPreloadImages, {
		base:'',
		ext:'',
		aPreloadImages : this.aPreloadImages,
		onFinish:function(){
			
			$("#headerImgAnim").hide();
			for( i=0 ; i < this.aPreloadImages.length ; i++){
				$("#headerImgAnim").append('<img style="display:none;" src="' + this.aPreloadImages[i] + '" />');
			}
			var oActiveImg = $("#headerImgAnim img:eq(" + this.iCurrentImg + ")");
			$("#headerImgAnim").removeClass('loading');
			//$("#headerImgAnim img:eq(0)").addClass('active');
			//oActiveImg.fadeIn(anim.iFadeSpeed, function(){ anim.playAnim(); $(this).addClass('active') })
			$("#headerImgAnim").fadeIn(anim.iFadeSpeed, function () {
				if (anim.aPreloadImages.length > 1) {
					anim.playAnim();
				}
			});
		}});
	},

	 playAnim : function(){
		var oActiveImg = $("#headerImgAnim img:eq(" + this.iCurrentImg + ")");
		oActiveImg.addClass('active');
		if(this.iCurrentImg == this.aPreloadImages.length - 1){
			this.iCurrentImg = 0;
		} else {
			this.iCurrentImg++;
		}
		var oNextImg = $("#headerImgAnim img:eq(" + this.iCurrentImg + ")");
		oNextImg.show();
		window.setTimeout(function(){ anim.resume(oActiveImg)},this.iShowMS);
	},
	resume : function(oActiveImg){
		oActiveImg.fadeOut(anim.iFadeSpeed, function(){ anim.playAnim(); $(this).removeClass('active') })
	}
}
