var g_feedURL = "/servlet/hotnewsrss";

Effect.DefaultOptions.duration=0.3;
NewsTicker=Class.create();
Object.extend(NewsTicker.prototype,{
	tickerDiv:"ticker",
    tickerLocation:"billboard",
    tickerTitle:"news-link",
    tickerLink:"/hotnews/",
    feedURL:g_feedURL,
    pauseLength:3500,
    scrollSpeed: 2,
    pauseScrollStart: 1500,
	timer:0,currentTitle:0,items:null,
	initialize:	function(){
		this.items=[];
		new Ajax.Request(this.feedURL,
			{	
				method:"get",
				onSuccess: function(a){
						this.parseXML(a.responseXML);
						this.buildTicker();
					}.bind(this),
				onFailure: function(){
						console.log("");
					},
				onException: function(b,a){
                }
			});
		},
	buildTicker: function(){
        if(this.items[this.currentTitle]){
            var link = this.items[this.currentTitle]["link"];
            if (link) {
                $(this.tickerTitle).setAttribute("href",link);
            }
//            $(this.tickerTitle).setAttribute("href",this.items[this.currentTitle]["link"]);
			$(this.tickerTitle).innerHTML=this.items[this.currentTitle]["title"];
            this.onDataSwitchComplete();
//            this.start();
		}
	},
	parseXML:function(a){
		$A(a.getElementsByTagName("item")).each( 
			function(c){
				title=c.getElementsByTagName("title")[0].childNodes[0].nodeValue;
				var b = "";
                if (c.getElementsByTagName("link")[0].childNodes.length > 0) {
                    b=c.getElementsByTagName("link")[0].childNodes[0].nodeValue;//NewsTicker.tickerLink;
                }
				this.items.push({title:title,link:b})
			}.bind(this))
	},
	start:function(){
        //this.interval=setInterval(this.showNext.bind(this),this.pauseLength)
        this.startTimeout();
	},
/*
    onShowTimeout:function() {
        this.showNext();
        this.startTimeout();
    },
*/
    startTimeout: function() {
        setTimeout(this.showNext.bind(this),this.pauseLength)
    },
	stop:function(){
		clearInterval(this.interval)
	},
	showNext:function(){
		if(this.currentTitle<this.items.length-1){
			this.currentTitle=this.currentTitle+1
		}else{
			this.currentTitle=0
		}
        new Effect.Fade("news-link",{
				afterFinish:function(){this.switchData();
				new Effect.Appear("news-link")}.bind(this)
			})
	},
	switchData:function(){
        var s = $("news-link-div");
        s.scrollLeft = 0;
		if(this.items[this.currentTitle]){
            if (this.items[this.currentTitle]["link"]) {
                $(this.tickerTitle).setAttribute("href",this.items[this.currentTitle]["link"]);//this.tickerLink
            } else {
                $(this.tickerTitle).removeAttribute("href");
            }
			$(this.tickerTitle).innerHTML=this.items[this.currentTitle]["title"]+ "&nbsp;&nbsp;&nbsp;";
		}
        this.onDataSwitchComplete();
	},

    onDataSwitchComplete: function() {
        var a_width = getElementWidth("news-link");
        var s_width = getElementWidth("news-link-div");
        var s = $("news-link-div");
//dono why, but s.scrollWidth is zero to this moment!!! later it becames normal 
        var s_scrollWidth  = this.items[this.currentTitle]["title"].length * 5;
        if (s_scrollWidth > s_width) {
            this.startScrollProc();
        } else {
            this.startTimeout();
        }
    },

    scrollOffset: 0,
    scrollInterval: null,
    startScrollProc: function() {
        this.scrollOffset = 0;
        setTimeout( this.startScroll.bind(this), this.pauseScrollStart);
    },
    startScroll: function() {
        var s = $("news-link-div");
        this.scrollInterval = setInterval(this.makeScrollIteration.bind(this),50);
    },
    makeScrollIteration: function() {
        var a_width = getElementWidth("news-link");
        var s_width = getElementWidth("news-link-div");
        var s = $("news-link-div");
        //HACK in other case dont work in opera
        if (s_width == 0) {
            var s_width  = this.items[this.currentTitle]["title"].length * 5;
        }

        this.scrollOffset += this.scrollSpeed;
        s.scrollLeft = this.scrollOffset;
        if (this.scrollOffset >= s.scrollWidth - s_width) {
            this.onScrollComplete();
        }
    },
    onScrollComplete: function() {
        clearInterval(this.scrollInterval);
        this.startTimeout();
    }
});

Event.observe(window,"load",function(){var a=new NewsTicker()});


function getElementWidth(Elem) {
    var ns4 = false;
    var op5 = (getNameBrowser() == "opera");
	op5=false;
    if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else 
        {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function getNameBrowser() {
 var ua = navigator.userAgent.toLowerCase();
 if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
   return "msie"
 }
 if (ua.indexOf("opera") != -1) {
   return "opera"
 }
 if (ua.indexOf("gecko") != -1) {
   return "gecko";
 }
 if (ua.indexOf("safari") != -1) {
   return "safari";
 }
 if (ua.indexOf("konqueror") != -1) {
   return "konqueror";
 }
 return "unknown";
} 