﻿/// <reference path="../../../Scripts/jquery-1.3.2-vsdoc.js" />

if (!Serpentine)
    var Serpentine = {};
Serpentine.TickerModule = function(element, list) {

    this._dispose();
    this._constructor(element, list);
}

Serpentine.TickerModule.prototype =
    {
        _dispose: function() {
            var pro;
            for (pro in this) {
                pro = null;
            }
        },
        _constructor: function(element, list) {
            this._id = "#" + element; //+" ";
            this.l = list;
        },
        getjQ: function(id) {
            return $(this._id).find(id);
        },
        gotoUrl: function(i) {
         window.open(i.arrayNews[i.currentLink].link, i.arrayNews[i.currentLink].window);
        },
        init: function() {

            this.tickerHeader = this.getjQ("#tickerHeader");
            this.tickerBody = this.getjQ("#tickerBody");
            this.tickerId = this.getjQ(this.l.tickerId);
            var i = this;
            this.tickerHeader.hover(function(e) { window.status = i.arrayNews[i.currentLink].link; }, function(e) { window.status = ""; });
            this.tickerBody.hover(function(e) { window.status = i.arrayNews[i.currentLink].link; }, function(e) { window.status = ""; });
            this.tickerHeader.click(function(e) {
                e.preventDefault();
                i.gotoUrl(i);
            }); //end click
            this.tickerBody.click(function(e) {
                e.preventDefault();
                i.gotoUrl(i);
            }); //end click

            this.loadNews();
        },
        tickerHeader: null,
        tickerBody: null,
        tickerId: null,
        curChar: 0,
        synopsis: "",
        arrayNews: [],
        currentIndex: 0,

        loadNews: function() {
            var d = Date();
            var i = this;
            $.getJSON(this.l.url, { d: d.toString() }, function(data) {
                i.arrayNews = data;
                i.typeNews();
            });
        },
        typeNews: function() {
            if (this.arrayNews.length > 0 &&
               this.currentIndex < this.arrayNews.length) {
               this.currentLink = this.currentIndex;
                var title = this.arrayNews[this.currentIndex].title + ":";
                var tooltip = this.arrayNews[this.currentIndex].tip;
                var i = this;

                i.tickerId.fadeOut(this.l.timeOutDelay, function() {
                    i.tickerHeader.text(title);
                    i.tickerBody.text(" ");
                    i.tickerId.attr("title", tooltip).show();

                    i.synopsis = i.getText(title, i.arrayNews[i.currentIndex].text);

                    i.curChar = 0;
                    i.type();

                });
            }
        },
        getText: function(title, text) {
            var fulltext = title + text;
            if (fulltext.length > this.l.maxLen) {
                var len = this.l.maxLen - title.length - 3;
                text = text.substring(0, len);
                text += " ...";
            }
            return text;
        },


        currentLink: 0,
        type: function() {

            var value = this.synopsis.substr(0, this.curChar++);
            this.tickerBody.text(value);
            if (this.curChar < this.synopsis.length + 1) {
                var i = this;
                setTimeout(function() { i.type(); }, i.l.typeDelay);
            } else {

                this.getNextSet();
            }
        },
        getNextSet: function() {
            var i = this;
            if (this.currentIndex + 1 < this.arrayNews.length) {
                this.currentIndex++;
                setTimeout(function() { i.typeNews() }, i.l.timeOutDelay);

            } else {
                this.currentIndex = 0;
                setTimeout(function() { i.loadNews() }, i.l.timeOutDelay);
            }
        }
    }