// The default xml configuration file. Swapped by domain (com, uk, fr, de, etc)
railbugs.xmlFilename = "resources_com.xml";
var domainStart = document.location.hostname.lastIndexOf('.',document.location.hostname.length);
if (domainStart !== -1) {
	var domain = document.location.hostname.substring(domainStart+1, document.location.hostname.length);
	railbugs.xmlFilename = "resources_"+domain+".xml";
}
//EMEA Additions to the RailBugs Object, to show tabulated versions.
RailBugs.prototype.showAllByTag = function()
{
	var bugs = new Array();
	var found = false;
	
	for(var i = 0; i < this.bugs.length; i++)
	{
		var groups = this.getGroups(this.bugs[i]);
	
		if(this.isSubset(this.showAllByTag.arguments, groups)) 
		{
			bugs.push(this.bugs[i]);
		}
	}
	if (bugs.length > 0)
	{
		this.tableBugs(bugs);
	} 
	else
	{
		this.tableBugs(this.bugs)
	}
}

RailBugs.prototype.tableInfo = function() 
{
	var releventBugs, bigInfo = new Array();
	var releventBugs = this.tableInfo.arguments[0];

	for(var i = 0; i < releventBugs.length; i++)
	{
		var row = new Array();
		row.push(this.getValue(releventBugs[i], 'formtitle', '&nbsp;'));
		row.push(this.getValue(releventBugs[i], 'calltoaction', '&nbsp;'));
		row.push(this.getValue(releventBugs[i], 'analyst', '&nbsp;'));
		row.push(this.getValue(releventBugs[i], 'compurl', 'null'));
		row.push(this.getValue(releventBugs[i], 'template', 'null'));
		row.push(releventBugs[i].getAttribute('id'));
		row.push(this.getValue(releventBugs[i], 'assetorder', 'null'));
		row.push(this.getGroups(releventBugs[i]));
		bigInfo.push(row);
	}
	bigInfo.sort(sortById);
	return bigInfo;
}

function sortById(a, b)
{
  function chunkify(t) {
    var tz = [], x = 0, y = -1, n = 0, i, j;

    while (i = (j = t.charAt(x++)).charCodeAt(0)) {
      var m = (i == 46 || (i >=48 && i <= 57));
      if (m !== n) {
        tz[++y] = "";
        n = m;
      }
      tz[y] += j;
    }
    return tz;
  }
	//assetorder is 7th element in the array:
  var aa = chunkify(a[6]);
  var bb = chunkify(b[6]);

  for (x = 0; aa[x] && bb[x]; x++) {
    if (aa[x] !== bb[x]) {
      var c = Number(aa[x]), d = Number(bb[x]);
      if (c == aa[x] && d == bb[x]) {
        return c - d;
      } else return (aa[x] > bb[x]) ? 1 : -1;
    }
  }
  return aa.length - bb.length;
}

RailBugs.prototype.tableBugs = function()
{
	var bugs = this.tableBugs.arguments[0];
	var bugInfo = this.tableInfo(bugs);
	var typeChangeFlag = 0;
	var hrefText = '';
	var src = "";
	src += '<table id="tablebugs">';
	for(var i=0; i<bugInfo.length; i++)
	{
		if (bugInfo[i][6] != typeChangeFlag)
		{
			src += '<tr><td class="header" colspan="2">'+bugInfo[i][7][0]+'</td></tr>';
			typeChangeFlag = 	bugInfo[i][6];
		}
		hrefText = bugInfo[i][3];
		if (bugInfo[i][4] == 'downloadphp') {
			hrefText = 'javascript:railbugs.phpformsHandler('+bugInfo[i][5]+')" onclick="pageTracker._trackPageview(\'/qfview\');';
		} 
		if (bugInfo[i][4] == 'downloadqf') {
			hrefText = 'javascript:railbugs.quickformsHandler('+bugInfo[i][5]+')" onclick="pageTracker._trackPageview(\'/qfview\');';
		}
		if (bugInfo[i][4] == 'window') {
			hrefText = 'javascript:railbugs.windowHandler('+bugInfo[i][5]+')';
		}
		if (bugInfo[i][4] == 'img_window') {
			hrefText = 'javascript:railbugs.windowHandler('+bugInfo[i][5]+')" onclick="pageTracker._trackPageview(\'/demoview\');';
		}
		src += '<tr onmouseover="this.className=\'current\';" onmouseout="this.className=\'\';">';
		src += '<td><a href="'+ hrefText +'">' + bugInfo[i][1] + '</a></td>';
		src += '<td><a href="'+ hrefText +'">' + bugInfo[i][2] + '</a></td>';
		src += '</tr>';
	}
	src += '</table>';
	document.write(src);
}

railbugs.load();