// General browser functions 
addLoadEvent(prepareCloseWindow);
addLoadEvent(preparePrint);
addLoadEvent(prepareNewWindow);
addLoadEvent(viewAddress);
addLoadEvent(fileDropdownSelect);

// Close opened window
function prepareCloseWindow(){
	if(!document.getElementById("close")) return false;
	var closewindow = document.getElementById("close").getElementsByTagName("a");
	closewindow[0].onclick = function(){
		parent.window.close();
		return false;
	};
};
// Print page
function preparePrint(){
	if(!document.getElementById("print")) return false;
	var printpage = document.getElementById("print").getElementsByTagName("a");
	printpage[0].onclick = function(){
		window.print();
		return false;
	};
};
// New window for external links
function prepareNewWindow(){
	if(!document.getElementsByTagName) return false;
	var arrayhref = document.getElementsByTagName("a");
	for(i = 0; i<arrayhref.length; i++){
    	if(arrayhref[i].getAttribute("rel") && arrayhref[i].getAttribute("rel") == "external"){
			arrayhref[i].onclick = function(){
				window.open(this.getAttribute("href"));
				return false;
			};
		}
	}
};
// Hide email links from spam
function viewAddress() {
	if (document.getElementsByTagName) {
		var a = document.getElementsByTagName("a")
		var i
		for (i = 0; i < a.length; i++) {
			if (a[i].className && a[i].className == "email") {
				var address_to_replace = a[i].firstChild;
				var real_address = address_to_replace.nodeValue.replace("*atsymbol*", "@");
				address_to_replace.nodeValue = real_address;
				address_to_replace.parentNode.setAttribute("href", "mailto:" + real_address);
			}
		}
	}
};
// Used with boo_file_download_list plugin
function fileDropdownSelect(){
	for(var f = 0; f<document.forms.length; f++){
	    if(document.forms[f].className.indexOf("boo_file_form") != -1){
			var thisform = document.forms[f];
		}else{
			continue;
		}		
	    thisform.onsubmit = function(){
			var parentselect = thisform.boo_file_select;
			var selectedfile = parentselect.options[parentselect.selectedIndex].value;
			
			document.location=selectedfile;
					
			return false;
	    };
	  }			
};
