// helper Dropdown widget

function WindowTabs(options){
  var options = options;
  var tabsData = [];
  var tabsLength = 0;
  var activeTabId = null;
  var prevActiveTabId = null;
  var nextActiveTabId = null;
  this.log = function(){
    return;
  };
  this.addTab = function(tabId,tabBgId,tabContainerId,tabControlShowId,tabControlHideId, onShowFunc, onHideFunc, onSwitchFunc){
    var data = {
      tabId: tabId, tabBgId: tabBgId, tabContainerId: tabContainerId,
      tabControlShowId: tabControlShowId, tabControlHideId: tabControlHideId,
      onShowFunc: onShowFunc, onHideFunc: onHideFunc, onSwitchFunc: onSwitchFunc,
      terminator: null
    };
    data["tabElem"] = $('#'+tabId);
    data["tabBgElem"] = $('#'+tabBgId);
    data["tabContainerElem"] = $('#'+tabContainerId);
    data["tabControlShowElem"] = $('#'+tabControlShowId);
    data["tabControlHideElem"] = $('#'+tabControlHideId);
    data["showed"] = false;
    tabsData.push( data );
    tabsLength = tabsData.length;
    return this;
  };
  this.getDataById = function(tabId){
    for( var i = 0; i < tabsLength; i++ ){
      if( tabsData[i]["tabId"] != null && tabsData[i]["tabId"] == tabId ){
        this.log('tabData found for ' + tabId, ', ', tabsData[i]);
        return tabsData[i];
      }
    }
    this.log('tabData not found for ' + tabId);
    return null;
  };
  this.activate = function(tabId){
    if( tabId != null ) {
      this.log('WindowTabs.activate('+tabId+') called');
      if( activeTabId != null ) {
        if( activeTabId == tabId ){
          this.log('WindowTabs.activate, activeTabId:', activeTabId, '=tabId:', tabId);
          return activeTabId;
        }
        if( options.enableSwitchFunc ) {
          var tabData = this.getDataById(tabId);
          this.log('try onSwitchFunc');
          tabData["onSwitchFunc"]( activeTabId, tabId );
          this.log('onSwitchFunc was called');
          activeTabId = tabId;
          return activeTabId;
        }
        this.log('need hide activeTabId:', activeTabId);
        if( this.hide(activeTabId) ){
          this.log('hide('+activeTabId+')=true');
          activeTabId = null;
        } else {
          this.log('hide('+activeTabId+')=false');
        }
      }
      if( this.show(tabId) ) {
        this.log('show('+tabId+')=true');
        activeTabId = tabId;
      } else {
        this.log('show('+tabId+')=false');
      }
      return activeTabId;
    }
    return null;
  };
  this.deactivate = function(){
    this.log('WIndowTabs.deactivate', activeTabId, 'called');
    if( activeTabId != null ) {
      if( this.hide(activeTabId) ){
        activeTabId = null;
        return true;
      }
    }
    return false;
  };
  this.isActive = function(tabId){
    this.log('WindowTabs.isActive('+tabId+') called');
    if( tabId != null ){
      this.log('tabId != null', tabId );
      this.log('activeTabId is ', activeTabId );
      if( activeTabId == tabId ){
        this.log('tab '+tabId+' is active');
        return true;
      }
    }
    this.log('WindowTabs.isActive('+tabId+') is false');
    return false;
  };
  this.haveShowed = function(){
    this.log('WindowTabs.haveShowed() called');
    var counter = 0;
    for( var i = 0; i < tabsLength; i++ ){
      if( tabsData[i]["showed"] == true ){
        counter++;
      }
    }
    return counter;
  };
  // for onShow and onHide func
  this.prevActiveTabId = function(){
    return prevActiveTabId;
  };
  // for onShow and onHide func
  this.nextActiveTabId = function(){
    return nextActiveTabId;
  };
  this.show = function(tabId){
    this.log('WindowTabs.show('+tabId+') called');
    var tabData = this.getDataById(tabId);
    if( tabData != null ) {
      options.zIndex && $(this).queue(
        function(obj,tabData){return function(next){ obj.zIndexToReal(tabData["tabElem"]); next(); }}(this,tabData)
      );
      options.visibility && $(this).queue(
        function(obj,tabData){return function(next){ obj.elemDoVisible(tabData["tabElem"]); next(); }}(this,tabData)
      );
      options.zIndex && $(this).queue(
        function(obj,tabData){return function(next){ obj.zIndexToReal(tabData["tabBgElem"]); next(); }}(this,tabData)
      );
      options.visibility && $(this).queue(
        function(obj,tabData){return function(next){ obj.elemDoVisible(tabData["tabBgElem"]); next(); }}(this,tabData)
      );
      options.zIndex && $(this).queue(
        function(obj,tabData){return function(next){ obj.zIndexToReal(tabData["tabContainerElem"]); next(); }}(this,tabData)
      );
      options.visibility && $(this).queue(
        function(obj,tabData){return function(next){ obj.elemDoVisible(tabData["tabContainerElem"]); next(); }}(this,tabData)
      );
      if( tabData["onShowFunc"] != null ){
        this.log('try call onShowFunc');
        $(this).queue(
            function(tabData){ return function(next){ tabData["onShowFunc"](); next(); }}(tabData)
        );
        this.log('onShowFunc was called');
      }
      tabData["showed"] = true;
      return true;
    }
    this.log('tabData is null');
    return false;
  };
  this.hide = function(tabId){
    this.log('WindowTabs.hide('+tabId+') called');
    var tabData = this.getDataById(tabId);
    if( tabData != null ) {
      options.zIndex && $(this).queue(
        function(obj,tabData){return function(next){ obj.zIndexToImaginary(tabData["tabElem"]); next(); }}(this,tabData)
      );
      options.visibility && $(this).queue(
        function(obj,tabData){return function(next){ obj.elemDoInvisible(tabData["tabElem"]); next(); }}(this,tabData)
      );
      options.zIndex && $(this).queue(
        function(obj,tabData){return function(next){ obj.zIndexToImaginary(tabData["tabBgElem"]); next(); }}(this,tabData)
      );
      options.visibility && $(this).queue(
        function(obj,tabData){return function(next){ obj.elemDoInvisible(tabData["tabBgElem"]); next(); }}(this,tabData)
      );
      options.zIndex && $(this).queue(
        function(obj,tabData){return function(next){ obj.zIndexToImaginary(tabData["tabContainerElem"]); next(); }}(this,tabData)
      );
      options.visibility && $(this).queue(
        function(obj,tabData){return function(next){ obj.elemDoInvisible(tabData["tabContainerElem"]); next(); }}(this,tabData)
      );
      if( tabData["onHideFunc"] != null ){
        this.log('try call onHideFunc');
        $(this).queue(
          function(tabData){ return function(next){ tabData["onHideFunc"](); next(); } }(tabData)
        );
        this.log('onHideFunc was called');
      }
      tabData["showed"] = false;
      return true;
    }
    this.log('tabData is null');
    return false;
  };
  this.zIndexToImaginary = function(elem){
    this.log('WindowTabs.zIndexToImaginary('+elem+') called');
    if( !elem ){
      return false;
    }
    if( !elem.css ){
      elem = $(elem);
    }
    if( !elem ) {
      return false;
    }
    var zIndex = elem.css('z-index');
    if( zIndex != null && zIndex > 0 ) {
      zIndex *= -1;
      elem.css('z-index',zIndex);
      return true;
    }
    return false;
  };
  this.zIndexToReal = function(elem){
    this.log('WindowTabs.zIndexToReal('+elem+') called');
    if( !elem ){
      return false;
    }
    if( !elem.css ){
      elem = $(elem);
    }
    if( !elem ) {
      return false;
    }
    var zIndex = elem.css('z-index');
    if( zIndex != null && zIndex < 0 ) {
      zIndex *= -1;
      elem.css('z-index',zIndex);
      return true;
    }
    return false;
  };
  this.elemDoVisible = function(elem){
    this.log('WindowTabs.elemDoVisible('+elem+') called');
    if( !elem ){
      return false;
    }
    if( !elem.css ){
      elem = $(elem);
    }
    if( !elem ) {
      return false;
    }
    elem.css('visibility','visible');
    return true;
  };
  this.elemDoInvisible = function(elem){
    this.log('WindowTabs.elemDoInvisible('+elem+') called');
    if( !elem ){
      return false;
    }
    if( !elem.css ){
      elem = $(elem);
    }
    if( !elem ) {
      return false;
    }
    elem.css('visibility','hidden');
    return true;
  };
};
