var dropdown_maxelements = 10;

function initCoolStuff () 
{
  /** beautify radio inputs **/
  
  $("#content div.search.section input[type=radio]").css("display","none")
  .parent().find("label").prepend("<span class=\"radio-box\"></span>")
  .bind("click",function(e) {
    $(this).parent().parent().find("span").removeClass("selected");
    $(this).find("span").addClass("selected");
    $(this).parent().find("input:radio").removeAttr("checked");
    $("#"+$(this).attr("for")).eq(0).attr("checked", "checked");
  });
  $("#content div.section.search input:checked").parent().find("span").addClass("selected");
  
  
  
  /** make bookmarks in sidebar collapseable **/
  
  var expandable_items = $("#sidebar li.expandable > a");
  
  jQuery.each (expandable_items, function () {
  
    this.onclick = function () {
      toggleExpandable(this);
      return false;
    }
  
  });
  
  /*$("p").click(function(e) {
    ($(this).hasClass("shadow")) ? removeShadow(this): addShadow(this);
  }); */
  
  
  /** do tooltips for icons **/
  
  var icons = $("#content a.icon");
  
  jQuery.each (icons, function () {
    if (!$(this).hasClass("disabled")) {
      this.innerHTML = "<span class=\"tooltip\">"+this.innerHTML+"<span></span></span>";
      $(this.parentNode.getElementsByTagName("span")[0]).css("display","none");
      this.onmouseover = function () {
        $(this.parentNode).find("span.tooltip").eq(0).fadeIn(1);
      }
      this.onmouseout = function () {
        $(this.parentNode).find("span.tooltip").eq(0).fadeOut(1);
      }
    }
  });
  
  /** search result entries act like a link **/
  jQuery.each($("#content div.entry"), function() {
    var url = $(this).find("div.entry-data a").get()[0].href;
    $(this).find("p").bind("click", function (e) {
      document.location.href = url;
    });
  });
  
  /** do confirmation for delete **/
  
  $("#content a.delete.icon").bind("click", function (e) {
  
  /** stilpiloten-version **/
  /*
    $(this).parent().prepend(""+
      "<ul class=\"popup-dropdown\"><li><strong>"+$(this).text()+"</strong></li>"+
      "  <li><a href=\"#\">ja</a></li>"+
      "  <li><a href=\"#\" class=\"abort\">abbrechen</a></li>"+
      "</ul>");
  */
  /** ende stilpiloten-version **/

/** gk-version **/
    $(this).parent().prepend(""+
      "<ul class=\"popup-dropdown\"><li><strong>"+$(this).text()+"</strong></li>"+
      "  <li><a href=\""+$(this).get()[0].href+"\">"+$(this).text()+"</a></li>"+
      "  <li><a href=\"\" class=\"abort\">"+$(this.parentNode.getElementsByTagName("div")[0]).text()+"</a></li>"+
      "</ul>");
/** ende gk-version **/
  
    $(this).parent().css("z-index","100");
    
    //addShadow returns the given dom object also
    var element = addShadow($(this).parent().parent().find("ul.popup-dropdown").get()[0]);
    $(element).find("a.abort").bind("click",function(e) {
      $(this).parent().parent().parent().css("z-index","1");
      $(this).parent().parent().remove();
      return false;
    });
    
    $("body").click(function(e) {
      if (!isChildOf(e.target, element)) {
        $(element).remove();
        $("body").unbind("click");
      }
    });
    return false;
  });
  
  /** text input **/
  jQuery.each ($("#content input[type=text]"), function() {
    var defaultValue = this.value;
    $(this).addClass("default-value");
    $(this).bind("focus", function() {
      if (this.value == defaultValue) {
        this.value = "";
        $(this).removeClass("default-value");
      }
    });
    $(this).bind("blur", function() {
      if (this.value == "") {
        this.value = defaultValue;
        $(this).addClass("default-value");
      }
    });
  });
  
  loadDropdowns();
}

