﻿Type.registerNamespace('NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior');

NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior = function(element) {
    this._wrapper = null;
    this._ticker = null;
    this._mouseOverHandler = null;
    this._mouseOutHandler = null;
    this._intervalID = null;
    this._isPaused = null;

    this._isTopToBottom = null;
    this._serviceMethod = null;
    this._servicePath = null;
    this._itemCssClass = null;
    this._itemMouseOverCssClass = null;
    this._pauseDuration = null;

    this._height = null;
    this._scroll = null;
    //this._aLinks = null;
    //this._aTitles = null;
    this._marqueeItems = null;
    this._arrCursor = null;
    this._arrMax = null;


    NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior.initializeBase(this, [element]);
}

NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior.prototype = {
    initialize: function() {
        NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior.callBaseMethod(this, 'initialize');        

        //Get cached references to the ticker and wrapper elements.
        this._wrapper = $get(this.get_id() + "_wrapper");
        this._ticker = $get(this.get_id() + "_ticker");

        this._scroll = 0;
        this._arrCursor = 0;
        this._height = 100;


        //Create Handler Delegates, passing anonymous methods to update the isPaused flag on mouse events.
        this._mouseOverHandler = Function.createDelegate(this, function() { this._isPaused = true; });
        this._mouseOutHandler = Function.createDelegate(this, function() { this._isPaused = false; });

        //Attach the handlers to the events.
        $addHandler(this._ticker, 'mouseover', this._mouseOverHandler);
        $addHandler(this._ticker, 'mouseout', this._mouseOutHandler);


        //Initiate the marquee
        this._startMarquee();
    },

    dispose: function() {

        //Clear the Timer.
        if (this._intervalID) {
            window.clearInterval(this._intervalID);
        }

        //Detach MousOver Event Handlers
        if (this._mouseOverHandler) {
            $removeHandler(this._ticker, 'mouseover', this._mouseOverHandler);
        }

        //Detach MouseOut Event Handlers
        if (this._mouseOutHandler) {
            $removeHandler(this._ticker, 'mouseout', this._mouseOutHandler);
        }

        //Call the base dispose method.
        NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior.callBaseMethod(this, 'dispose');
    },


    _startMarquee: function() {

        //Set initial pause state.
        this._isPaused = true;

        //Clear any existing timers from a previous run.
        if (this._intervalID) {
            window.clearInterval(this._intervalID);
        }

        //Get the Marquee Data from the Web Service and await the callback to onServiceMethodComplete.
        Sys.Net.WebServiceProxy.invoke(
        this._servicePath,
        this._serviceMethod,
        false,
        null,
        Function.createDelegate(this, this._onServiceMethodComplete));
    },

    _onServiceMethodComplete: function(result, userContext, methodName) {
        this._marqueeItems = result;

        var item = $get(this.get_id() + "_ticker");
        //  attach mousover and out events to the item
        //  so we can pause the marquee when it is moused over
        var itemMouseOverCssClass = this.get_ItemMouseOverCssClass();
        if (itemMouseOverCssClass) {
            $addHandler(item, 'mouseover',
                    Function.createDelegate(item, function() {
                        Sys.UI.DomElement.addCssClass(this, itemMouseOverCssClass);
                    })
                );
            $addHandler(item, 'mouseout',
                    Function.createDelegate(item, function() {
                        Sys.UI.DomElement.removeCssClass(this, itemMouseOverCssClass);
                    })
                );
        }

        this._arrMax = this._marqueeItems.length - 1;


        this._isPaused = false;

        //var base = $get("jump_base");        
        //this._height = base.offsetHeight;

        var divi = parseInt(this._height / 5);
        this._height = divi * 5;

        var td1 = document.getElementById("td1");
        var td2 = document.getElementById("td2");
        var td3 = document.getElementById("td3");
        td1.height = this._height - 5;
        td2.height = this._height - 5;
        td3.height = this._height - 5;

        this._intervalID = window.setInterval(Function.createDelegate(this, this.onTimer), this._pauseDuration);


    },



    setLink: function() {
        var ilink = document.getElementById("jump_link");
        ilink.innerHTML = this._marqueeItems[this._arrCursor];
        //ilink.href = this._aLinks[this._arrCursor];
    },

    onTimer: function() {

        //var base = document.getElementById("jump_base");
        var base = $get(this.get_id() + "_wrapper");


        if (this._scroll === NaN || this._scroll === undefined)
            this._scroll = 0;

        this._scroll += 5;

        if (this._scroll > (this._height * 2)) {
            this._scroll = 0;
            this._arrCursor++;
            if (this._arrCursor > this._arrMax)
                this._arrCursor = 0;

            this.setLink();
        }

        if (this._scroll == this._height) {
            this.pause();
            this._intervalID = setTimeout(Function.createDelegate(this, this.resume), 4000);
        }
        base.scrollTop = this._scroll;

    },

    pause: function() {
        clearInterval(this._intervalID);
    },

    resume: function() {
        this._intervalID = window.setInterval(Function.createDelegate(this, this.onTimer), this._pauseDuration);
    },

    getData: function() {
        //fetch the marquee items from the web method
        //alert("here");
        Sys.Net.WebServiceProxy.invoke(
               this._servicePath,
               this._serviceMethod,
               false,
               null,
               Function.createDelegate(this, this._onServiceMethodComplete));
    },



    _onServiceMethodFailed: function(webServiceError, userContext, methodName) {
        var e = this.get_element();
        e.innerHTML = 'Unable to retrieve Marquee';
    },


    get_ServiceMethod: function() {
        return this._serviceMethod;
    },
    set_ServiceMethod: function(value) {
        if (this._serviceMethod != value) {
            this._serviceMethod = value;
            this.raisePropertyChanged('ServiceMethod');
        }
    },

    get_ServicePath: function() {
        return this._servicePath;
    },
    set_ServicePath: function(value) {
        if (this._servicePath != value) {
            this._servicePath = value;
            this.raisePropertyChanged('ServicePath');
        }
    },

    get_IsTopToBottom: function() {
        return this._isTopToBottom;
    },

    set_IsTopToBottom: function(value) {
        if (this._isTopToBottom != value) {
            this._isTopToBottom = value;
            this.raisePropertyChanged('IsTopToBottom');
        }
    },

    get_ItemMouseOverCssClass: function() {
        return this._itemMouseOverCssClass;
    },

    set_ItemMouseOverCssClass: function(value) {
        if (this._itemMouseOverCssClass != value) {
            this._itemMouseOverCssClass = value;
            this.raisePropertyChanged('ItemMouseOverCssClass');
        }
    },

    get_ItemCssClass: function() {
        return this._itemCssClass;
    },

    set_ItemCssClass: function(value) {
        if (this._itemCssClass != value) {
            this._itemCssClass = value;
            this.raisePropertyChanged('ItemCssClass');
        }
    },

    get_PauseDuration: function() {
        return this._pauseDuration;
    },

    set_PauseDuration: function(value) {
        if (this._pauseDuration != value) {
            this._pauseDuration = value;
            this.raisePropertyChanged('PauseDuration');
        }
    }


}

NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior.registerClass('NaturalRhythm.WebControls.AJAX.VerticalMarquee.VerticalMarqueeBehavior', Sys.UI.Control);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();