//Block Safari < 1.2 from execuing any ready functions
if(jQuery.browser.safari && parseFloat(jQuery.browser.version) < 124) {
	//Since we don't use ready on any other element than the document, we can quite safely assume it was called on the document element and replace it with load
	jQuery.fn.ready = function(callback) {
		var window_query = jQuery(window);
		return window_query.load.apply(window_query, arguments);
	}
}

if(jQuery.browser.safari) {
	jQuery(document).ready(function() {
		jQuery("head").append('<style type="text/css">.footnote-after-table {margin-top: 0px !important;}</style>');
	});
}

if(!jQuery.browser.safari || parseFloat(jQuery.browser.version) >= 526.5) {
	jQuery(document).ready(function() {
		jQuery("head").append('<style type="text/css">button:first-letter, button div:first-letter, .submit:first-letter, input.delete:first-letter, input.back:first-letter { text-transform: uppercase; }</style>');
	});
}

//Set fieldcheck to be an empty function so that form controls that rely on it are usable on non-standardformular pages
var fieldcheck = function(form_element) {
	return true;
};

//Rollover-Init
jQuery(document).ready(function() {
	//Fix for stupid Rico JS bug
	if(window.Rico && Object.prototype.extend)
		delete Object.prototype["extend"];
	
	var all_rollover_links = jQuery("#main_navigation > a, #sub_navigation > a, #header_language_switch > a, #header_meta > a, #header_search_button, #left_navigation .underline > a, a.info_icon, #main_content a, .link");
	all_rollover_links.each(function(index) {
	    var link = jQuery(this);
		if(link.is(".active")) {
			return true;
		}
		
		var is_rollover_link = false;
		var images = link.children("img");
		images.each(function() {
			this.inactive_url = this.getAttribute("src");
			this.active_url = this.getAttribute("src").replace("_n.", "_a.");
			if(this.inactive_url === this.active_url) {
				this.inactive_url = null;
				this.active_url = null;
				return true;
			}
			is_rollover_link = true;
		});
		
		if(is_rollover_link) {
			link.real_target = link.target;
			link.hover(handleRollover, handleRollover);
		}
	});
});

//Span with link class hover fix for ie lt 7
if(jQuery.browser.msie && jQuery.browser.version < 7) {
	jQuery(document).ready(function() {
		jQuery(".link").each(function() {
		    jQuery(this).hover(function() {jQuery(this).addClass("active")}, function() {jQuery(this).removeClass("active")});
		});
	});
}

function handleRollover(event) {
    var is_over = event.type === "mouseover" || event.type === "mouseenter";
	var images = jQuery(this).find("img");
	
	images.attr("src", function() {
		var src = this.src;
		if (this.inactive_url != null && this.active_url != null) {
			return is_over ? this.active_url : this.inactive_url;
		}
		return this.src;
	});
	
	images.trigger("srse:hover_changed");
}

//Clearing-Input elements init
jQuery(document).ready(function(event) {
	var relevant_input_elements = jQuery("input.clearing");
	relevant_input_elements.each(function(index) {
	    var input = this;
	    jQuery(input).bind("focus", function(event) {
			if(this.value === this.getAttribute("value")) {
				this.value="";
			}
		});
	});
});

//Classes for unsupported CSS3-Selectors (:last-child & +)
jQuery(document).ready(function(event) {
	jQuery(".column_wrapper:last-child").addClass("last-child");
	jQuery(".column_wrapper:first-child").addClass("first-child");
	jQuery("h2:first-child").addClass("first-child");
	jQuery(".table + .footnote, table + .footnote").addClass("footnote-after-table");
});

//Flexible accordions
jQuery(document).ready(function(event) {
	jQuery(".accordion.flexible").each(function(){
		var tabs = jQuery(".tab", this);
		var active_tab = jQuery(".tab.active")[0];
		if(!active_tab) {
			active_tab = tabs[0];
		}
		if(tabs.length < 2) {
			jQuery(this).removeClass("flexible");
		} else {
			var open_index = tabs.index(active_tab)+1;
			var acc = new Accordion(this, open_index);
			jQuery(window).bind("load", function() {acc.pack.call(acc);});
			jQuery(window).bind("resize", function() {acc.pack.call(acc);});
		}
	});
});

//Hinweise-Links
jQuery(document).ready(function(){
	jQuery("a.footerblinder").click(function(){ jQuery(this).next().slideToggle('slow'); });
	jQuery("a.footerblinder").click(function(){ jQuery(this).toggleClass("expanded").toggleClass("collapsed"); return false;});
});

//DHTML-Popups (content)
jQuery(document).ready(function(){
	var move_handler = function(event) {
		var opener = jQuery(this);
		var popup = opener.data('popup');
		if(!popup)
			return;
		var top = event.pageY + 5;
		var left = event.pageX - 5;
		if((left - popup.get(0).clientWidth - 5) > 0) {
			left -= popup.get(0).clientWidth;
		}
		if(jQuery.browser.msie) {
			top -= 5;
			left += 5;
		}
		popup.css({top: ""+top+"px", left: ""+left+"px"});
		popup.show();
	};
	
	var openers = jQuery(".content_popup_opener");
	
	openers.each(function() {
		var opener = jQuery(this);
		var popup = opener.nextAll(".content_popup");
		popup = popup.slice(0, 1);
		opener.data('popup', popup);
		if(jQuery.fn.bgiframe) {
			popup.bgiframe();
		}
		popup.appendTo(jQuery("#main_content_wrapper"));
	});
	
	hover_openers = openers.filter(':not(.click)');
	hover_openers.mouseover(move_handler);
	hover_openers.mousemove(move_handler);
	
	hover_openers.mouseout(function(event) {
		var popup = jQuery(this).data('popup');
		if(!popup)
			return;
		popup.hide();
	});
	
	click_openers = openers.filter('.click');
	click_openers.each(function() {
		var opener = jQuery(this);
		var popup = opener.data('popup');
		popup = popup.dialog({autoOpen: false, resizable: false, height: '', title: opener.attr('title')});
		opener.attr('title', '');
		popup.removeClass('content_popup');
		popup = popup.parents('.ui-dialog');
		opener.data('dialog', popup);
		popup.find('.ui-dialog-titlebar-close').insertBefore(popup.find('.ui-dialog-title'));
		move_handler.call(popup, window.event);
	});
	click_openers.click(function() {
		var opener = jQuery(this);
		opener.data('popup').dialog('open');
		var position = opener.offset();
		if(position.left - 305 > 0) {
			position.left -= 305;
		}
		opener.data('dialog').css({top: ""+position.top+"px", left: ""+position.left+"px"});
	});
});