/**
 *	FUNTIONS
 *
 * 	knauf_37_98_0_7165_0_0
 *        |  |  |   |  | |
 * 	titelId  |  |   |  | |
 * untertitelId |   |  | |
 * symbolgruppeId   |  | |
 *           systemId  | |
 *         gruppierungId |
 *               bausteinId
 *
 */

/**
 *	offnet alle Bausteine eines System
 */
function toggleChildren(systemId)
{
//	$("ul.bausteine").fadeOut("fast");

	if ( $("#systeme li ul#children_" + systemId).html() == '' )
	{
		showLoadingScreen();

   		$.post(
   				"ajax_system_kicker.php?todo=get_children&id="+id,
   				function(data)
         	{
         		$("#systeme li ul#children_" + systemId).html(data);
			      $("#systeme li a#system_" + systemId).css("background-image","url(../assets/common/icons/folderopen.gif)");
				  	$("a.active").removeClass("active");
	         	endLoadingScreen();
         	}
   		);
  }
  else
  {
//   	$("#systeme li ul#children_" + systemId).toggle();

   	if ($("#systeme li ul#children_" + systemId).length > 0)
	   	$("#systeme li ul#children_" + systemId).css("display", "none");
   	else
	   	$("#systeme li ul#children_" + systemId).css("display", "block");


    $("#systeme li a#system_" + systemId).css("background-image","url(../assets/common/icons/folderclosed.gif)");
  	$("a.active").removeClass("active");
  }

	//setActive('system_' + systemId);		// aktiv setzen
  makeDragable(".drag");
}


/**
 *	alle Bausteine eines Systems offnen oder schliessen
 */
function toggleAllOpen()
{
	$('#systeme li ul').each(function()
	{
		systemId = str_replace("children_", "", $(this).attr("id"));
   	$("#systeme li ul#children_" + systemId).toggle();
	});
}

/**
 *	toggle Gewerk
 */
function toggleGewerk(gewerkId)
{
		$("#gewerke li ul:not(#gewerke li ul#gewerk_titel_" + gewerkId + ")").css("display", "none");
		$("#gewerke li a").removeClass("akt");

		if ( $("ul#gewerk_titel_" + gewerkId).css("display") == "block" )
	  {
	   	$("ul#gewerke li a.gewerk_" + gewerkId).removeClass("akt");
	   	$("ul#gewerk_titel_" + gewerkId).fadeOut('fast');
		}
	  else
	  {
	   	$("ul#gewerke li a.gewerk_" + gewerkId).addClass("akt");
	   	$("ul#gewerk_titel_" + gewerkId).fadeIn('fast');
		}
}

/**
 *	toggle Titel
 */
function toggleTitel(titelId)
{
		$("ul.under2:not(#gewerk_untertitel_" + titelId + ")").css("display", "none");
		$("ul.under1 li a").removeClass("akt");

		if ( $("ul#gewerk_untertitel_" + titelId).css("display") == "block" )
	  {
	   	$("a.titel_" + titelId).removeClass("akt");
	   	$("ul#gewerk_untertitel_" + titelId).fadeOut('fast');
		}
	  else
	  {
	   	$("a.titel_" + titelId).addClass("akt");
	   	$("ul#gewerk_untertitel_" + titelId).fadeIn('fast');
		}

//   	$("#gewerke li ul li ul").fadeOut("fast");
   	//$("#gewerke li ul li ul#gewerk_untertitel_" + titelId).toggle();
/*
		showLoadingScreen()
   	$("ul.under1").removeClass("akt");
   	$("ul.under2").removeClass("akt");
   	$("ul.under2").css("display", "none");
   	$("#gewerke li ul li ul#gewerk_untertitel_" + titelId).css("display", "block");

	 	element = $("ul.under1 li a#" + titelId);
	 	checkUlAkt(element);


  	$("a.active").removeClass("active");
   	endLoadingScreen();
*/
}

/**
 *	toggle Untertitel
 */
