function rssItem(title, link){
	this.title = title;
	this.link = link;
}

// Add version code
function FeedTicker(id, feedkey, inType, inTime, outType, outTime, delay, outputFormat, width, height, fontSize, feedObj) {
	this.id = id;
	this.feedkey = feedkey;
	this.inType = inType;
	this.inTime = parseInt(inTime, 10);
	this.outType = outType;
	this.outTime = parseInt(outTime, 10);
	this.delay = delay;
	this.outputFormat = outputFormat;
	this.width = parseInt(width, 10);
	this.height = parseInt(height, 10);
	this.fontSize = parseInt(fontSize, 10);
	this.feedObj = feedObj;
	
	this.updateDelay = 300000;
	
	this.req = null;
	
	this.feedArray = new Array();
	this.anchorObject = document.getElementById("feedlink"+id);
	this.anchorObject.style.position = "absolute";
	this.anchorObject.style.background = "transparent";
	this.anchorObject.target = "_blank";
	
	this.scrollDiv = document.createElement("div");
	this.scrollDiv.style.display = "none";
	this.anchorObject.parentNode.appendChild(this.scrollDiv);
	
	//this.anchorObject.style.backgroundColor = "#000000";
	
	this.center = Math.floor((this.height/2)-((this.fontSize+4)/2));	// Four is the magic number!

	this.resetTicker();	
}
	
	FeedTicker.prototype.switchTransition = function(){
		//document.getElementById("epginfo").innerHTML += "<br>switchTransition, inMode: "+this.inMode;
		
		if(this.inMode)
			this.currentStory = ++this.currentStory%this.feedArray.length;
		
		//document.getElementById("epginfo").innerHTML = "feedArray.length: "+this.feedArray.length+" currentStory: "+this.currentStory;
		
		this.title = this.feedArray[this.currentStory].title;
		
		this.currentTop = this.center;
		this.anchorObject.style.top = this.currentTop+"px";
		
		this.opacity = 100;
			
		this.anchorObject.style.opacity = this.opacity/100;
		this.anchorObject.style.MozOpacity = this.opacity/100;
		this.anchorObject.style.KHTMLOpacity = this.opacity/100;
		this.anchorObject.style.filter = "alpha(opacity="+(this.opacity)+");";
						
		switch(this.inMode ? this.inType.toLowerCase() : this.outType.toLowerCase()){
			case "none":
				this.noneTicker(this);
				break;
			case "typewriter":
				if(this.inMode)
					this.currentLength = 0;
				else
					this.currentLength = this.title.length;
					
				this.typewriterTicker(this);
				break;
			case "up":
				if(this.inMode){
					this.currentTop = this.height;
				}
				this.anchorObject.style.top = this.currentTop+"px";
								
				this.upTicker(this);
				break;
			case "down":
				if(this.inMode){
					this.currentTop = -this.height;
				}
				this.anchorObject.style.top = this.currentTop+"px";
				
				this.downTicker(this);
				break;
			case "fade":
				if(this.inMode)
					this.opacity = 0;		
				
				this.anchorObject.style.opacity = this.opacity/100;
				this.anchorObject.style.MozOpacity = this.opacity/100;
				this.anchorObject.style.KHTMLOpacity = this.opacity/100;
				this.anchorObject.style.filter = "alpha(opacity="+(this.opacity)+");";	
				
				this.fadeTicker(this);
				break;
			case "scroll":// Somewhat hacky, maybe find a better way...
				this.anchorObject.style.display = "none";
				this.scrollDiv.style.display = "block";
				this.scrollDiv.style.position = "absolute";
				this.scrollDiv.style.height = this.height + "px";
				this.scrollDiv.style.left = this.scrollDiv.parentNode.clientWidth + "px";
				this.scrollDiv.style.width = "5000px";
								
				var width = 0;
				var links = new Array();
				for(var i=0; i<this.feedArray.length; ++i){
					var link = document.createElement("a");
					link.href = this.feedArray[i].link;
					link.innerHTML = this.feedArray[i].title;
					link.style.position = "absolute";
					link.target = "_blank";
					
					this.scrollDiv.appendChild(link);
					var w = link.clientWidth;
					link.style.width = w + "px";
					link.style.left = width + "px";
					width += w+100;
					this.scrollDiv.removeChild(link);
					links.push(link);
				}
				
				for(var i=0; i<links.length; ++i){
					this.scrollDiv.appendChild(links[i]);
				}
				this.scrollDiv.style.width = width + "px";
				
				this.scrollTicker(this);
				break;
		}
	}
	
	FeedTicker.prototype.noneTicker = function(){
		this.anchorObject.href = this.feedArray[this.currentStory].link;
		
		if(this.inMode){
			this.anchorObject.innerHTML = this.feedArray[this.currentStory].title;
			this.inMode = false;
			this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", this.delay);
		}
		else{
			this.anchorObject.innerHTML = "";
			this.inMode = true;
			this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", 0);
		}
	}
		
	FeedTicker.prototype.typewriterTicker = function(){
		this.anchorObject.href = this.feedArray[this.currentStory].link;	
				
		if(this.inMode){
			if(this.currentLength < this.title.length){
				this.currentLength++;
				this.anchorObject.innerHTML = this.title.substring(0, this.currentLength);
				this.timeOut = setTimeout(this.feedObj+".typewriterTicker("+this.feedObj+")", this.inTime);
			}
			else{
				this.inMode = false;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", this.delay);
			}
		}
		else{
			if(this.currentLength > 0){
				this.currentLength--;
				this.anchorObject.innerHTML = this.title.substring(0, this.currentLength);
				this.timeOut = setTimeout(this.feedObj+".typewriterTicker("+this.feedObj+")", this.outTime);
			}
			else{
				this.anchorObject.innerHTML = "";
				this.inMode = true;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", 0);
			}
		}
	}
	
	FeedTicker.prototype.upTicker = function(){
		this.anchorObject.href = this.feedArray[this.currentStory].link;	
						
		if(this.inMode){
			if(this.currentTop > this.center){
				this.anchorObject.innerHTML = this.title;
				
				this.currentTop--;
				this.anchorObject.style.top = this.currentTop+"px";
				
				this.timeOut = setTimeout(this.feedObj+".upTicker("+this.feedObj+")", this.inTime);
			}
			else{
				this.inMode = false;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", this.delay);
			}
		}
		else{
			if(this.currentTop + this.height-this.center > 0){
				this.anchorObject.innerHTML = this.title;

				this.currentTop--;
				this.anchorObject.style.top = this.currentTop+"px";
				
				this.timeOut = setTimeout(this.feedObj+".upTicker("+this.feedObj+")", this.outTime);
			}
			else{
				this.inMode = true;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", 0);
			}
		}
	}
	
	FeedTicker.prototype.downTicker = function(){
		this.anchorObject.href = this.feedArray[this.currentStory].link;	
		
		if(this.inMode){
			if(this.currentTop < this.center){
				this.anchorObject.innerHTML = this.title;
				
				this.currentTop++;
				this.anchorObject.style.top = this.currentTop+"px";
				
				this.timeOut = setTimeout(this.feedObj+".downTicker("+this.feedObj+")", this.inTime);
			}
			else{
				this.inMode = false;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", this.delay);
			}
		}
		else{
			if(this.currentTop < this.height){
				this.anchorObject.innerHTML = this.title;
	
				this.currentTop++;
				this.anchorObject.style.top = this.currentTop+"px";
				
				this.timeOut = setTimeout(this.feedObj+".downTicker("+this.feedObj+")", this.outTime);
			}
			else{
				this.inMode = true;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", 0);
			}
		}
	}
	
	FeedTicker.prototype.fadeTicker = function(){
		this.anchorObject.href = this.feedArray[this.currentStory].link;
		
		if(this.inMode){
			if(this.opacity < 100){
				this.anchorObject.innerHTML = this.title;
				this.opacity += 10;
				
				//document.getElementById("epginfo").innerHTML = this.opacity;
				
				this.anchorObject.style.opacity = this.opacity/100;
				this.anchorObject.style.MozOpacity = this.opacity/100;
				this.anchorObject.style.KHTMLOpacity = this.opacity/100;
				this.anchorObject.style.filter = "alpha(opacity="+(this.opacity)+");";
				
				
				this.timeOut = setTimeout(this.feedObj+".fadeTicker("+this.feedObj+")", this.inTime);
			}
			else{
				this.inMode = false;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", this.delay);
			}
		}
		else{
			if(this.opacity > 0){
				this.anchorObject.innerHTML = this.title;
				this.opacity -= 10;
												
				//document.getElementById("epginfo").innerHTML = this.opacity;
				
				this.anchorObject.style.opacity = this.opacity/100;
				this.anchorObject.style.MozOpacity = this.opacity/100;
				this.anchorObject.style.KHTMLOpacity = this.opacity/100;
				this.anchorObject.style.filter = "alpha(opacity="+(this.opacity)+");";
							
				
				
				this.timeOut = setTimeout(this.feedObj+".fadeTicker("+this.feedObj+")", this.outTime);
			}
			else{
				this.inMode = true;
				this.timeOut = setTimeout(this.feedObj+".switchTransition("+this.feedObj+")", this.delay);
			}
		}
	}
	
	FeedTicker.prototype.scrollTicker = function(){
		//alert(parseInt(this.scrollDiv.style.left, 10) + "  " + -this.scrollDiv.clientWidth);
		if(parseInt(this.scrollDiv.style.left, 10) > -this.scrollDiv.clientWidth){
			this.scrollDiv.style.left = (parseInt(this.scrollDiv.style.left, 10) - 1) + "px";
			this.timeOut = setTimeout(this.feedObj+".scrollTicker("+this.feedObj+")", 10);
		}
		else{
			this.switchTransition();
		}
	}
	
	FeedTicker.prototype.resetTicker = function(){
		this.anchorObject.style.left = "0px";
		this.anchorObject.style.top = "0px";
		//this.anchorObject.style.width = this.width+"px";
		this.anchorObject.style.height = this.height+"px";
	
			
		this.timeOut = null;
		
		this.currentStory = -1;
		this.inMode = true;
		this.timeout = 0;
		
		// Typewriter
		this.currentLength = 0;
		this.title = "";
		
		// Up and down
		this.currentTop = this.center;
		this.anchorObject.style.top = this.center+"px";
			
		// Fade
		this.opacity = 100;
	}
	
	FeedTicker.prototype.updateFeeds = function(){
		clearTimeout(this.timeOut);
		this.resetTicker();
		this.startFeed(false);
	}
	
	FeedTicker.prototype.processResponse = function(){
		var xmlobject;
		if(4 == this.req.readyState){
			if(200 == this.req.status){
				var res = this.req.responseText;
				if(res != "" && res != null){
					var stringDelimiter = res.charAt(0);
					var lineDelimiter = res.charAt(1);
					res = res.slice(2);
					
					var iarray = res.split(lineDelimiter);
					var tarray;
					for(var i=0; i<iarray.length; ++i){
						tarray = iarray[i].split(stringDelimiter);
					
						var t = this.outputFormat.replace(/###title###/i, tarray[0]);
						t = t.replace(/###date###/i, tarray[2]);
						this.feedArray.push(new rssItem(t, tarray[1]));
					}
					
					setTimeout(this.feedObj+".updateFeeds()", this.updateDelay);
					this.switchTransition();
				}
			}
		}
	}
	
	FeedTicker.prototype.startFeed = function(preview){
		//var me = this;
		try{
			this.req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(oc){
				this.req = null;
			}
		}
		
		if(!this.req && typeof XMLHttpRequest!="undefined"){
			this.req = new XMLHttpRequest();
		}
		
		if(this.req!=null){
			var me = this;
			if(true == preview){
				for(var i=0; i<5; ++i){
					var t = this.outputFormat.replace(/###title###/i, "Title"+i);
					t = t.replace(/###date###/i, "01.01.07 00:00:00");
					this.feedArray.push(new rssItem(t, "http://www.vg.no"));
				}
				this.switchTransition();
			}
			else{
				this.req.onreadystatechange = function() {me.processResponse()};
				
				var url = 'http://' + window.location.hostname + ':' + window.location.port + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')+1) + 'FeedManager.aspx?cmd=get&feedkey=';
				
				this.req.open("GET", url + this.feedkey, true)
				
				//this.req.open("GET", "http://localhost:1747/Player/FeedManager.aspx?cmd=get&feedkey="+this.feedkey, true);
				this.req.send(null);
			}
		}	
	}