jQuery(document).ready(function(){

    //When mouse rolls over
    jQuery("#navigation li").mouseover(function(){
        jQuery(this).children('a').stop().animate({paddingTop:'8px', paddingBottom:'8px'},{queue:false, duration:300, easing:'easeOutCubic'});
        jQuery('#navigation').stop().animate({top:'-8px'},{queue:false, duration:300, easing:'easeOutCubic'});
		//jQuery("#" + this.className + '_logo').stop().animate({width:'190px'},{queue:false, duration:600, easing: 'easeOutBounce'}).fadeTo(600, 0.5);
    });

    //When mouse is removed
    jQuery("#navigation li").mouseout(function(){
        jQuery(this).children('a').stop().animate({paddingTop:'0px', paddingBottom:'0px'},{queue:false, duration:300, easing:'easeOutCubic'});
        jQuery('#navigation').stop().animate({top:'0px'},{queue:false, duration:300, easing:'easeOutCubic'});
		//jQuery("#" + this.className + '_logo').stop().animate({width:'100px'},{queue:false, duration:600, easing: 'easeOutBounce'}).fadeTo(600, 0);
    });

});



addLoadEvent(function(){
	
	var bgc = document.getElementById('backgroundcontainer');
	if(bgc) bgc.style.height = getBrowserSize().height + 'px';
	
	var refreshed_content = Array('!content');
	bda.loadText = "<span class=\"loading\">Loading content, please wait ...</span>";
	bda.calculateText = "<span class=\"loading\">Almost there, I'm preloading the images...</span>";
	bda.errorText = "<div class=\"post\"><h1>I can't find what you're looking for :-(</h1><p>I'm sorry, but I could not find what you're looking for.<br/>Have you tried our <a onclick=\"searchFocus();\">search function</a>? Its in the top right corner !</p><p>If you already tried the search function and still haven't found what you're looking for, maybe you can browse the <a onclick=\"showBB('categories');\">blog categories</a>. They are at the bottom right corner.</p></div>";
	bda.imagesNotLoadedText = "<div class=\"post\"><h1>Some images could not be loaded</h1>While preloading I was unable to load some images. <br/>The page will display without these images.</div>";
	bda.externalLocation = "<div class=\"post\"><h1>The page you requested cannot be loaded through AJAX, I'll open it in a new window</h1><p>I can\'t parse content from the retrieved data.<br/>The page you requested will open in a new window, and you will <a href=\"javascript:history.go(-1); \">return to the previous page</a> shortly.<p></div>";

	
	bda.denyURLRules.push('url.contains("/wp-admin/")');
	bda.denyURLRules.push('url.contains("/feed/")');
	bda.denyURLRules.push('url.contains("/trackback/")');
	
	bda.addEventListener('load_complete','loadCompleteHandler');
	bda.addEventListener('load_begin','loadBeginHandler');
	bda.addEventListener('init_complete', 'loadFixHandler');
	bda.addEventListener('tween_complete', 'tweenCompleteHandler');
	
	bda.imageLoadTimeout = 10;
	bda.transitionTweenType = Tween.regularEaseInOut;
	bda.start(refreshed_content);
	
	applyStealth();
	fixULCategories();
		
	qs = bd$('s');
});
var minHeightMenu = 600;

window.onresize = function(){
	var height = getBrowserSize().height;
	var menu = bd$('sidebar');
	var bgc = document.getElementById('backgroundcontainer');
	if(bgc) bgc.style.height = height + 'px';
	
	if(isBottombarOpen){
		if(height < minHeightMenu) menuTween = new OpacityTween(menu, Tween.regularEaseIn, 100, 0, 0.3);
		if(height >= minHeightMenu) menuTween = new OpacityTween(menu, Tween.regularEaseIn, 0, 100, 0.3);
		menuTween.start();
	}
}



/* events */
function loadCompleteHandler(e){
	if(bd$('hideCommentForm')) bd$('hideCommentForm').style.display = 'none';
	if(bd$('submitCommentForm')) bd$('submitCommentForm').style.display = 'none';
	if(bd$('showCommentForm')) bd$('showCommentForm').style.display = 'inline';
	if(bd$('comment_form')) bd$('comment_form').style.height = '0px';
	
	applyStealth();

	if(!_typeface_js) return; 
	var elements = bd$('content').getElementsByTagName('*');
	
	var elementsLength = elements.length;
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].className.match(/(^|\s)typeface-js(\s|$)/) || elements[i].tagName.match(/^(H1|H2|H3|H4|H5|H6)$/)) {
			_typeface_js.replaceText(elements[i]);
			elements[i].style.visibility = 'visible';
		}
	}
	
}

