$.fn.bspbutton = function(){
	return this.each(function(){
		var settings = {
			speedOfFade: 300
		}
		
		if(document.all){
			settings.speedOfFade = 0;
		}
		
		var $$ = $(this);
		
		//
		
		var onHoverIn = function(){
			$$.children("div.hover").stop(true, true).fadeIn(settings.speedOfFade);
		}
		
		var onHoverOut = function(){
			$$.children("div.hover").stop(true, true).fadeOut(settings.speedOfFade);
			$$.children("div.click").stop(true, true).fadeOut(settings.speedOfFade / 3);
		}
		
		var onMouseDown = function(){
			$$.children("div.click").stop(true, true).fadeIn(settings.speedOfFade / 3);
		}
		
		var onMouseUp = function(){
			$$.children("div.click").stop(true, true).fadeOut(settings.speedOfFade / 3);
			
			var clickaction = $$.find("a").attr("href");
			if(!clickaction){
				clickaction = $$.find("a").attr("js");
			}
			
			window.location.href = clickaction;
		}
		
		//
		
		if(!$$.attr("preanchored")){
			$$.hover(onHoverIn, onHoverOut);
			$$.mousedown(onMouseDown);
			$$.mouseup(onMouseUp);
		}
		
		
		//
		
		var closestAnchor = $$.parent().children("a.bspbutton");
		if(closestAnchor){
			closestAnchor.hover(onHoverIn, onHoverOut);
			closestAnchor.mousedown(onMouseDown);
			closestAnchor.mouseup(onMouseUp);
		}
	});
}

$(function(){
	$("div.bspbutton").bspbutton();
});
