// Kompatibilitätsmodus
var $j = jQuery.noConflict();

// Events initialisieren
$j(document).ready(layout_initialize);

var login_default_username = 'Benutzername';
var login_default_password = '********';

/**
 * Weist den Elementen des Layouts ihre jeweiligen Startwerte zu.
 * Einige Elemente im DOM werden hier manipuliert und bekommen die
 * weiter unten definierten Funktionen zugewiesen.
 */
function layout_initialize ()
{
	$j('#menu ul').addClass('scripted');
	
	$j('#menu li').filter(function (index) {
		return window.location.href.indexOf($j(this).children('a:first').attr('href')) > -1;
	}).addClass('current');
	
	$j('#crm-login input[type=submit]').hide().after('<input id="submit-image" type="image" src="images/layout/login.png" width="16" height="20" alt="Anmelden" />');
	$j('input#submit-image').css('width', '16px');
	$j('#crm-login input[type=text], #crm-login input[type=password]').css('height', '18px').css('width', '95px');
	
	$j('#crm-login input[type=text]').attr('value', login_default_username).focus(login_username_focus).blur(login_username_blur);
	$j('#crm-login input[type=password]').attr('value', login_default_password).focus(login_password_focus).blur(login_password_blur);
	
	// Externe Links in neuem Fenster öffnen
	$j('a[href^=http://], a[href$=.pdf], a[href$=.png], a[href$=.jpg]').attr('target', '_blank');
	
	glossary_initialize();
}

function login_username_focus () {
	if ($j(this).attr('value') == login_default_username)
		$j(this).attr('value', '');
}

function login_username_blur () {
	if ($j(this).attr('value') == '')
		$j(this).attr('value', login_default_username);
}

function login_password_focus () {
	if ($j(this).attr('value') == login_default_password)
		$j(this).attr('value', '');
}

function login_password_blur () {
	if ($j(this).attr('value') == '')
		$j(this).attr('value', login_default_password);
}


var glossary_filter_count = 0;

function glossary_initialize ()
{
	$j('div#glossary-index > ul').addClass('glossary-main');
	$j('div#glossary-index ul ul').addClass('glossary-sub');
	$j('div[id^=glossary] a[href*=#]').click(glossary_click);
	
	$j('div#glossary-index h2').hide();
	$j('div#glossary-index ul ul').hide();
	$j('div#glossary-index').nextAll().hide();
	
	$j('div#glossary-index').after('<div id="glossary-content"></div>');
	$j('div#path ul li:last').addClass('glossary-title');
	
	$j('div#glossary-index').parent().children('h1:first').before('<div class="glossary-search"><label for="glossary-query">Suchen</label> <input type="text" id="glossary-query" /></div>');
	$j('input#glossary-query').keyup(glossary_filter);
	
	glossary_hash();
}

function glossary_click (event)
{
	location.hash = glossary_get_hash($j(this).attr('href'));
	glossary_hash();
	event.preventDefault();
}

function glossary_filter (event)
{
	if (this.value.length < 2) return;
	
	glossary_set_content();
	glossary_set_letter();
	glossary_set_submenu();
	
	glossary_set_path();
	glossary_set_path('Suchergebnis');

	glossary_filter_count = 0;
	$j('div#glossary-content').hide();
	
	$j('div[id^=glossary-] > div').each(function () {
		if ($j(this).text().toUpperCase().indexOf($j('input#glossary-query').attr('value').toUpperCase()) != -1) {
			$j(this).clone(true).appendTo('div#glossary-content');
			glossary_filter_count++;
		}
	});
	
	if (glossary_filter_count == 0)
{
		$j('div#glossary-content').html('<h2>Suche</h2><p>Zu Ihrem Suchwort sind keine Themen verf&uuml;gbar.</p><p><input type="button" id="glossary-new-search" value="Neue Suche" /></p>');
		$j('input#glossary-new-search').click(function () {
			$j('div#glossary-content').empty();
			$j('input#glossary-query').attr('value', '').focus();
		});
	}
	else if (glossary_filter_count > 5)
	{
		$j('div#glossary-content').append('<h2>Suchergebnis</h2><p>Zu Ihrem Suchwort sind die folgenden Themen verf&uuml;gbar:</p><ul id="glossary-search-result"></ul>');
		$j('div#glossary-content > div').each(function () {
			$j('ul#glossary-search-result').append('<li><a href="#' + $j(this).attr('id') + '">' + $j(this).children('h2:first').text() + '</a></li>');
		}).remove();
		$j('ul#glossary-search-result a').click(glossary_click);
	}
	
	// Artikel mit dem Suchwort im Titel am Anfang platzieren
	$j('div#glossary-content').prepend('<div id="glossary-results-top"></div>');
	$j('div#glossary-content > div').each(function () {
		if ($j(this).children('h2, h3').text().toUpperCase().indexOf($j('input#glossary-query').attr('value').toUpperCase()) != -1) {
			$j(this).insertBefore('div#glossary-results-top');
		}
	});
	$j('div#glossary-results-top').remove();
	
	$j('div#glossary-content').show();
}

function glossary_hash ()
{
	hash = glossary_get_hash(location.hash);
	
	glossary_set_content();
	glossary_set_path();
	glossary_set_letter();
	
	if (hash == '') return;
	
	glossary_set_letter(hash);

	if (hash.indexOf('glossary-') != -1) {
		glossary_set_submenu(hash);
		glossary_set_path(hash.substring(hash.indexOf('glossary-') + 9).toUpperCase());
	}
	else {
		glossary_set_submenu($j('div#' + hash).parents('div[id^=glossary-]').attr('id'));
		glossary_set_content(hash);
		glossary_set_path($j('div#glossary-content h2, div#glossary-content h3').filter(':first').text());
	}
}

function glossary_get_hash (link)
{
	return link.substring(link.indexOf('#') + 1);
}

function glossary_set_submenu (hash)
{
	$j('div#glossary-index > ul.glossary-sub').remove();
	
	if (hash) {
		$j('div#glossary-index a[href$=' + hash + ']').next('ul').clone(true).appendTo('div#glossary-index').show();
	}
}

function glossary_set_content (hash)
{
	if (!hash) {
		$j('div#glossary-content').empty();
	} else {
		$j('div#' + hash).clone(true).appendTo('div#glossary-content');
	}
}

function glossary_set_path (title)
{
	if (!title) {
		$j('div#path ul li.glossary').remove();
		$j('div#path ul li.glossary-title').show();
	} else {
		$j('div#path ul li.glossary-title').hide();
		$j('div#path ul').append('<li class="glossary"> <a href="' + location.href.substring(0, location.href.indexOf('#')) + '">' + $j('div#path li.glossary-title').text() + '</a></li>');
		$j('div#path ul').append('<li class="glossary"> <span>' + title + '</span></li>');
	}
}

function glossary_set_letter (hash)
{
	if (!hash) {
		$j('ul.glossary-main li.current').removeClass('current');
	} else {
		$j('ul.glossary-main li:has(a[href$=#' + hash + '])').addClass('current');
	}
}