function loadBeginHandler(e){
	if(isBottombarOpen) showHideBottombar(false);
}

var loadFixed;
function loadFixHandler(e){
	if(loadFixed) return;
	if(_typeface_js) _typeface_js.renderDocument(); 
	loadFixed = true;
}

function tweenCompleteHandler(){
	var c = bd$('content');
	if(c) c.style.height = 'auto';
}


/* menu animation 
var menuItemTween = new Array();
function menuMouseOver(id){
	var logo = bd$(id.replace('_a','_logo'));
	if(!logo) return;
	menuItemTween[id] = new Tween(logo.style, 'width', Tween.regularEaseIn, parseInt(logo.style.width), 190, 0.5, 'px');
	menuItemTween[id].start();
	
	menuItemTween[id + '_opacity'] = new OpacityTween(logo, Tween.regularEaseIn, 0, 100, 0.5);
	if(!bda.isIE) menuItemTween[id + '_opacity'].start();
}

function menuMouseOut(id){
	var logo = bd$(id.replace('_a','_logo'));
	if(!logo) return;
	menuItemTween[id] = new Tween(logo.style, 'width', Tween.regularEaseIn, parseInt(logo.style.width), 100, 0.5, 'px');
	menuItemTween[id].start();
	
	menuItemTween[id + '_opacity'] = new OpacityTween(logo, Tween.regularEaseIn, 100, 0, 0.5);
	if(!bda.isIE) menuItemTween[id + '_opacity'].start();
}*/


/* fix ul categories */
function fixULCategories(){
	var c = bd$('ul_categories');
	if(!c) return;
	var children = c.getElementsByTagName('li');
	children[children.length -1].className = 'last';
	children[children.length -2].className = 'last';
}

/* comments */
var formTween;
var lastFormClick;
var isFormOpen;

function showHideCommentForm(show){
	if(isFormOpen == show) return;
	var d = new Date();
	if(lastFormClick + 1000 > d.getTime()) return;
	else lastFormClick = d.getTime();
		
	var form = bd$('comment_form');
	if(!form) return;
	if(show){
		formTween = new Tween(form.style, 'height', Tween.regularEaseIn, 0, 400, 0.5, 'px');
		if(bd$('hideCommentForm')) bd$('hideCommentForm').style.display = 'inline';
		if(bd$('submitCommentForm')) bd$('submitCommentForm').style.display = 'inline';
		if(bd$('showCommentForm')) bd$('showCommentForm').style.display = 'none';
	}else{
		formTween = new Tween(form.style, 'height', Tween.regularEaseIn, 400, 0, 0.5, 'px');
		if(bd$('hideCommentForm')) bd$('hideCommentForm').style.display = 'none';
		if(bd$('submitCommentForm')) bd$('submitCommentForm').style.display = 'none';
		if(bd$('showCommentForm')) bd$('showCommentForm').style.display = 'inline';
	}
	formTween.start();
	isFormOpen = show;
}

function commentPage(pagenr){
	var ul = bd$('commentscontainer');
	if(!ul) return;
	var comments = ul.getElementsByTagName('li');
	for(var i = 0; i < comments.length; i++) comments[i].style.display = 'none';
	comments = getElementsByClassName('commentpage' + pagenr, 'li', ul);
	for(i = 0; i < comments.length; i++) comments[i].style.display = 'block';
	
	var pagelists = getElementsByClassName('pagelist');
	for(i = 0; i < pagelists.length; i++){
		var list = pagelists[i];
		var numbers = list.getElementsByTagName('a');
		for(i = 0; i < numbers.length; i++){
			var number = numbers[i];
			number.className = 'typeface-js bda_ignore pagenumber' + (i+1) + ' ' + ((number.className.indexOf('pagenumber'+pagenr+' ') > 0)?'active':'normal');
		}
	}
}



/* search */
var lastSearchClick;
var searchTween;
var isSearchOpen;

