/* -------------------------------------------------------------- */

var ninbar;
var headlinesJSON;
var tickerFadeTime;

/* -------------------------------------------------------------- */

function setOpacity(obj, opacity) {
    opacity = (opacity == 100)?99.999:opacity;
    // IE/Win
    obj.style.filter = "alpha(opacity:"+opacity+")";
    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity/100;
    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity/100;
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;
}

function fadeTickerOut(objId,opacity) {
    if (document.getElementById) {
        obj = document.getElementById(objId);
        if (opacity >= 0) {
            setOpacity(obj, opacity);
            opacity -= 2;
            tickerFadeTime = window.setTimeout(function() { fadeTickerOut(objId,opacity); }, 1);
        }
    }
}

function fadeTickerIn(objId,opacity) {
    if (document.getElementById) {
        obj = document.getElementById(objId);
        if (opacity <= 100) {
            setOpacity(obj, opacity);
            opacity += 2;
            tickerFadeTime = window.setTimeout(function() { fadeTickerIn(objId,opacity); }, 1);
        }
    }
}

/* Ticker constructor */
HeadlineTicker = function() {
    this.insertScript("http://www.news.com.au/js/jsonheadlines/0,21324,,00.js");
    this.index = -1;
    this.pointer = document.getElementById("top-story");
    this.currentSection = 0;
    this.HEADLINE_MAX = 4; /* pretend const */
    this.scrollMutex = 0;  
    this.isPaused = 0;
    this.tickerPauseTime = null;  
}

/* Dynamic script insertion */
HeadlineTicker.prototype.insertScript = function(source) {
    var ninbarJSON = document.createElement("script");
    ninbarJSON.setAttribute("type", "text/javascript");
    ninbarJSON.setAttribute("src", source);
    var headElement = document.getElementsByTagName("head")[0];
    headElement.appendChild(ninbarJSON);
}

HeadlineTicker.prototype.replaceHeadline = function(newItem, itemPointer) {
     document.getElementById("headlines").replaceChild(newItem, itemPointer);
}

/* public ticker function on HeadlineTicker.prototype */
HeadlineTicker.prototype.previousHeadline = function() {
    this.nextHeadline(1, 1);
}

HeadlineTicker.prototype.prev = function() {
    this.next(1, 1);
}

HeadlineTicker.prototype.next = function(immediate, reverse) {
    if (!reverse) {
            if(this.index === (headlinesJSON.response.topnews.headline.length - 1)) {
                this.index = 0;
            } else {
                ++(this.index);
            }
        } else { 
            // backwards is pre routine decrement
            if(this.index === 0) {
                this.index = headlinesJSON.response.topnews.headline.length - 1;
            } else { 
                --(this.index);
            }
        } 
        
        /* topnews is default */
        if((headlinesJSON.response.topnews.headline[this.index]) == null) {
            return(false);
        }
        
        /* get link from json */
        var headlineText = headlinesJSON.response.topnews.headline[this.index].value + " \u00BB";
        var headlineHref = headlinesJSON.response.topnews.headline[this.index].href;
    
        /* create new DOM element */
        var newItem = document.createElement("li");
        var newHeadline = document.createElement("a");
        newHeadline.setAttribute("href", headlineHref);
        var newTextNode = document.createTextNode(headlineText);        
        newHeadline.appendChild(newTextNode);
        newItem.appendChild(newHeadline);
        document.getElementById("ticker").className = "activated";
    
        var pointer = this.pointer;
        var self = this;
    
        /* switch, fading if immediate is not set */
        if(!immediate) {
            if(typeof(OpacityTween) != "undefined") {
                 var fadeObject = document.getElementById("headlines");
				 fadeObject.style.filter = 'Alpha(opacity=100);';
                 var opacityTween = new OpacityTween(fadeObject,Tween.regularEaseIn,100,0,1);
                 opacityTween.start();
                 opacityTween.onMotionFinished = function() {
				     fadeObject.style.filter = 'Alpha(opacity=0);';
                     var opacityTweenIn = new OpacityTween(fadeObject,Tween.regularEaseIn,0,100,1);
                     opacityTweenIn.start();
                     self.replaceHeadline(newItem, pointer);
                       
                 };
            } else {
                fadeTickerOut("headlines", 100);
                setTimeout(function(){self.replaceHeadline(newItem, pointer);}, 1650); 
                clearTimeout(self.tickerPauseTime);
                self.tickerPauseTime = setTimeout(function(){fadeTickerIn("headlines", 0);}, 1700);
            }
        } else {
            self.replaceHeadline(newItem, pointer); 
        }
    
    this.pointer = newItem;
}

/* calls next() */
HeadlineTicker.prototype.nextHeadline = function(immediate, reverse) {
    if( (this.scrollMutex) || (this.isPaused) || (!headlinesJSON) ) {
        return false;
    }
    
    this.next(immediate, reverse);
}

HeadlineTicker.prototype.unlock = function() {
    this.scrollMutex = 0;
}

HeadlineTicker.prototype.lock = function() {
    this.scrollMutex = 1;
}

HeadlineTicker.prototype.isLocked = function() {
    return this.scrollMutex;
}

HeadlineTicker.prototype.pause = function() {
    this.isPaused = 1;
}

HeadlineTicker.prototype.unpause = function() {
    this.isPaused = 0;
}

/* Constructir for Ninbar */
Ninbar = function() {
    this.headlineTicker = new HeadlineTicker();
    var nb = this;
    
    
    /* sync the DOM */
    document.getElementById("headlines").onmouseover = function() {
        nb.headlineTicker.pause();
    };
     
    document.getElementById("next-story").onclick = function() {
        nb.headlineTicker.pause();
        nb.headlineTicker.next(1, 0);
        var t = setTimeout(function() { nb.headlineTicker.unpause(); }, 7000);
    };
        
    document.getElementById("headlines").onmouseout = function() {
        var t = setTimeout(function() { nb.headlineTicker.unpause(); }, 7000);
    };
    
    document.getElementById("prev-story").onclick = function() {
        nb.headlineTicker.pause();
        ninbar.headlineTicker.prev(1);
        var t = setTimeout(function() { nb.headlineTicker.unpause(); }, 7000);
    };
    
    document.getElementById("pause-story").onclick = function() {
        if(!nb.isPaused) {
            this.src = "/images/button-ticker-pause-active.gif";
            nb.headlineTicker.pause();
            nb.headlineTicker.lock();
        } else {
            this.src = "/images/button-ticker-pause.gif";
            nb.headlineTicker.unpause();
            nb.headlineTicker.unlock();
        }      
    };
    
    var tti = window.setTimeout(function() {
                                    nb.headlineTicker.nextHeadline(1, 0); 
                                }, 300);
	 
}