function toggleUntertitel(titelId, untertitelId)
{
	$("ul.systeme:not(#untertitel_system_" + titelId + "_" + untertitelId +")").css("display", "none");
	$("ul.symbolgruppen:not(#untertitel_symbolgruppe_" + titelId + "_" + untertitelId +")").css("display", "none");
	$("ul.under2 li a").removeClass("akt");
	$("ul.bausteine").css("display", "none");

			debug("titel= " + titelId);
			debug("untertitel= " + untertitelId);

  if ( $("ul#untertitel_system_" + titelId + '_' + untertitelId).html() == '' )
	{
		showLoadingScreen();

	 		$.post( "ajax_system_kicker.php?todo=build_system&untertitelId="+untertitelId,
	       	function(data)
	       	{
	       		$("ul#untertitel_system_" + titelId + '_' + untertitelId).html(data);
				   	$("ul#untertitel_system_" + titelId + '_' + untertitelId).css("display", "block");
				   	$("ul#untertitel_symbolgruppe_" + titelId + '_' + untertitelId).css("display", "block");
				   	$("a.untertitel_" + untertitelId).addClass("akt");

	         	endLoadingScreen();
	       	}
	 		);
	}
	else
	{
		if ( $("ul#untertitel_symbolgruppe_" + titelId + '_' + untertitelId).length > 0 )
		{
			if ( $("ul#untertitel_symbolgruppe_" + titelId + '_' + untertitelId).css("display") == "block" )
		  {
		   	$("a.untertitel_" + untertitelId).removeClass("akt");
		   	$("ul#untertitel_symbolgruppe_" + titelId + '_' + untertitelId).fadeOut('fast');
			}
		  else
		  {
		   	$("a.untertitel_" + untertitelId).addClass("akt");
		   	$("ul#untertitel_symbolgruppe_" + titelId + '_' + untertitelId).fadeIn('fast');
			}
		}

		if ( $("ul#untertitel_system_" + titelId + '_' + untertitelId).length > 0 )
		{
			if ( $("ul#untertitel_system_" + titelId + '_' + untertitelId).css("display") == "block" )
		  {
		   	$("a.untertitel_" + untertitelId).removeClass("akt");
		   	$("ul#untertitel_system_" + titelId + '_' + untertitelId).fadeOut('fast');
			}
		  else
		  {
		   	$("a.untertitel_" + untertitelId).addClass("akt");
		   	$("ul#untertitel_system_" + titelId + '_' + untertitelId).fadeIn('fast');
			}
		}

		return true;
	}
}

function toggleGruppierung(id_string)
{
		arrayIds = putHtmlIdInArray(id_string);

		typ = 						arrayIds[0]['typ'];
		titelId = 				arrayIds[0]['titelId'];
		untertitelId = 		arrayIds[0]['untertitelId'];
		symbolgruppeId =	arrayIds[0]['symbolgruppeId'];
		systemId = 				arrayIds[0]['systemId'];
		gruppierungId = 	arrayIds[0]['gruppierungId'];
		bausteinId = 			arrayIds[0]['bausteinId'];

		$("ul.gruppierung_bausteine:not(#gruppierung_" + systemId + "_" + gruppierungId +")").css("display", "none");
		$("ul.bausteine li a").removeClass("akt");

		if ( $("ul#gruppierung_" + systemId + '_' + gruppierungId).css("display") != "none" )
	  {
	   	$("a#" + id_string).removeClass("akt");
	   	$("ul#gruppierung_" + systemId + '_' + gruppierungId).fadeOut('fast');
		}
	  else
	  {
	   	$("a#" + id_string).addClass("akt");
	   	$("ul#gruppierung_" + systemId + '_' + gruppierungId).fadeIn('fast');
		}
}

function toggleSymbolgruppe(id_string, inactive)
{
		arrayIds = putHtmlIdInArray(id_string);

		typ = 						arrayIds[0]['typ'];
		titelId = 				arrayIds[0]['titelId'];
		untertitelId = 		arrayIds[0]['untertitelId'];
		symbolgruppeId =	arrayIds[0]['symbolgruppeId'];
		systemId = 				arrayIds[0]['systemId'];
		gruppierungId = 	arrayIds[0]['gruppierungId'];
		bausteinId = 			arrayIds[0]['bausteinId'];

	$("ul.systeme:not(#symbolgruppe_system_" + untertitelId + "_" + symbolgruppeId +")").css("display", "none");
	$("ul.symbolgruppen li a").removeClass("akt");

  if ( $("ul#symbolgruppe_system_" + untertitelId + '_' + symbolgruppeId).html() == '' )
	{
		showLoadingScreen();

	 		$.post( "ajax_system_kicker.php?todo=build_symbolgruppe_system&untertitelId="+ untertitelId + "&symbolgruppeId=" + symbolgruppeId,
	       	function(data)
	       	{
	       		$("ul#symbolgruppe_system_" + untertitelId + '_' + symbolgruppeId).html(data);
				   	if (inactive != 't')
					   	$("a#" + id_string).addClass("akt");
				   	$("ul#symbolgruppe_system_" + untertitelId + '_' + symbolgruppeId).css("display", "block");
	         	setActive(id_string);
						$("ul.symbolgruppen li a").removeClass("active");
		        endLoadingScreen();
	       	}
	 		);
	}
	else
	{
		if ( $("ul#symbolgruppe_system_" + untertitelId + '_' + symbolgruppeId).css("display") == "block" )
	  {
	   	$("a#" + id_string).removeClass("akt");
	   	$("ul#symbolgruppe_system_" + untertitelId + '_' + symbolgruppeId).fadeOut('fast');
		}
	  else
	  {
	   	$("a#" + id_string).addClass("akt");
	   	$("ul#symbolgruppe_system_" + untertitelId + '_' + symbolgruppeId).fadeIn('fast');
		}
	}
}

