﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Windows");

Windows.IE8SliderBehavior = function(element) {
	Windows.IE8SliderBehavior.initializeBase(this, [element]);


	this.scrollLeftButtonClickDelegate = Function.createDelegate(this, this.scrollLeftClicked);
	this.scrollRightButtonClickDelegate = Function.createDelegate(this, this.scrollRightClicked);

	this.slideRightScrollContentTick = Function.createDelegate(this, this.SlideRightScrollContentTick);
	this.slideLeftScrollContentTick = Function.createDelegate(this, this.SlideLeftScrollContentTick);

	this.currentPosition = 0;
	this.destinationPosition = 0;
	this.currentSlideIndex = 0;
	this.isScrolling = false;
}

Windows.IE8SliderBehavior.prototype = {
	initialize: function() {
		Windows.IE8SliderBehavior.callBaseMethod(this, 'initialize');

		this._scrollLeftButtonPanelElement = document.getElementById(this.get_ScrollLeftButtonPanel());
		this._scrollRightButtonPanelElement = document.getElementById(this.get_ScrollRightButtonPanel());
		$addHandler(this._scrollLeftButtonPanelElement, "click", this.scrollLeftButtonClickDelegate);
		$addHandler(this._scrollRightButtonPanelElement, "click", this.scrollRightButtonClickDelegate);

		this._leftImageButton = $get(this.get_LeftArrowImageID());
		this._rightImageButton = $get(this.get_RightArrowImageID());
		this.RefreshArrows();

		this._contentItemsSliderContainerPanelElement = $get(this.get_ContentItemsSliderContainerPanel());
	},
	dispose: function() {
		Windows.IE8SliderBehavior.callBaseMethod(this, 'dispose');
	},

	SlideRightScrollContentTick: function() {
		if (this.currentPosition > this.destinationPosition) {
			this.currentPosition -= 10;
			this._contentItemsSliderContainerPanelElement.style.left = this.currentPosition + 'px';
		}
		else {
			clearInterval(this.slideInterval);
			this.isScrolling = false;
			this._contentItemsSliderContainerPanelElement.style.left = this.destinationPosition + 'px';
			this.currentPosition = this.destinationPosition;
			this.RefreshArrows();
		}
	},
	SlideLeftScrollContentTick: function() {
		if (this.currentPosition < this.destinationPosition) {
			this.currentPosition += 10;
			this._contentItemsSliderContainerPanelElement.style.left = this.currentPosition + 'px';
		}
		else {
			clearInterval(this.slideInterval);
			this.isScrolling = false;
			this._contentItemsSliderContainerPanelElement.style.left = this.destinationPosition + 'px';
			this.currentPosition = this.destinationPosition;
			this.RefreshArrows();
		}
	},
	scrollLeftClicked: function() {
		if (!this.isScrolling) {
			if (this.currentSlideIndex != 0) {
				this.isScrolling = true;
				this.currentSlideIndex--;
				clearInterval(this.slideInterval);
				this.destinationPosition = this.currentPosition + this.get_NumberOfPixelsMovedForOneSlide();
				this.slideInterval = setInterval(this.slideLeftScrollContentTick, 2);
			}
		}
	},
	scrollRightClicked: function() {
		if (!this.isScrolling) {
			if (this.currentSlideIndex != this.get_NumberOfScrollClicksToReachEndOfSlides()) {
				this.isScrolling = true;
				clearInterval(this.slideInterval);
				this.currentSlideIndex++;
				this.destinationPosition = this.currentPosition - this.get_NumberOfPixelsMovedForOneSlide();
				this.slideInterval = setInterval(this.slideRightScrollContentTick, 2);
			}
		}
	},

	RefreshArrows: function() {
		if (this.currentSlideIndex == 0 && this.get_NumberOfScrollClicksToReachEndOfSlides() == 0) {
			this.SetLeftArrowToLink();
			this.SetRightArrowToLink();
		}
		else if (this.currentSlideIndex == 0) {
			this.SetLeftArrowToLink();
			this.SetRightArrowToScroll();
		}
		else if (this.currentSlideIndex == this.get_NumberOfScrollClicksToReachEndOfSlides()) {
			this.SetLeftArrowToScroll();
			this.SetRightArrowToLink();
		}
		else {
			this.SetLeftArrowToScroll();
			this.SetRightArrowToScroll();
		}
	},

	SetLeftArrowToScroll: function() {
		this._leftImageButton.style.visibility = "visible";
		this._leftImageButton.setAttribute("src", this.get_LeftArrowImagePath());
		this._scrollLeftButtonPanelElement.removeAttribute("href");
	},
	SetLeftArrowToLink: function() {
		if (this.get_BeginningOfSlidesClickThroughLink() && this.get_BeginningOfSlidesClickThroughLink() != "") {
			this._leftImageButton.style.visibility = "visible";
			this._leftImageButton.setAttribute("src", this.get_LeftArrowLinkImagePath());
			this._scrollLeftButtonPanelElement.setAttribute("href", this.get_BeginningOfSlidesClickThroughLink());
		}
		else {
			this._leftImageButton.style.visibility = "hidden";
		}
	},
	SetRightArrowToScroll: function() {
		this._rightImageButton.style.visibility = "visible";
		this._rightImageButton.setAttribute("src", this.get_RightArrowImagePath());
		this._scrollRightButtonPanelElement.removeAttribute("href");
	},
	SetRightArrowToLink: function() {
		if (this.get_EndOfSlidesClickThroughLink() && this.get_EndOfSlidesClickThroughLink() != "") {
			this._rightImageButton.style.visibility = "visible";
			this._rightImageButton.setAttribute("src", this.get_RightArrowLinkImagePath());
			this._scrollRightButtonPanelElement.setAttribute("href", this.get_EndOfSlidesClickThroughLink());
		}
		else {
			this._rightImageButton.style.visibility = "hidden";
		}
	},


	//Properties:
	get_ScrollLeftButtonPanel: function() {
		return this._ScrollLeftButtonPanel;
	},
	set_ScrollLeftButtonPanel: function(value) {
		if (this._ScrollLeftButtonPanel != value) {
			this._ScrollLeftButtonPanel = value;
			this.raisePropertyChanged('ScrollLeftButtonPanel');
		}
	},
	get_ScrollRightButtonPanel: function() {
		return this._ScrollRightButtonPanel;
	},
	set_ScrollRightButtonPanel: function(value) {
		if (this._ScrollRightButtonPanel != value) {
			this._ScrollRightButtonPanel = value;
			this.raisePropertyChanged('ScrollRightButtonPanel');
		}
	},
	get_ContentItemsSliderContainerPanel: function() {
		return this._ContentItemsSliderContainerPanel;
	},
	set_ContentItemsSliderContainerPanel: function(value) {
		if (this._ContentItemsSliderContainerPanel != value) {
			this._ContentItemsSliderContainerPanel = value;
			this.raisePropertyChanged('ContentItemsSliderContainerPanel');
		}
	},
	get_NumberOfPixelsMovedForOneSlide: function() {
		return this._NumberOfPixelsMovedForOneSlide;
	},
	set_NumberOfPixelsMovedForOneSlide: function(value) {
		if (this._NumberOfPixelsMovedForOneSlide != value) {
			this._NumberOfPixelsMovedForOneSlide = value;
			this.raisePropertyChanged('NumberOfPixelsMovedForOneSlide');
		}
	},
	get_NumberOfScrollClicksToReachEndOfSlides: function() {
		return this._NumberOfScrollClicksToReachEndOfSlides;
	},
	set_NumberOfScrollClicksToReachEndOfSlides: function(value) {
		if (this._NumberOfScrollClicksToReachEndOfSlides != value) {
			this._NumberOfScrollClicksToReachEndOfSlides = value;
			this.raisePropertyChanged('NumberOfScrollClicksToReachEndOfSlides');
		}
	},
	get_ContentItemsCount: function() {
		return this._ContentItemsCount;
	},
	set_ContentItemsCount: function(value) {
		if (this._ContentItemsCount != value) {
			this._ContentItemsCount = value;
			this.raisePropertyChanged('ContentItemsCount');
		}
	},
	get_LeftArrowImageID: function() {
		return this._LeftArrowImageID;
	},
	set_LeftArrowImageID: function(value) {
		if (this._LeftArrowImageID != value) {
			this._LeftArrowImageID = value;
			this.raisePropertyChanged('LeftArrowImageID');
		}
	},
	get_RightArrowImageID: function() {
		return this._RightArrowImageID;
	},
	set_RightArrowImageID: function(value) {
		if (this._RightArrowImageID != value) {
			this._RightArrowImageID = value;
			this.raisePropertyChanged('RightArrowImageID');
		}
	},
	get_LeftArrowImagePath: function() {
		return this._LeftArrowImagePath;
	},
	set_LeftArrowImagePath: function(value) {
		if (this._LeftArrowImagePath != value) {
			this._LeftArrowImagePath = value;
			this.raisePropertyChanged('LeftArrowImagePath');
		}
	},
	get_LeftArrowLinkImagePath: function() {
		return this._LeftArrowLinkImagePath;
	},
	set_LeftArrowLinkImagePath: function(value) {
		if (this._LeftArrowLinkImagePath != value) {
			this._LeftArrowLinkImagePath = value;
			this.raisePropertyChanged('LeftArrowLinkImagePath');
		}
	},
	get_RightArrowImagePath: function() {
		return this._RightArrowImagePath;
	},
	set_RightArrowImagePath: function(value) {
		if (this._RightArrowImagePath != value) {
			this._RightArrowImagePath = value;
			this.raisePropertyChanged('RightArrowImagePath');
		}
	},
	get_RightArrowLinkImagePath: function() {
		return this._RightArrowLinkImagePath;
	},
	set_RightArrowLinkImagePath: function(value) {
		if (this._RightArrowLinkImagePath != value) {
			this._RightArrowLinkImagePath = value;
			this.raisePropertyChanged('RightArrowLinkImagePath');
		}
	},
	get_EndOfSlidesClickThroughLink: function() {
		return this._EndOfSlidesClickThroughLink;
	},
	set_EndOfSlidesClickThroughLink: function(value) {
		if (this._EndOfSlidesClickThroughLink != value) {
			this._EndOfSlidesClickThroughLink = value;
			this.raisePropertyChanged('EndOfSlidesClickThroughLink');
		}
	},
	get_BeginningOfSlidesClickThroughLink: function() {
		return this._BeginningOfSlidesClickThroughLink;
	},
	set_BeginningOfSlidesClickThroughLink: function(value) {
		if (this._BeginningOfSlidesClickThroughLink != value) {
			this._BeginningOfSlidesClickThroughLink = value;
			this.raisePropertyChanged('BeginningOfSlidesClickThroughLink');
		}
	}

}
Windows.IE8SliderBehavior.registerClass('Windows.IE8SliderBehavior', Sys.UI.Behavior);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
