(function() {
  initTabbedContainer = function() {
    $('.tabbedContainer > .tabs a').click( function() {
      var tabbedContainer = $(this).closest('.tabbedContainer');
      $(this).closest('table').find('a').removeClass('selected');
      $(this).addClass('selected');
      var tabIndex = $(this).closest('td').index();
      $(' > .tabContent', tabbedContainer).hide();
      $(' > .tabContent:eq(' + tabIndex + ')', tabbedContainer).show();
      var container = $(' > .tabContent > div:eq(' + tabIndex + ')', tabbedContainer);
      container.show();
      
      var context = tabbedContainer.find('.tabContent');
      window[$(this).attr('href')](context);
      
      //[this][tagesgeld](context);
      
      return false;
    });
    //$('.tabbedContainer > .tabs tr:eq(0) td:eq(0) a').trigger('click');
    
    $.each($('.tabbedContainer'), function(index, value) {
      $("> .tabs tr:eq(0) td:eq(0) a", value).trigger('click');
    });
    
    //var functionName = $('.tabbedContainer > .tabs tr:eq(0) td:eq(0) a').attr('href');
    //var context = $('.tabbedContainer > .tabContent');
    //tagesgeld(context);
    
  }

  initMiniCombiTabs = function() {
    $(".combiTabs tr").each(function () {
      $(":eq(1)", this).addClass("selected");
    });
    $(".combiTabs tr a").click(function () {
      $(this).parent().siblings().removeClass('selected');
      $(this).parent().addClass('selected');
      var key = "";
      var closestMini = $(this).closest('.mini');
      $(".combiTabs tr td.selected", closestMini).each(function() {
        key += $(this).find('a').attr('href').replace(/\%/g, ""); 
      });    
      closestMini.find('.tabContent').hide();
      closestMini.find("div[combi-key=\"" + key + "\"]").show();
      return false;
    });
  }

  initialTabs = function(context) {

    chartsparbuch(context);
    chartsparbrief(context);
    chartfestgeld(context);    
    charttagesgeld(context);
    
    charthypotheken(context);
    chartratenkredit(context);
    chartdispozins(context);
    
    hypotheken(context);
    tagesgeld(context);
    festgeld(context);
    forward(context);
    
    //$('#rightBar .tabbedContainer .tabs td:first-child a').trigger('click');
    //$('#rightBar .tabbedContainer .combiTabs tr:eq(0) td:eq(1) a').trigger('click');
    /*
    triggerTab(context, 'hypotheken', 1, 2);
    triggerTab(context, 'forward', 1, 2);
    triggerTab(context, 'forward', 3, 3);
    triggerTab(context, 'tagesgeld', 0, 1);
    triggerTab(context, 'festgeld', 1, 1);
    triggerTab(context, 'festgeld', 2, 1);
    */
    /*
    $('> .mini.hypotheken .combiTabs tr:eq(2) td:eq(2) a', context).trigger('click');
    $('> .mini.forward .combiTabs tr:eq(1) td:eq(2) a',    context).trigger('click');
    $('> .mini.forward .combiTabs tr:eq(3) td:eq(3) a',    context).trigger('click');
    $('> .mini.tagesgeld .combiTabs tr:eq(0) td:eq(1) a',  context).trigger('click');
    $('> .mini.festgeld .combiTabs tr:eq(1) td:eq(1) a',   context).trigger('click');
    $('> .mini.festgeld .combiTabs tr:eq(2) td:eq(1) a',   context).trigger('click');
    */
  }

  chartsparbuch = function(context) {
    triggerTab(context, 'chartsparbuch', 0, 1);    
  }

  chartsparbrief = function(context) {
    triggerTab(context, 'chartsparbrief', 0, 1);    
  }

  chartfestgeld = function(context) {
    triggerTab(context, 'chartfestgeld', 0, 1);    
  }

  charttagesgeld = function(context) {
    triggerTab(context, 'charttagesgeld', 0, 1);    
  }

  charthypotheken = function(context) {
    triggerTab(context, 'charthypotheken', 0, 1);    
  }

  chartratenkredit = function(context) {
    triggerTab(context, 'chartratenkredit', 0, 1);    
  }

  chartdispozins = function(context) {
    triggerTab(context, 'chartdispozins', 0, 1);    
  }

  tagesgeld = function(context) {
    triggerTab(context, 'tagesgeld', 0, 1);
    triggerTab(context, 'tagesgeld', 1, 2);
  }

  hypotheken = function(context) {
    triggerTab(context, 'hypotheken', 2, 2);    
  }

  forward = function(context) {
    triggerTab(context, 'forward', 1, 2);
    triggerTab(context, 'forward', 3, 3);
  }
  
  festgeld = function(context) {
    triggerTab(context, 'festgeld', 1, 1);
    triggerTab(context, 'festgeld', 2, 1);
  }

  triggerTab = function(context, miniName, trIndex, tdIndex) {
    //$('> .mini.' + miniName + ' .combiTabs tr:eq(' + trIndex + ') td:eq(' + tdIndex + ') a', context).trigger('click');
    //$('> .mini.' + miniName + ' .combiTabs tr:eq(' + trIndex + ') td:eq(' + tdIndex + ') a', context).each(function () {
    //  $(this).trigger('click');
    //})
    $('> .mini.' + miniName, context).each(function () {
      $(' .combiTabs tr:eq(' + trIndex + ') td:eq(' + tdIndex + ') a', $(this)).trigger('click');
    });
  }

  $(document).ready(function() {    
    initMiniCombiTabs();
    initialTabs($('#rightBar'));
    initTabbedContainer();
  });
}).call(this);
