﻿function AutoStartRotator(sender, args) {
	//alert("here");
	startRotator($get("startButton"), sender, Telerik.Web.UI.RotatorScrollDirection.Left);
}


function startstopRotator(clickedButton, rotator) {
	if (rotator.autoIntervalID) {
		stopRotator(clickedButton, rotator);
	} else {
		startRotator(clickedButton, rotator, Telerik.Web.UI.RotatorScrollDirection.Up);
	}
}




function startRotator(clickedButton, rotator, direction) {
	if (!rotator.autoIntervalID) {
		refreshButtonsState(clickedButton, rotator);
		rotator.autoIntervalID = window.setInterval(function () {
			rotator.showNext(direction);
		}, rotator.get_frameDuration());
	}
}

function stopRotator(clickedButton, rotator) {
	if (rotator.autoIntervalID) {
		refreshButtonsState(clickedButton, rotator)
		window.clearInterval(rotator.autoIntervalID);
		rotator.autoIntervalID = null;
	}
}

function showNextItem(clickedButton, rotator, direction) {
	stopRotator(clickedButton, rotator);
	rotator.showNext(direction);
	refreshButtonsState(clickedButton, rotator);
}

// Refreshes the Stop and Start buttons
function refreshButtonsState(clickedButton, rotator) {
	var jQueryObject = $telerik.$;
	var className = jQueryObject(clickedButton).attr("class");

	switch (className) {
		case "start":
			{// Start button is clicked

				jQueryObject(clickedButton).removeClass();
				jQueryObject(clickedButton).addClass("startSelected");

				// Find the stop button. stopButton is a jQuery object
				var stopButton = findSiblingButtonByClassName(clickedButton, "stopSelected");

				if (stopButton) {// Changes the image of the stop button
					stopButton.removeClass();
					stopButton.addClass("stop");
				}

			} break;
		case "stop":
			{// Stop button is clicked

				jQueryObject(clickedButton).removeClass();
				jQueryObject(clickedButton).addClass("stopSelected");

				// Find the start button. startButton is a jQuery object
				var startButton = findSiblingButtonByClassName(clickedButton, "startSelected");
				if (startButton) {// Changes the image of the start button
					startButton.removeClass();
					startButton.addClass("start");
				}

			} break;
	}
}

// Finds a button by its className. Returns a jQuery object
function findSiblingButtonByClassName(buttonInstance, className) {
	var jQuery = $telerik.$;
	var ulElement = jQuery(buttonInstance).parent().parent(); // get the UL element
	var allLiElements = jQuery("li", ulElement); // jQuery selector to find all LI elements

	for (var i = 0; i < allLiElements.length; i++) {
		var currentLi = allLiElements[i];
		var currentAnchor = jQuery("A:first", currentLi); // Find the Anchor tag

		if (currentAnchor.hasClass(className)) {
			return currentAnchor;
		}
	}
}
