
if (DynamicDonateBanner==undefined) {

	var DynamicDonateBanner = function(targets,map) {
		this.segments = [];
		this.map = {};
		// this.msg = "";
		if (targets && map) {
			this.setup(map);
			var url = this.calculate();
			// alert(targets + ": " + url + "\n" + this.msg);
			if (url) {
				this.updateAll(targets,url);
			}
		}		
	}

	DynamicDonateBanner.prototype = {
	
		setup: function(map) {

			this.segments = window.location.pathname.replace(/\/$/,'').split('/');
			
			if (map) {
				this.map=map;
			} else {
				this.map = {
					url:'/',
					segment:null,
					children:false
				};
			}
		},
	
		calculate: function(map,depth) {
			var url = false;
			if (map==undefined) map=this.map;
			if (depth==undefined) depth=0;
			if (depth<20 && depth<this.segments.length) {
				var segment = this.segments[depth];
				if (segment==map.segment) {
					// this.msg += "\ndepth:" + depth + "\nreal-segment: " +segment + "\nmap-segment:" + map.segment + "\nurl:" + map.url + "\n";
					url=map.url;
					if (map.children && map.children.length>0 && (depth+1)<this.segments.length) {
						var subUrl = false;
						for(var i=0; i<map.children.length; i++) {
							subUrl = this.calculate(map.children[i],depth+1);
							if (subUrl) {
								url=subUrl;
								break;
							}
						}
					}
				}
			}
			return url;
		},

		updateAll: function(targets,url) {
			targets = (
				targets.indexOf(',')>-1 ? 
					targets.split(',') : 
					[ targets ]
			);
			for(var i=0;i<targets.length;i++) {
				this.update(targets[i],url);
			}
		},
	
		update: function(target,newUrl) {
			target = document.getElementById(target);
			if (target && newUrl) {
				var anchor = this.getAnchor(target);
				if (anchor) {
					var url = null;
					var href = false;
					var onclick;
					for (var j=0;j<anchor.attributes.length;j++) {
						if (anchor.attributes[j].nodeName=="href") {
							url = anchor.attributes[j].nodeValue;
							href = true;
							break;
						}
					}
					if (!href && anchor.getAttribute("href")) {
						url = anchor.getAttribute("href");
						href = true;
					}
					if (href && url) {
						anchor.setAttribute('href',newUrl);
					}
				}
			}
		},
		
		getAnchor: function(element) {
			var anchor = null;
			if (element.nodeType==1 && element.nodeName.toLowerCase()=='a') {
				return element;
			} else if (element.hasChildNodes()) {
				for (var i=0; i<element.childNodes.length; i++) {
					anchor = this.getAnchor(element.childNodes[i]);
					if (anchor) {
						break;
					}
				}
				return anchor;
			}
			return false;
		} 
		
	}
}
