Formidable.Classes.npa_slider = Formidable.Classes.CodeBehindClass.extend({

	fiTimerId:0,

	fbIsTweening:false,
	fbIsPreloading:false,
	fbTimerRunning:false,

	fiCurrentImageIndex:0,
	fsCurrentMouseOverTargetId:null,

	foSliderFrontDiv:null,
	foSliderFrontImage:null,

	foSliderBackDiv:null,
	foSliderBackImage:null,

	constructor: function() {
	},



	_handleShowImageLoaded: function( oImageRefData) {
		var scope = this;

		this.fbIsPreloading = false;
		this.fbIsTweening = true;
		this.foSliderFrontDiv.fade({
			from: 1,
			to: 0,
			duration: 1,
			transition:Effect.Transitions.linear,
			afterFinish: function() {scope.foSliderFrontImage.src = scope.foSliderBackImage.src; scope.fbIsTweening = false;}
		});
		window.setTimeout(function() {
				$("slider_headline").update(oImageRefData.headline);
				$("slider_headline_image").update(oImageRefData.headlineAsImage);
				$("slider_desc").update(oImageRefData.description);
			},
			100
		);

		this._updateSlideTimer();
	},

	_showImageByIndex: function( scope, iImageIndex) {

		/*
		if( window) {
			window.console.log(" * scope = "+scope);
			window.console.log(" * iImageIndex = "+iImageIndex);
			window.console.log(" * fiCurrentImageIndex = "+scope.fiCurrentImageIndex);
		}
		*/

		var oOldImageRefData;
		if( scope.fiCurrentImageIndex != null) {
			$('npa_slider.slider_thumbnail_'+scope.fiCurrentImageIndex).removeClassName("selected");
		}

		scope.fiCurrentImageIndex = iImageIndex;
		var targetId = 'npa_slider.slider_thumbnail_'+scope.fiCurrentImageIndex;


		$(targetId).addClassName("selected");

		var oImageRefData = eval("window.npa_image_"+iImageIndex);

		scope.fbIsPreloading = true;

		scope.foSliderBackImage.src = oImageRefData.src;

		scope.foSliderFrontDiv.show();
		scope.foSliderBackImage.onload = function() {
			scope._handleShowImageLoaded( oImageRefData);
		}.bind(scope);



	},

	_showNextImage: function() {
		if( this.fiCurrentImageIndex > window.npa_images.length) {
			this._showImageByIndex(this, 0);
		}
		else {
			this._showImageByIndex(this, this.fiCurrentImageIndex+1);
		}
	},

	_updateSlideTimer: function() {
		var scope=this;

		if( this.fbTimerRunning==true) {
			clearTimeout( this.fiTimerId);
		}

		this.fiTimerId = setTimeout( function() {
				if( scope.fbIsPreloading == true)
					return;

				scope._showNextImage()
			},
			6000
		);
		this.fbTimerRunning=true;
	},

	handleThumbnailClick: function(event) {
		var event;
		var eventType;

		if( event != undefined) {

			var aNode;
			var aImage;

			aNode = (event.currentTarget) ? event.currentTarget : event.srcElement;

			/*
			if( window) {
				window.console.log( " * node id = "+aNode.id);
				window.console.log( " * parent node id = "+aNode.parentNode.id);
				window.console.log( " * first child node id = "+aNode.childNodes[0]);
			}
			*/

			if( aNode.parentNode.id == "slider_thumbnails") {
				aImage = aNode.childNodes[0];
			}
			else if( aNode.parentNode.parentNode.id == "slider_thumbnails") {
				aImage = aNode.parentNode.childNodes[0];
			}

			if( aImage != undefined) {
				var aImageId = aImage.id.split("..");

				var iImageIndex  = parseInt(aImageId[1]);

				if( this.fbIsPreloading == true)
					return;

				if( this.fbIsTweening == true)
					return;

				if( this.fbTimerRunning == true) {
					this.fbTimerRunning = false;
					clearTimeout( this.fiTimerId);
				}

				this._showImageByIndex(this, iImageIndex);
			}
		}
	},



	handleThumbnailMouseOver: function(params){
		var event;
		var eventType;

		if( this.fsCurrentMouseOverTargetId != null) {
			$(this.fsCurrentMouseOverTargetId).removeClassName("hover");
			this.fsCurrentMouseOverTargetId = null;
		}

		if( params != undefined) {
			event = params[0];

			if( event.currentTarget != undefined) {
				this.fsCurrentMouseOverTargetId = event.currentTarget.id;

				$(event.currentTarget.id).addClassName("hover");
			}
		}

	},


	handleThumbnailMouseOut: function(params){
		if( this.fsCurrentMouseOverTargetId != null) {
			$(this.fsCurrentMouseOverTargetId).removeClassName("hover");
			this.fsCurrentMouseOverTargetId = null;
		}
	},



	init: function() {
		var scope = this;

		this.foSliderFrontDiv = $("slider_front_image");
		this.foSliderFrontImage = this.foSliderFrontDiv.childNodes[0];

		this.foSliderBackDiv = $("slider_back_image");
		this.foSliderBackImage = this.foSliderBackDiv.childNodes[0];

		this.foSliderFrontDiv.hide();
		this.fiCurrentImageIndex = 0;

		this._updateSlideTimer();
	}
});

