﻿
//<![CDATA[

//Original telerik functions

// Button Commands
function startRotator(clickedButton, rotator, direction) {
    if (!rotator.autoIntervalID) {
        refreshButtonsState(clickedButton, rotator);
        rotator.autoIntervalID = window.setInterval(function () {
            rotator.showNext(direction);
        }, rotator.get_frameDuration());
        rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
    }
}

function stopRotator(clickedButton, rotator) {
    if (rotator.autoIntervalID) {
        refreshButtonsState(clickedButton, rotator)
        window.clearInterval(rotator.autoIntervalID);
        rotator.autoIntervalID = null;
    }
}

function showNextItem(clickedButton, rotator, direction) {
    // Added the following to stop the auto-rotation when showing items manually - gpd
    stopRotator(findButtonByTitle(clickedButton,"Stop"), rotator);
    //Original code
    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;
        }
    }
}


// additional dobbs functions

function startSlideshow(btn, rotator, buttontitle) {
    //var button = findButtonByTitleInUL(ul, buttontitle);
    var button = btn;
    startRotator(button, rotator, Telerik.Web.UI.RotatorScrollDirection.Left);
}


// Finds a button by its Title inside the provided unordered list. Returns a jQuery object
// Requires the parent UL from which it can traverse the children to find the requested button.
// Used to auto-start the SLideshow rotator
function findButtonByTitleInUL(ul, title) {
    var jQuery = $telerik.$;
    var ulElement = ul; // 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.attr("title") == title) {
            return currentAnchor;
        }
    }
}




//Button Commands
// Finds a button by its Title. Returns a jQuery object
// Requires a button instance in order to use it to get the parent UL from which it can traverse the children to find the requested button.
function findButtonByTitle(buttonInstance, title) {
    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.attr("title")==title) {
            return currentAnchor;
        }
    }
}




// Additional radRotator Client Functionality

// Get the ItemTemplate that wraps our desired content in the radRoratorItem
function getItemTemplate(rotatorItem) {
    var itemElem = rotatorItem.get_element();
    var wrapper = itemElem.firstChild;
    return wrapper;
}


// To get HTML elements inside the radRotator item template (for example, an HTML image) use this function:
function findHtmlElement(id, itemTemplate) {
    // Get the element ;  
    var element = $get(id, itemTemplate);
    return element;
}

// To get ASP .Net control elements inside the radRotator item template (for example, an asp:Image) use this function:
function findAspControl(id, itemTemplate) {
    // Get the control ;  
    var control = $get(itemTemplate.id + "_" + id, itemTemplate);
    return control;
}

// To get ASP .Net AJAX-Enabled control elements inside the radRotator item template (for example, an asp:Image) use this function:
function findAspAjaxControl(id, itemTemplate) {
    // Get the control ;  
    var control = $find(itemTemplate.id + "_" + id, itemTemplate);
    return control;
} 






















//]]>

