// Ausklappen der Antwort-Statistik nach Parteien
$(document).ready(function(){
	$("#statistik_link").click(function() {
		$("#statistik_alle").slideToggle("slow");
		$("#statistik_alle").load('/startseite/antwort_statistik.php?build=1&show=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
			$("#statistik_alle").slideToggle(7000);
		});
	});
});

// intelligentes on:blur
$("input[type=text],textarea").livequery('blur', function() {
	if($(this).val() == "") {
		$(this).val($(this).attr("alt"));
		/* if($(this).attr('id') != 'thema'){ $(this).css("color","#a1a1a1"); } */
	}
});

// intelligentes on:focus
$("input[type=text],textarea").livequery('focus', function() {
	$(this).css("color","#000");
	if($(this).val() == $(this).attr("alt")) {
		$(this).val("");
	}
});

// Allen Tabs auf der Seite onclick-Methode verpassen
$('div.tabs li').livequery('click', function() {
	if($(this).attr('class').indexOf('disabled') == -1){
		$(this).parent().children('li').each(function() {
			$(this).removeClass('selected').addClass('enabled');
		});
		$(this).removeClass('enabled').addClass('selected');
		if($(this).attr('id')){
			ReloadTab($(this).attr('id'));
			return false;
		}
		return true;
	}
	return false;
});

// Klicks zaehlen, z.B. um Performance von Buttons zu ermitteln (#c_)
if(location.hash.length > 3 && location.hash.substr(0, 3) == '#c_'){
	$.get("/scripts/ajax/count_klicks.php?art=" + location.hash.substr(3), function(){
		location.href = location.href.substr(0, location.href.indexOf('#'));
	});
}

// Customized Scrollbalken hinzufuegen
$('div.scroll-pane').livequery(function(){
	if(!$.browser.msie || jQuery.browser.version.substr(0,1) > 6){
		$(this).jScrollPane({showArrows:true,arrowSize:12,scrollbarWidth:11});
	}
});

// Ergebnis blinken lassen
$('.blink').livequery(function(){
	$(this).animate({opacity: 0}, 500, function(){
		$(this).children().css('color', '#f00');
		$(this).css('color', '#f00');
		$(this).animate({opacity: 1}, 1000, function(){
			$(this).animate({opacity: 0}, 1500, function(){
				$(this).animate({opacity: 1}, 1000);
			});
		});
	});
});

// onclick auf Card bei Profilen (Liste)
$('div.card').livequery('click', function(){

	// Schauen, ob base target gesetzt
	if($('base').length == 1 && $('base').attr('target') == '_blank'){
		window.open($(this).find('a').attr('href'));
	}
	else{
		location.href = $(this).find('a').attr('href');
	}
	return false;
});

// Debug
function DumpObject(obj){
	var str = '';
	for (var a in obj){ str += a+"\n"; }
	alert(str);
}

// Weiterlesen-Text anzeigen
function TextReadOn(a_elems_off){
	if(a_elems_off){
		// Elemente unsichtbar machen
		$('div.readon').animate({opacity: 0}, 500);
		for(var i=0;i<a_elems_off.length;i++){
			a_elems_off[i].animate({height: 0}, 500, function(){
				if(i == a_elems_off.length){

					// Willkommenstext anzeigen
					$('div.pe_welcome').animate({height: saved_height+'px'}, 1500, function(){
						$('div.pe_welcome').addClass('scroll-pane');
						$('div.pe_welcome').css('height', 'auto');
					});
				}
			});
		}
	}
	return false;
}

// Lade Grafik anzeigen / verstecken
function ShowLoadImage(n){
	if(n){
		var n = FindDependingNode(n, 'loading');
		if(n) $(n).css('display', 'inline');
	}
	else{
		$('img.loading').each(function(){
			$(this).css('display', 'none');
		});
	}
}

// In der Herarchie hoeher steigen bis in den Kindknoten oder in dem Knoten
// selbst das Ziel des loads auftaucht (class=reload).
function FindDependingNode(n, cls_name){
	while(n.parents().length > 0){
		if(n.attr('class').indexOf(cls_name) > -1){
			return n;
		}
		n = n.parent();
		if(n.find('.'+cls_name).length > 0){
			return n.find('.'+cls_name+':first');
		}
	}
	return false;
}

// Profilbild ein- und ausblenden
function ProfilPortraitVisible(visible){
	var op = visible?1:0;
	$('div.pe_version div.pe div.portrait').animate({opacity: op}, 1000);
}

