﻿// Paths

	var URL_USERCOUNT = "UserCount.php?date={0}";
	
// Formats
	
	var COLOR_EI = "#ff7900";
	var COLOR_HEADING_DEFAULT = "#ffffff";
	var COLOR_HEADING_HIGHLIGHT = COLOR_EI;

	var FORMAT_ELLIPSIS = "...";	
	var FORMAT_USERCOUNT = "There are <strong>{0}</strong> active users";	
	
	var FF = navigator.userAgent.indexOf("Firefox")>-1;


// DOM

	var UserCount = null;
	var MainHeading = null;
	var Headings = null;

// Misc

	var headingIndex = -1;
	var prevHeadingIndex = -1;


// Initialize

	function Initialize() {
		InitializeDom();
		PostInitializeDom();
		InitializeFonts();
		InitializeUserCount();
		InitializeHeading();
		InitializeIE();
		InitializeComplete();
	}
	
	function SetupNextHeading() {
		setTimeout("InitializeHeading()", 5000);
	}
	
	function InitializeHeading() {
		if (++headingIndex<Headings.length) {
			var fx = new Fx.Style(Headings[headingIndex], 'color', { duration:2000 });
			fx.addEvent('onComplete', SetupNextHeading);
			fx.start(COLOR_HEADING_HIGHLIGHT);
			
			var fx2 = new Fx.Style(Headings[headingIndex], 'left', { duration:2000 });
			fx2.start(830, -5);
			
			if (prevHeadingIndex>-1) {
				var fx3 = new Fx.Style(Headings[prevHeadingIndex], 'color', { duration:1500 });
				fx3.start(COLOR_HEADING_DEFAULT);
							    
				var fx4 = new Fx.Style(Headings[prevHeadingIndex], 'left', { duration:1500 });
				fx4.start(0, -930);			    
			}
			
			prevHeadingIndex = headingIndex;
			
		} else {
		    headingIndex = -1;
		    setTimeout("InitializeHeading()", 60000);
		
		}
	}
	
	function InitializeUserCount() {
	
		function UserCountSucess() {
			var data = this.response["text"];
			UserCount.setHTML(FORMAT_USERCOUNT.replace(/\{0\}/, data));
			var fx = new Fx.Style(UserCount, 'color', {duration:1000});
			var fx2 = new Fx.Style($$("#UserCount strong")[0], 'color', {duration:1000});
			fx.start('#000000').chain(function() {
				fx2.start(COLOR_EI);
			});
		}
		
		function UserCountSucessFailed() {
			//ErrorHandler("UserCountSucessFailed");
		}
		
		var userCountXHR = new XHR({
			method: 'get',
			onSuccess: UserCountSucess,
			onFailure: UserCountSucessFailed
		});

		userCountXHR.send(URL_USERCOUNT.replace(/\{0\}/, new Date()));
	
	}

	function InitializeFonts() {
		var fontDetector = new FontDetector();
		var fontData = fontDetector.Test("Calibri");
		if (fontData.Detected) {
			var linkElement = new Element("link", {
				'rel': 'stylesheet',
				'type': 'text/css',
				'href': 'EIStyles/BetterFonts.css'
			});
			var head = $$("head")[0];
			head.adopt(linkElement);
		}	
	}
		
	function InitializeDom() {
		UserCount = $("UserCount");
		MainHeading = $$("#Header h1 em")[0];
		Headings = $$("#Header h1 em strong");
	}
	
	function PostInitializeDom(){}	
	function InitializeComplete(){}
	function InitializeIE(){}


// Xml Helper

	function GetNodeData(node, tag) {
		var value = "";
		var tagnode = node.getElementsByTagName(tag);
		if (tagnode.length>0) {
			if (tagnode[0].firstChild) {
				value = tagnode[0].firstChild.nodeValue;
			}
		}
		return value;
	}	

// Error Handling

function ErrorHandler(str) {
	alert (str);
}


window.addEvent("domready", Initialize);
