var beyondCodeDisabled = false;

var hasWindowBeenShownOnce = false;

var lastContentCode = 0;

var savedXML = null;
var foundEntry = false;

var beyondCodeContent = null;

function BeyondCode (title, text, linkText, link, keyword) {
    this.title = title;
    this.text = text;
    this.linkText = linkText;
    this.link = link;
    this.keyword = keyword;
}

function loadCodeFromXML () {
  var httpRequest = null;
  if (typeof XMLHttpRequest != 'undefined') {
    httpRequest = new XMLHttpRequest();
  }
  else if (typeof ActiveXObject != 'undefined') {
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
      httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch (e) { }
    @end @*/
  }
  if (httpRequest != null) {
    httpRequest.open('GET', 'BeyondCode.xml', true);
    httpRequest.onreadystatechange = function () {
      if (httpRequest.readyState == 4 && httpRequest.status == 200) {
        loadBeyondCodeInfo(httpRequest.responseXML);
      }
    };
    httpRequest.send(null);
  }
}

/**
 * Stores the data in the XML file for this users session.
 */
function loadBeyondCodeInfo(xmlDoc){
	var curContentNode;
	var bcTitle = null;
	var bcText = null;
	var bcLinkUrl = null;
	var bcLinkText = null;
	var bcKeyword = null;

	beyondCodeContent = new Array();
	for (var i = 0; i < xmlDoc.documentElement.childNodes.length; i++){
    	curContentNode = xmlDoc.documentElement.childNodes[i];

		var curChildNode;
		var text = "";
		var title = "";
		var linkUrl = "";
		var linkText = "";
		

		for (var j = 0; (curContentNode.nodeName == "ContentBlock") && (j < curContentNode.childNodes.length); j++){
			curChildNode = curContentNode.childNodes[j];

			if(curChildNode.nodeName == "title") title = curChildNode.firstChild.nodeValue;
			if(curChildNode.nodeName == "text") text = curChildNode.firstChild.nodeValue;
			if(curChildNode.nodeName == "link"){
				var curChildLinkNode;
				for(var k = 0; (curChildNode.nodeName == "link") &&  (k < curChildNode.childNodes.length); k++){
					curChildLinkNode = curChildNode.childNodes[k];
					if(curChildLinkNode.nodeName == "linkUrl") linkUrl = curChildLinkNode.firstChild.nodeValue;
					if(curChildLinkNode.nodeName == "linkText") linkText = curChildLinkNode.firstChild.nodeValue;
				}
			}

			if(curChildNode.nodeName == "keyword"){
				bcTitle = title;
				bcText = text;
				bcLinkUrl = linkUrl;
				bcLinkText = linkText;
				bcKeyword = curChildNode.firstChild.nodeValue;
				break;
			}
		}

		if(bcTitle != "" && bcText != ""){
			beyondCodeContent[beyondCodeContent.length] = new BeyondCode(bcTitle, bcText, bcLinkText, bcLinkUrl, bcKeyword);
			bcTitle = "";
			bcText = "";
			bcLinkText = "";
			bcLinkUrl = "";
			bcKeyword = "";
		}
	}
}

/**
 * Goes through Beyond Code Content list and finds a suitable match to the provided
 * contentId.
 */
function getBeyondCodeInfo(contentId){
	var potentialContent = new Array();
    for (var i = 0; i < beyondCodeContent.length; i++){
    	if(beyondCodeContent[i].keyword == contentId){
    		potentialContent[potentialContent.length] = beyondCodeContent[i];
		}
	}

	if(potentialContent.length > 0){
		var contentIndex = Math.floor(Math.random() * potentialContent.length);
		return potentialContent[contentIndex]
	}

	return null;
}

/**
 * Returns true if content is found, false if not. If content found and window is showing
 * it displays it.
 */
function displayBeyondCode(contentId){
	if(beyondCodeContent == null){
		loadCodeFromXML();
	}
	
	var content = getBeyondCodeInfo(contentId);
	if(content != null){
		lastContentCode = contentId;
		if(windows['beyondCode'] && !windows['beyondCode'].closed && (typeof windows['beyondCode'].loadCode) != "undefined"){
			windows['beyondCode'].loadCode(content.title, content.text, content.linkText, content.link);
		}
		return true;
	}
	return false;
}

/**
 * Called by all the various JSPs to display content in the Beyond Code Window
 * and to make the notification icon flash.
 */
function showBeyondCode(contentId){	
	if(displayBeyondCode(contentId)){
		if(!beyondCodeDisabled){
			top.results.beyondCodeNotify();
			if(!hasBeyondCodeBeenShownOnce()){	
				displayBeyondCodeWindowOnTop(contentId);
			}
		}
	}
}

/**
 * Because of pop-up blockers we can't show this window right away like we want. 
 * So we show it as soon as someone clicks a tab, and then set whether the window
 * has been shown at least once.  Then we don't auto open it anymore after that.
 */
function hasBeyondCodeBeenShownOnce(){
	return hasWindowBeenShownOnce;
}

function setBeyondCodeHasBeenShown(hasShown){
  hasWindowBeenShownOnce = hasShown;
}

function displayBeyondCodeWindowOnTop(whichCode){
	if(windows['beyondCode'] && !windows['beyondCode'].closed){
		windows['beyondCode'].focus();
	}else{
		if(!whichCode){
			if(lastContentCode){
				whichCode = lastContentCode;
			}else{
				whichCode = 0;
			}
		}else{
			lastContentCode = whichCode;
		}
		if(!windows['beyondCode'] || windows['beyondCode'].closed){
			top.checkWindow('beyond_code.jsp?whichCode='+whichCode, 'beyondCode', 500, 300);		
			setBeyondCodeHasBeenShown(true);
		}
	}
}

function isBeyondCodeDisabled(){
	return beyondCodeDisabled;
}

/**
 * Call when a user changes their beyond code preference.
 */
function setBeyondCodeEnabled(beyondCodeSw){
	beyondCodeDisabled = !beyondCodeSw;
	//setBeyondCodeHasBeenShown(beyondCodeDisabled);
}