function showHideSearch(){
	var d = new Date();
	if(lastSearchClick + 600 > d.getTime()) return;
	else lastSearchClick = d.getTime();
	var show = !isSearchOpen;
		
	var s = bd$('s');
	if(!s) return;
	if(show){
		searchTween = new OpacityTween(s, Tween.regularEaseIn, 0, 100, 0.2); 
		s.style.display = 'block';
	}else{
		searchTween = new OpacityTween(s, Tween.regularEaseIn, 100, 0, 0.2); 
		searchTween.addListener({onMotionFinished:function(){s.style.display = 'none';}});
	}
	searchTween.start();
	bd$('search_handler').className = show?'active':'normal';
	isSearchOpen = show;
}

function searchFocus(){
	if(!isSearchOpen) showHideSearch();
	qs.focus();
}

var qs;
function quicksearch(){
	setTimeout("quicksearch_dosearch()",1000);
}

function quicksearch_dosearch(){
	bda.postForm(bd$('searchform'),'<span class="loading"> Searching for "<b/>' + qs.value + '</b>".</span>');
}

function quicksearch_onfocus(){
	if(qs.value == 'type to search...') qs.value = '';
	qs.style.borderWidth = '1px';
	qs.style.padding = '0px';
	qs.style.background = '#3EAA7E';
	qs.style.cursor = 'text';
}

function quicksearch_onblur(){
	qs.value = 'type to search...';
	qs.style.borderWidth = '0px';
	qs.style.padding = '1px';
	qs.style.paddingLeft = '0px';
	qs.style.background = '#3f8a6c';
	qs.style.cursor = 'pointer';
}



/* bottombar */
var barTween;
var menuTween;
var isBottombarOpen = false;
var bottombarContent;
var lastBarClick;

function showHideBottombar(show, o){
	var bar = bd$('bottombar_content');
	if(!bar) return;
	if(bda.isIE && !show && !o){
		bar.style.display = 'none';
		return;
	}
	if(show) barTween = new OpacityTween(bar, Tween.regularEaseIn, 0, 100, 0.3); 
	else barTween = new OpacityTween(bar, Tween.regularEaseIn, 100, 0, 0.3); 
	if(o) barTween.addListener({onMotionFinished:o});
	if(!show && !o) barTween.addListener({onMotionFinished:function(){bar.style.display = 'none';}});
	if(show)bar.style.display = 'block';
	barTween.start();
	
	var r = bd$('recent_handler');
	var c = bd$('categories_handler');
	r.className = 'typeface-js bda_ignore ' + ((show && bottombarContent == 'recent')?'active':'normal');
	c.className = 'typeface-js bda_ignore ' + ((show && bottombarContent == 'categories')?'active':'normal');
	
	var height = getBrowserSize().height;
	var menu = bd$('sidebar');
	if(height < minHeightMenu){
		if(show) menuTween = new OpacityTween(menu, Tween.regularEaseIn, 100, 0, 0.3);
		else menuTween = new OpacityTween(menu, Tween.regularEaseIn, 0, 100, 0.3); 
		menuTween.start();
	}
	
	isBottombarOpen = show;
}

function showBB(type, isScript){
	if(!type) return;
	if(!isScript){
		var d = new Date();
		if(lastBarClick + 600 > d.getTime()) return;
		else lastBarClick = d.getTime();
	}
	var r = bd$('bottombar_recent');
	var c = bd$('bottombar_categories');
	if(!(r && c)) return;
	if(bottombarContent == type && isBottombarOpen){ // closing
		showHideBottombar(false);
		return;
	}else if(bottombarContent == type && !isBottombarOpen) showHideBottombar(true); // everything will be ok soon
	else if(bottombarContent != type && !isBottombarOpen){ // everything will be ok after this switch
		if(type == 'recent'){
			r.style.display = 'block';
			c.style.display = 'none';
		}else{
			c.style.display = 'block';
			r.style.display = 'none';
		}
		bottombarContent = type;
		showHideBottombar(true);
	}else{
		showHideBottombar(false, function(){showBB(type, true);}); // please come again
		return;
	}
}

/* supercool stealth function */
function stealth(obj){
	obj.onfocus = obj.blur;
}

function applyStealth(){
	var allA = document.getElementsByTagName('a');
	for(var i = 0; i < allA.length; i++) stealth(allA[i]);
}


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

function getBrowserSize() {
  var myWidth = 0, myHeight = 0, o = new Object();
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  o.width = myWidth;
  o.height = myHeight;
  return o;
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};