var ubData = {
	commonLinks:{
		"faq":{
			"link":"http://www.uba.uva.nl/hulp/object.cfm/51936B0B-D05D-4051-88C917EB3A91C1E1", "txt":"veelgestelde vragen" 
		},
		"acta":{
			"link":"http://www.uba.uva.nl/bibliotheeklocaties/bibliotheeklocaties.cfm/6B9FF369-0255-4A3D-875736B52D095AAE", "txt":"Tandheelkunde"
		},
		"jb":{
			"link":"http://cf.uba.uva.nl/nl/jb/", "txt":"Rechtsgeleerdheid"
		},
		"law":{
			"link":"http://cf.uba.uva.nl/en/law/", "txt":"Law"
		}
	},

	autoLinks:{
	
		"Direct_naar":[{
			"link":"http://diensten.uba.uva.nl/com/open/", "txt": "Beschikbare pc's"
		
		}],
		"Direct_to":[{
			"link":"http://diensten.uba.uva.nl/com/open/?l=en", "txt": "Available PC's"
		}], 
		"Hulp": [{
			"link":"http://wegwijsbijdeuba.wordpress.com/", "txt": "wegwijs bij de UBA","target":"_blank"
			
		}],
		"Help": [{
			"link":"http://ubwise.wordpress.com/", "txt": "UBwise","target":"_blank"
		}]
	},

	replaceHref:{
		"\/help\/faq.cfm" : "\/help\/object.cfm\/594783E1-F4F6-43DC-90510D2AFBA00A3F",
		"www.science.uva.nl\/library\/locations\/HvAnl.htm" : "www.bib.hva.nl",
		"www.science.uva.nl\/library\/locations\/HvAeng.htm" : "www.bib.hva.nl\/hva-homepage-bladermenu\/start.cfm"
		
	},
	
	ucFirst:function ( str ) {
		return str.substring ( 0 , 1 ).toUpperCase () + str.substring ( 1 ).toLowerCase() ;
	},
	
	writeLink:function(linkKey,options){
		if (typeof(this.commonLinks[linkKey]) == 'object'){
			if (typeof(options) == 'undefined' ) options = {}; 
			var sBlank = '',sClass = ''; 
			var txt = this.commonLinks[linkKey].txt; 
			if(typeof(options.target) != 'undefined') sBlank = ' target="'+options.target+'" '; 
			if(typeof(options.cssClass) != 'undefined') sClass = ' class="'+options.cssClass+'" '; 
			if(typeof(options.ucFirst) != 'undefined') txt = this.ucFirst(txt);
			
			document.write('<a href="'+this.commonLinks[linkKey].link+'" '+sBlank+sClass+' >'+txt+'</a>'); 
		}
	}, 
	replaceHrefs : function(){
		$('a').each(function(){
			for (var x in ubData.replaceHref){
				if (this.href.indexOf(x)!==-1){
					this.href = this.href.replace(x,ubData.replaceHref[x]);
				}
			}
		})
	},
	
	writeFooter:function(options){
		var lng = 'nl',i,ahtml=[]; 
		if (options){
			lng = ( options.lng || lng ); 
		}
		
		var links = {
			"nl":{
				"leader":"Direct naar de bibliotheken van de UvA/HvA:",
				"links":[
					{"txt":"UBA Home","link":"http://cf.uba.uva.nl/nl/" },
					{"txt":"Bijzondere Collecties","link":"http://www.uba.uva.nl/bbc/" },
					{"txt":"Economie en Bedrijfskunde","link": "http://cf.uba.uva.nl/nl/prb/"},
					{"txt":"Geesteswetenschappen", "link":"http://cf.uba.uva.nl/nl/gw/"},
					{"txt":"Geneeskunde", "link": " http://www.uba.uva.nl/bibliotheeklocaties/object.cfm/objectid=BC7E7E01-E800-4D58-93E6B55BFA8282F3"},
					{"txt":"Maatschappij&ndash;&nbsp;en&nbsp;Gedragswetenschappen", "link":"http://www.uba.uva.nl/fmg/locaties.cfm"},
					{"txt":"Natuurwetenschappen, Wiskunde en Informatica", "link":"http://cf.uba.uva.nl/nl/fnwi/"},
					{"txt":"Rechtsgeleerdheid", "link":"http://cf.uba.uva.nl/nl/jb/"},
					{"txt":"Tandheelkunde", "link":"http://www.uba.uva.nl/bibliotheeklocaties/bibliotheeklocaties.cfm/6B9FF369-0255-4A3D-875736B52D095AAE"},
					{"txt":"HvA", "link":"http://www.science.uva.nl/library/locations/HvAnl.htm"}
				]
			},
			"en":{
				"leader":"Direct to the libraries of the UvA/HvA:",
				"links":[
					{"txt":"UBA Home", "link":"http://cf.uba.uva.nl/en/"},
					{"txt":"Dentistry", "link":"http://www.uba.uva.nl/libraries/locations.cfm/EFB680AA-16D9-4D7D-AFFFBF0EE9897F96"},
					{"txt":"Economics and Business Studies", "link":"http://cf.uba.uva.nl/en/prl/"},
					{"txt":"Humanities", "link":"http://cf.uba.uva.nl/en/humanities/"},
					{"txt":"Law", "link":"http://cf.uba.uva.nl/en/law/"},
					{"txt":"Medicine", "link":"http://www.uba.uva.nl/libraries/locations.cfm/5B54AC50-D5A1-4112-A926FC7EA23B9D7B"},
					{"txt":"Science", "link":"http://cf.uba.uva.nl/en/science/"},
					{"txt":"Social&nbsp;and&nbsp;Behavioural&nbsp;Sciences", "link":"http://www.uba.uva.nl/fsb/locations.cfm"},
					{"txt":"Special Collections", "link":"http://www.uba.uva.nl/special_collections/overview.cfm"},
					{"txt":"HvA", "link":"http://www.science.uva.nl/library/locations/HvAeng.htm"}
				]
			}
		}
			
		
		for (i = 0; i < links[lng].links.length; i++){
			if (location.href == links[lng].links[i].link || location.href == links[lng].links[i].link+'index.html' ) continue; 
			ahtml.push('<a href="'+links[lng].links[i].link+'" class="footer" style="padding:0px">'+ links[lng].links[i].txt+'</a>');
		}
		$('p.footer').html('<b style="padding-right:10px;">'+links[lng].leader+'</b>'+ahtml.join ('<span style="padding:6px">-</span>')); 
		
		
		
	}
}