/**
 *	toggle System
 */
function toggleSystem(id_string)
{
		arrayIds = putHtmlIdInArray(id_string);

		typ = 						arrayIds[0]['typ'];
		titelId = 				arrayIds[0]['titelId'];
		untertitelId = 		arrayIds[0]['untertitelId'];
		symbolgruppeId =	arrayIds[0]['symbolgruppeId'];
		systemId = 				arrayIds[0]['systemId'];
		gruppierungId = 	arrayIds[0]['gruppierungId'];
		bausteinId = 			arrayIds[0]['bausteinId'];

		$("ul.bausteine:not(#children_" + untertitelId + "_" + systemId +")").css("display", "none");
		$("ul.systeme li a").removeClass("akt");
		$("ul.gruppierung_bausteine").css("display", "none");

		if ( $("ul#children_" + untertitelId + '_' + systemId).html() == '' )
		{
			showLoadingScreen();

	   		$.post( "ajax_system_kicker.php?todo=get_children&systemId=" + systemId + "&untertitelId=" + untertitelId,
	         	function(data)
	         	{
	         		$("ul#children_" + untertitelId + '_' + systemId).html(data);
/*
				      $("a#system_" + systemId).css("background-image","url(../assets/common/icons/folderopen.gif)");
					   	$("ul#children_" + untertitelId + '_' +systemId).css('display', 'block');
*/
					   	$("a#" + id_string).addClass("akt");
					   	$("ul#children_" + untertitelId + '_' +systemId).css('display', 'block');
					   	$("ul.gruppierung_bausteine").css("display", "none");

		         	endLoadingScreen();
	         	}
	   		);
	  }
	  else
	  {
/*
	   	$("ul#children_" + systemId).css("display", "block");

			// pruefen ob die liste angezeigt wird oder nicht
			var value = $("ul#children_" + systemId).css("display");
			if (value == 'none')
		    $("a#system_" + systemId).css("background-image","url(../assets/common/icons/folderclosed.gif)");
		  else
		    $("a#system_" + systemId).css("background-image","url(../assets/common/icons/folderopen.gif)");
*/
			if ( $("ul#children_" + untertitelId + '_' + systemId).css("display") == "block" )
		  {
		   	$("a#" + id_string).removeClass("akt");
		   	$("ul#children_" + untertitelId + '_' + systemId).fadeOut('fast');
			}
		  else
		  {
		   	$("a#" + id_string).addClass("akt");
		   	$("ul#children_" + untertitelId + '_' + systemId).fadeIn('fast');
			}
	  }

			/* wird nur auf ie6 benoetigt :/ */
			$("ul.systeme li").each(function(){
					if ( $(this).html() == '' )
						$(this).remove();
			 });
}

function toggleMarmoritSystem(systemId)
{
	$("ul.systeme").fadeOut("fast");

	if ( $("ul#marmorit_system_" + systemId).html() == '' )
	{
		showLoadingScreen();

			$.post( "ajax_system_kicker.php?todo=build_marmorit_system&systemId="+systemId,
	       	function(data)
	       	{
	       		$("ul#marmorit_system_" + systemId).html(data);
				   	$("ul#marmorit_system_" + systemId).css({'display' : 'block'});
	         	endLoadingScreen();
	       	}
	 		);
	}
	else
	{
   	$("#marmorit_system_" + systemId).toggle();
  }


			$("ul.systeme li").each(function(){
					if ( $(this).html() == '' )
						$(this).remove();
			 });
}

function toggleMarmoritBausteine(bausteinId)
{
	$("ul.bausteine").fadeOut("fast");

	if ( $("ul#marmorit_baustein_" + bausteinId).html() == '' )
	{
		showLoadingScreen();

			$.post( "ajax_system_kicker.php?todo=build_marmorit_bausteine&bausteinId=" + bausteinId,
	       	function(data)
	       	{
	       		$("ul#marmorit_baustein_" + bausteinId).html(data);
				   	$("ul#marmorit_baustein_" + bausteinId).css({'display' : 'block'});
	         	endLoadingScreen();
	       	}
	 		);
	}
	else
	{
   	$("#marmorit_baustein_" + bausteinId).toggle();
  }
}


/**
 *	alle Bausteine eines Systems offnen oder schliessen
 */
function toggleAll()
{
  $("#gewerke li ul").toggle();
}


