﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("FlipperSiteDeveloper.BusinessLogic.Framework");

FlipperSiteDeveloper.BusinessLogic.Framework.RadRotatorExtender = function(element) {
    FlipperSiteDeveloper.BusinessLogic.Framework.RadRotatorExtender.initializeBase(this, [element]);

    this._nextSlideElement = null;
    this._previousSlideElement = null;
    this._keyboardScopeElement = null;

    // The transition effects that will be used when navigating to the
    // next and previous slides.
    this._nextTransitionString = null;
    this._prevTransitionString = null;

    // Set the default events
    this._nextSlideEventName = null;
    this._prevSlideEventName = null;

    // The pre-ASP.NET AJAX rotator object. It does not share an ID with any element in the DOM.
    this._rotatorObject = null;
}

FlipperSiteDeveloper.BusinessLogic.Framework.RadRotatorExtender.prototype = {

    get_nextTransitionString: function() {
        return this._nextTransitionString;
    },

    set_nextTransitionString: function(value) {
        this._nextTransitionString = value;
    },

    get_prevTransitionString: function() {
        return this._prevTransitionString;
    },

    set_prevTransitionString: function(value) {
        this._prevTransitionString = value;
    },

    get_keyboardScopeElement: function() {
        return this._keyboardScopeElement;
    },

    set_keyboardScopeElement: function(value) {
        this._keyboardScopeElement = value;
    },

    get_rotatorObject: function() {
        return this._rotatorObject;
    },

    set_rotatorObject: function(value) {
        this._rotatorObject = value;
    },

    get_previousSlideElement: function() {
        return this._previousSlideElement
    },

    set_previousSlideElement: function(value) {
        this._previousSlideElement = value;
    },

    get_nextSlideElement: function() {
        return this._nextSlideElement;
    },

    set_nextSlideElement: function(value) {
        this._nextSlideElement = value;
    },

    get_prevSlideEventName: function() {
        return this._prevSlideEventName;
    },

    set_prevSlideEventName: function(value) {
        this._prevSlideEventName = value;
    },

    get_nextSlideEventName: function() {
        return this._nextSlideEventName;
    },

    set_nextSlideEventName: function(value) {
        this._nextSlideEventName = value;
    },

    _nextSlideClickHandler: function(e) {
        e.preventDefault();
        this.ShowNextSlide();
    },

    _prevSlideClickHandler: function(e) {
        e.preventDefault();
        this.ShowPreviousSlide();
    },

    _keyboardEventHandler: function(e) {
        switch (e.keyCode) {
            case 37: this.ShowPreviousSlide(); break;
            case 39: this.ShowNextSlide(); break;
        }
    },

    _setRotatorTransitionString: function(transitionString) {
        if (transitionString != null)
            this.get_rotatorObject().TransitionString = transitionString;
    },

    ShowNextSlide: function() {
        if (this.EnsureHeightFixed()) {
            this._setRotatorTransitionString(this.get_nextTransitionString());
            this.get_rotatorObject().ShowNextFrame();
        }
    },

    ShowPreviousSlide: function() {
        if (this.EnsureHeightFixed()) {
            this._setRotatorTransitionString(this.get_prevTransitionString());
            this.get_rotatorObject().ShowPrevFrame();
        }
    },

    ScrollToFrame: function(frameIndex) {
        // Sets the frame indicated by fameIndex as the visible frame.
        // frameIndex is 0 based.
        var rot = this.get_rotatorObject();
        var frameCount = rot.FrameIdArray.length;

        // Find the index of the frame directly before frameIndex
        var leadingFrame = frameIndex - 1;
        if (leadingFrame >= frameCount)
            leadingFrame = frameCount - 1;
        if (leadingFrame < 0)
            leadingFrame = frameCount - 1;

        // Set the current frame to the frame before the one we want, then
        // scroll forward one frame.
        rot.CurrentFrame = leadingFrame;
        this.ShowNextSlide();
    },

    // Ensures the rotator has had a chance to determine its dimensions.
    // Returns true if the rotator has valid dimensions, false if the dimensions 
    // could not be determined. The rotator must be visible in the user agent in order to 
    // determine its dimensions.
    EnsureHeightFixed: function() {
        var rot = this.get_rotatorObject();
        if (rot.FrameHeight != null & rot.FrameHeight > 0)
            return true;
        rot.FixHeight();
        return (rot.FrameHeight != null & rot.FrameHeight > 0);
    },

    initialize: function() {
    FlipperSiteDeveloper.BusinessLogic.Framework.RadRotatorExtender.callBaseMethod(this, 'initialize');

        $addHandler(this.get_previousSlideElement(), this.get_prevSlideEventName(), Function.createDelegate(this, this._prevSlideClickHandler));
        $addHandler(this.get_nextSlideElement(), this.get_nextSlideEventName(), Function.createDelegate(this, this._nextSlideClickHandler));

        // Hook the keyboard events if a scope is defined.
        if (this.get_keyboardScopeElement() != null) {
            $addHandler(this.get_keyboardScopeElement(), "keydown", Function.createDelegate(this, this._keyboardEventHandler));
        }

        if (this.get_nextTransitionString() == null) {
            this.set_nextTransitionString(this.get_rotatorObject().TransitionString);
        }

        if (this.get_prevTransitionString() == null) {
            this.set_prevTransitionString(this.get_rotatorObject().TransitionString);
        }
    },

    dispose: function() {
        //Add custom dispose actions here
    FlipperSiteDeveloper.BusinessLogic.Framework.RadRotatorExtender.callBaseMethod(this, 'dispose');
    }
}
FlipperSiteDeveloper.BusinessLogic.Framework.RadRotatorExtender.registerClass('FlipperSiteDeveloper.BusinessLogic.Framework.RadRotatorExtender', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();