var divajaxticker = Class.create({
    updateInterval: 60,
    scrollDiv: null,
    usersDiv: null,
    url: '',
    newHtml: '',
    currentHtml: '',
	allowUpdates: true,
    mtIntervalId: 0,
    mtTicker: null,
    subDivSize: 10,
    subDivName: '',
    scrolldir: 0,
	hidetime: .5, 
	showtime: .5,

    initialize: function(scrollDivName, usersDivName, url) {
		this.scrollDiv = $(scrollDivName);
		this.usersDiv = $(usersDivName);
		this.url = url;
		this.CreateTicker();
    },

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

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

	doSwitch: function() {
		//if (!this.allowUpdates) {
			//return false;
		//}
		this.usersDiv.fade({duration: this.hidetime, afterFinish: this.afterFade.bind(this)});
	},

	afterFade: function(obj) {
		this.usersDiv.update(this.newHtml);
		this.scrollDiv.scrollLeft = 0;
		this.usersDiv.appear({duration: this.showtime, afterFinish: this.afterAppear.bind(this)});
	},

	afterAppear: function(obj) {
	},

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

        if (this.scrollDiv) {
            var winW = 0;
            if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1) {
                winW = window.innerWidth;
            } 
            if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {
                winW = document.body.offsetWidth;
            }
            if (winW == 0) {
                winW = document.viewport.getWidth();
            }

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

            subwidth += this.subDivSize;
            this.scrollDiv.style.width = winW - subwidth;
        }
    },

	CreateTicker: function() {
		this.mtTicker = new divticker(this.scrollDiv);
	},

    SetSpeed: function(val)
    {
        if (!this.mtTicker) {
			this.CreateTicker();
        }
        this.mtTicker.SetSpeed(val);
    },

    TickTime: function(val)
    {
        if (!this.mtTicker) {
			this.CreateTicker();
        }
        this.mtTicker.TickTime(val);
    },

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

   update: function() {
        new Ajax.Request(this.url, {
            method: 'get',
            onComplete: (function(response) { 
                if (response.status == 200) { 
                    this.newHtml = "<table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td nowrap='nowrap'>"+response.responseText+"</td><td nowrap='nowrap'>"+response.responseText+"</td></tr></table>";

					var updated = false;
					if (this.usersDiv.empty()) {
                    	this.usersDiv.update(this.newHtml);
						updated = true;
					}
					else if (this.newHtml != this.currentHtml) {
						this.currentHtml = this.newHtml;
						this.doSwitch();
						updated = true;
					}

					if (updated) {
                    	this.fixDiv(); 
                    	this.mtTicker.start();
					}
                } 
            }).bind(this)
        });

        return true;
   },

   fixDiv: function() {
        if (this.usersDiv) {
            this.usersDiv.scrollLeft = 0;
        }
        this.onResize(null);
   },

   start: function() {
		if (!this.mtTicker) {
			this.CreateTicker();
		}

		Event.observe(window, 'resize', this.onResize.bind(this));
		this.update(this);
       
		this.startUpdates();
	},

	stopUpdates: function() {
       if (this.mtIntervalId) {
            clearInterval(this.mtIntervalId);
            this.mtIntervalId = 0;
       }
	},

	startUpdates: function() {
		this.stopUpdates();
		this.mtIntervalId = setInterval(this.update.bind(this), this.updateInterval * 1000);
    },

    Pause: function() {
        if (this.mtTicker) {
            this.mtTicker.Pause();
        }
		this.allowUpdates = false;
    },

    Resume: function(e) {
        if (this.mtTicker) {
            this.mtTicker.Resume(e);
        }
		this.allowUpdates = true;
    },

    Stop: function() {
		this.stopUpdates();
    }
});
