/**
 * HPA Banner Control
 * by: Paul McLanahan <paul dot mclanahan at digitalinsight>
 * modified by: Darnell Arford <darnell dot arford at digitalinsight>
 * copyright: 2009 Digital Insight Inc. All Rights Reserved
 * usage: Place the following code on the site:

<div id="hpaBanner" style="display:none"><!-- HPA Banner --></div>
<script src="/mstinc/hpa/js/hpa.js" type="text/javascript"><!-- hpa --></script>
<script type="text/javascript">if(typeof hpa!='undefined')hpa.show();</script>

 * You may call hpa.show with an argument which is the domain
 * of the site you wish to use for the config. If you leave it
 * out the script will work out which domain the site is being
 * viewed from.  You may need to specify the domain name in situations
 * where the FI has multiple domain names or a setting for one domain
 * needs to be used for multiple sites.
 */
var hpa = {
	domains : {},
	banners : [],
	
	addBanner : function(bImg,bURL,bRURL){
		hpa.banners[hpa.banners.length] = {'img' : bImg, 'url' : bURL, 'rules_url' : bRURL};
	},
	
	addDomain : function(dName,dInfo){
		hpa.domains[dName] = dInfo;
	},
	
	show : function(site){
		if(!document.getElementsByTagName || !document.getElementById)return;
		hpa.site = site || hpa.getDomain();
		hpa.require('hpaDomains_'+(/^[a-z]/i.test(hpa.site)?hpa.site.charAt(0):0)+'.js');
	},
	
	init : function(){
	    // quit if this function has already been called
	    if (arguments.callee.done) return;
	
	    // flag this function so we don't do the same thing twice
	    arguments.callee.done = true;
			
		var el = document.getElementById('hpaBanner');
		if(!el)return;
		var doDisplay = true, content = '';
		if(hpa.siteIsActive(hpa.site) && hpa.isActive){
			content = hpa.getBannerCode();
		}
		else if(hpa.domains[hpa.site] && hpa.domains[hpa.site].altcode){
			content = hpa.domains[hpa.site].altcode;
		}
		else doDisplay = false;
		if(doDisplay){
			el.innerHTML = content;
			el.style.display = '';
		}
	},
	
	getDomain : function(){
		var hostName = /^https?:\/\/([^\/]+)\/?/.exec(document.URL)[1];
		if(hostName.match(/\./g).length > 1){
			hostName = /[^.]+\.[^.]+$/.exec(hostName)[0];
		}
		return hostName;
	},
	
	getBannerCode : function(){
		var d = hpa.domains[hpa.site];
		var b = hpa.banners[d.banner];
		var out = '', aTag = false;
		if(b.url || d.alturl){
			out += '<a style="margin:0;padding:0" href="'+(d.alturl?d.alturl:b.url)+'"'+(d.popup?' target="_blank"':'')+'>';
			aTag = true;
		}
		out += '<img style="margin:0;padding:0" src="'+(d.altimg?d.altimg:hpa.path+'images/'+b.img)+'" alt="'+d.alttext+'" border="0" />';
		if(aTag)out += '</a>';
		//out += '<p style="margin:0;padding:0;font-family:Arial,Helvetica,sans-serif;font-size:8pt;">Click here for <a href="'+b.rules_url+'" target="_blank">official rules</a></p>';
		return out;
	},
	
	siteIsActive : function(site){
		var enabled = false;
		if(hpa.domains[site]){
			enabled = hpa.domains[site].enabled;
		}
		return enabled;
	},
	
	require: function(sLib){
		if(!hpa.path){
			if(!document.getElementsByTagName)return;
			scripts = document.getElementsByTagName('script');
			for(var s=0;s < scripts.length;s++){
				if(scripts[s].src.match(/js\/hpa\.js$/)){
					var hpaScriptSrc = scripts[s].src;
					break;
				}
			}
			hpa.path = hpaScriptSrc.replace(/js\/hpa\.js$/,'');
		}
		document.write('<scr'+'ipt type="text/javascript" src="'+hpa.path+'js/'+sLib+'"></scr'+'ipt>');
	}
}
hpa.require('hpaDomains.js');