// Bei einem Klick auf einen Tab Inhalte nachladen.
function ReloadTab(tab_id, reload_name){

	if($('.tabs:has(#'+tab_id+')')){

		// Name / Klasse des Reloadknotens bestimmen
		reload_name = reload_name || 'reload';

		// Tabs-Collection (<div>) raussuchen in dem der geklickte Tab liegt.
		var n_tab = $('.tabs:has(#'+tab_id+')');

		var n_reload = FindDependingNode(n_tab, reload_name);

		// Reloadknoten wurde gefunden
		if(n_reload && $('#'+tab_id+' a').length > 0){
			// n_reload.children().filter('.effect').each(function(){ });

			// Loading...
			ShowLoadImage(n_reload);

			// Anhand der ID des Tabs entscheiden welche Datei nachgeladen
			// werden soll.
			var param_show = tab_id.indexOf('tab_') > -1?tab_id.substr(4):tab_id;
			switch(param_show){

				// Tabs fuer Startseite
				case 'antworten_wahlen':
				n_reload.load('/startseite/antworten/list.php?build=1&show=antworten_wahlen&filter=all&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'antworten_parlamente':
				n_reload.load('/startseite/antworten/list.php?build=1&show=antworten_parlamente&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;

				// Tabs fuer parlamentarische Arbeit bei Abgeordneten
				case 'profil_ausschussmitgliedschaften':
				n_reload.parent().load('/profile/parlament.php?build=1&show=ausschussmitgliedschaften&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'profil_redebeitraege':
				n_reload.parent().load('/profile/parlament.php?build=1&show=redebeitraege&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'profil_nebentaetigkeiten':
				n_reload.parent().load('/profile/parlament.php?build=1&show=nebentaetigkeiten&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'profil_abstimmungsverhalten':
				if($('#'+tab_id+' a').attr('name')){
					var load_url = '/profile/parlament.php?build=1&show=abstimmungsverhalten&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&filter=' + $('#'+tab_id+' a').attr('name');
					$('#'+tab_id+' a').removeAttr('name');
				}
				else{
					var load_url = '/profile/parlament.php?build=1&show=abstimmungsverhalten&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&filter=all';
				}
				n_reload.parent().load(load_url, function(){
					ShowLoadImage(false);
				});
				break;

				// Tabs fuer Abstimmungsverhalten auf Abstimmungsdetailseite
				case 'abstimmungsverhalten':
				n_reload.load('/abstimmungen/abstimmungsverhalten.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'abstimmung':
				n_reload.load('/abstimmungen/hintergrund.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
					
				// Tabs fuer Ausschusseite
				case 'mitglieder':
				n_reload.load('/ausschuesse/list.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'stellvertreter':
				n_reload.load('/ausschuesse/list.php?build=1&show=stellvertreter&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;

				// Tabs oben in der PE
				case 'profil_uebersicht':
				var load_url = '/profile/pe/uebersicht.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
				case 'profil_person':
				if(!load_url) var load_url = '/profile/pe/person.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
				case 'profil_ziele':
				if(!load_url) var load_url = '/profile/pe/ziele.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
				/*
				 * case 'profil_video': if(!load_url) var load_url =
				 * '/profile/pe/video.php?build=1&cmd=' + a_conf.cmd + '&id=' +
				 * a_conf.id;
				 */
				case 'profil_termine':
				if(!load_url){
					if($('#'+tab_id+' a').attr('name')){
						var load_url = '/profile/pe/termine.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&filter=' + $('#'+tab_id+' a').attr('name');
						$('#'+tab_id+' a').removeAttr('name');
					}
					else{
						var load_url = '/profile/pe/termine.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
					}
				}
				case 'profil_links':
				if(!load_url) var load_url = '/profile/pe/links.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;

				// Profilbild ausblenden
				/*
				 * if(param_show == 'profil_video'){
				 * ProfilPortraitVisible(false); }
				 */

				// Aktuelle Inhalte ausblenden
				n_reload.animate({opacity: 0}, 1000, function(){
					// Neue Inhalte einblenden
					$(this).load(load_url, function(){
						$(this).animate({opacity: 1}, 0, function(){
							ShowLoadImage(false);
						});
					});
				});
				break;

				// Tabs bei den Fragen und Antworten
				case 'profil_fragen_antworten':
				n_reload.load('/profile/public_questions.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'profil_twitter':
				n_reload.load('/profile/public_twitter.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'profil_kandidatencheck':
				n_reload.load('/profile/public_kandidatencheck.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;

				// Tabs in der Liste der Profile
				case 'profiles_simple':
				n_reload.load('/profile/list/sort_simple.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				case 'profiles_advanced':
				n_reload.load('/profile/list/sort_advanced.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				default:
				if(param_show.indexOf('_plz_') > -1){
					n_reload.load('/startseite/einstieg_plz.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&show=' + param_show, function(){
						ShowLoadImage(false);
					});
				}
				break;

				// Tabs im Mitgliederbereich
				case 'mitglieder_login':
				n_reload.load('/mitglieder/form_login.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'mitglieder_register':
				n_reload.load('/mitglieder/form_register.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'mitglieder_no_password':
				n_reload.load('/mitglieder/form_password.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
				case 'mitglieder_politiker':
				n_reload.load('/mitglieder/politiker.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
					ShowLoadImage(false);
				});
				break;
			}
			return false;
		}
	}
	return true;
}