var JSON={
    copyright: '(c)2005 JSON.org',
    license: 'http://www.crockford.com/JSON/license.html',
    stringify: function (v){
        var a=[];
        function e(s){
            a[a.length]=s;
        }
        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: 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;
var tickerPauseTime;
/* -------------------------------------------------------------- */
function setOpacity(obj, opacity){
    opacity=(opacity == 100)?99.999:opacity;
    obj.style.filter="alpha(opacity:"+opacity+")";
    obj.style.KHTMLOpacity=opacity/100;
    obj.style.MozOpacity=opacity/100;
    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("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("fadeTickerIn('"+objId+"',"+opacity+")", 1);
        }
    }
}
HeadlineTicker=function(){
    this.index=-1;
	this.pointer=document.getElementById("top-story");
	this.currentSection=0;
	this.refJSON="/goto.php?json";
	this.HEADLINE_MAX=4;
	this.scrollMutex=0;
    var ninbarJSON=document.createElement("script");
	ninbarJSON.setAttribute("type", "text/javascript");
	ninbarJSON.setAttribute("src", this.refJSON);
	var headElement=document.getElementsByTagName("head")[0];
	headElement.appendChild(ninbarJSON);
}
HeadlineTicker.prototype.previousHeadline=function(){this.nextHeadline(1, 1);}
HeadlineTicker.prototype.nextHeadline=function(immediate, reverse){
	if(headlinesJSON != null){ 	
	    if (!reverse){if(this.index == headlinesJSON.response.topnews.headline.length - 1){this.index=0;}else{++(this.index);}}
		else{if(this.index == 0){this.index=headlinesJSON.response.topnews.headline.length - 1;}else{ --(this.index);}} 
	}else{this.index=0;}
	if(!headlinesJSON){return false;}else{
		if((headlinesJSON.response.topnews.headline[this.index]) == null){return(false);}
	    var headlineText=headlinesJSON.response.topnews.headline[this.index].value;
	    var headlineHref=headlinesJSON.response.topnews.headline[this.index].href;
		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;
		if(!immediate){
		    fadeTickerOut("headlines", 100);
		    setTimeout(function(){self.replaceHeadline(newItem, pointer);}, 1650); 
			clearTimeout(tickerPauseTime);
		    tickerPauseTime=setTimeout(function(){fadeTickerIn("headlines", 0);}, 1700);
		}else{self.replaceHeadline(newItem, pointer);}
	    this.pointer=newItem;
	}
}
HeadlineTicker.prototype.lock=function(){this.scrollMutex=1;}
HeadlineTicker.prototype.isLocked=function(){return(this.scrollMutex);}
HeadlineTicker.prototype.pause=function(){clearInterval(ninbar.tickInterval);}
HeadlineTicker.prototype.replaceHeadline=function(newItem, itemPointer){document.getElementById("headlines").replaceChild(newItem, itemPointer);}
Ninbar=function(){
    this.headlineTicker=new HeadlineTicker();
	this.tickInterval=0;
	document.getElementById("headlines").onmouseover=function(){ninbar.headlineTicker.pause();} 
	document.getElementById("next-story").onclick=function(){
	    clearInterval(ninbar.tickInterval);
	    ninbar.headlineTicker.nextHeadline(1, 0);
		if(!ninbar.headlineTicker.isLocked()){
		    ninbar.tickInterval=setInterval("ninbar.headlineTicker.nextHeadline()", 7000);
		}
	}
	document.getElementById("headlines").onmouseout=function(){
	    if(!ninbar.headlineTicker.isLocked()){ninbar.tickInterval=setInterval("ninbar.headlineTicker.nextHeadline()", 7000);}
	}
	document.getElementById("prev-story").onclick=function(){
	    clearInterval(ninbar.tickInterval);
	    ninbar.headlineTicker.previousHeadline(1);
		if(!ninbar.headlineTicker.isLocked()){ninbar.tickInterval=setInterval("ninbar.headlineTicker.nextHeadline()", 7000);}
	}
	this.tickInterval=setInterval("ninbar.headlineTicker.nextHeadline()", 7000); 
}
