"use strict";

// CAN BE USED FOR IE BROWSERS TO DETERMINE IF THE SITE IS LOADED (FOR FF BROWSERS THIS CAN BE SET
// TO FALSE)
var bLoaded	= true;

var rComL = new Number(0);
var rModL = new Number(0);
var rClaL = new Number(0);

//
// Until further notice the links are defined at this place
// as there seems to be a major problem with loading them from an full URL
//
var xmlData = "<div class=\"message\">XXXCOML0 XXXCOMN0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXMODL0 XXXMODN0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXCLAL0 XXXCLAN0</div><div class=\"message\">XXXCOML0 XXXCOMN0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXMODL0 XXXMODN0&nbsp;|&nbsp;XXXMODL1 XXXMODN1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXCLAL0 XXXCLAN0</div><div class=\"message\">XXXCOML0 XXXCOMN0&nbsp;|&nbsp;XXXCOML1 XXXCOMN1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXMODL0 XXXMODN0&nbsp;|&nbsp;XXXMODL1 XXXMODN1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXCLAL0 XXXCLAN0</div><div class=\"message\">XXXCOML0 XXXCOMN0&nbsp;|&nbsp;XXXCOML1 XXXCOMN1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXMODL0 XXXMODN0&nbsp;|&nbsp;XXXMODL1 XXXMODN1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XXXCLAL0 XXXCLAN0&nbsp;|&nbsp;XXXCLAL1 XXXCLAN1</div>";

// -------------------------------------------------------------------
// RETREIVES THE MINIMUM SIZE OF THE ELEMENT OR THE WINDOW SIZE DEPENDING ON WHICH VALUE IS SMALLER
// -------------------------------------------------------------------
function getWindowWidth()
{
	//
	// SIZE OF THE TABLE OF THE COMMUNITY BAR
	//
	var nElementSize = document.getElementById("flc_table").clientWidth;

	//
	// ALL EXCEPT EXPLORER
	//
	if (self.outerWidth)
	{
		if (self.outerWidth < nElementSize)
		{
			return self.outerWidth;
		}
		else
		{
			return nElementSize;
		}
	}

	//
	// EXPLORER 6 STRICT MODE
	//
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		if (document.documentElement.clientWidth < nElementSize)
		{
			return document.documentElement.clientWidth;
		}
		else
		{
			return nElementSize;
		}
	}

	//
	// OTHER EXPLORERS
	//
	else if (document.body)
	{
		if (document.body.clientWidth < nElementSize)
		{
			return document.body.clientWidth;
		}
		else
		{
			return nElementSize;
		}
	}

	return -1;
}

// -------------------------------------------------------------------
// USED TO REPLACE THE IDENTIFIERS FROM THE TICKER CONTENT FILE (XMLFILE) WITH THE LINKS
// -------------------------------------------------------------------
function generateDynLinks(x_arCommLinks, x_iArrayIndex, x_szInnerHtml, x_szReplaceLinkElement, x_szReplaceNameElement)
{
	var iNumber = 0, iCnt = 0, iIndex = 0, iListFull = 0, szRegexWhile = new RegExp(x_szReplaceLinkElement), szReplaceString, szRegex, i;

	do
	{
		//
		// CREATE A RANDOM INDEX
		//
		do
		{
			iIndex = parseInt(Math.floor(Math.random() * x_arCommLinks.length), 10);
		}
		while ((iIndex === x_iArrayIndex) || (x_arCommLinks[iIndex].status === 1));
		x_iArrayIndex = iIndex;
		x_arCommLinks[iIndex].status = 1;

		//
		// MARK EVERY LINK AS DISPLAYED SO NO LINK GETS DISPLAYED BEFORE EVERY LINK FROM THE ARRAY WAS SHOWN AT LEAST ONCE
		//
		iListFull = 0;
		for (i = 0; i < x_arCommLinks.length; i += 1)
		{
			if (x_arCommLinks[i].status === 1)
			{
				iListFull += 1;
			}
		}

		if (iListFull === x_arCommLinks.length)
		{
			for (i = 0; i < x_arCommLinks.length; i += 1)
			{
				x_arCommLinks[i].status = 0;
			}
		}

		//
		// CREATE NUMBER TO GENERATE A STRING INFORMATION
		//
		iNumber	 = new Number(iCnt);

		//
		// REGEX EXPRESSION FOR REPLACEMENT FOR THE LINK
		//
		szReplaceString = new String();
		szReplaceString = szReplaceString.concat(x_szReplaceLinkElement, iNumber.toString());
		szRegex = new RegExp(szReplaceString);

		//
		// REPLACE THE SEARCH STRING BY THE LINK
		//
		x_szInnerHtml = x_szInnerHtml.replace(szRegex, '<a href="' + encodeURI(x_arCommLinks[iIndex].url) + '" target="_blank">');

		//
		// EMPTY THE STRING
		//
		szReplaceString = new String();

		//
		// REGEX EXPRESSION FOR REPLACEMENT FOR THE LINK NAME
		//
		szReplaceString = szReplaceString.concat(" ", x_szReplaceNameElement, iNumber.toString());
		szRegex = new RegExp(szReplaceString);

		//
		// REPLACE THE SEARCH STRING BY THE LINK NAME
		//
		x_szInnerHtml = x_szInnerHtml.replace(szReplaceString, x_arCommLinks[iIndex].name + '</a>');

		//
		// INCREASE COUNTER
		//
		iCnt += 1;
	}
	while (x_szInnerHtml.match(szRegexWhile));

	return x_szInnerHtml;
}

