﻿function changeRotateDirection(clickedButton, rotator, direction) {

//    var rotatecount = 5;

//    while (rotatecount!=0) {
//        showNextItem(clickedButton, rotator, direction);
//        //alert(rotatecount);
//        rotatecount--;
//        pause(500);
//    }
  
    //startRotator(clickedButton, rotator, direction);
    showNextItem(clickedButton, rotator, direction);
    showNextItem(clickedButton, rotator, direction);
    showNextItem(clickedButton, rotator, direction);
    showNextItem(clickedButton, rotator, direction);
    showNextItem(clickedButton, rotator, direction);
 
//    rotator.stop();
//    rotator.set_scrollDirection(direction);
//    rotator.set_animationDirection(direction);
//    rotator.start();


}


function showNextItem(clickedButton, rotator, direction) {
//    alert('show next item');
//    alert(rotator);
    rotator.showNext(direction);

    //refreshButtonsState(clickedButton, rotator);s
}




function startRotator(clickedButton, rotator, direction) {

    if (!rotator.autoIntervalID) {
        //refreshButtonsState(clickedButton, rotator);

//        if (rotatecount == 0) {
//            alert('stop rotate');
//            stopRotator(clickedButton, rotator);
//        }

        rotator.autoIntervalID = window.setInterval(function () {
            rotator.showNext(direction);
        }, rotator.get_frameDuration());

        //roatetcount--;
    }
}

function stopRotator(clickedButton, rotator) {
    if (rotator.autoIntervalID) {
        //refreshButtonsState(clickedButton, rotator)
        window.clearInterval(rotator.autoIntervalID);
        rotator.autoIntervalID = null;
    }
}



// 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;
        }
    }
}


//function OnClientTabSelected(sender, eventArgs) {
//    var tab = eventArgs.get_tab();
//    alert(tab.get_text());

//    switch (tab.get_text()) {
//        case 'our favourites':
//            alert('of');
//            showNextItem(this, document.getElementById('RadRotator2'), Telerik.Web.UI.RotatorScrollDirection.Right);
//            break;

//    }

//}   

function pause(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < millis);
} 