/* dom ready */
$(function(){
	var ll = location.href.indexOf('.nl/en/')!== -1 ? "en" : "nl";
	if (ll === "en"){
		ubData.writeFooter({lng:"en"});
	}else{
		$('.navbalk > div').eq(0).prepend('<a href="http://www.uba.uva.nl/dienstverlening/contact.cfm" style="font-size:8pt;font-weight:bold;color:#cccccc;padding-right:20px;margin-right:20px;border-right:1px solid #cccccc;">Contact</a>');
		ubData.writeFooter();
	}
	
	var autoAddLinks = (function(){
		$('.kop1').each(function(){
			var $this = $(this), autolinkString='',i,tx,appendRoot;

			tx = $.trim($this.text()).replace(/\W+/,'_'); 
			
			if (ubData.autoLinks.hasOwnProperty(tx)){
				if (this.tagName === 'H1'){
					appendRoot = $this.next(); 
				}else{
					appendRoot = $this.parents('h1').next();					
				}
				for(i=0; i<ubData.autoLinks[tx].length; i++){
					autolinkString = '<li><a href="'+ubData.autoLinks[tx][i].link+'" ' 
					+ (ubData.autoLinks[tx][i]["target"] ? ' target="'+ubData.autoLinks[tx][i]['target'] + '"' : '') 
					+ '>'+ubData.autoLinks[tx][i].txt+'</a><br></li>';
					
					if (ubData.autoLinks[tx][i].hasOwnProperty('position')){
						appendRoot.children().eq(ubData.autoLinks[tx][i].position-1).after(autolinkString);
					}else{
						appendRoot.append(autolinkString);	
					}
				}
			}
		});
	})();
	

	ubData.replaceHrefs();
});
