
$(document).ready(function(){
	
	//fuegt geschlecht und produkt als url-Parameter der form-action hinzu 
	 var form = jQuery('#bvvcalculator1data').bind('submit', function (e) {
		var action = form.attr('action');
		var gender = form.find('[name=personGender]').val();
		
		 var product="";
		if( form.find('[name=formstatus]').val() == "0"){
		  product = "arlep_" + form.find('[name=agreementProductVariant]:checked').val() + "_H";
		}else{
		  product = "arlep_" + form.find('[name=agreementProductVariant]').val() + "_H";
		}
		if (gender) {
			 action += '&gender=' + gender;
			action += '&product=' + product;
			form.attr('action', action);
		 }
	 });
	 //fuegt geschlecht und produkt als url-Parameter der form-action hinzu 
	 var form2 = jQuery('#bvvcalculator2data').bind('submit', function (e) {
		 var action = form2.attr('action');
		 var gender = form2.find('[name=personGender]').val();
		 
		  var product="";
		if( form2.find('[name=formstatus]').val() == "0"){
		  product = "arlep_" + form2.find('[name=agreementProductVariant]:checked').val() + "_H";
		}else{
		  product = "arlep_" + form2.find('[name=agreementProductVariant]').val() + "_H";
		}
		if (gender) {
			 action += '&gender=' + gender;
			action += '&product=' + product;
			form2.attr('action', action);
		 }
	 });
	 
	 if( $('meta[name=extranetuser]').attr("content")  != null ) {
		 //fuegt extranet user als URL-Parameter allen a-tags hinzu
		 //$('a:not([href^=mailto],[href*=#])').each(function() {
		 $('a').not('[href^=mailto]').not('[href*=#]').each(function() {
			var hrefExtranet = $(this).attr('href');
			if(hrefExtranet != null && ( hrefExtranet.indexOf("http:") == -1 && hrefExtranet.indexOf("https:") == -1) ){
				if( hrefExtranet.indexOf("?") != -1 )
				{
					$(this).attr('href',hrefExtranet + '&user=' + $('meta[name=extranetuser]').attr('content'));
				}else{
					$(this).attr('href',hrefExtranet + '?user=' + $('meta[name=extranetuser]').attr('content'));

				}
			}
		});

		//fuegt extranet user als URL-Parameter allen form-tags hinzu
		$('form').submit(function (e) {
		 var actionExtranet = $(this).attr('action');
		if(actionExtranet != null){
		    if( actionExtranet.indexOf("?") != -1 )
		    {
			$(this).attr('action',actionExtranet + '&user=' + $('meta[name=extranetuser]').attr('content'));
		    }else{
			$(this).attr('action',actionExtranet + '?user=' + $('meta[name=extranetuser]').attr('content'));
		    }
		}
		});
	}
	
	 
	// ie < 7.0 kann kein li:hover
	if ($.browser.msie && $.browser.version < '7') {
	    var ie6navigation = function (e) {
			if (this.hovering) {
				this.hovering = false;
			} else {
				this.hovering = true;
			}
			$(this).toggleClass('hover');
		};
		function buttonHover(e) {
			ie6navigation.call(this, e);
			$(this.parentNode).toggleClass(this.className);
		}

		$('ul#Navigation > li').bind('mouseover', ie6navigation).bind('mouseout', ie6navigation);
		$('.tipp').bind('mouseover', ie6navigation).bind('mouseout', ie6navigation);
		$('.buttonGo').bind('mouseover', buttonHover).bind('mouseout', buttonHover);
		$('.buttonGoZwei').bind('mouseover', buttonHover).bind('mouseout', buttonHover);
		$('#typo .links input.logout').bind('mouseover', buttonHover).bind('mouseout', buttonHover);
		$('div.rechnerPfad input.pfadbutton').bind('mouseover', buttonHover).bind('mouseout', buttonHover);
		$('.formular .Druckansicht input').bind('mouseover', buttonHover).bind('mouseout', buttonHover);

		
	}
	
	$('div.tabsWithJavaScript').show();
	$('span.searchWithJavaScript').show();

	
	/*
	// schriftgrößen
	$("a#toggleMinus").bind('click', function(){
		$("body").addClass("minus");
		$("body").removeClass("standard");
		$("body").removeClass("plus");
		return false;
	});
	$("a#toggleStandard").bind('click', function(){
		$("body").addClass("standard");
		$("body").removeClass("minus");
		$("body").removeClass("plus");
		return false;
	});
	$("a#togglePlus").bind('click', function(){
		$("body").addClass("plus");
		$("body").removeClass("standard");
		$("body").removeClass("minus");
		return false;
	});
	
*/

	// tooltips, positionierung insgesamt, popup im ie6
	function initTooltips(tips) {
		tips.mouseover(function (e) {
			var left;
			var top;
			var button = this.tooltip.siblings('a.button').get(0);
			var offset = $(this).offset();
			var width;
			var height;
			var tooltip = this.tooltip;
			var oldTooltip = this.oldTooltip;
		  	if ($.browser.msie && $.browser.version < '8') {
				oldTooltip.show();
				width = oldTooltip.width();
				height = oldTooltip.height();
				//offset = oldTooltip.offset();
				left = offset.left;
				top = offset.top;
				tooltip.css({left: left + 'px', top: top + 'px', width: (!this.inForm ? 17 : 0) + width - 6 + 'px', height: height + 2 + 'px'});
				tooltip.children('span.innertip').css({width: (!this.inForm ? 17 : 0) + width - 6 - 34 + 'px'});
				this.iframe.css({width: width - 6 + 'px', height: height + 2 + 'px'});
				tooltip.show();
				oldTooltip.hide();
			} else if (!$.browser.opera) {
				left = button.offsetLeft;
				top = button.offsetTop + button.offsetHeight;
				this.tooltip.css({left: left + 'px', top: top + 'px'});
			} else {
				left = 0;
				top = 0;
				this.tooltip.css({left: right + 'px', top: top + 'px', 'z-index': 15});
			}
		}).each(function (i, item) {
		    var tooltip = $(this).children('span.tooltip');
		    var oldTooltip = tooltip;
		    var iframe;
		    var idIframe;
		    var backgroundColor;
		    var inForm = !!$(this).parents('*.formular').length;
			if ($.browser.msie && $.browser.version < '8') {
		    	idIframe = 'iframe' + Math.ceil(Math.random() * 100000);
				iframe = $('<iframe frameborder="no" src="none" id="' + idIframe + '" name="' + idIframe + '" />');
				tooltip = $('<span class="reattached">' + tooltip.html() + '</span>');
				tooltip.children('span.innertip').mouseout(function (e) {
					if (e.pageX < tooltip.offset().left + 5 || e.pageY < tooltip.offset().top + 5 || e.pageX > tooltip.offset().left + tooltip.width() - 5 || e.pageY > tooltip.offset().top + tooltip.height() - 5) {
						tooltip.hide();
					}
				}).find('*').mouseout(function (e) {
					event.cancelBubble = true;
				});
				backgroundColor = '#fff';
				tooltip.prepend(iframe);
				$('div#typo div.content').append(tooltip);
				window.setTimeout('$(self.frames.' + idIframe + '.document.documentElement).css({"background-color": "' + backgroundColor + '"});', 500);
			}
		    
		    this.inForm = inForm;
		    this.iframe = iframe;
		    this.oldTooltip = oldTooltip;
		    this.tooltip = tooltip;
		}
	)};
	initTooltips($('span.tipp'));


	// Klappfunktion
	$("dl.defList dl dt a").bind("click", function(e) {
		$(this).parent("dt").next("dd").toggleClass('aktiv');
		$(this).toggleClass("aktiv").toggleClass('aktivD');
		return false;
	});
	$("dl.defList dl dt a").click().removeClass('aktiv').removeClass('aktivD');
	$("dl.defList > dt a").bind("click", function(e) {
		$(this).parent("dt").siblings("dd").toggleClass('aktiv');
		$(this).toggleClass("aktiv").toggleClass('aktivD');
		return false;
	});
	$("dl.defList > dt a").click().removeClass('aktiv').removeClass('aktivD');
	
	function openGlossar(id) {
		id = id.substring(id.lastIndexOf('#'));
		var element = $('dt' + id);
		var y;
		element.addClass('aktiv').parents('dl').children('dd').addClass('aktiv').siblings('dt').find('a').addClass('aktivD');
		y = element.offset().top;
		window.scrollTo(0, y);
	}
 	$('a.glossar').click(function (e) {
		openGlossar(this.href);
	});
    if (location.hash) {
		openGlossar(location.hash);
	}
	
 	// content für reiternavigation von anderer url
	var spanReiter = $('div.Reitercontainer li.reiter a');
	var divReiter = $('div.Reitercontainer').siblings('div.reitercontent');
	function getSpanIndex(node) {
		var i, elements = -1, length, nodes = node.parentNode.parentNode.childNodes;
		for (i = 0, length = nodes.length; i < length; ++i) {
		    if (nodes[i].nodeType == 1 && nodes[i].nodeName.toLowerCase() == 'li') {
				++elements;
			}
			if (nodes[i] == node.parentNode) {
				return elements;
			}
		}
		return -1;
	}
	spanReiter.click(function () {
		var regexId = /tabid=([\w\d]+)/;
		var id = regexId.exec(this.href);
		var that = this;
		if (id) {
			divReiter.load(tabSources[id[1]], null, function (data) {
				spanReiter.parent().removeClass('reiterSelected');
				$(that.parentNode).addClass('reiterSelected');
				tb_init(divReiter.find('a.thickbox'));
				initTooltips(divReiter.find('span.tipp'));
			});
			//var i = getSpanIndex(this);
			//divReiter.removeClass('reiterBlock').filter(':eq(' + i + ')').addClass('reiterBlock');
		}
		return false;
	});
	
});

