// Dynamic Breadcrumbs
// By Harry Love (http://harrylove.org/)
// License: http://creativecommons.org/licenses/by-sa/3.0/
// Updated: June 20, 2007

// Link to the script in the head of your page and change the customizations
// below to fit your scenario. That's it.
// The breadcrumb output is a div with id="breadcrumbs" so you can style it with CSS

function Breadcrumb() {
	//////////////////////
	// Begin customization
	//////////////////////
	this.homeLinkText = 	'swissinfo.ch'; 	// Text for the home link
	this.separator = 		' > ';			// Character(s) to use between breadcrumbs
	this.homeLinkPosition = 1;				// Where is home? 0 = domain, 1 = 1st directory, and so on ...
	this.attachTo = 		'#breadcrumb';	// Attach breadcrumb to a tag or an ID: e.g., body, h1, div, #header, #breadcrumb
	this.replaceTheseCharacters = [		// You can replace any text with customized text
	//	["this text", "will be replaced with this text"],
		[ "front" , "\u9996\u9875" ],
		[ "international" , "\u56FD\u9645\u65B0\u95FB" ],
		[ "imagenews" , "\u56FE\u7247\u65B0\u95FB" ],
		[ "business" , "\u7ECF\u6D4E\u91D1\u878D" ],
		[ "culture" , "\u793E\u4F1A\u6587\u5316" ],
		[ "health" , "\u5065\u5EB7\u533B\u836F" ],
		[ "science_technology" , "\u79D1\u6280\u73AF\u4FDD" ],
		[ "travel" , "\u65C5\u6E38\u89C2\u5149" ],
		[ "sport" , "\u4F53\u80B2\u7EB5\u6A2A" ],
		[ "einsatznews" , "\u4E00\u53E5\u8BDD\u65B0\u95FB" ],
		[ "politics" , "\u745E\u58EB\u653F\u6CBB" ],
		[ "political_system" , "\u653F\u6CBB\u4F53\u7CFB" ],
		[ "cantons" , "\u745E\u58EB\u5404\u5DDE" ],
		[ "country_information" , "\u745E\u58EB\u8D44\u8BAF" ],
		[ "chinese_abroad" , "\u745E\u58EB\u534E\u4EBA" ],
		[ "cityguide" , "\u57CE\u5E02\u6307\u5357" ],
		[ "multimedia" , "\u591A\u5A92\u4F53" ],
		[ "picture_gallery" , "\u753B\u5ECA" ],
		[ "swisslinks" , "\u745E\u58EB\u70ED\u94FE" ],
		[ "traffic" , "\u4EA4\u901A\u4FE1\u606F" ],
		[ "weather" , "\u745E\u6B27\u6C14\u8C61" ],
		[ "education" , "\u7559\u5B66\u6559\u80B2" ],
		[ "universities" , "\u56FD\u7ACB\u5927\u5B66" ],
		[ "highschools" , "\u4E13\u79D1\u5B66\u9662" ],
		[ "hotelschools" , "\u9152\u5E97\u7BA1\u7406" ],
		[ "specials" , "\u7CBE\u5178\u7279\u520A" ],
		[ "video" , "\u89C6\u9891" ],
		[ "picture_of_the_day" , "\u4ECA\u65E5\u56FE\u7247" ],
		[ "audios_podcast" , "\u97F3\u9891" ],
		[ "interactive" , "\u4E92\u52A8\u4FE1\u606F" ],
		[ "euro2008" , "2008\u6B27\u9526\u8D5B" ],
		[ "uefa_news" , "\u80CC\u666F\u8D44\u6599" ],
		[ "cities" , "\u4E3B\u529E\u57CE\u5E02" ],
		[ "contact" , "\u8054\u7CFB\u6211\u4EEC" ],
		[ "services" , "\u670D\u52A1\u54A8\u8BE2" ],
		[ "currency_converter" , "\u8D27\u5E01\u5151\u6362" ],
		[ "rss" , "rss\u670D\u52A1" ],
		[ "desktopnews" , "\u684C\u9762\u745E\u58EB\u65B0\u95FB" ],
		[ "mobile" , "\u624B\u673A\u4FE1\u606F\u670D\u52A1" ],
		[ "climate_§change" , "\u6C14\u5019\u53D8\u8FC1" ],
		[ "research" , "\u79D1\u5B66\u7814\u7A76" ],
		[ "energy" , "\u80FD\u6E90" ],
		[ "tourism" , "\u65C5\u6E38\u4E1A" ],
		[ "marriage" , "\u8DE8\u56FD\u7C4D\u5A5A\u59FB" ],
		[ "shortnews" , "\u745E\u58EB\u65B0\u95FB\u7B80\u8BAF" ],
		[ "news" , "\u65F6\u4E8B\u65B0\u95FB" ],
		[ "browse_by_themes" , "\u4E13\u9898\u6D4F\u89C8" ],
		[ "places_of_interest" , "\u65C5\u6E38\u4F11\u95F2\u63A8\u8350" ],
		[ "chinese_gallery" , "\u7279\u520A\u753B\u5ECA" ],
		[ "federer" , "\u7F57\u6770\u30FB\u8D39\u5FB7\u52D2" ],
		[ "extraordinary_exiles" , "\u4E16\u754C\u540D\u4EBA\u5728\u745E\u58EB" ],
		[ "integration" , "\u878D\u5165\u745E\u58EB\u793E\u4F1A" ],
		[ "swissinfo" , " " ],
		[ "index" , " " ],
		[ "detail" , " " ],
		[ "olympic_games_2008" , "2008\u5317\u4EAC\u5965\u8FD0\u4F1A" ],
		[ "switzerland_china" , "\u745E\u58EB-\u4E2D\u56FD" ],
		[ "olympics_background" , "\u5965\u6797\u5339\u514B\u8FD0\u52A8\u4F1A" ],
		[ "portraits" , "\u745E\u58EB\u8FD0\u52A8\u5458\u4ECB\u7ECD" ],
		[ "tickerimages" , "\u5965\u8FD0\u56FE\u7247\u5FEB\u8BAF" ],
		[ "tickertexts" , "\u8FD0\u52A8\u5458\u7B80\u4ECB" ],
		[ "finance_crisis" , "\u745E\u58EB\u548C\u91D1\u878D\u5371\u673A" ],
		[ "schweiz_schengen" , "\u745E\u58EB\u4E0E\u7533\u6839" ],
		[ "switzerland_for_the_record" , "\u745E\u58EB\u4E4B\u6700" ],
		[ "_" , " " ],
		['','']];
	
	var youarein = '\u60A8\u76EE\u524D\u7684\u4F4D\u7F6E: ';
	youarein=unescape(youarein);
	var docurl = document.URL;
	var FileDocName =(docurl.substring(docurl.lastIndexOf("\/")+1,docurl.lastIndexOf("\.")))
	var FileSearchName = 'Result'; 
	var FileSearchNameReplacement = 'search result(s)';
	
	////////////////////
	// End customization
	////////////////////

	this.output = writeHTML;
	var homeText = this.homeLinkText;
	var sep = this.separator;
	var position = this.homeLinkPosition;
	var tag = this.attachTo;
	var replacements = this.replaceTheseCharacters;
	var d = document, text = url = d.location.href.split('//')[1];
	//var title = d.title;
	
	// This next set of "if" clauses removes the cgi-bin++ portion of the URL for
	// pages that include the cgi-bin directory.
	if(url.search("cgi-bin") > 0) {
		// Get rid of the stuff after .cgi?...
		var badReg = /\w+\.cgi\?.*/i;
		var goodReg =  "" + url.match(badReg);
		goodReg = goodReg.substring(0,goodReg.indexOf("?"));
		url=url.replace(badReg,goodReg);
		
		badReg = /\/cgi-bin\/.+url=/i;
		url=url.replace(badReg,"");
		if(url.search("cgi-bin") > 0) {
			badReg = /\/cgi-bin\//i;
			url=url.replace(badReg,"/hsl/");
			
			if(url.search("admin") > 0) {
				badReg = /\/admin/i;
				url=url.replace(badReg,"");
			}
		}
		
		if(url.search("/hsl/hsl/") > 0) {
			badReg = /\/hsl\/hsl\//i;
			url=url.replace(badReg,"/hsl/");
		}
	}
	
	if(url.search("&url=") > 0) {
		badReg = /\/hsl\/.+url=/i;
		url=url.replace(badReg,"");
	}
	
	//if(url.search("washington.edu") > 0) {
	//	badReg = /healthlinks.washington.edu/i;
	//	url=url.replace(badReg,"");
	//}

	// End cgi-bin removal clauses
	
	
	url = url.split('/');
	if(url[url.length-1] == '') {url.pop();}
	url.pop();
	for (var i=0; i < replacements.length; i++) {
		rex = new RegExp(replacements[i][0], "g");
		//text = text.replace(rex,replacements[i][1]);
		text = text.replace(rex,unescape(replacements[i][1]));
		//title = title.replace(rex,replacements[i][1]);
		//FileDocName = FileDocName.replace(rex,replacements[i][1]);
		FileDocName = FileDocName.replace(rex,unescape(replacements[i][1]));
	}
	text = text.split('/');
	
	function createBreadcrumbs() {
		var href = '', a, div = d.createElement('span');
		div.setAttribute('id', 'breadcrumbs');
		div.appendChild(d.createTextNode(youarein));
		for (var i = 0; i < url.length; i++) {
			href += url[i] + '/';
			if (i < position) {continue;}
			if (i == position) {text[i] = homeText;}
			text[i] = text[i].charAt(0).toLowerCase() + text[i].substr(1);
			a = d.createElement('a');		
			a.setAttribute('href', 'http://' + href);
			a.appendChild(d.createTextNode(text[i]));
			div.appendChild(a);
			div.appendChild(d.createTextNode(sep));
		}

		if (FileDocName != FileSearchName) {
			//div.appendChild(d.createTextNode(title));		// output the title at the end //
			div.appendChild(d.createTextNode(FileDocName));	// output the filename at the end //
		}
		else {
			div.appendChild(d.createTextNode(FileSearchNameReplacement));
			}
		return div;
	}
	
	function writeHTML() {
		tag = tag.match('#') ? d.getElementById(tag.substring(1)) : d.getElementsByTagName(tag)[0];
		if(tag) {
			tag.appendChild(createBreadcrumbs());
		}
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function() {
	var crumb = new Breadcrumb();
	crumb.output();					  
});