function loadDropdowns()
{
  /** add functionality to the drop down menues **/
  
	var dropdowns = $("#content div.dropdown");
	var dropdown_listitems, dropdown;


	jQuery.each(dropdowns, function (i) 
	{
		var tUL = $(this).find("div.content ul");
		if ($(this).find("div.content").css("width") == "auto") 
			$(this).find("div.content").css("width",$(this).css("width"));
	  
	    dropdown_listitems = this.getElementsByTagName("li");

	    if (dropdown_listitems.length > dropdown_maxelements)
	    	$(tUL).css("height",dropdown_maxelements*22);
	    
//		if($(tUL).attr('id') != "ul_rubric" && $(tUL).attr('id') != "ul_branche")
	   		$(tUL).jScrollPane({showArrows:true, scrollbarWidth: 8, arrowSize: 14});
			
	    addShadow($(this).find("div.content").get());
		/* ie7 doesnt understand width: inherit, so we have to set it manually */
		
		/* hide all dropdown content by default */
		$(this).find("div.content").css("display","none"); 
		
		this.getElementsByTagName("em")[0].onclick = function(){ closeDropDowns(this);toggleExpandable(this);return false;}


		if( $(this).attr('id') != "ul_sort" || $(this).attr('id') != "ul_filter"   )
		{

		jQuery.each($(dropdown_listitems), 
										function () 
										{
//											if( $(this).parent().attr('id')!= "ul_rubric" || $(this).parent().attr('id') != "ul_branche" )
//											{
											  this.onclick = function () 
											  {
												    var text = this.childNodes[0].firstChild.nodeValue;
												    
												    if (text.length > 16)
												    	text = text.substring(0,16)+"&hellip;";
												    
												    $(this.parentNode.parentNode.parentNode.parentNode).find("em a").eq(0).html(text);
												    $(this.parentNode.parentNode.parentNode.parentNode).find("input").eq(0).val = this.id.split("__")[1];
												    removeSelectedStatusInDropDowns(this.parentNode.parentNode);
												    $(this).addClass("selected");
												    toggleExpandable(this.parentNode.parentNode.parentNode);

														document.location.href = url;
												    
												    /*
												    if ((i+1) < dropdowns.length) 
												    {
												      var next_element = $(dropdowns[i+1]).find("div.content").eq(0);
												      if($(this.parentNode.parentNode.parentNode.parentNode.parentNode).hasClass("search-select") &&
												      next_element.parent().parent().hasClass("search-select"))
												    	  toggleExpandable(next_element.get(0));
												      else if ($(this.parentNode.parentNode.parentNode.parentNode).hasClass("search-select")) 
												            void(0);
												     }
												     */
												     if( $(this).parent().attr('id') == "ul_rubric" )
												     {
												     	return true;
												     }
												     else
												     {
												     	return false;
												     }
											   }
//											}

										});
										
		}
	}); 
}

