$j(document).ready(function () {
  
  /* Hover fix for IE */
  $j('#userlinks > li, #tln .tlnmain li').hover(function() {
    $j(this).addClass('sfhover');
  }, function() {
		$j(this).removeClass('sfhover');
	});
	
	/* left hand navigation tree expand/collapse */
	$j('img.img_navindicator').click(function() {
    var parent = $j(this).parent().parent();
    if (parent.hasClass('leaf')) {return;}
    
    parent.toggleClass('closed');
  });	
  
  /* add inner markup for sdnButton */
  $j('.sdnButton').wrapInner('<span></span>');
  
  /* tln - add last li for closing border */
  var tlnLastLi = $j('#tln .tlnmain > ul > li:last-child');
  if (tlnLastLi.hasClass('active')) {
    tlnLastLi.after('<li class="last postactive"></li>');
  } else {
    tlnLastLi.after('<li class="last"></li>');
  }
  
  /* add angledquote to linklist head links */
  $j('.linklistbody h4 > a, .communityteaser h4 > a').append(' &raquo;');
  
  
  
  // USER MANAGEMENT 
  
  // hide initially   
  $j(".regbox, .required, .regbox h5, .hint, .marker, #changePassword, #uacDiscipline, #uacDisciplineCheckbox, #umTooltipDecline").hide();
  // hide initially but only on registration pages
  $j("#regheader ~ #umTou, #usermanagement ~ #umTou").hide();
  
  
  // degrade if JS is disabled
  $j(".showHide").css("visibility", "hidden");
  
  // no radio button is checked on load of page
  $j("#regnav input").attr('checked', false);
  // hide jobRoleOthers initially
  $j(".other").parent().hide();
  // hide profile content initially 
  $j("#profile").children().addClass("tabInactive");
  // hide business card content initially
  $j("#bcBody").children().addClass("tabInactive");

  // show additional input in communites/newsletters
  $j.fn.showInput = function() {
      if ( $j("#communities input[@type=checkbox]:eq(4)").is(":checked") ) {
      $j("#uacDiscipline, #uacDisciplineCheckbox").show();
    } else {
      $j("#uacDiscipline, #uacDisciplineCheckbox").hide();  
    }
  };
  $j.fn.showInput();
  $j("#communities input[@type=checkbox]").click(function() {
      $j.fn.showInput();
  });
  
  // show hints next to input fields
  $j(".regbox input, #regmandatory input, #regmandatory select").focus(function(){
     $j(this).next().show().css("background-position", "-212px -182px");
  }).blur(function(){
     $j(this).next().hide();
  });
  
  // BUSINESS CARD MODE CHANGE
  $j("#bcEditButton").click(function(){
    $j("#businesscard").removeClass("bcViewMode").addClass("bcEditMode"); 
    return false;  
  });
          
  $j("#bcViewButton").click(function(){
    $j("#businesscard").removeClass("bcEditMode").addClass("bcViewMode"); 
    $j("#bcViewButton").next("span").hide();
    return false;  
  });
    
  $j("#bcSaveButton").click(function(){
    $j("#bcSaved").fadeIn('slow').animate({opacity: 1.0}, 1200).fadeOut('slow');
     return false;  
  });
    
  $j("#businesscard .surroundLink").click(function(){
    $j("#businesscard").removeClass("bcEditMode").addClass("bcViewMode"); 
    return false;      
  });
  
  $j("#businesscard").addClass("bcViewMode");
    
  // show required in Profile
  $j("#profile .required").show();
    
  // PROFILE TABS
  // activate one nav item, marker and tab content
  $j("#profileNav li:first").addClass("active").children().next().show();
  $j("#account").removeClass("tabInactive").addClass("tabActive");
   
  $j(".navItem").click(function () {
     var profileContent =  $j(this).attr("rel");
                                    
    // Remove the 'active' class from the active tab.
    $j("#profileNav > ul > li.active").removeClass("active").children().next().hide();
    
   	// Add the 'active' class to the clicked tab.
    $j(this).parent().addClass('active').children().next().show();
    
    // Hide and show content
    $j("#profile").children().removeClass("tabActive").addClass("tabInactive");
    $j(profileContent).removeClass("tabInactive").addClass("tabActive");
    
 });
 
 // BUSINESS CARD TABS
  // activate one nav item and tab content
  $j("#bc li:first").addClass("active");
  $j("#personalData").removeClass("tabInactive").addClass("tabActive");
   
  $j(".tabItem").click(function () {
     var bcContent =  $j(this).attr("rel");
                                    
    // Remove the 'active' class from the active tab.
    $j("#bcHeader > ul > li.active").removeClass("active");
    
   	// Add the 'active' class to the clicked tab.
    $j(this).addClass('active');
    
    // Hide and show content
    $j("#bcBody").children().removeClass("tabActive").addClass("tabInactive");
    $j(bcContent).removeClass("tabInactive").addClass("tabActive");
 });

  // select content of input field
  $j(".bcSurround > input, #bcBody input").focus(function(){
    this.select();
  });
  
  // switching between the user types
  $j("#regnav input, #switchToPu").click(function() {
     
    if (this.id == "switchToPu") {
      this.id = "publicRegistration";
    }
    
    $j("li").removeClass("regnavActive");
    // set messagebox invisible if there is any
    // $j("#regheader").children().next("div").hide(); 
    $j(".regbox > div").hide();
    $j(this).parents("li").addClass("regnavActive");
    $j(".regbox, .required").show();
    
    switch( this.id ) {
      case "sUserRegistration": $j("#regPartnerCustomer").show(); break;
      case "employeeRegistration": $j("#regEmployee").show(); break;
      case "publicRegistration": $j("#regPublicUser").show();   
    }
    
    // this is needed to activate the tab when #switchToPu was selected
    if (this.id == "publicRegistration") {
     $j("#regnav li:eq(2)").addClass("regnavActive").children().children().attr('checked', true); 
    }       
  });
  
  // custom checkbox
  $j(".customcheck").click(function(){
		($j(this).hasClass("customchecked")) ? $j(this).removeClass("customchecked") : $j(this).addClass("customchecked");
		$j(this.hash).trigger("click");
		return false;
	});
	
	// tooltip
	$j("#umTou #decline").hover(function() {
		//if ( $j("#businesscard").hasClass("bcEditMode") ) {
  		  $j(this).next("div").animate({opacity: "show"}, "fast");
  	//    }
    }, function() {
  		  $j(this).next("div").animate({opacity: "hide"}, "fast");
	});
	$j("#umTou #accept, #umTou #decline").click(function() {
	   $j(".tooltipBottom").hide()
  });
});
