// 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
		["arabic_international", "\u0639\u0631\u0628\u064A \u0648\u062F\u0648\u0644\u064A"],
		["business", "\u0627\u0642\u062A\u0635\u0627\u062F"],
		["geneve", "\u0645\u0646 \u062C\u0646\u064A\u0641"],
		["science_technology", "\u0639\u0644\u0648\u0645 \u0648\u062A\u0642\u0646\u064A\u0629"],
		["social_affairs", "\u0645\u062C\u062A\u0645\u0639 \u0648\u062B\u0642\u0627\u0641\u0629"],
		["politics", "\u0633\u064A\u0627\u0633\u0629 \u0633\u0648\u064A\u0633\u0631\u064A\u0629"],
		["foreign_affairs", "\u0634\u0624\u0648\u0646 \u062E\u0627\u0631\u062C\u064A\u0629"],
		["country_information", "\u0645\u0639\u0644\u0648\u0645\u0627\u062A \u062D\u0648\u0644 \u0633\u0648\u064A\u0633\u0631\u0627"],
		["political_system", "\u0627\u0644\u0646\u0638\u0627\u0645 \u0627\u0644\u0633\u064A\u0627\u0633\u064A"],
		["feature", "\u062A\u062D\u062A \u0627\u0644\u0645\u062C\u0647\u0631"],
		["specials", "\u0645\u0644\u0641\u0627\u062A \u062E\u0627\u0635\u0629"],
		["multimedia", "\u0635\u0648\u062A \u0648\u0635\u0648\u0631\u0629"],
		["picture_gallery", "\u0645\u0639\u0627\u0631\u0636 \u0635\u0648\u0631"],
		["swisslinks", "\u0645\u0648\u0627\u0642\u0639 \u0633\u0648\u064A\u0633\u0631\u064A\u0629"],
		["weather", "\u0627\u0644\u0637\u0642\u0633"],
		["video", "\u0641\u064A\u0640\u0640\u0640\u062F\u064A\u0648"],
		["picture_of_the_day", "\u0635\u0648\u0631\u0629 \u0627\u0644\u064A\u0648\u0645"],
		["audios_podcast", "\u0635\u0640\u0648\u062A"],
		["interactive", "\u062A\u0641\u0627\u0639\u0644\u064A"],
		["services", "\u062E\u062F\u0645\u0627\u062A"],
		["currency_converter", "\u0627\u0644\u0639\u0645\u0644\u0627\u062A"],
		["desktopnews", "\u0623\u062E\u0628\u0627\u0631 \u0633\u0637\u062D \u0627\u0644\u0645\u0643\u062A\u0628"],
		["mobile", "\u062E\u062F\u0645\u0627\u062A \u0627\u0644\u0623\u062C\u0647\u0632\u0629 \u0627\u0644\u0646\u0642\u0627\u0644\u0629"],
		["contact", "\u0625\u062A\u0635\u0644\u0648\u0627 \u0628\u0646\u0627"],
		["news", "\u0623\u062E\u0628\u0627\u0631"],
		["minaret_controversy", "\u0627\u0644\u0645\u0622\u0630\u0646 \u0628\u064A\u0646 \u0631\u0641\u0636 \u0648\u0642\u0628\u0648\u0644"],
		["international", "\u0622\u062E\u0631 \u0627\u0644\u0623\u0646\u0628\u0627\u0621"],
		["rss", "\u0623\u062E\u0628\u0627\u0631 \u0633\u0648\u064A\u0633 \u0627\u0646\u0641\u0648 \u0628\u0646\u0638\u0627\u0645 RSS"],
		["", ""]];
	
	var youarein = '\u0623\u0646\u062A \u0641\u064A: ';
	youarein=unescape(youarein);
	var docurl = document.URL;
	var FileDocName =(docurl.substring(docurl.lastIndexOf("\/")+1,docurl.lastIndexOf("\.")))
	var FileSearchName = 'Result'; 
	var FileSearchNameReplacement = '%646%62A%64A%62C%629%20%627%644%628%62D%62B';
	
	////////////////////
	// 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();					  
});
