var NewsTicker = Class.create({
    initialize: function(container) {
        this.container = container;
        this.newsIndex = -1;
        this.playing = false;

        var newsText = document.createElement("div");
        newsText.className = "newsText";
        newsText.innerHTML = "Loading News...";

        var controlContainer = document.createElement("div");
        controlContainer.className = "controlContainer";

        var nextButton = new ControlButton("/college/psc/news_controls/control_fastforward.png", "/college/psc/news_controls/control_fastforward_blue.png");
        nextButton.bindAction(this.nextNews, this);
        var prevButton = new ControlButton("/college/psc/news_controls/control_rewind.png", "/college/psc/news_controls/control_rewind_blue.png");
        prevButton.bindAction(this.previousNews, this);
        var pauseButton = new ControlButton("/college/psc/news_controls/control_pause.png", "/college/psc/news_controls/control_pause_blue.png");
		pauseButton.bindAction(this.playTicker, this);
		pauseButton.bindToggleableAction(this.pauseTicker, this, "/college/psc/news_controls/control_play.png", "/college/psc/news_controls/control_play_blue.png");
        controlContainer.appendChild(prevButton.getDOMObject());
        controlContainer.appendChild(pauseButton.getDOMObject());
        controlContainer.appendChild(nextButton.getDOMObject());

        this.newsText = newsText;

        this.container.appendChild(newsText);
        this.container.appendChild(controlContainer);

        this.loadNews();
    },
    loadNews: function() {
        var tick = this;
        var newsAJAX = new Ajax.Request("/college/psc/news_json.php", {
            method: 'get',
            onSuccess: function(transport) {
                var newsObject = eval('(' + transport.responseText + ')');
                tick.newsData = newsObject;
                tick.playing = true;
                tick.nextNews();
            }
        });
    },
    nextNews: function() {
        var index = ++this.newsIndex;
        if (index < 0 || index >= this.newsData.length) {
            index = 0;
            this.newsIndex = 0;
        }
		if (this.newsData[index][2] != null) {
			this.newsText.innerHTML = "<a href=\"" + this.newsData[index][2] + "\"><strong>" + this.newsData[index][0] + "</strong></a><br />" + this.newsData[index][1];
		} else {
			this.newsText.innerHTML = "<strong>" + this.newsData[index][0] + "</strong><br />" + this.newsData[index][1];
		}
        if (this.playing)
            this.playTicker();
    },
    previousNews: function() {
        var index = --this.newsIndex;
        if (index < 0 || index >= this.newsData.length) {
            index = this.newsData.length - 1;
            this.newsIndex = index;
        }
		if (this.newsData[index][2] != null) {
			this.newsText.innerHTML = "<a href=\"" + this.newsData[index][2] + "\"><strong>" + this.newsData[index][0] + "</strong></a><br />" + this.newsData[index][1];
		} else {
			this.newsText.innerHTML = "<strong>" + this.newsData[index][0] + "</strong><br />" + this.newsData[index][1];
		}
        if (this.playing)
            this.playTicker()
    },
	playTicker: function() {
        this.playing = true;
		itemTime = (this.newsData[this.newsIndex][1].length) / 22 * 1000;
		var boundNext = this.nextNews.bind(this);
		window.clearTimeout(this.timeout);
		this.timeout = window.setTimeout(boundNext, itemTime);
	},
	pauseTicker: function() {
        this.playing = false;
		window.clearTimeout(this.timeout);
	}
});
var ControlButton = Class.create({
    initialize: function(img, over_img) {
        this.img_src = img;
        this.over_img_src = over_img;
        this.stateOver = false;
		this.isToggleable = false;
		this.isToggled = false;
		this.normalAction = null;
		this.toggledAction = null;

        this.link = document.createElement("a");
        this.link.href = "javascript:void(0)";
        this.img = document.createElement("img");
        this.img.src = this.img_src;
        this.img.border = "0px";

        this.img.onmouseover = this.toggleHover.bindAsEventListener(this);
        this.img.onmouseout = this.toggleHover.bindAsEventListener(this);
		this.link.onclick = this.handleClick.bindAsEventListener(this);

        this.link.appendChild(this.img);
    },
    fixTransparency: function() {
        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])

        if ((version >= 5.5 && version < 7.0) && (document.body.filters)) {
            if (this.stateOver) {
                var rep_src = this.over_img_src;
            } else {
                var rep_src = this.img_src;
            }
            this.img.style = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + rep_src + "',sizingMethod='scale');";
        }
    },
    toggleHover: function() {
        if (this.stateOver) {
            this.stateOver = false;
			if (this.isToggled) {
				this.img.src = this.toggled_img;
			} else {
	            this.img.src = this.img_src;
			}
        } else {
            this.stateOver = true;
			if (this.isToggled) {
				this.img.src = this.toggled_over_img;
			} else {
	            this.img.src = this.over_img_src;
			}
        }	
        this.fixTransparency();
    },
    getDOMObject: function() {
        return this.link;
    },
	handleClick: function(ev) {
		if (this.normalAction != null && !this.isToggleable) {
			var clickHandler = this.normalAction.bind(this.normalActionContext);
		} else if (this.isToggleable && this.toggledAction != null) {
			if (!this.isToggled) {
				this.stateOver = true;
				this.isToggled = true;
				this.img.src = this.toggled_over_img;
				var clickHandler = this.toggledAction.bind(this.toggledActionContext);
			} else {
				this.stateOver = true;
				this.isToggled = false;
				this.img.src = this.over_img_src;
				var clickHandler = this.normalAction.bind(this.normalActionContext);
			}
		}
		clickHandler();
	},
    bindAction: function(func, func_context) {
		this.normalAction = func;
		this.normalActionContext = func_context;
    },
	bindToggleableAction: function(toggleaction, togglecontext, toggled_img, toggled_over_img) {
		this.toggledAction = toggleaction;
		this.toggledActionContext = togglecontext;
		this.toggled_img = toggled_img;
		this.toggled_over_img = toggled_over_img;
		this.isToggleable = true;
	}
});
