var fontsizeCookie = 'usp_fontsize';
var defaultFontsize = 'fontsize_normal';
var loginareaCookie = 'usp_login';
var debugareaCookie = 'usp_debugarea';
var stdCookieOptions = { 'path': '/', 'expires': 3560 };
var debugTime = false;

jQuery(document).ready(function() {

	// set stylesheet initial
	jQuery.fn.setStylesheet();

	// switch stylesheet on click
	jQuery('#fontsize a').click(function() {
		jQuery.fn.switchStylesheet(jQuery(this).attr('id'));
		return false;
	});

	// debug area
	jQuery('#debug').debugArea();

	// darstellung von uebersichten optimieren
	if ( debugTime ) console.time('uebersichten optimieren');
	jQuery('ul.overview li:last').css('margin-bottom', 0);
	jQuery('div.overview_content p:last').css('margin-bottom', 0);
	jQuery('div.overview_content ul:last').css('margin-bottom', 0);
	jQuery('div.overview_content ul li:last').css('margin-bottom', 0);
	if ( debugTime ) console.timeEnd('uebersichten optimieren');

	// truncate all elements with class truncate
	jQuery('.truncate').each(function() {
		jQuery(this).truncateText();
	});

	// transform all links with class link2button to buttons
	jQuery('.link2button').each(function() {
		jQuery(this).linkToButton();
	});

	// links with class popup should open in a popup and not in a "normal" new window
	jQuery("a.popup").each(function() {
		jQuery(this).popup();
	});

	// links should not stay active
	jQuery('a').click(function() {
		jQuery(this).blur();
	});

	// breadcrumb shortening
	var x = 1;
	var sum = 0;
	var count_links = jQuery("#breadcrumb_content a").get().length;
	var spaces = (((count_links - 1) * 13) + 27);
	var width_avail = (jQuery(".breadcrumb_space").width() - spaces);
	jQuery(jQuery("#breadcrumb_content a").get().reverse()).each(function() {
		jQuery(this).html(jQuery.trim(jQuery(this).html()));
		jQuery(this).children("dfn").remove();
		jQuery(this).html(jQuery(this).html().replace(/ /ig, "&nbsp;"));
		if(x != count_links) {
			sum = (sum + jQuery(this).width());
		}
		if(sum > width_avail) {
			if(x == count_links) {
				jQuery(this).after(" <img src='/USP_Static/img/breadcrumb_spacer.png' alt='' class='breadcrumb_spacer' /><span> &hellip; </span>");
			} else {
				jQuery(this).addClass("hidden").next("img.breadcrumb_spacer").eq(0).addClass("hidden");
			}
		}
		x = (x + 1);
	});

	// move logout area down when a skiplink has the focus
	if ( jQuery('#logout form').length != 0 ) { // only when loggedin
		jQuery('#skiplinks a').live('focusin', function() {
			jQuery('#logout').css('margin-top', '1.1em');
		}).live('focusout', function() {
			jQuery('#logout').css('margin-top', '0');
		});
	}

	// login area (only if div#login_wrapper is present)
	if ( jQuery('#login_wrapper').length > 0 ) {

		jQuery('#Login #fon_area form').attr('autocomplete', 'off');

		jQuery('#login_wrapper h2').addClass('hidden');

		jQuery("#Login #startMobileBku").click(function(){
			jQuery("#Login #bku_area").html("<iframe src='$!{portal.properties.login_mobilebku_url}' width='215' frameborder='0' scrolling='no'></iframe>");
			return false;
		});

		// add login navigation
		jQuery('#login_wrapper').prepend('<ul class="login_type"><li id="fon_trigger"><span>Finanz Online</span></li><li id="bku_trigger"><a href="#">B&uuml;rgerkarte</a></li></ul>');
		jQuery('<input type="button" id="reset_fon_login" value="Zur&uuml;cksetzen" class="button" />').insertAfter('#fon_area .button');

		// set initial login method
		jQuery.fn.toggleLoginarea(jQuery.cookie(loginareaCookie));

		// trigger to show the fon area
		jQuery('#fon_trigger a').live('click', function() {
			jQuery.fn.toggleLoginarea('fon');
			return false;
		});

		// trigger to show the bku area
		jQuery('#bku_trigger a').live('click', function() {
			jQuery.fn.toggleLoginarea('bku');
			return false;
		});

		jQuery('#TID').label2value();
		jQuery('#TID').toggleValue();
		jQuery('#BENID').label2value();
		jQuery('#BENID').toggleValue();
		jQuery('#PIN').label2value();
		jQuery('#PIN').password2text();

		jQuery('#reset_fon_login').live('click', function() {
			jQuery('#TID').label2value();
			jQuery('#BENID').label2value();
			jQuery('#PIN').label2value();
			jQuery('#PIN').password2text();
			jQuery(this).blur();
			return false;
		});
	}
});