var NewsFeeds = [];
var newsItems = [
        "May 1, 2008: Qiiu Systems releases the Qiiu Platform and Qiiu Framework", "technology.html",
        "May 1, 2008: Qiiu Systems introduces new brand and launches new corporate web site", "http://www.qiiusystems.com/",
        "April 14, 2008: Qiiu Systems attends 2008 ALK Transportation and Technology Summit in Princeton, NJ", "http://www.alk.com/techsummit2008/"
];

function NewsFeed(newsItems, textElem, cursorElem, linkElem) {
    this._newsFeedsIndex = NewsFeeds.length;
    NewsFeeds[this._newsFeedsIndex] = this;
    
    this._deleteSpeed = 4;
    this._typeTextDelay = 25;
    this._betweenMessagesDelay = 250;
    this._cursorFlashDelay = 200;
    this._showMessageDelay = 5000;
    this._newsItems = newsItems;
    this._cursorFlashing = false;
    this._textElem = textElem;
    this._cursorElem = cursorElem;
    this._linkElem = linkElem;
    this._curItem = 0;
    
    this._log = document.getElementById("log");

    textElem.onclick = this._LaunchLink;
    
    if ( linkElem != null )
        linkElem.onclick = this._LaunchLink;
    
    setTimeout("NewsFeeds[" + this._newsFeedsIndex + "]._TypeNextText();", 1000);
    this._SetCursorFlashing(true);
}

NewsFeed.prototype._deleteSpeed;
NewsFeed.prototype._typeTextDelay;
NewsFeed.prototype._betweenMessagesDelay;
NewsFeed.prototype._cursorFlashDelay;
NewsFeed.prototype._showMessageDelay;
NewsFeed.prototype._newsItems;
NewsFeed.prototype._cursorFlashing;
NewsFeed.prototype._textElem;
NewsFeed.prototype._curTypeText;
NewsFeed.prototype._cursorElem;
NewsFeed.prototype._newsFeedsIndex;
NewsFeed.prototype._curItem;
NewsFeed.prototype._log;
NewsFeed.prototype._linkElem;
NewsFeed.prototype._curLink;


function setText(elem, text) {
    if ( document.all )
        elem.innerText = text;
    else
        elem.textContent = text;
}

function getText(elem) {
    if ( document.all )
        return elem.innerText;
    else
        return elem.textContent;
}

NewsFeed.prototype._DeleteText = function() {
    var elem = this._textElem;
    this._curTypeText = null;
    this._SetCursorFlashing(false);
    this._linkElem.style.display="none";
    setText(elem, getText(elem).substring(0,getText(elem).length - this._deleteSpeed));
    if (getText(elem).length > 0)
        setTimeout("NewsFeeds[" + this._newsFeedsIndex + "]._DeleteText()", 1);
    else {
        this._SetCursorFlashing(true);
        setTimeout("NewsFeeds[" + this._newsFeedsIndex + "]._TypeNextText()", this._betweenMessagesDelay);
    }
}


NewsFeed.prototype._TypeNextText = function() {
    var elem = this._textElem;
    var text;
    this._SetCursorFlashing(false);
    if ( this._curTypeText == null ) {
        this._curTypeText = this._newsItems[this._curItem * 2];
        this._linkElem._curLink = this._newsItems[this._curItem * 2 + 1];
        this._textElem._curLink = this._newsItems[this._curItem * 2 + 1];
        if ( this._linkElem._curLink != null )
            this._textElem.style.cursor = "pointer";
        else
            this._textElem.style.cursor = "auto";
        if ( ++this._curItem * 2 >= this._newsItems.length )
            this._curItem = 0;
    }
    setText(elem, getText(elem) + this._curTypeText.substring(0,1));
    this._curTypeText = this._curTypeText.substring(1,this._curTypeText.length);
    if (this._curTypeText.length > 0)
	    setTimeout("NewsFeeds[" + this._newsFeedsIndex + "]._TypeNextText()", this._typeTextDelay);
    else {
	    this._SetCursorFlashing(true);
	    if ( this._linkElem._curLink != null )
	        this._linkElem.style.display="inline";
	    setTimeout("NewsFeeds[" + this._newsFeedsIndex + "]._DeleteText()", this._showMessageDelay);
    }
}

NewsFeed.prototype._SetCursorFlashing = function(flashing) {
    //log.innerHTML += "SetCursorFlashing(" + flashing + ")<br/>";
    if ( flashing && ( this._cursorIntervalId == null ) ) {
        //log.innerHTML += "SetCursorFlashing changing to true<br/>";
        this._cursorIntervalId = setInterval("NewsFeeds[" + this._newsFeedsIndex + "]._FlashCursor(true);", this._cursorFlashDelay);
    }
    else if ( !flashing && ( this._cursorIntervalId != null ) ) {
        //log.innerHTML += "SetCursorFlashing changing to false<br/>";
        clearInterval(this._cursorIntervalId);
        this._cursorIntervalId = null;
        this._FlashCursor(false);
    }
}

NewsFeed.prototype._FlashCursor = function(isTimeout) {
    var cursor = this._cursorElem;
    //log.innerHTML += "FlashCursor(" + isTimeout + ")<br/>";
    if (( cursor.style.visibility == "hidden" ) || ( this._cursorIntervalId == null ))
	    cursor.style.visibility = "visible";
    else
	    cursor.style.visibility = "hidden";
}

NewsFeed.prototype._LaunchLink = function(){
    if ( this._curLink != null )
        parent.location = this._curLink;
}
