﻿function RotatorInstance(name, topImageID, bottomImageID, debugLabelID, interval, fadeSpeed, random, clickDelegate, images) {
    this._topImageID = topImageID;
    this._bottomImageID = bottomImageID;
    this._debugLabelID = debugLabelID;
    this._interval = interval;
    this._fadeSpeed = fadeSpeed;
    this._random = random;
    this._images = images;
    this._newImageReady = false;
    this._intervalExpired = false;
    this._currentImageIndex = -1;
    this._visibleImageIndex = 0;
    this._name = name;
    this._backImageShown = false;
    this._currentTimer = null;
    this._onclickDelegate = clickDelegate;

    this.OnFadeComplete = RotatorInstance$OnFadeComplete;
    this.Fade = RotatorInstance$Fade;
    this.OnIntervalExpired = RotatorInstance$OnIntervalExpired;
    this.OnImageLoaded = RotatorInstance$OnImageLoaded;
    this.NextImage = RotatorInstance$NextImage;
    this.Init = RotatorInstance$Init;
    this.HandleClick = RotatorInstance$HandleClick;
}

function RotatorInstance$OnFadeComplete() {
	if (!this._backImageShown) {
		$('#' + this._bottomImageID).show();
		this._backImageShown = true;
	}

	var bottomImage = $('#' + this._bottomImageID);

	bottomImage.attr('src', this._images[this._currentImageIndex].path);
	bottomImage.attr('title', this._images[this._currentImageIndex].tooltip);
	bottomImage.attr('alt', this._images[this._currentImageIndex].tooltip);
    
    $('#' + this._topImageID).hide();

    this.NextImage();
}

function RotatorInstance$Fade() {
    if (this._intervalExpired && this._newImageReady) {
        var callBackCode = this._name + '.OnFadeComplete();';
        $('#' + this._topImageID).fadeIn(this._fadeSpeed, function() { eval(callBackCode); });
    }
}

function RotatorInstance$OnIntervalExpired() {
    this._intervalExpired = true;
    this.Fade();
}

function RotatorInstance$OnImageLoaded(event) {
	if (event.currentTarget.complete) {
		event.data._newImageReady = true;
		event.data.Fade();
	}
}

function RotatorInstance$OnImageReadyStateChange(event) {
	if (event.currentTarget.readyState == 'complete') {
		event.data._newImageReady = true;
		event.data.Fade();
	}
}

function RotatorInstance$OnImageClick(event) {
	event.data.HandleClick();
}

function RotatorInstance$HandleClick()
{
	if (this._visibleImageIndex >= 0 && this._visibleImageIndex < this._images.length)
	{
		var currentImage = this._images[this._visibleImageIndex];

		if (currentImage.link != '')
		{
			if (this._onclickDelegate != null && typeof (this._onclickDelegate) == 'function')
			{
				this._onclickDelegate(currentImage.objectid);
			}
		
			if (currentImage.openInNew)
			{
				window.open(currentImage.link);
			}
			else
			{
				location.href = currentImage.link;
			}
		}
	}
}

function RotatorInstance$NextImage() {
    var nextImageIndex = this._currentImageIndex;
    nextImageIndex = (nextImageIndex + 1) % this._images.length;

    var nextImage = this._images[nextImageIndex];
    this._visibleImageIndex = this._currentImageIndex;
    this._currentImageIndex = nextImageIndex;

    this._newImageReady = false;
    this._intervalExpired = false;

    $('#' + this._topImageID).attr('src', nextImage.path);

    if (this._currentTimer != null) {
    	clearTimeout(this._currentTimer);
    }

    if (this._images.length > 1)
    {
    	this._currentTimer = setTimeout(this._name + '.OnIntervalExpired()', this._interval);
    }
}

function RotatorInstance$Init() {
	if (this._random) {
		for (var j, x, i = this._images.length; i; j = parseInt(Math.random() * i), x = this._images[--i], this._images[i] = this._images[j], this._images[j] = x);
	}

    $('#' + this._topImageID).bind('load', this, RotatorInstance$OnImageLoaded);
    $('#' + this._topImageID).bind('readystatechange', this, RotatorInstance$OnImageReadyStateChange);
    $('#' + this._topImageID).bind('click', this, RotatorInstance$OnImageClick);
    $('#' + this._bottomImageID).bind('click', this, RotatorInstance$OnImageClick);
    this.NextImage();

    this._intervalExpired = true;
    this.Fade();
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();