function getTreeIds(systemId)
{

}
/**
 *	Session Liste fuer temporaere Bausteine
 *  hinzufuegen per Drag
 *
 * 	knauf_37_98_0_7165_0_0
 *        |  |  |   |  | |
 * 	titelId  |  |   |  | |
 * untertitelId |   |  | |
 * symbolgruppeId   |  | |
 *           systemId  | |
 *         gruppierungId |
 *               bausteinId
 *
 */

function toggleTree(systemId, inactive)
{
	$.post('ajax_system_kicker.php',
	{
		todo: "get_id_string",
		systemId: systemId
	},
	function(data)
	{

		gewerkId = data.gewerkId;
		id_string = data.id_string;
		flag_symbolgruppe = data.flag_symbolgruppe;

		arrayIds = putHtmlIdInArray(id_string);

		typ = 						arrayIds[0]['typ'];
		titelId = 				arrayIds[0]['titelId'];
		untertitelId = 		arrayIds[0]['untertitelId'];
		symbolgruppeId =	arrayIds[0]['symbolgruppeId'];
		systemId = 				arrayIds[0]['systemId'];
		gruppierungId = 	arrayIds[0]['gruppierungId'];
		bausteinId = 			arrayIds[0]['bausteinId'];

  	toggleGewerk(gewerkId);		// gewerk einblenden
  	toggleTitel(titelId);			// titel einblenden


		/* css */
		$("ul.systeme:not(#untertitel_system_" + titelId + "_" + untertitelId +")").css("display", "none");
		$("ul.symbolgruppen:not(#untertitel_symbolgruppe_" + titelId + "_" + untertitelId +")").css("display", "none");
		$("ul.under2 li a").removeClass("akt");
		$("ul.bausteine").css("display", "none");

		if (flag_symbolgruppe != 't')
		{
		  if ( $("ul#untertitel_system_" + titelId + '_' + untertitelId).html() == '' )
			{
				showLoadingScreen();
					$.ajax({
					  url: "ajax_system_kicker.php?todo=build_system&untertitelId=" + untertitelId,
					  cache: false,
					  success: function(data){
					    	$("ul#untertitel_system_" + titelId + '_' + untertitelId).html(data);
						   	$("ul#untertitel_system_" + titelId + '_' + untertitelId).css("display", "block");
						   	$("ul#untertitel_symbolgruppe_" + titelId + '_' + untertitelId).css("display", "block");
						   	$("a.untertitel_" + untertitelId).addClass("akt");

			         	endLoadingScreen();
								toggleSystem(id_string);		// system oeffnen
								showSystem(id_string);
			         	return true;
					  }
					});
			} // if untertitel_system
		}
		else
		{
		  if ( $("ul#untertitel_system_" + titelId + '_' + untertitelId).html() == '' )
			{
				showLoadingScreen();
					$.ajax({
					  url: "ajax_system_kicker.php?todo=build_system&untertitelId=" + untertitelId,
					  cache: false,
					  success: function(data){
					    	$("ul#untertitel_system_" + titelId + '_' + untertitelId).html(data);
						   	$("ul#untertitel_system_" + titelId + '_' + untertitelId).css("display", "block");
						   	$("ul#untertitel_symbolgruppe_" + titelId + '_' + untertitelId).css("display", "block");
						   	$("a.untertitel_" + untertitelId).addClass("akt");
						   	$("a.untertitel_" + untertitelId).addClass("akt");

			         	endLoadingScreen();
								toggleSymbolgruppe(id_string, inactive);		// symbolgruppe oeffnen

								// nur ordner oeffnen
								if (inactive == 't')
								{
									var new_id_string = typ + '_' + titelId + '_' + untertitelId + '_' + symbolgruppeId + '_0_0_0';
							   	$("a#" + new_id_string).addClass("akt");
//							   	$("a#" + id_string).removeClass("akt");
								}
								else
								{
									showSystem(id_string);
								}

			         	return true;
					  }
					});
			} // if untertitel_system
		} // flag_symbolgruppe == t

	}, "json"); // post

}

function addSessionListByDrag()
{
		/* Session liste zum drucken fuellen */
		$("#session_list").droppable(
		{
			accept: ".drag",
			tolerance: 'intersect',
			activeClass: 'droppable-active',
			hoverClass: 'droppable-hover',
			refreshPositions: true,
			drop: function(ev, ui)
			{
				chooseSessionOption($(ui.draggable).attr("id") ,$(ui.draggable).html());

				/* nur fuer ie6, grml :(
				id = $(ui.draggable).attr("id");
				$("ul.systeme li > a#" + id).css("z-index", "0");
				$("ul.systeme li > a#" + id).css("top", "0");
				$("ul.systeme li > a#" + id).css("left", "0");
	*/

				if ($('a.drag').hasClass("grouped"))
				{
					$(".grouped").each(function(i)
					{
						id_string = $(this).attr('id');
						if (id_string != $(ui.draggable).attr("id"))
						{
							chooseSessionOption(id_string, $(this).html());
						}
					});

					$('a.drag').removeClass("grouped");
				}
			}
		});
}

