﻿
var indexToScrollTo = -1;
var lastShownButton;

function showItemByIndex(i, r) {
    // get reference to the rotator object
    var oRotator = $find(r);
    if (oRotator) {
        // Set currently shown item by its index
        oRotator.set_currentItemIndex(i);
    }

    if (lastShownButton)
        lastShownButton.removeClass("RotatorHighlightButton");


    var currentButton = getButtonByIndex(i);
    currentButton.addClass("RotatorHighlightButton");
    lastShownButton = currentButton;
}

function OnClientItemShown(oRotator, args) {

    var currentIndex = args.get_item().get_index();

    if (oRotator.get_rotatorType() == Telerik.Web.UI.RotatorType.CoverFlow) {
        // Change the scroll direction of the rotator, in case it has displayed its last item for the current scroll direction.
        if (0 == currentIndex || currentIndex == (oRotator.get_items().length - 1)) {
            var directionEnum = Telerik.Web.UI.RotatorScrollDirection;
            var newScrollDirection = 0 == currentIndex ? directionEnum.Left : directionEnum.Right;

            oRotator.set_scrollDirection(newScrollDirection);
            oRotator.set_animationDirection(newScrollDirection);
        }
    }

    if (lastShownButton)
        lastShownButton.removeClass("RotatorHighlightButton");

    var currentButton = getButtonByIndex(currentIndex);
    currentButton.addClass("RotatorHighlightButton");
    lastShownButton = currentButton;

}

function OnClientItemClicked(oRotator, args) {
    var clickMode = oRotator._element.getAttribute('ItemClickMode');
    switch (clickMode) {
        case 'popup':
            var img = $(args.get_item().get_element()).find('img');
            var src = img.attr('src');
            var w = img.attr('oWidth');
            var h = img.attr('oHeight');
            var t = img.attr('title');
            //src = src.replace('Current', 'Original');
            var content = '<img src="' + src + '" style="width:' + w + ';height:' + h + ';" title="' + t + '"></img>';
            $.fancybox(content,
		        {
		            'titlePosition': 'over',
		            'autoDimensions': true,
		            'title': t
		        }
	        );
            break;
        case 'url':
            var img = $(args.get_item().get_element()).find('img');
            var url = img.attr('url');
            if (url != undefined) {
                if ($('.hidToolkitShowcaseIsLive').val() == 'True') {
                    location.href = url;
                }
                else {
                    window.open(url);
                }
            }
            break;
        case 'focus':
            oRotator.set_currentItemIndex(args.get_item().get_index(), true);
            break;

        default:

    }

}

function getButtonByIndex(index) {
    var buttonIdSelector = String.format(".navigationButtonRotatorContainer a:eq({0})", index);
    var currentButton = $(buttonIdSelector)
    return currentButton;
}
