var ticker = {

	// Buffer of results.
	buffer: new Array(),
	
	// Visible html nodes.
	items: new Array(),

	// Time of first event.
	firstTime: 0,
	
	// Number of visible items.
	visibleItems: 5,
	
	// Item height.
	itemHeight: 52,
	
	// Ticker width.
	tickerWidth: 297,
	
	// Seconds since start.
	secondsSinceStart: 0,
	
	// Id of latest item.
	lastId: 0,
	lastProductId: 0,
	
	// Avoid simultaneous buffer updates.
	loadLock: false,
	
	// Result count.
	count: 27000000,

	// First result count
	firstResultCount: true,
	
	//ProductType
	productType: "",

	// Nodes.
	baseNode: "",
	scrollBase: "",
	scrollNode: "",

	log: function(msg) {
		//console.log(msg);
	},
	
	init: function() {
	
		// Quit, if ticker div doesn't exists.
		if ($("#ticker").length == 0) {
			return false;
		}

		var tickervisibileitems = $("#ticker").attr("visibleitems");
		if($("#ticker").attr("visibleitems") != undefined){
			ticker.visibleItems = Number($("#ticker").attr("visibleitems"));
		}

		// Create initial layout.
		this.baseNode = $("<div></div>");
		this.scrollBase = $("<div></div>");
		this.scrollNode = $("<div></div>");

		// Set height of the ticker.
		$(this.baseNode).css("width", this.tickerWidth + "px"); 
		$(this.baseNode).css("height", (this.itemHeight * this.visibleItems) + "px"); 
		$(this.baseNode).css("overflow", "hidden"); 
		$(this.baseNode).css("position", "relative"); 
	
		$(this.scrollBase).css("width", this.tickerWidth + "px"); 
		$(this.scrollBase).css("height", (this.itemHeight * this.visibleItems) + "px"); 
		$(this.scrollBase).css("position", "relative"); 
	
		// Create inner.
		$(this.scrollNode).css("width", this.tickerWidth + "px"); 
		$(this.scrollNode).css("position", "absolute"); 
		
		$("#ticker").append($(this.baseNode).append($(this.scrollBase).append(this.scrollNode)));
	
		$("#tickerContainer").show();

		// Handle buffer.
		var self = this;
		setInterval(function() {
		
			// Increase counter.
			self.secondsSinceStart++;

			ticker.log("Consume buffer: " + self.buffer.length);
		
			// Refill buffer before items runs out.
			if (self.buffer.length < 3 && self.secondsSinceStart % 10 < 5) {
				self.loadData(self);
			}
			
			// If this is the first item, save timestamp.
			if (self.firstTime == 0) {
				if (self.buffer.length == 0) {
					return;
				} else {

					// If stage is empty, add five projects immediately.
					for (var i = 0; i < self.visibleItems; i++) {
						if (self.buffer.length > 0) {
							var item = self.buffer.shift();
							self.animate(self, item, true);
						}
					}
					
					//TeroV iffi
					if (self.buffer.length == 0) {
						return;
					}

					self.firstTime = self.buffer[0].time;
					
				}
			}

			// Quit if buffer is empty.
			if (self.buffer.length == 0) {
				return;
			}
			
			// Consume buffer.
			ticker.log("Time to next item: " + (self.buffer[0].time - self.firstTime - self.secondsSinceStart));
			if (self.buffer[0].time - self.firstTime  < self.secondsSinceStart) {
			
				// Take first.
				var item = self.buffer.shift();
				
				// Add to stage.
				self.animate(self, item, false);
			
			}
			
		}, 1000);
	
	},
	
	/**
	 * Animate new item.
	 */
	animate: function(self, item, skipAnimation) {
	
		ticker.log("Animate.");

		var src = $("<div class=\"item\">"
						+ "<div class=\"borderHorizontal\"></div>"
						+ "<div class=\"itemContent\">"
							+ "<div class=\"padding\">"
								+ "<div class=\"scoreArea\">"
									+ "<div class=\"score\">" + item.score + "</div>"
									+ "<div class=\"product\">" + item.product + "</div>"
								+ "</div>"
								+ "<div class=\"infoArea\"><div>"
									+ item.cpu + " " + item.cpuSpeed + " " +item.cpuSpeedType + "<br />"
									+ item.gpu
								+ "</div></div>"
								+ "<div style=\"clear:both\"></div>"
							+ "</div>"
						+ "</div>"
					+ "</div>");
					
		// Add new item to list.	
		self.items.unshift(src);
					
		// Add new item.
		$(self.scrollNode).prepend(src);
	
		// Animate.
		if (!skipAnimation) {
		
			// Move scroller upwards.
			$(self.scrollNode).css("top", "-" + self.itemHeight + "px");

			// Animate.
			$(self.scrollNode).animate({
				top: 0
			}, 500);
			
		}
		
		// Hide last item.
		ticker.log("Type: " + typeof(self.items[self.visibleItems - 1]));
		ticker.log("Items: " + self.items.length);

		if (typeof(self.items[self.visibleItems - 1]) != "undefined") {
			$(self.items[self.visibleItems - 1]).fadeOut();
		}
		
		// Increase counter.
		self.count++;
		
		// Output.
		var numbers = (self.count + "").split("");
		var countString = "";
		var k = 0;
		for (var i = numbers.length - 1; i >= 0; i--) {
			countString = numbers[i] + countString;
			if (k % 3 == 2) countString = "," + countString;
			k++;
		}
		$("#resultDisplay").html(countString + " benchmark results and counting...");

		//Benchmarks page
		var resultDisplay2 ="<div class=\"MainInfoBox1\" style=\"margin: 50px 0px 2px;\">"
				+ "<div class=\"MainInfoBox2\" style=\"padding: 3px;\">"
				+ "<div><b>Benchmark results overall: </b>"
				+ "<div style=\"color: #fd6f13; padding: 0px; text-align: left;\">"
				+ countString + "</div></div></div></div>";
		$("#resultDisplay2").html(resultDisplay2);

		//Hardware page
		var resultDisplay3 = "<b>Benchmark Results:</b><br /><div class=\"orange\">" + countString + "</div>";
		$("#resultDisplay3").html(resultDisplay3);
	
	},
	
	/**
	 * Load data from xml.
	 */
	loadData: function(obj) {
	
		// Avoid running two updates simultaneously.
		if (obj.loadLock) return false;
		
		// Enable lock.
		obj.loadLock = true;

		ticker.log("Loading...");
		
		// Request new items (newest first).
		$.ajax({
			type: "GET",
			url: "/ticker.xml?lastid=" + obj.lastId + "&lasttype=" + obj.lastProductId + "&r=" + Math.random(), 
			success: obj.parseXml,
			dataType: "xml",
			self: obj
		});
	
	},
	
	parseXml: function(xml, status) {
					
		ticker.log("Parsing...");
		
		// Get object instance.
		var self = this.self;
		
		// New set of items.
		var newBuffer = new Array();
		
		// Setting total count
		if( self.firstResultCount ) {
			var totalProjects = xml.getElementsByTagName('Total_Projects');
			if( totalProjects.length > 0 ) {
				self.count = totalProjects.item(0).firstChild.nodeValue;
			}
			self.firstResultCount = false;
		}
		
		var results = xml.getElementsByTagName('item');
		for (var i = 0; i < results.length; i++) {

			var nodeName = "";
			var score = "";
			var product = "";
			var cpu = "";
			var cpuSpeed = "";
			var cpuSpeedType = "MHz";
			var gpu = "";
			var time = "";
			var id = "";
			var productId = "";
			
			for (var k = 0; k < results.item(i).childNodes.length; k++) {
			

				nodeName = results.item(i).childNodes.item(k).nodeName;
				if (nodeName == "score") {
					score = results.item(i).childNodes.item(k).firstChild.nodeValue;
				}
				if (nodeName == "product") {
					product = results.item(i).childNodes.item(k).firstChild.nodeValue;
				}
				if (nodeName == "cpu") {
					cpu = results.item(i).childNodes.item(k).firstChild.nodeValue;
				}
				if (nodeName == "cpuspeed") {
					cpuSpeed = results.item(i).childNodes.item(k).firstChild.nodeValue;
				}
				if (nodeName == "gpu") {
					gpu = results.item(i).childNodes.item(k).firstChild.nodeValue;
				}
				if (nodeName == "time") {
					time = Math.round(results.item(i).childNodes.item(k).firstChild.nodeValue / 1000);
				}
				if (nodeName == "id") {
					id = results.item(i).childNodes.item(k).firstChild.nodeValue;
				}
				if (nodeName == "productid") {
					productId = results.item(i).childNodes.item(k).firstChild.nodeValue;
				}
			}
			
			
			// Create result.
			var result = {
				"score": score,
				"product": product,
				"cpu": cpu,
				"cpuSpeed": cpuSpeed,
				"cpuSpeedType": cpuSpeedType,
				"gpu": gpu,
				"time": time,
				"id": id,
				"productId": productId
			};
			
			// Add result to the first place.
			if( self.productType == "" ) {
				newBuffer.unshift(result);
			} else if( self.productType == productId ) {
				newBuffer.unshift(result);	
			}
			
			
			ticker.log("Result: time " + time + "score" + score + " " + product + " " + cpu + " " + cpuSpeed + " " + gpu);
	
		}	

		// Save the id of the newest item.
		if (newBuffer.length != 0) {
			self.lastId = newBuffer[newBuffer.length - 1].id;
			self.lastProductId = newBuffer[newBuffer.length - 1].productId;
		}

		// Add new items to buffer.
		self.buffer = self.buffer.concat(newBuffer);

		// Release lock.
		self.loadLock = false;
		
	}
	
			

}
