Type.registerNamespace('ThomasGlobal.Web');

ThomasGlobal.Web.SearchTicker = function(element) {
    this._wrapper = null;

    this._ticker = null;

    this._mouseOverHandler = null;
    this._mouseOutHandler = null;
    this._intervalID = null;
    this._isPaused = null;

    this._isTopToDown = null;
    this._serviceMethod = null;
    this._servicePath = null;
    this._itemCssClass = null;
    this._itemMouseOverCssClass = null;
    this._pixelsPerSecond;
    this._loaded = false;

    ThomasGlobal.Web.SearchTicker.initializeBase(this, [element]);
}

ThomasGlobal.Web.SearchTicker.prototype = {
    initialize: function() {

        ThomasGlobal.Web.SearchTicker.callBaseMethod(this, 'initialize');
        //  cache the ticker and wrapper elements
        this._wrapper = $get(this.get_id() + "_wrapper");
        this._ticker = $get(this.get_id() + "_ticker");

        //  attach to the mouse over/out events
        this._mouseOverHandler = Function.createDelegate(this, function() { this._isPaused = true; });
        $addHandler(this._ticker, 'mouseover', this._mouseOverHandler);

        this._mouseOutHandler = Function.createDelegate(this, function() { this._isPaused = false; });
        $addHandler(this._ticker, 'mouseout', this._mouseOutHandler);

        this._loadDataFromService(this._ticker);
    },

    dispose: function() {
        if (this._intervalID) {
            window.clearInterval(this._intervalID);
        }
        if (this._mouseOverHandler) {
            $removeHandler(this._ticker, 'mouseover', this._mouseOverHandler);
        }
        if (this._mouseOutHandler) {
            $removeHandler(this._ticker, 'mouseout', this._mouseOutHandler);
        }

        ThomasGlobal.Web.SearchTicker.callBaseMethod(this, 'dispose');
    },

    _loadDataFromService: function(ticker) {
        //  do any clean up from previous running
        this._isPaused = true;
        if (this._intervalID) {
            window.clearInterval(this._intervalID);
        }

        //  fetch the ticker items from the web method
        Sys.Net.WebServiceProxy.invoke(
           this._servicePath,
           this._serviceMethod,
           false,
           null,
           Function.createDelegate(this, this._onServiceMethodComplete),
           Function.createDelegate(this, this._onServiceMethodFailed),
           ticker);
    },

    _onServiceMethodComplete: function(result, ticker, methodName) {
        this._initTicker(result, ticker);

        this._initLayout(this.get_IsTopToDown(), this._ticker);

        this._isPaused = false;
        this._intervalID = window.setInterval(Function.createDelegate(this, this._onInterval), 100);
        this._loaded = true;
    },

    _onServiceMethodFailed: function(webServiceError, userContext, methodName) {
        var e = this.get_element();
        e.innerHTML = ''; // 'Unable to retrieve information from the service!';
    },

    _onInterval: function() {
        this._performLayout(this.get_IsTopToDown(), this._ticker);
    },

    _clearTicker: function(ticker) {

        //  clear the handlers if the ticker
        //  has any children
        if (ticker.childNodes.length > 0) {
            //  remove the handlers
            for (i = 0; i < ticker.childNodes.length; i++) {
                $clearHandlers(ticker.childNodes[i]);
            }
            //  clear any child elements
            ticker.innerHTML = '';
        }
    },

    _initTicker: function(result, ticker) {
        var i;

        this._clearTicker(ticker);

        //  loop through the results and create a span for 
        //  each of the items
        for (i = 0; i < result.length; i++) {
            //  create the items container and add it to
            //  the document
            var item = document.createElement('span');
            var itemCssClass = this.get_ItemCssClass();
            if (itemCssClass) {
                Sys.UI.DomElement.addCssClass(item, itemCssClass);
            }
            item.innerHTML = result[i];
            ticker.appendChild(item);
            // create a separator and add it to the document
            if (i != (result.length - 1)) {
                var separatorItem = document.createElement('br');
                ticker.appendChild(separatorItem);
            }

            //  attach mousover and out events to the item
            //  so we can pause the ticker 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);
                    })
                );
            }
        }
    },

    _initLayout: function(isTopToDown, ticker) {

        var tickerHeight = this._getTickerHeight(ticker);

        ticker.style.height = tickerHeight + 'px';
        ticker.style.left = '0px';

        if (isTopToDown) {
            ticker.style.top = -tickerHeight + 'px';
        }
        else {
            ticker.style.top = $common.getSize(this._wrapper).height + 'px';
        }
    },

    _performLayout: function(isTopToDown, ticker) {
        if (!this._isPaused) {

            if (!this._loaded) {
                return;
            }

            if (isTopToDown) {
                // subtract a value from the left
                var newTop = ($common.parseUnit(ticker.style.top).size + Math.ceil((this.get_PixelsPerSecond() / 100)));
                if (newTop > $common.getSize(this._wrapper).height) {
                    this._initLayout(this.get_IsTopToDown(), ticker);
                } else {
                    ticker.style.top = newTop + 'px';
                }
            }
            else {
                var newTop = ($common.parseUnit(ticker.style.top).size - Math.ceil((this.get_PixelsPerSecond() / 100)));

                if (-newTop > $common.getSize(ticker).height) {
                    this._initLayout(this.get_IsTopToDown(), ticker);
                } else {
                    ticker.style.top = newTop + 'px';
                }
            }
        }
    },

    _getTickerHeight: function(ticker) {
        var height = 0;

        //  add all of the items to the body
        for (var i = 0; i < ticker.childNodes.length; i++) {

            var originalNode = ticker.childNodes[i];

            if (originalNode.nodeName &&
                originalNode.nodeName == "SPAN") {

                var paddingBox = $common.getPaddingBox(originalNode);
                var marginBox = $common.getMarginBox(originalNode);
                var borderBox = $common.getBorderBox(originalNode);

                var contentHeight = $common.getContentSize(originalNode).height;
                contentHeight += (paddingBox.top + paddingBox.bottom);
                contentHeight += (marginBox.top + marginBox.bottom);

                height += contentHeight;
            }
        }

        return height;
    },

    get_ServicePath: function() {
        return this._servicePath;
    },
    set_ServicePath: function(value) {
        if (this._servicePath != value) {
            this._servicePath = value;
            this.raisePropertyChanged('ServicePath');
        }
    },

    get_ServiceMethod: function() {
        return this._serviceMethod;
    },
    set_ServiceMethod: function(value) {
        if (this._serviceMethod != value) {
            this._serviceMethod = value;
            this.raisePropertyChanged('ServiceMethod');
        }
    },

    get_IsTopToDown: function() {
        return this._isTopToDown;
    },
    set_IsTopToDown: function(value) {
        if (this._isTopToDown != value) {
            this._isTopToDown = value;
            this.raisePropertyChanged('IsTopToDown');
        }
    },

    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_PixelsPerSecond: function() {
        return this._pixelsPerSecond;
    },
    set_PixelsPerSecond: function(value) {
        if (this._pixelsPerSecond != value) {
            this._pixelsPerSecond = value;
            this.raisePropertyChanged('PixelsPerSecond');
        }
    }
}

ThomasGlobal.Web.SearchTicker.registerClass('ThomasGlobal.Web.SearchTicker', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();