function reLoadUL(aUL)
{
  /** add functionality to the reloaded drop down menues( ul_rubric , ul_brache ) **/
  
	var dropdownsParent 	= null;
	var dropdowns 			= null;
	var dropdown_listitems 	= $('ul#'+aUL).find("li");
	var next_element 		= (aUL == "ul_branche")?'ul_rubric':null;

	if($('ul#'+aUL).parent().hasClass('jScrollPaneContainer'))
	{
		dropdownsParent = $('ul#'+aUL).parent().parent().parent().parent();		
		dropdowns 		= $('ul#'+aUL).parent().parent().parent();
	}
	else
	{
		dropdownsParent = $('ul#'+aUL).parent().parent().parent();
		dropdowns 		= $('ul#'+aUL).parent().parent();		
	}
	
	if ($(dropdowns).find("div.content").css("width") == "auto")
		$(dropdowns).find("div.content").css("width",$(dropdowns).css("width"));
	
    if (dropdown_listitems.length > dropdown_maxelements)
    	$('ul#'+aUL).css("height",dropdown_maxelements*22);
    
    addShadow($(dropdowns).find("div.content").get());
    toggleExpandable($(dropdowns).children('div.content'));	
	//$('ul#'+aUL).jScrollPane({showArrows:true, scrollbarWidth: 8, arrowSize: 14});
				
	jQuery.each($(dropdown_listitems), 
									function () 
									{
									  $(this).click(
												function () 
												{
											  		//Select action//
												    var text = this.childNodes[0].firstChild.nodeValue;
												    if (text.length > 16) text = text.substring(0,16)+"&hellip;";
												    $(dropdowns).find("em a").eq(0).html(text);
												    $(dropdowns).find("input").eq(0).val = this.id.split("__")[1];
												    removeSelectedStatusInDropDowns($('ul#'+aUL));
												    $(this).addClass("selected");

													toggleExpandable($(dropdowns).children('div.content'));		 	
													
													
													if(next_element == null)
													{
														document.location.href = url;
													}
													
													/*
													
													if(next_element != null)
													{
														var tNext_element = $('ul#'+next_element);
														
														if($(tNext_element).parent().hasClass('jScrollPaneContainer'))
															toggleExpandable($(tNext_element).parent().parent());														
														else
															toggleExpandable($(tNext_element).parent());															
													} 
													else if($(dropdownsParent).hasClass("search-select")) 
												    	void(0);
													*/	
												     return false;
												});
									});
	// can be disabled 
	$('ul#'+aUL).jScrollPane({showArrows:true, scrollbarWidth: 8, arrowSize: 14});							
}


function closeDropDowns(notElement) 
{
	var dropdowns = $("#content fieldset.search-select div.dropdown");
	notElementID = $(notElement).parent().find("ul").attr("id");

	jQuery.each($(dropdowns), 
							function () 
							{
								if($(this).find("div.content ul").attr("id") != notElementID)
								{
									  if ($(this).hasClass("open")) 
									  {
									    $("body").unbind("click");
										$(this).find("div.content").eq(0).slideUp("fast");
									    $(this).toggleClass("open").toggleClass("closed");
									  }
								}
							 });
}


function removeSelectedStatusInDropDowns(element)
{
  jQuery.each($(element).find("li"),function(){$(this).removeClass("selected");});
  return element;
}


// output true if element1 is child of element2 in the dom structure
function isChildOf(element1,element2) 
{
  if (!element1.parentNode) 
    return false;
  else if (element1 == element2) 
    return true;
  else 
    return isChildOf(element1.parentNode, element2);
}


var firstClickTrigger; //needed to prevent the event to be fired at first click

function toggleExpandable (element) 
{
  firstClickTrigger = false;
  
  if ($(element).parent().hasClass("open")) 
  {
    $("body").unbind("click");
	$(element).parent().find("div.content").eq(0).slideUp("fast");
    $(element).parent().toggleClass("open").toggleClass("closed");
  }
  else 
  {
    $(element).parent().find("div.content").eq(0).slideDown("fast");
    $(element).parent().toggleClass("closed").toggleClass("open");
    
    // dropdowns auto-collapse when they loose focus
    if($(element).parent().hasClass("dropdown"))
	{
      $("body").click(function(e) 
	  {
        if (firstClickTrigger && !isChildOf(e.target, element.parentNode)) 
		{
          toggleExpandable (element);
          $("body").unbind("click");
        }
        firstClickTrigger = true;
      });
    }
  }
  return element;
}

function addShadow(element) {
  
  $(element).addClass("shadow");
  $(element).append(
    "<span class=\"shadowpart topright\"></span>"+
    "<span class=\"shadowpart right\" style=\"height: "+($(element).outerHeight()-15)+"px\"></span>"+
    "<span class=\"shadowpart bottomright\"></span>"+
    "<span class=\"shadowpart bottom\" style=\"width: "+($(element).outerWidth()-15)+"px\"></span>"+
    "<span class=\"shadowpart bottomleft\"></span>"+
    "<span class=\"shadowpart left\" style=\"height: "+($(element).outerHeight()-15)+"px\"></span>"+
    "<span class=\"shadowpart topleft\"></span>"
  );
  return element;
}

function removeShadow(element) {
  $(element).removeClass("shadow");
  $(element).find("span.shadowpart").remove();
  return element;
}