function chooseSessionOption(id_string, html)
{
		arrayIds = putHtmlIdInArray(id_string);

		typ = 						arrayIds[0]['typ'];
		titelId = 				arrayIds[0]['titelId'];
		untertitelId = 		arrayIds[0]['untertitelId'];
		symbolgruppeId =	arrayIds[0]['symbolgruppeId'];
		systemId = 				arrayIds[0]['systemId'];
		gruppierungId = 	arrayIds[0]['gruppierungId'];
		bausteinId = 			arrayIds[0]['bausteinId'];

		showLoadingScreen();
		if (typ == 'knauf')
		{
			if (bausteinId > 0)
			{															 									/* einzelner Baustein */
				//debug('createSessionListBaustein()');
				createSessionListBaustein(id_string, html);
			}
			else if (systemId > 0 && gruppierungId > 0)		 	/* alle gruppierten bausteine */
			{
				//debug('getBausteinGruppierung()');
				getBausteinGruppierung(id_string);
			}
			else if (systemId > 0 && symbolgruppeId == 0 && gruppierungId == 0)
			{
				//debug('getSystem()');
				getSystem(id_string);													/* alle bausteine eines systems */
			}
			else if (systemId > 0 && symbolgruppeId == 0)
			{
				//debug('getGruppierung()');
				getGruppierung(id_string);
			}
			else
			{
				//debug('createSessionListSystem()');
				createSessionListSystem(id_string, html);			/* system */
			}
		}
   	endLoadingScreen();
}

/**
 *	Sortable fuer die Sessionlist
 *  hinzufuegen per Drag
 */
function makeSortable()
{
	$("#session_list ul").sortable(
	{
		out: function(ev, ui)
		{
			id = ui.item.attr("id");
			$('#session_list li#' + id).remove();
			$('#session_list li#' + id).addClass("unsichtbar");
			$('#session_list li#' + id).removeClass("active");
			$('#session_list li#' + id).removeAttr("id");
			$('#session_list li#' + id + " img").remove();
	  }

	});
}

function addSessionlistByDoubleClick(html_id)
{
		$('a#' + html_id).bind("dblclick", function()
		{
	  	addSessionListByLink();
	  });
}

/**
 *	Session Liste fuer temporaere Bausteine
 *  hinzufuegen per Link
 */
function addSessionListByLink()
{
		var text = '';
		var id_string = 0;

		if ($('a.active').hasClass('active'))
		{
			id_string = $('a.active').attr("id");
			text = $('a.active').html();
		}

		if (id_string == ''  && text == '')
		{
			alert("Es wurde kein Element ausgew"+ unescape("%E4") +"hlt!");
			return false;
		}

		debug(id_string);
		debug(text);

		chooseSessionOption(id_string, text)

		if ($('a.drag').hasClass("grouped"))
		{
			$(".grouped").each(function(i)
			{
				id_string_group = $(this).attr('id');
				if (id_string != id_string_group)
				{
					chooseSessionOption(id_string, $(this).html());
				}
			});

			$('a.drag').removeClass("grouped");
		}

}

/*
 * div list fuellen
 */
function createSessionListBaustein(id_string, text)
{
///	arrayIds = putHtmlIdInArray(id_string);
//	bausteinId = arrayIds[0]['bausteinId'];

/*  Session list schon vorhanden
	if ( $("#session_list ul li." + id_string).length > 0 )
	{
		setActiveSessionList(id_string);
//		debug("Element: '" + text + "' wurde bereist ausgew"+ unescape("%E4") +"hlt!");
		return false;
	}
*/
	if ( $("#session_list ul").html() )	$("#session_list p").css("display", "none");

	$("#session_list ul").html(
		$("#session_list ul").html() +
		'<li id="'+ id_string +'" class="'+ id_string +'" onclick="setActiveSessionList(\''+ id_string +'\'); showBaustein(\''+ id_string +'\');">' +
			'<img src="images/delete.gif" onclick="deleteBaustein(\''+ id_string +'\');" />' +
 			'<a href="#">' + text + '</a>' +
		'</li>'
	);

	//onmouseover="showBaustein(\''+ id_string +'\'); setActiveSessionList(\''+ id_string +'\');"
	if ( $("#session_list ul").html() )	$("#session_list p").css("display", "none");

	saveSessionList(id_string, text);
}


/*
 * div list fuellen
 */
