var JSON = {
    copyright: '(c)2005 JSON.org',
    license: 'http://www.crockford.com/JSON/license.html',
/*
    Stringify a JavaScript value, producing a JSON text.
*/
    stringify: function (v) {
        var a = [];
/*
    Emit a string.
*/
        function e(s) {
            a[a.length] = s;
        }

/*
    Convert a value.
*/
        function g(x) {
            var b, c, i, l, v;

            switch (typeof x) {
            case 'string':
                e('"');
                if (/["\\\x00-\x1f]/.test(x)) {
                    l = x.length;
                    for (i = 0; i < l; i += 1) {
                        c = x.charAt(i);
                        if (c >= ' ') {
                            if (c == '\\' || c == '"') {
                                e('\\');
                            }
                            e(c);
                        } else {
                            switch (c) {
                            case '\b':
                                e('\\b');
                                break;
                            case '\f':
                                e('\\f');
                                break;
                            case '\n':
                                e('\\n');
                                break;
                            case '\r':
                                e('\\r');
                                break;
                            case '\t':
                                e('\\t');
                                break;
                            default:
                                c = c.charCodeAt();
                                e('\\u00' +
                                    Math.floor(c / 16).toString(16) +
                                    (c % 16).toString(16));
                            }
                        }
                    }
                } else {
                    e(x);
                }
                e('"');
                return;
            case 'number':
                e(isFinite(x) ? x : 'null');
                return;
            case 'object':
                if (x) {
                    if (x instanceof Array) {
                        e('[');
                        l = a.length;
                        for (i = 0; i < x.length; i += 1) {
                            v = x[i];
                            if (typeof v != 'undefined' &&
                                    typeof v != 'function') {
                                if (b) {
                                    e(',');
                                }
                                g(v);
                                b = true;
                            }
                        }
                        e(']');
                        return;
                    } else if (typeof x.valueOf == 'function') {
                        e('{');
                        l = a.length;
                        for (i in x) {
                            v = x[i];
                            if (typeof v != 'undefined' &&
                                    typeof v != 'function' &&
                                    (!v || typeof v != 'object' ||
                                    typeof v.valueOf == 'function')) {
                                if (b) {
                                    e(',');
                                }
                                g(i);
                                e(':');
                                g(v);
                                b = true;
                            }
                        }
                        return e('}');
                    }
                }
                e('null');
                return;
            case 'boolean':
                e(x);
                return;
            default:
                e('null');
                return;
            }
        }
        g(v);
        return a.join('');
    },
/*
    Parse a JSON text, producing a JavaScript value.
    It returns false if there is a syntax error.
*/
    parse: function (text) {
        try {
            return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                    text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
                eval('(' + text + ')');
        } catch (e) {
            return false;
        }
    }
};






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

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");
                 var opacityTween = new OpacityTween(fadeObject,Tween.regularEaseIn,100,0,1);
                 opacityTween.start();
                 opacityTween.onMotionFinished = function() {
                     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() {
        ninbar.headlineTicker.prev(1);
        var t = setTimeout(function() { nb.headlineTicker.unpause(); }, 7000);
    };
    
    document.getElementById("pause-story").onclick = function() {
        if(!nb.isPaused) {
            this.src = "/images/ninnbar/ticker-pause-active.gif";
            nb.headlineTicker.pause();
            nb.headlineTicker.lock();
        } else {
            this.src = "/images/ninnbar/ticker-pause.gif";
            nb.headlineTicker.unpause();
            nb.headlineTicker.unlock();
        }      
    };
    
    var tti = window.setTimeout(function() {
                                    nb.headlineTicker.nextHeadline(1, 0); 
                                }, 300);
}
