$(document).ready(function(){

	/* search box */
	var $s = $("#keywords");
	
	
	var searchLabel = "Search...";

	function restoreSearch() {   
		if ($s.val() == "") {$s.val(searchLabel);} 
		if ($s.val() == searchLabel) {$s.addClass("faded");}
	 }
	  
	restoreSearch();
	  
	$s.focus(function() {
		$s.removeClass("faded");
		if ($s.val() == searchLabel) {$s.val("");}
	})
	.blur(restoreSearch); 

	
	// focus and blur: change background:
	$("input").focus(function() {	
		$(this).addClass("active");
	});
	$("input").blur(function() {
		$(this).removeClass("active")
	});	

	/* / search box */

	/* update navigation: add current class to active menu item */
	markCurrentNavitem()
	
	
	$('.newsletter li a').click(onLiLinkClicked);
	
	
	// open all external links in a new browser window
	$('a[href^="http://"]').attr({target: "_blank", title: "Opens in a new window"});
	if (site_url) {
		//except those beginning with our own site url
		$('a[href^="' + site_url + '"]').attr({target: "", title: ""});
	}
 
});

function onLiLinkClicked(e) {
	//alert("clicked: " + e.target);
	//e.preventDefault();
	//e.target.animate({scrollTop:100}, 400);	
}


/* cufon stuff */
if ($.browser.msie && $.browser.version.substring(0,1) === '6') {   
	// cufon in ie6fix.js on document ready to avoid error
}else{
	if ($.browser.msie && $.browser.version.substring(0,1) === '7' && window.location.hash) {  
		Cufon.replace("h1")("h4");
	}else{
		Cufon.replace("h1")("h3")("h4");
	}
	Cufon.replace("#newsletter h2");
	Cufon.replace("#article h2");
}

/**
 * write e-mail adresses on page
 * mailsubject and title can be left blank
 * if mailSubject is blank it will be replaced with "Information Request (branch)"
 * if title is blank it will be replaced with the email address
 */
function writeEmailAddress(branch, mailSubject, title, withIcon){ 
	var emailPrefix = "";
	var emailSuffix = "";
	var ad = "@";
	if (mailSubject == "" || mailSubject == undefined) {
		mailSubject = "Information Request (" + branch + ")"; 
	}
	if(withIcon == "" || withIcon == undefined) {
		withIcon = "true";
	}
	
	if (branch == "be") {
		emailPrefix = "hello";
		emailSuffix = "but.be";
	} else if (branch == "ae") {
		emailPrefix = "hello";
		emailSuffix = "but.ae";
	} else if (branch == "learn24") {
		emailPrefix = "info";
		emailSuffix = "learn24.com";
	} else if (branch == "safety24") {
		emailPrefix = "info";
		emailSuffix = "safety24.com";
	} else {
		emailPrefix = "unknownBranch";
		emailSuffix = "but.be";
	}
	
	if (title == "" || title == undefined) {
		title = emailPrefix + ad + emailSuffix;
	}
	
	if(withIcon=="true") {
		document.write("<a class='mail' href='" + "mai" + "lto" + ":" + emailPrefix + ad + emailSuffix + "?subject=" + mailSubject + "'>" + title + "</a>" );
	} else {
		document.write("<a href='" + "mai" + "lto" + ":" + emailPrefix + ad + emailSuffix + "?subject=" + mailSubject + "'>" + title + "</a>" );
	}

}




/* update navigation: add current class to active menu item */
function markCurrentNavitem() {
	
	var str=$(this).attr("href");
	var searchString = getSegment(1); //eg. work
	
	if (searchString != ""){
	
		$("#main ul li a").each(function() {
			// find matching menu item						 
			if ($(this).attr("href").indexOf(searchString) != -1) {
				$(this).parent().addClass("current"); 
			}
		
		});
	}else{
		//we are on the homepage
			$("#main ul li a:first").parent().addClass("current"); 
	
	}
	
}

/* get a segment from the url */
function getSegment(num) {
    num += 2;
    var segments = window.location.href.toString();
    segments = segments.split('/');
    if (segments[num] != undefined) {
        return segments[num];
    }
    return '';
}



/* 
IE drops the windows Cleartype rendering after fade
Fix: http://blog.bmn.name/2008/03/jquery-fadeinfadeout-ie-cleartype-glitch/

*/
(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);