// ajax links on home page tabbed box
imgTemp = new Image();
imgTemp.src = "/_images/ajax-loader.gif";
var homePageAjaxRequest; // create request object in a shared scope so that we can avoid threading issues
var tabbedBoxAJAXLinks						= {
	sDisciplineListID : 'disciplineLinks',									// this is the id of the discipline link list
	sDisciplineTextID : 'disciplineContent',								// this is the id for the discipline content div
	sCreativeLinkID : 'disclinkCreative',
	sSectorListID : 'sectorLinks',											// this is the id of the sector link list
	sSectorTextID : 'sectorContent',										// this is the id for the sector content div
	sMagazineListID : 'magazineLinks',										// this is the id of the magazine link list
	sMagazineTextID : 'magazineContent',									// this is the id for the magazine content div
	sContentLink : '/home/index.cfm?fuseaction=BR.Home.AJAX.LoadContent.',	// this is the link to the content loader
	sSelectedItem : 'listSelectedItem',										// this is the class of the parent li when the link is active
	sLoadingText : '<div class="loading"><img src="'+imgTemp.src+'" alt="loading" /></div>',											// this is the text displayed when the content is loading
	init : function() {
		if(!document.getElementById||!document.createTextNode){return;}
		tabbedBoxAJAXLinks.loadMouseOvers(tabbedBoxAJAXLinks.sDisciplineListID,tabbedBoxAJAXLinks.sDisciplineTextID,'Discipline');
		tabbedBoxAJAXLinks.loadMouseOvers(tabbedBoxAJAXLinks.sSectorListID,tabbedBoxAJAXLinks.sSectorTextID,'Sector');
		tabbedBoxAJAXLinks.loadMouseOvers(tabbedBoxAJAXLinks.sMagazineListID,tabbedBoxAJAXLinks.sMagazineTextID,'Magazine');
	},
	
	loadMouseOvers : function(sListID,sContentID,sLinkType) {
		if (!document.getElementById(sListID)) {return;}
		var eLinkList						= document.getElementById(sListID);
		var aLinks							= getAllChildren(eLinkList,'a');
		
		if (!aLinks.length) {return;} // only go on if we have links
		
		for (var i = 0; i < aLinks.length; i++) {
			if (!tabbedBoxAJAXLinks.isActiveLink(aLinks[i])) {
				aLinks[i].onmouseover			= function() {
					if (this.id == tabbedBoxAJAXLinks.sCreativeLinkID) {
						tabbedBoxAJAXLinks.loadContent('creative',sContentID,sLinkType); // handle creative link
					}else {
						tabbedBoxAJAXLinks.loadContent(tabbedBoxAJAXLinks.clearNonNumbers(this.id),sContentID,sLinkType);
					}
					tabbedBoxAJAXLinks.removeSelection(sListID); // remove selected link
					this.parentNode.className	= tabbedBoxAJAXLinks.sSelectedItem; // set this item as selected
					tabbedBoxAJAXLinks.loadMouseOvers(sListID,sContentID,sLinkType);
				}
			}
			else { // remove any previous mouseover events
				aLinks[i].onmouseover			= '';
			}
		}		
	},
	
	removeSelection : function(sListID) {
		var eLinkList						= document.getElementById(sListID);
		var aLinkItems						= getAllChildren(eLinkList,'li');
		
		for (var i = 0; i < aLinkItems.length; i++) {
			aLinkItems[i].className			= '';
		}
	},
	
	loadContent : function(sContentLabel,sContentID,sLinkType) {
		var sLink							= tabbedBoxAJAXLinks.sContentLink + sLinkType + '&sContentLabel=' + sContentLabel;
	
		try {
			// Opera 8.0+, Firefox, Safari
			homePageAjaxRequest				= new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer Browsers
			try {
				homePageAjaxRequest			= new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try{
					homePageAjaxRequest		= new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e){
					// something went wrong
					return false;
				}
			}
		}
		
		tabbedBoxAJAXLinks.contentLoading(sContentID);
		
		// Create a function that will receive data sent from the server
		homePageAjaxRequest.onreadystatechange = function() {
			if (homePageAjaxRequest.readyState == 4) {
				// did this work?
				if (homePageAjaxRequest.status == 200) {
					tabbedBoxAJAXLinks.updateContent(homePageAjaxRequest.responseText, sContentID);
				}
				else { // error
					tabbedBoxAJAXLinks.loadingError(sContentID);
				}
			}
		}
		homePageAjaxRequest.open('GET', sLink, true);
		homePageAjaxRequest.send(null);
	},
	
	contentLoading : function(sContentID) {
		var el								= document.getElementById(sContentID);
		el.innerHTML						= tabbedBoxAJAXLinks.sLoadingText;
	},
	
	updateContent : function(sHTML, sContentID) {
		var el								= document.getElementById(sContentID);
		el.innerHTML						= sHTML.replace(/^\s+/,'').replace(/\s+$/,''); // trim whitespace
	},
	
	loadingError : function(sContentID) {
		//alert('ERROR loading into ' + sContentID);
	},
	
	clearNonNumbers : function(sID) {
		return sID.replace(/\D/gi, '');
	},
	
	isActiveLink : function(eNode) {
		return (eNode.parentNode.className == tabbedBoxAJAXLinks.sSelectedItem) ?  true: false;
	}
}


WindowListener.add("load","tabbedBoxAJAXLinks.init()");