function createSessionListSystem(id_string, text)
{
	showLoadingScreen();

/*  Session list schon vorhanden
	if ( $("#session_list ul li." + id_string).length > 0 )
	{
		setActiveSessionList(id_string);
//		debug("Element: '" + text + "' wurde bereist ausgew"+ unescape("%E4") +"hlt!");
		return false;
	}
*/
	if ( $("#session_list ul").html() )	$("#session_list p").css("display", "none");

	$("#session_list ul").html(
		$("#session_list ul").html() +
		'<li id="'+ id_string +'" class="'+ id_string +'" onclick="setActiveSessionList(\''+ id_string +'\'); showSystem(\''+ id_string +'\');">' +
			'<img src="images/delete.gif" onclick="deleteBaustein(\''+ id_string +'\');" />' +
 			'<a href="#">' + text + '</a>' +
		'</li>'
	);

	if ( $("#session_list ul").html() )	$("#session_list p").css("display", "none");

	saveSessionList(id_string, text);
 	endLoadingScreen();
}



/**
 *	loescht einen Baustein in dem Session div per ID
 */
function deleteBaustein(id)
{
		$('#session_list ul li').each(function(index)
		{
			id_element = $(this).attr("id");

			if (id_element == id)
			{
				sid = addSid();
				$.post("ajax_system_kicker.php?todo=delete_session_baustein&"+ sid +"&id_string=" + id,
			       	function(data){}
				);
				$(this).remove();
			}
		});

	if (! $("#session_list ul li").attr('id') )
		$("#session_list p").css("display", "block");
}

/**
 *	loescht den kompletten inhalt der session_list
 */
function deleteSessionList()
{
	sid = addSid();
	$.post("ajax_system_kicker.php?todo=delete_session_baustein&"+ sid,
       	function(data){}
	);

	$("#session_list ul li").remove();
	$("#session_list ul").html("");

	if ( $("#session_list ul").html() == '')
		$("#session_list p").css("display", "block");
}

//*
function deleteSessionListActive()
{
	id = $("#session_list ul li.active").attr('id');
	sid = addSid();
	$.post("ajax_system_kicker.php?todo=delete_session_baustein&"+ sid +"&id_string=" + id,
       	function(data){}
	);

	$("#session_list ul li.active").remove();

	if ( $("#session_list ul").html() == '')
		$("#session_list p").css("display", "block");
}
//*/

/**
 *	gibt alle pipes zureuck
 */
function getSessionListIds()
{
	var string = '';
	$('#session_list ul li').each(function(){
		if ( $(this).attr("id") )
			string += $(this).attr("id") + '|';
	});

	string = string.substr(0, string.length-1);		// entfernt die letzte pipe

	return string;
}

/**
 *	holt alle bausteine eines Systems
 */
function getSystem(id_string)
{
	sid = addSid();
	$.post( "ajax_system_kicker.php?todo=get_system&id_string=" + id_string + '&' + sid,
	 	function(data)
   	{
   		$("#session_list ul").append(data);
   	}
	);

	checkSessionList();
}


/**
 *	holt alle bausteine einer Gruppierung
 */
function getBausteinGruppierung(id_string)
{
	sid = addSid();
	$.post( "ajax_system_kicker.php?todo=get_gruppierung&id_string=" + id_string + '&' + sid,
	 	function(data)
   	{
   		$("#session_list ul").append(data);
   	}
	);
	checkSessionList();
}

/**
 *	holt alle gruppierungen eines systems
 */
function getGruppierung(id_string)
{
	sid = addSid();
	$.post( "ajax_system_kicker.php?todo=get_gruppierung_by_system&id_string=" + id_string + '&' + sid,
	 	function(data)
   	{
   		$("#ajaxTemp").html(data);
   	}
	);
}

function getMarmoritSystem(systemId)
{
	$.post( "ajax_system_kicker.php?todo=get_marmorit_system&systemId="+systemId,
	 	function(data)
   	{
   		$("#ajaxTemp").html(data)
   	}
	);
}



/**
 *	Bsp.
 * html_id
 * 	knauf_37_98_0_7165_0_0
 *        |  |  |   |  | |
 * 	titelId  |  |   |  | |
 * untertitelId |   |  | |
 * symbolgruppeId   |  | |
 *           systemId  | |
 *         gruppierungId |
 *               bausteinId
 */
 function putHtmlIdInArray(id_string)
 {
		var html_id_array_new = new Array();
		html_id_array_new[0] = new Object();
		var html_id_array = id_string.split("_");

		html_id_array_new[0]['typ'] = 						html_id_array[0];
		html_id_array_new[0]['titelId'] = 				html_id_array[1];
		html_id_array_new[0]['untertitelId'] = 		html_id_array[2];
		html_id_array_new[0]['symbolgruppeId'] = 	html_id_array[3];
		html_id_array_new[0]['systemId'] = 				html_id_array[4];
		html_id_array_new[0]['gruppierungId'] = 	html_id_array[5];
		html_id_array_new[0]['bausteinId'] = 			html_id_array[6];

		return html_id_array_new;
 }

