/**
 * tooltips
 */
 
 /**
  * add lft- and rgt-image to a-tags with class button 
  * change button images on mouseover and mouseout
  * required:
  * + jquery.js
  * 
  * required css:
  * 
	#content a.button:link, #content a.button:visited, #content a.button:hover, #content a.button:active
	{
		text-decoration: none;
		white-space: nowrap;
	}
	
	#content form span.button
	{
		white-space: nowrap;
	}
	
	#content a.button span, #content form span.button input
	{
		color: #FFF;
		background-color: #0088CC;
		font-weight: bold;
		font-family: Arial;
		font-size: 11px;
		padding:2px 3px 2px 3px;
		background-image: url(../img/b_bg_0088CC_a.gif);
		background-repeat: repeat-x;
		background-position: 0px 0px;
	}
	
	#content form span.button input
	{
		height:18px;
		padding:0px 1px 2px 1px;
		border: none;
		cursor: pointer;
		vertical-align:top;
		width:auto;
	 	overflow:visible;
	}
	
	
	* html #content form span.button input, *:first-child+html #content form span.button input
	{
		padding:0px 3px 2px 3px;
	}
	
	#content a.button img, #content form span.button img
	{
		border:none;
		vertical-align:-5px;
	}
	
	#content a.buttonLink span, #content form span.buttonLink input
	{
		background-color: #0088CC;
		background-image: url(../img/b_bg_0088CC_a.gif);
	}
	
	#content a.buttonAction span, #content form span.buttonAction input
	{
		background-color: #0CAF00;
		background-image: url(../img/b_bg_0CAF00_a.gif);
	}
	
	#content a.buttonCaution span, #content form span.buttonCaution input
	{
		background-color: #D91E36;
		background-image: url(../img/b_bg_D91E36_a.gif);
	}
  */
  
$(document).ready(function(){
	
	var regExpImgPos = /((^|\/)b_)(bg|rgt|lft)/;
	var regExpImgPosReplaceLft = "$1lft";
	var regExpImgPosReplaceRgt = "$1rgt";
	var regExpCssUrl = /^url\(("?([^"]*)"?|'?([^"]*)'?)\)$/;
	var regExpCssUrlReplace = "$2";
	//wrap span around text
	$("a.button").wrapInner('<span></span>');
	//add span, lft- and rgt-images
	$("a.button").each(function(){
		var imgUrl = $("> span", this).css("background-image").replace(regExpCssUrl, regExpCssUrlReplace);
		$(this).prepend( '<img src="'+imgUrl.replace(regExpImgPos,regExpImgPosReplaceLft)+'" alt="" />');
		$(this).append( '<img src="'+imgUrl.replace(regExpImgPos,regExpImgPosReplaceRgt)+'" alt="" />');
	})
	
	//hover functionality
	var regExpImg = /(a|b)(\.[a-z]{3,4}("|')?\)?)$/i;
	var regExpImgReplaceA = "a$2";
	var regExpImgReplaceB = "b$2";
	
	$("a.button")
	.mouseover(function(){
		$("> span", this).css("background-image", $("> span", this).css("background-image").replace(regExpImg, regExpImgReplaceB));
		$("> img:first-child", this).attr("src", $("> img:first-child", this).attr("src").replace(regExpImg, regExpImgReplaceB));
		$("> img:last-child", this).attr("src", $("> img:last-child", this).attr("src").replace(regExpImg, regExpImgReplaceB));
	})
	.mouseout(function(){
		$("> span", this).css("background-image", $("> span", this).css("background-image").replace(regExpImg, regExpImgReplaceA));
		$("> img:first-child", this).attr("src", $("> img:first-child", this).attr("src").replace(regExpImg, regExpImgReplaceA));
		$("> img:last-child", this).attr("src", $("> img:last-child", this).attr("src").replace(regExpImg, regExpImgReplaceA));
	});
	
	//INPUT (button,submit) Elements
	$("form span.button").each(function(){
		var imgUrl = $("> input", this).css("background-image").replace(regExpCssUrl, regExpCssUrlReplace);
		$(this).prepend( '<img src="'+imgUrl.replace(regExpImgPos,regExpImgPosReplaceLft)+'" alt="" />');
		$(this).append( '<img src="'+imgUrl.replace(regExpImgPos,regExpImgPosReplaceRgt)+'" alt="" />');
	});
	
	$("form span.button")
	.mouseover(function(){
		$("> input", this).css("background-image", $("> input", this).css("background-image").replace(regExpImg, regExpImgReplaceB));
		$("> img:first-child", this).attr("src", $("> img:first-child", this).attr("src").replace(regExpImg, regExpImgReplaceB));
		$("> img:last-child", this).attr("src", $("> img:last-child", this).attr("src").replace(regExpImg, regExpImgReplaceB));
	})
	.mouseout(function(){
		$("> input", this).css("background-image", $("> input", this).css("background-image").replace(regExpImg, regExpImgReplaceA));
		$("> img:first-child", this).attr("src", $("> img:first-child", this).attr("src").replace(regExpImg, regExpImgReplaceA));
		$("> img:last-child", this).attr("src", $("> img:last-child", this).attr("src").replace(regExpImg, regExpImgReplaceA));
	});
	
	$("form span.button > input")
	.focus(function(){
		$(this).blur();
	});
	
	$("form span.button > input[type=submit]")
	.mouseup(function(){
		$(this).css("cursor","wait");
		$("body").css("cursor","wait");
		$(this).parents("form").submit(function(){
			if($(this).data("submitted") != true)
			{
				$(this).data("submitted", true);
				$(this).submit();	
			}
			else
				return false;
		});
	});
});

