﻿Type.registerNamespace('Asbmr.ClassLibrary.Behaviors');

Asbmr.ClassLibrary.Behaviors.Rotator = function(element) {
	Asbmr.ClassLibrary.Behaviors.Rotator.initializeBase(this, [element]);

	//	this._imageLink = null;
	//	this._image = null;
	this._profileInfoLabel = null;
	this._showProfileDetailLink = null;

	this._delayInMilliseconds = null;
	this._fadeTimeInMilliseconds = null;

	this._profileInfo = null;
	this._profileItems = null;

	this._currentIndex = 0;
	this._currentlyProcessing = false;

	this._pauseOnMouseOverDelegate = null;
	this._resumeOnMouseOutDelegate = null;
	this._intervalElapsedDelegate = null;
	this._loadItemDelegate = null;
}

Asbmr.ClassLibrary.Behaviors.Rotator.prototype = {
	initialize: function() {
		Asbmr.ClassLibrary.Behaviors.Rotator.callBaseMethod(this, 'initialize');

		this._pauseOnMouseOverDelegate = Function.createDelegate(this, this._pause);
		$addHandler(this.get_element(), "mouseover", this._pauseOnMouseOverDelegate);

		this._resumeOnMouseOutDelegate = Function.createDelegate(this, this._resume);
		$addHandler(this.get_element(), "mouseout", this._resumeOnMouseOutDelegate);

		this._intervalElapsedDelegate = Function.createDelegate(this, this._intervalElapsed);

		this._profileItems = Sys.Serialization.JavaScriptSerializer.deserialize(this._profileInfo);
		this._loadItemDelegate = Function.createDelegate(this, this._loadItem);
		this._loadItem();
		this._startProcess();

	},

	dispose: function() {

		this._pause();

		if (this.get_element() && this._pauseOnMouseOverDelegate) {
			$removeHandler(this.get_element(), "mouseover", this._pauseOnMouseOverDelegate);
		}

		if (this.get_element() && this._resumeOnMouseOutDelegate) {
			$removeHandler(this.get_element(), "mouseout", this._resumeOnMouseOutDelegate);
		}

		this._pauseOnMouseOverDelegate = null;
		this._resumeOnMouseOutDelegate = null;
		this._intervalElapsedDelegate = null;
		this._loadItemDelegate = null;

		//		this._imageLink = null;
		//		this._image = null;
		this._profileInfoLabel = null;
		this._showProfileDetailLink = null;

		this._delayInMilliseconds = null;
		this._fadeTimeInMilliseconds = null;

		this._profileInfo = null;
		this._profileItems = null;

		this._currentIndex = 0;

		this._currentlyProcessing = false;

		Asbmr.ClassLibrary.Behaviors.Rotator.callBaseMethod(this, 'dispose');
	},

	_pause: function() {
		if (this._currentlyProcessing) {
			clearInterval(this._processingIntervalId);
			this._currentlyProcessing = false;
			return;
		}
	},

	_resume: function() {
		this._startProcess();
	},

	_startProcess: function() {
		if (!this._currentlyProcessing) {
			this._processingIntervalId = setInterval(this._intervalElapsedDelegate, this._delayInMilliseconds);
			this._currentlyProcessing = true;
			return;
		}
	},

	_intervalElapsed: function() {
		var element = $(this.get_element());
		var loadItemDelegate = this._loadItemDelegate;
		element.fadeOut(this._fadeTimeInMilliseconds, loadItemDelegate).fadeIn(this._fadeTimeInMilliseconds);
	},

	_loadItem: function() {
		this._populate();
		var index = this._currentIndex;
		if (this._currentIndex < this._profileItems.length - 1) {
			this._currentIndex++;
		}
		else {
			this._currentIndex = 0;
		}
		//		this._preloadImage();
		//		this._preloadThumbnail(index);
	},

	_populate: function() {
		this.get_element().title = this._profileItems[this._currentIndex].Title;
		//		$(this._image).attr("src", "/profile/image.axd?id=" + this._profileItems[this._currentIndex].ImageId);
		$(this._profileInfoLabel).get(0).innerHTML = this._profileItems[this._currentIndex].ProfileInfo;
	},

	//	_preloadImage: function() {
	//		var image = new Image();
	//		if (this._profileItems[this._currentIndex].ImageId) {
	//			image.src = "/profile/image.axd?id=" + this._profileItems[this._currentIndex].ImageId;
	//		}
	//		else {
	//			this._currentIndex++;
	//			this._preloadImage();
	//		}
	//	},

	//	_preloadThumbnail: function(index) {
	//		var thumbnail = new Image();
	//		if (this._profileItems[index].ImageId) {
	//			thumbnail.src = "/spotlight/image.axd?id=" + this._profileItems[index].ImageId;
	//		}
	//		else {
	//			index++;
	//			this._preloadThumbnail(index);
	//		}
	//	},

	//	get_ImageLink: function() {
	//		return this._imageLink;
	//	},

	//	set_ImageLink: function(elt) {
	//		this._imageLink = elt;
	//	},

	//	get_Image: function() {
	//		return this._image;
	//	},

	//	set_Image: function(elt) {
	//		this._image = elt;
	//	},

	get_ProfileInfoLabel: function() {
		return this._profileInfoLabel;
	},

	set_ProfileInfoLabel: function(elt) {
		this._profileInfoLabel = elt;
	},

	get_ShowProfileDetailLink: function() {
		return this._showProfileDetailLink;
	},

	set_ShowProfileDetailLink: function(elt) {
		this._showProfileDetailLink = elt;
	},

	get_DelayInMilliseconds: function() {
		return this._delayInMilliseconds;
	},

	set_DelayInMilliseconds: function(val) {
		this._delayInMilliseconds = val;
	},

	get_FadeTimeInMilliseconds: function() {
		return this._fadeTimeInMilliseconds;
	},

	set_FadeTimeInMilliseconds: function(val) {
		this._fadeTimeInMilliseconds = val;
	},

	get_ProfileInfo: function() {
		return this._profileInfo;
	},

	set_ProfileInfo: function(val) {
		this._profileInfo = val;
	}
}

Asbmr.ClassLibrary.Behaviors.Rotator.registerClass('Asbmr.ClassLibrary.Behaviors.Rotator', AjaxControlToolkit.BehaviorBase);
if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();