var element;
var size;
var c;
var allInputs = document.getElementsByTagName("input");
	size = allInputs.length;
	for (c=0;c<size;++c) {
		element = allInputs[c];
		if (element.type=="text" || element.type=="password"){
			element.onfocus = function() {
				var oldfunc = element.onfocus;
				return function (e) {
					if (oldfunc) {
						oldfunc.call(this, e);
					}
					this.style.backgroundColor="#fef3d4";
				}
			}();
			element.onblur = function() {
				var oldfunc = element.onblur;
				return function (e) {
					if (oldfunc) {
						oldfunc.call(this, e);
					}
					this.style.backgroundColor="#ffffff";
				}
			}();
	}
}

// Such-Parser

  function searchQueryParser (id) {
  	if (document.getElementById(id).query.value != "") {
  		var queryValue;
  		var queryTmp = document.getElementById(id).query.value;
  		if(document.getElementById(id).word.checked){
    		var testUrl = document.getElementById(id).resultsURL.value.indexOf("\?")==-1;
    			document.getElementById(id).wildcardSearch.value = "none";
    			
    			if(testUrl==true){
    			 document.getElementById(id).resultsURL.value = document.getElementById(id).resultsURL.value + "?word=none";
    			 document.getElementById(id).noResultsURL.value = document.getElementById(id).noResultsURL.value + "?word=none";
    			}else{
    				document.getElementById(id).resultsURL.value = document.getElementById(id).resultsURL.value + "&word=none";
    			  document.getElementById(id).noResultsURL.value = document.getElementById(id).noResultsURL.value + "&word=none";
    			}
  
  		  }else{
  			document.getElementById(id).wildcardSearch.value = "contains";
  				if(document.getElementById(id).resultsURL.value.indexOf("\?")==-1){
      			document.getElementById(id).resultsURL.value = document.getElementById(id).resultsURL.value + "?word=contains";
      			document.getElementById(id).noResultsURL.value = document.getElementById(id).noResultsURL.value + "?word=contains";
      		}else{
      		  document.getElementById(id).resultsURL.value = document.getElementById(id).resultsURL.value + "&word=contains";
      			document.getElementById(id).noResultsURL.value = document.getElementById(id).noResultsURL.value + "&word=contains";
      		}
    		}
    		queryTmp = queryTmp.replace(/\*/g, "");
    		queryTmp = queryTmp.replace(/( AND )|( OR )/g, " ");
    		/*if(!document.getElementById(id).word.checked){
    			queryTmp = queryTmp.replace(/ /g, "* ");
    			queryTmp += "*";
    		}*/
    		if(document.getElementById(id).AndOr[0].checked){
    			queryValue = queryTmp.replace(/ /g, " OR ");
    			document.getElementById(id).resultsURL.value = document.getElementById(id).resultsURL.value + "&op=or";
    			document.getElementById(id).noResultsURL.value = document.getElementById(id).noResultsURL.value + "&op=or";
    		} else {
    			queryValue = queryTmp.replace(/ /g, " AND ");
    			document.getElementById(id).resultsURL.value = document.getElementById(id).resultsURL.value + "&op=and";
    			document.getElementById(id).noResultsURL.value = document.getElementById(id).noResultsURL.value + "&op=and";
    		}
  		document.getElementById(id).query.value = queryValue;
  	}
  }