/**
 *	aktiviert die elemte damit sie Dragable sind
 */
function makeDragable(element)
{
   $(element).draggable(
   {
			helper: 'clone',
    	dragPrevention: "input,textarea,li,img",
			zIndex: 	1000,
			ghosting:	true,
			opacity: 	0.7,
			revert:	'invalid'
   });
}

function makeGrouped()
{
	$("a.drag").click(function(event)
	{
		if (event.shiftKey)
		{
			$(this).toggleClass('grouped');
		}
	});
}

function sendExportMethodForm()
{
		string = unescape("ausf%FChren");

		$('#exportMethodForm').ajaxSubmit(
		{
			success: showResponse,
			dataType: 'json',
			beforeSubmit:function(){
				if ($("#exportbutton").val() != string)
				{
					return checkInputEmail();
				}
			}
		});
}

function checkEmailSendForm()
{
	$(".exportemail").css("display", "none");

	$("input.exportMediumId").click(
		function()
		{
			wert = $(this).val();
			if (wert == 2)
			{
				$(".exportemail").fadeIn();
				$("#exportbutton").val("senden");
			}
			else
			{
				$(".exportemail").fadeOut();
				string = unescape("ausf%FChren");
				$("#exportbutton").val(string);
			}
		}
	);
}

/**
 *	prueft ob es sich um ein System oder ein Baustein handelt
 */
function checkSystemOrBaustein(id)
{


	var string = " " + id;
	var ergebnis = string.search(/system_/);
	if (ergebnis != -1)
		return 'system';
	else
		return 'baustein';
}

function checkClick()
{
//	$('a.gewerk').click(function()	/* gewerk */
//	{
//		$('a.titel').removeClass("akt");
//		$('a.gewerk').removeClass("akt");
//		$(this).addClass("akt");
//	});

	$('a.titel').click(function()	/* titel */
	{
		$('a.untertitel').removeClass("akt");
		$('a.titel').removeClass("akt");
		$(this).addClass("akt");
	});

	$('a.untertitel').click(function()	/* untertitel */
	{
		$("ul.systeme li a").removeClass("akt");
		$('a.untertitel').removeClass("akt");
		$(this).addClass("akt");
	});
	$("ul.systeme li a").click(function()
	{
		$("ul.bausteine li a").removeClass("akt");
		$("ul.systeme li a").removeClass("akt");
		$(this).addClass("akt");
	});
	$("ul.bausteine li a").click(function()
	{
		$("ul.bausteine li a").removeClass("akt");
		$(this).addClass("akt");
	});
	$("ul.gruppierung_bausteine li a").click(function()
	{
		$("ul.gruppierung_bausteine li a").removeClass("akt");
		$(this).addClass("akt");
	});
}

function checkInputEmail()
{
	var str = $('.exportemail:input').val();
//	var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var filter=/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

	if (filter.test(str))
		return true;
	else
	{
		var str = $('.exportemail:input').css('border', '1px solid red');
		string = unescape("Bitte geben Sie eine g%FCltige Email-Adresse ein!");
		alert(string);
		return false;
	}
}

function checkUlAkt(element)
{

	if ( $(element).hasClass("akt") )
		$(element).removeClass("akt");
	else
		$(element).addClass("akt");

}

function checkSessionList()
{
		if ( $("#session_list ul").length > 0)
			$("#session_list p").css("display", "none");
		else
			$("#session_list p").css("display", "block");
}

function showResponse(value)
{
	id_string = getSessionListIds();
	sid = addSessionId();

	$.post('ajax_system_kicker.php',
					{
						todo: "export_start",
						id_string: id_string,
						exportMethodId: value.exportMethodId,
						exportMediumId: value.exportMediumId,
						email: value.email,
						PHPSESSID: sid
					},
					function(data)
					{
						$('#TB_ajaxContent').html(data)
					}
				);
}


function showLoadingScreen()
{
//	tb_show('Seite wird geladen','#TB_inline?height=24&width=230&inlineId=LoadingContent','');
	//tb_show('Seite wird geladen','loading.php?height=50&width=230','');
	$("#LoadingScreen").fadeIn("fast");
}
function endLoadingScreen()
{
	$("#LoadingScreen").fadeOut("fast");
}


function showBaustein(id_string)
{
	showLoadingScreen();
	$.post( "ajax_system_kicker.php?todo=show_baustein&id_string=" + id_string,
		function(data)
		{
			$("#content_auschreibung").html(data);
		}
  );

  setActive(id_string);
 	endLoadingScreen();
}

