/* -------------------------------------------------------------
 * @Project: Frankfurt Airport City
 * -------------------------------------------------------------
 *
 * (Id: jquery.tabContent.js 1524 2010-08-30 10:49:46Z mrozati )
 */

$(document).ready(function() {
  // if there's any tab-navigation
  var navSelector = "#main-content .tab-navigation";
  if ($(navSelector).length > 0) {
    $(navSelector + " li a.tabbing").click(function() {
        // if clicked element is not the current tab
        if (!$(this).parent().hasClass("active") && !$(this).parent().hasClass("last-active")) {
          // reset current active tab
          $(this).parent().parent().children("li.active").removeClass("active");
          $(this).parent().parent().children("li.last-active").removeClass("last-active").addClass("last");
          $(this).parent().parent().children("li.before-active").removeClass("before-active");

          // make this tab active
          if (!$(this).parent().hasClass("last")) {
            $(this).parent().addClass("active");
          }
          else {
            $(this).parent().addClass("last-active");
          }

          // if there's a tab before the current active, change its class (needed for bg-image)
          if ($(this).parent() != $(this).parent().parent().children("li:first")) {
            $(this).parent().prev().addClass("before-active");
          }
        }
        
        // build the jq selector of the tab-content to be activated
        var panelSelector = $(this).attr("href");
        if (typeof panelSelector !== "undefined") {
          // NOTE: idPrefix must correspond with de.fraport.dfra.airport.components.paragraph.Abstract_tab.FRAGMENT_ID_PREFIX (DFRA-592).
          var idPrefix = "_";
          panelSelector = "#" + idPrefix + panelSelector.substr(panelSelector.indexOf("#") + 1);
        }
        
        // activate the panel only if it's hidden
        if ($(panelSelector).hasClass("hidden")) {
          // hide all tabs
          $("#main-content .inner-tab-content").addClass("hidden");

          // hide eventually opened detailpanel
          if (typeof fVc !== "undefined") {
            if (fVc.detailpanel !== false && !fVc.detailpanel) {
              hideDetailPanel(fVc.detailpanel);
              fVc.detailpanel = false;
            }
          }

          // user tracking for tab click
          dfra.config.onClickTrackWithPagename({
            "eventTargetTitle": userTrackingRemoveSpecialChars($(this).text())
          });

          // show next tab
          $(panelSelector).removeClass("hidden");

          // DFRA-568: re-apply sIFR to the headline of the visible tab
          $(panelSelector).find('h2').sifr( {
            path : dfra.config.sIFRFontPath,
            font : 'StoneSanItcTSem',
            zoom : 1.3,
            zoomLeft : -0.01
          });

          if (typeof fVc !== "undefined") {
            switch (panelSelector) {
            case "#_arrivals":
              fVc.switch2Arrivals();
              break;

            case "#_departures":
              fVc.switch2Departures();
              break;
            }

          }
        }
        return false;
      });
    
      // If there's a fragment id in the url, activate the corresponding tab.
      var cUrl = window.location.href;
      var tabId;
      if (cUrl.indexOf("#") != -1) {
        tabId = cUrl.substr(cUrl.indexOf("#"));
      }
      var tabSelector = navSelector + " li a[href=" + tabId + "]";
      // DFRA-592: Activate tab which is selected by the url *or* the first tab
      if ($(tabSelector).length > 0) {
        $(tabSelector).click();
      }
      else {
        $(navSelector).find("li a").eq(0).click();
      }
    }
  }
);