// -------------------------------------------------------------------
// CREATES AN AJAX OBJECT
// -------------------------------------------------------------------
function createAjaxObj()
{
	var httprequest = false;
	if (window.XMLHttpRequest)
	{
		//
		// IF MOZILLA, SAFARI ETC
		//
		httprequest = new XMLHttpRequest();
		if (httprequest.overrideMimeType)
		{
			httprequest.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject)
	{
		//
		// IF IE
		//
		try
		{
			httprequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (err1)
		{
			try
			{
				httprequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err2)
			{
			}
		}
	}
	return httprequest;
}

// -------------------------------------------------------------------
// MAIN AJAX TICKER OBJECT FUNCTION
// COMMBARTICKER (XMLFILE, DIVID, DIVCLASS, DELAY, OPTIONALFADEORNOT)
// -------------------------------------------------------------------
function commBarTicker(x_XmlFile, x_DivId, x_DivClass, x_Delay, x_FadeOrNot)
{
	this.xmlfile = x_XmlFile;
	this.tickerid = x_DivId;	// ID OF TICKER DIV TO DISPLAY INFORMATION
	this.delay = x_Delay;	   // DELAY BETWEEN MSG CHANGE, IN MILISECONDS.
	this.mouseoverBol = 0;	  // BOOLEAN TO INDICATE WHETHER MOUSE IS CURRENTLY OVER TICKER (AND PAUSE IT IF IT IS)
	this.pointer = 0;
	this.opacityString = (typeof x_FadeOrNot !== "undefined") ? "width: 100%; filter:progid:DXImageTransform.Microsoft.alpha (opacity = 50); -moz-opacity: 1" : "";
	if (this.opacityString !== "")
	{
		this.delay += 500;	  // ADD 1/2 SEC TO ACCOUNT FOR FADE EFFECT, IF ENABLED
	}
	this.opacitysetting = 0.2;  // OPACITY VALUE WHEN RESET. INTERNAL USE.
	this.messages = [];			// ARRAYS TO HOLD EACH MESSAGE OF TICKER
	this.ajaxobj = createAjaxObj();
	document.write('<div id = "' + x_DivId + '" class = "' + x_DivClass + '"><div style = "' + this.opacityString + '"><center>Initializing Community Ticker...</center></div></div>');
	this.getXMLfile();
}

// -------------------------------------------------------------------
// GETXMLFILE ()- USE AJAX TO FETCH XML FILE (TXT)
// -------------------------------------------------------------------
commBarTicker.prototype.getXMLfile = function ()
{
	if (this.ajaxobj)
	{
		var instanceOfTicker = this;
		var url = this.xmlfile + "?bustcache = " + new Date().getTime();

		if (bLoaded !== true)
		{
			setTimeout(function ()
			{
				instanceOfTicker.getXMLfile();
			}, 1000);
		}
		else
		{
			//this.ajaxobj.open('GET', url, true);

			//this.ajaxobj.onreadystatechange = function ()
			//{
				instanceOfTicker.initialize();
			//};

			//this.ajaxobj.send(null);
		}
	}
};

// -------------------------------------------------------------------
// INITIALIZE ()- INITIALIZE TICKER METHOD.
// -GETS CONTENTS OF XML FILE AND PARSE IT USING JAVASCRIPT DOM METHODS
// -------------------------------------------------------------------
commBarTicker.prototype.initialize = function ()
{
	//if (this.ajaxobj.readyState === 4)
	//{
		//
		// IF REQUEST OF FILE COMPLETED
		//
		//if ((this.ajaxobj.status === 200) || (window.location.href.indexOf("http") === -1))
		//{
			//
			// IF REQUEST WAS SUCCESSFUL
			//
			this.contentdiv = document.getElementById(this.tickerid).firstChild;//div of inner content that holds the messages
			//var xmldata = this.ajaxobj.responseText;
			this.contentdiv.style.display = "none";
			//this.contentdiv.innerHTML = xmldata;
			this.contentdiv.innerHTML = xmlData;


			var curr_domain = document.domain;
			if (curr_domain.indexOf("www.") === 0)
			{
				curr_domain = curr_domain.substring(4);
			}
			var banned = false;
			var ban_id = -1;
			for (var i = 0; i < banned_domains.length; i++)
			{
				if (banned_domains[i] === curr_domain)
				{
					ban_id = i;
					banned = true;
					break;
				}
			}

			//
			// IF NO MESSAGES WERE FOUND OR DOMAIN WAS BANNED
			//
			if ((this.contentdiv.getElementsByTagName("div").length === 0) || (banned === true))
			{
				this.contentdiv.innerHTML = "<strong>Banned:&nbsp;</strong>" + ban_reasons[ban_id];
				this.contentdiv.style.display = "block";
				return;
			}

			var instanceOfTicker = this;
			document.getElementById(this.tickerid).onmouseover = function ()
			{
				instanceOfTicker.mouseoverBol = 1;
			};
			document.getElementById(this.tickerid).onmouseout = function ()
			{
				instanceOfTicker.mouseoverBol = 0;
			};

			//
			// CLEAN UP LOOSE REFERENCES IN IE
			//
			if (window.attachEvent)
			{
				window.attachEvent("onunload", function ()
				{
					instanceOfTicker.contentdiv = instanceOfTicker.ajaxobj = null;
				});
			}

			//
			// CYCLE THROUGH XML OBJECT AND STORE EACH MESSAGE INSIDE ARRAY
			//
			for (i = 0; i < this.contentdiv.getElementsByTagName("div").length; i += 1)
			{
				if (this.contentdiv.getElementsByTagName("div")[i].className === "message")
				{
					this.messages[this.messages.length] = this.contentdiv.getElementsByTagName("div")[i].innerHTML;
				}
			}
			this.contentdiv.innerHTML = "";
			this.contentdiv.style.display = "block";
			this.rotatemsg();
		//}
	//}
};

// -------------------------------------------------------------------
// ROTATEMSG ()- ROTATE THROUGH TICKER MESSAGES AND DISPLAYS THEM
// -------------------------------------------------------------------
commBarTicker.prototype.rotatemsg = function ()
{
	var instanceOfTicker = this;

	//
	// IF MOUSE IS CURRENTLY OVER TICKER, DO NOTHING (PAUSE IT)
	//
	if (this.mouseoverBol === 1)
	{
		setTimeout(function ()
		{
			instanceOfTicker.rotatemsg();
		}, 100);
	}

	//
	// ELSE, CONSTRUCT ITEM, SHOW AND ROTATE IT!
	//
	else
	{
		if (this.opacitysetting >= 1)
		{
			this.fadetimer2 = setInterval(function ()
			{
				instanceOfTicker.fadetransition('down', 'fadetimer2');
			}, 100);

			//
			// UPDATE CONTAINER PERIODICALLY
			//
			setTimeout(function ()
			{
				this.timeOutId = instanceOfTicker.rotatemsg();
			}, 1100);
		}
		else
		{
			//
			// FADE EFFECT- RESET OPACITY
			//
			this.fadetransition("reset");

			//
			// CALCULATE THE NUMBER OF LINKS TO DISPLAY
			// 250px IS THE SIZE OF THE COMMUNITY DROP DOWN IMAGE
			// 200px IS THE SIZE RESERVED FOR ONE (1) LINK
			//
			var xDimension = getWindowWidth();
			var nCountDisplay = parseInt(((xDimension - 250) / 200), 10);

			//
			// BASED ON THE SIZE SELECT THE TICKER MESSAGE TO DISPLAY
			//
			if (nCountDisplay <= 3)
			{
				this.contentdiv.innerHTML = this.messages[0];
			}
			else if ((nCountDisplay > 3) && (nCountDisplay <= 4))
			{
				this.contentdiv.innerHTML = this.messages[1];
			}
			else if ((nCountDisplay > 4) && (nCountDisplay <= 5))
			{
				this.contentdiv.innerHTML = this.messages[2];
			}
			else
			{
				this.contentdiv.innerHTML = this.messages[3];
			}

			//
			// REPLACE THE IDENTIEFIER IN THE TICKER MESSAGE WITH THE APPROPRIATE LINKS
			//
			this.contentdiv.innerHTML = generateDynLinks (community_links, rComL, this.contentdiv.innerHTML, "XXXCOML", "XXXCOMN");
			this.contentdiv.innerHTML = generateDynLinks (servermod_links, rModL, this.contentdiv.innerHTML, "XXXMODL", "XXXMODN");
			this.contentdiv.innerHTML = generateDynLinks (clan_links, rClaL, this.contentdiv.innerHTML, "XXXCLAL", "XXXCLAN");

			//
			// RELACE SPECIAL HTML CHARACTERS
			//
			this.contentdiv.innerHTML.replace(/ö/, '&ouml;');
			this.contentdiv.innerHTML.replace(/ä/, '&auml;');
			this.contentdiv.innerHTML.replace(/ü/, '&Uuml;');
			this.contentdiv.innerHTML.replace(/Ö/, '&Ouml;');
			this.contentdiv.innerHTML.replace(/Ä/, '&Auml;');
			this.contentdiv.innerHTML.replace(/Ü/, '&Uuml;');
			this.contentdiv.innerHTML.replace(/ß/, '&szlig;');
			this.contentdiv.innerHTML.replace(/</, '&lt;');
			this.contentdiv.innerHTML.replace(/>/, '&gt;');

			//
			// FADE EFFECT- PLAY IT
			//
			this.fadetimer1 = setInterval(function ()
			{
				instanceOfTicker.fadetransition('up', 'fadetimer1');
			}, 100);

			//
			// UPDATE CONTAINER PERIODICALLY
			//
			setTimeout(function ()
			{
				this.timeOutId = instanceOfTicker.rotatemsg();
			}, this.delay);
		}
	}
};

// -------------------------------------------------------------------
// FADETRANSITION ()- CROSS BROWSER FADE METHOD FOR IE5.5+ AND MOZILLA/FIREFOX
// -------------------------------------------------------------------
commBarTicker.prototype.fadetransition = function (fadetype, timerid)
{
	var contentdiv = this.contentdiv;
	if (fadetype === "reset")
	{
		this.opacitysetting = 0.2;
	}

	if ((contentdiv.filters) && (contentdiv.filters[0]))
	{
		if (typeof contentdiv.filters[0].opacity === "number") //IE6+
		{
			contentdiv.filters[0].opacity = this.opacitysetting * 100;
		}
		else //IE 5.5
		{
			contentdiv.style.filter = 'alpha (opacity = ' + this.opacitysetting * 100 + ')';
		}
	}
	else if (typeof contentdiv.style.MozOpacity !== "undefined" && this.opacityString !== "")
	{
		contentdiv.style.MozOpacity = this.opacitysetting;
	}
	else  // chrome
	{
		contentdiv.style.opacity = this.opacitysetting;
	}

	if (fadetype === "up")
	{
		this.opacitysetting += 0.1;
	}

	if (fadetype === "up" && this.opacitysetting >= 1)
	{
		clearInterval(this[timerid]);
	}

	if (fadetype === "down")
	{
		this.opacitysetting -= 0.1;
	}

	if (fadetype === "down" && this.opacitysetting <= 0.2)
	{
		clearInterval(this[timerid]);
	}
};

// -------------------------------------------------------------------
// This code is a mix of codes form http://www.dynamicdrive.com/
// It was extended to correlate with the initial requirement
// All changes are done by Huor Súrion
// For any concerns or feedback please contact admin.fl@trf.mc-sig.de
// -------------------------------------------------------------------