function saveSessionList(id_string, text)
{
	//return false;  // erstmal ausgeschaltet

	sid = addSid();
//	text = htmlentities(text, 'UTF-8');
	$.post( "ajax_system_kicker.php?todo=save_session&"+ sid +"&id_string=" + id_string + "&text=" + encodeURIComponent(text),
		function(data)
		{
			$("#ajaxTemp").html(data);		// wird nicht verwebdet
		}
  );

}

function loadSessionList()
{
	Check = confirm("Wollen Sie die gespeicherten Elemente laden ?");
	if (Check != true)
	{
		deleteSessionList();
		return false;
	}

	showLoadingScreen();
	sid = addSid();
//	text = htmlentities(text, 'UTF-8');
	$.post( "ajax_system_kicker.php?todo=load_session&"+ sid,
		function(data)
		{
			$("#session_list ul").append(data);
			endLoadingScreen();
			checkSessionList();
		}
  );
}

function showSystem(id_string)
{
	showLoadingScreen();
	$.post( "ajax_system_kicker.php?todo=show_system&id_string=" + id_string,
		function(data)
		{
			$("#content_auschreibung").html(data);
		}
  );

  setActive(id_string);
 	endLoadingScreen();
}

function openLink(url)
{
	fenster = window.open('','_blank');
	fenster.location = url;
}

/**
  * setzt den aktiven href bei dem spaeter per klick die ID abgerufen wird.
  */
function setActive(str_id)
{
	$('a').removeClass("active");
	$('a.titel').removeClass("active");
	$('a.untertitel').removeClass("active");
	$('ul.systeme li a').removeClass("active");
	$('ul.symbolgruppen li a').removeClass("active");
	$('ul.gruppierung_bausteine li a').removeClass("active");
	$('ul.bausteine li a').removeClass("active");

	$('a#' + str_id).addClass("active")
}
function setActiveSuche(str_id)
{

	$('div#results ul li a').removeClass("active");
	$('div#results ul li a#' + str_id).addClass("active")
}
function setActiveSessionList(id_string)
{
		$('#session_list ul li.active').removeClass("active");
		$('#session_list ul li.' + id_string).addClass("active");
}

function searchResultsInterval()
{
	text = $('#suche #suchfeld').val();
	//text = text.replace(/ /g, "#_#");

	if (text.length > 0)
	{
		$('#resultsStatusBeginn').css('display', 'block');
	}

	if (text.length > 3)
	{

		$('#resultsStatusBeginn').css('display', 'none');
		$('#resultsStatus').css('display', 'block');
	}

	if (timeout == 0)
	{
		timeout = 1;

		intervalAktiv = window.setInterval("searchResults()", 1000);
	}
}


function showStatus()
{
	$('#suche #resultsStatus').css('display', 'block');
}



function searchResults()
{
	window.clearInterval(intervalAktiv);
	text = $('#suche #suchfeld').val().replace(/ /g, "_;_");
	text = str_replace('"', "_:_",text);
	text = str_replace("'", "_:_",text);

	//text = text.replace(/ /g, "#_#");

	//console.debug(text);

	// wenn kein text eingeben wurde.
	if (text.length > 0)
		$('#suche #results').css('display', 'block');


	  $('#suche #results').load(
	    'ajax_system_kicker.php?todo=search&text=' + text,
	    function ()
	    {
				timeout = 0;

				$('#suche #results').css('display', 'block');

				$('#suche #suchfeld').focus();
	    }
		);
		$('#suche #suchfeld').focus();
}

function searchClose()
{
	$('#suche #suchfeld').val("");
	$('#suche #results').css('display', 'none');
}

function checkEingabe() {

	if(typeof keypr != 'undefined')
		clearTimeout(keypr);
	else
		keypr = setTimeout(debug('start suche'),2000);
}

function debug(error)
{
	if (window.console)
	{
		console.debug(error);
	}
}


function str_replace (search, replace, subject)
{
  var result = "";
  var  oldi = 0;
  for (i = subject.indexOf (search)
     ; i > -1
     ; i = subject.indexOf (search, i))
  {
    result += subject.substring (oldi, i);
    result += replace;
    i += search.length;
    oldi = i;
  }
  return result + subject.substring (oldi, subject.length);
}

function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
   return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}


function sort_up()
{
 var current = $('#session_list ul li.active').attr('id');
 var parent = $('#session_list ul li.' + current).parent().attr('id');
 var prev = $("#session_list ul li." + current).prev().attr('id');

 $("#session_list ul li." + prev).insertAfter("#session_list ul li." + current);
}

function sort_down(eventObject)
{
  var current = $('#session_list ul li.active').attr('id');
  var parent = $('#session_list ul li.' + current).parent().attr('id');
  var next = $("#session_list ul li." + current).next().attr('id');

  $("#session_list ul li." + next).insertBefore("#session_list ul li." + current);
}