var divajaxmsgticker = Class.create({
    mtouinterval: 60*5,
    divajaxupdate: '',
    divscrollname: '',
    mtpage: '',
    mtintervalid: 0,
    subdivsize: 0,
    subdivname: '',
    mtticker: null,
    scrolldir: 0,

    initialize: function(divscrollname,divajaxupdate,httppage) {
        this.divajaxupdate = divajaxupdate;
        this.divscrollname = divscrollname;
        this.mtpage = httppage;
    },

    LimitDiv :function(val)
    {
        this.subdivname = val;
    },

    LimitSize :function(val) 
    {
        this.subdivsize = val;
    },

    onResize :function(event) {
        if (event) {
            if (event.type == 'load') {
                var element = Event.element(event);
                if (element) {
                    element.stopObserving('load');
                }
            }
        }

        var tmpdiv = $(this.divscrollname);
        if (tmpdiv) {
            var winW = document.viewport.getWidth();

            var subwidth = 0;
            if (!this.subdivname.empty()) {
                var subdiv = $(this.subdivname);
                if (subdiv) {
                    subwidth += subdiv.getWidth() ;
                }
            }

            subwidth += this.subdivsize;
            var newwidth = winW - subwidth;

            tmpdiv.setStyle({ width: newwidth+'px' });
        }
    },

    SetSpeed: function(val)
    {
        if (!this.mtticker) {
            this.mtticker = new divmsgticker(this.divscrollname);
        }
        this.mtticker.SetSpeed(val);
    },

    TickTime: function(val)
    {
        if (!this.mtticker) {
            this.mtticker = new divmsgticker(this.divscrollname);
        }
        this.mtticker.TickTime(val);
    },

    Interval: function(val)
    {
        this.mtouinterval = val;
    },

    update: function() {
        var temp = this;
        new Ajax.Request(this.mtpage, {
            method: 'get',
            onComplete: function(response) { 
                if (response.status == 200) { 
                    //temp.fixDiv();
                    $(temp.divajaxupdate).update("<table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td nowrap='nowrap'>"+response.responseText+"</td><td nowrap='nowrap'>"+response.responseText+"</td></tr></table>");
                    temp.fixDiv(); 
                    temp.mtticker.start();
                } 
            }
        });

        return true;
   },

   fixDiv: function() {
        var docObj = $(this.divajaxupdate);
        if (docObj) {
            docObj.scrollLeft = 0;
        }
        this.onResize(null);
   },

   start: function(firstupdate) {
       if (!this.mtticker) {
            this.mtticker = new divmsgticker(this.divscrollname);
       }

       Event.observe(window, 'resize', this.onResize.bind(this));
       //this.onResize(null);
       this.update(this);
       
       if (this.mtintervalid) {
            clearInterval(this.mtintervalid);
            this.mtintervalid = 0;
       }

       this.mtintervalid = setInterval(this.update.bind(this),this.mtouinterval * 1000);
    },

    Pause: function() {
        if (this.mtticker) {
            this.mtticker.Pause();
        }
    },

    Resume: function(e) {
        if (this.mtticker) {
            this.mtticker.Resume(e);
        }
    },

    Stop: function() {
        if (this.mtintervalid) {
            clearInterval(this.mtintervalid);
            this.mtintervalid = 0;
        }
    }
});

