// fix image blinking bug in IE
try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch (e) {}

// random
function rndnm(n) {
	var r = Math.floor(Math.random() * n);
	return r;
}

// sitemap
function sitemap(){
	var stmp = document.getElementById('sitemap');
	if (stmp) {
		if (Util.DOM.hasClass('body', 'expanded')) { return }
		var body = document.getElementsByTagName('body')[0];

		var u = document.getElementById('sitemap').getElementsByTagName('ul');
		for (i=0; i < u.length; i++) {
			if (/sitemap/i.test(u[i].className)) {
				u[i].style.display = 'none';
			}
			var a = u[i].getElementsByTagName('a');
			for(z=0; z < a.length; z++) {
				if (Util.DOM.hasClass(a[z], 'parent')) {
					var li = a[z].parentNode;
					var imgChk = li.getElementsByTagName('img');
					if( imgChk.length == 0) {
						var img = document.createElement('img');
						img.className = 'icon';
						img.src = globalImagesPath + 'sitemapn_plus.gif';
						img.style.verticalAlign = 'middle';
						li.insertBefore(img, a[z]);
					}
					li.className = 'parent';
					ul = a[z].nextSibling;
					while (ul.nodeType != 1) {
						ul = ul.nextSibling;
					}
					ul.style.display = 'none';
					img.onclick = function() {
						li = this.parentNode;
						ul = li.getElementsByTagName('ul')[0];
						var ulStatus = (ul.style.display == 'none') ? 'block' : 'none';
						ul.style.display = ulStatus;
						imgStatus = (ulStatus == 'block') ? '_minus' : '_plus';
						this.src = globalImagesPath + 'sitemapn' + imgStatus + '.gif';
					}
				}
			}
		}
	}
}

// display link to print version only if Javascript is enabled
function show_print_version() {
	if (document.getElementById('contenttoolbox')) {
		document.getElementById('contenttoolbox').style.display="block";
	}
}

// corporate tool box print link
function corpToolboxPrintpage() {
	if(document.getElementById("corp-toolbox-printpage")) {
		var print_li = document.getElementById("corp-toolbox-printpage");
		if(print_li)	{
			print_li.style.display="inline";
			var print_a = print_li.getElementsByTagName("a")[0];
			print_a.removeAttribute("href");
			print_li.onclick = function() {
				window.print();
			}
		}
	}
}

// corporate tool box fonts size
// TODO: Delete when all migrations to new Change Text Size is done
// Replaced by /eipf/script/tools/eipf.changeTextSize.js
function corpToolboxChangetextsize() {
	window.eipf = window.eipf || {};
	
	if (eipf && eipf.changeTextSize) {
		return;
	}

	var size_li = document.getElementById("corp-toolbox-changetextsize");
	var wrapie = document.getElementById("wrapie");
	var cookie_status = Util.Cookie.get("increase");
	if(size_li)	{
		size_li.style.display="inline";
		var img = document.createElement("img");
		size_li.appendChild(img);
		var size_a = size_li.getElementsByTagName("a")[0];
		size_a.removeAttribute("href");
		var increase_text = "Increase text size";
		var decrease_text = "Decrease text size";
		if(cookie_status == "true") {
			Util.DOM.addClass(wrapie, "increase");
			img.src = globalImagesPath + "decrease.png";
			img.title = img.alt = decrease_text;
		}
		else {
			img.src = globalImagesPath + "increase.png";
			img.title = img.alt = increase_text;
		}
		img.onclick = function() {
			if(cookie_status == "true") {
				Util.Cookie.set("increase", "false", 356);
				Util.DOM.removeClass(wrapie, "increase");
				img.src = globalImagesPath + "increase.png";
				img.title = img.alt = increase_text;
				cookie_status = Util.Cookie.get("increase");
			}
			else {
				Util.DOM.addClass(wrapie, "increase");
				Util.Cookie.set("increase", "true", 356);
				img.src = globalImagesPath + "decrease.png";
				img.title = img.alt = decrease_text;
				cookie_status = Util.Cookie.get("increase");
			}
		}
	}
}

// show/hide text in the search input
function searchFocus() {
	if (typeof console !== 'undefined') {
		if (typeof console.warn == 'function') {
			console.warn("Function searchFocus() defined in eipf/script/default.js is deprecated, please use eipf.toggleInputLabel.js");
		}
	}
	
	var el = document.getElementById("search-input");
	if(el && !el.className.match(/ac_input/)) {
		var pre_value = el.value;
		el.onfocus= function() {
			if(this.value != "") {
				this.value = "";
			}
		}
		el.onblur = function() {
			if(this.value == "") {
				this.value = pre_value;
			}
		}
	}
}

function banner() {}


// fire all JS
function initJS() {
	sitemap();
	show_print_version();
	searchFocus();
	corpToolboxPrintpage();
	banner();

	// TODO: Delete when all migrations to new Change Text Size is done
	// Replaced by /eipf/script/tools/eipf.changeTextSize.js
	corpToolboxChangetextsize();
}

window.onload=initJS;
//Util.Event.addHandler(d, 'ready', initJS);

