﻿// Register the namespace for the control.
Type.registerNamespace('Polyton.Web.UI');


var navigateCtrl = null;

Polyton.Web.UI.NavigateControl = function(element) {
  Polyton.Web.UI.NavigateControl.initializeBase(this, [element]);

  this._uniqueID = null;
  this._endRequestHandlerDelegate = null;
  this._pageRequestManager = null;

  this._menuIndex = -1;
  this._menuSubIndex = -1;
  this._timer = null;
  this._interval = 5000;


  this.DHTML = 0;
  this.DOM = 0;
  this.MS = 0;
  this.NS = 0;
  this.OP = 0;

  this.myNavigation = [];
  this._NavgationTopBar = null;
  this._TableRowNavgationTopBarGruppe = null;
  this._TableNavigationLinks = null;
  this._TableCellNavigationCorner = null;
}

Polyton.Web.UI.NavigateControl.prototype = {

  _handleEndRequest: function(sender, arg) {
    var dataItem = arg.get_dataItems()[this.get_uniqueID().replace(/\$/g, '_')];
    if (dataItem) {
      this.set_menuindex(dataItem[0]);
      this.set_menusubindex(dataItem[1]);
    }
  },

  updateMenu: function() {
    this.changeNavigation(this._menuIndex);
    if (this._menuSubIndex != -1) {
      var name = 'SectionMenu' + this._menuSubIndex;
      var menu = $get(name);
      OpenRoot(menu);
    }
  },

  getElementAttribute: function(elem, attr) {
    if (elem != null) {
      if (this.DOM || this.MS) {
        return elem.getAttribute(attr);
      }
      else if (this.NS) {
        if (typeof elem[0] == "object")
          return elem[0][attr];
        else
          return elem[attr];
      }
    }
    return void (0);
  },

  setElementAttribute: function(elem, attr, val) {
    if (elem != null) {
      if (this.DOM || this.MS) {
        elem.setAttribute(attr, val);
      }
      else if (this.NS) {
        if (typeof elem[0] == "object")
          elem[0][attr] = val;
        else
          elem[attr] = val;
      }
    }
    return;
  },

  detectBrowser: function() {
    if (window.opera) {
      this.OP = 1;
    }
    if (document.getElementById) {
      this.DHTML = 1;
      this.DOM = 1;
    }
    if (document.all && !this.OP) {
      this.DHTML = 1;
      this.MS = 1;
    }
    if (window.netscape && window.screen && !this.DOM && !this.OP) {
      this.DHTML = 1;
      this.NS = 1;
    }
  },

  get_menuindex: function() {
    /// <value type="Number">Interval in milliseconds</value>
    return this._menuIndex;
  },
  set_menuindex: function(value) {
    if (this._menuIndex != value) {
      this._menuIndex = value;
      this.raisePropertyChanged('menuindex');

      if (!this.get_isUpdating()) {
        this.updateMenu();
      }
    }
  },
  get_menusubindex: function() {
    /// <value type="Number">Interval in milliseconds</value>
    return this._menuIndex;
  },
  set_menusubindex: function(value) {
    if (this._menuSubIndex != value) {
      this._menuSubIndex = value;
      this.raisePropertyChanged('menusubindex');

      if (!this.get_isUpdating()) {
        this.updateMenu();
      }
    }
  },

  _startTimer: function() {
    // save timer cookie for removal later
    this._timer = window.setTimeout(Function.createDelegate(this, this._timerCallback), this._interval);
  },

  _stopTimer: function() {
    if (this._timer) {
      window.clearTimeout(this._timer);
      this._timer = null;
    }
  },

  _timerCallback: function() {
    this._timer = 0;
    this.updateMenu();
  },

  ColorNavigationGruppe: function(index, image) {
    if (this._TableRowNavgationTopBarGruppe != null && this._TableRowNavgationTopBarGruppe.cells != null) {
      var a;
      for (a = 2; a < this._TableRowNavgationTopBarGruppe.cells.length - 2; a++) {
        var cell = this._TableRowNavgationTopBarGruppe.cells[a];
        if (cell.style != null && cell.style.visibility != null) {
          if (a == (index + 1)) {
            if (image != null) {
              var child = cell.firstChild;
              if (child != null && child.tagName == "IMG") {
                child.src = image;
              }
            }
            cell.style.visibility = "visible";
          }
          else {
            cell.style.visibility = "hidden";
          }
        }
      }
    }
  },

  ColorNavigationLinks: function(lastIndex, FarbeA, FarbeB) {
    if (this._TableNavigationLinks != null) {
      if (this._TableNavigationLinks.lastChild != null && this._TableNavigationLinks.lastChild.rows != null) {
        if (this._TableCellNavigationCorner != null) {
          if (lastIndex < 0) {
            this._TableCellNavigationCorner.className = "";
          }
          else {
            this._TableCellNavigationCorner.className = "NavigationCorner";
          }
        }
        var a;
        for (a = 0; a < this._TableNavigationLinks.lastChild.rows.length; a++) {
          var row = this._TableNavigationLinks.lastChild.rows[a];
          if (row.cells != null && row.cells.length > 1) {
            row.cells[0].style.fontWeight = "normal";
            if (lastIndex == a) {
              row.cells[0].bgColor = FarbeA;
            }
            else if ((a & 1) == 0 && a > 1) {
              row.cells[0].bgColor = FarbeB;
            }
            else if ((lastIndex - 1) == a) {
              row.cells[0].style.fontWeight = "bold";
            }
            if (a <= lastIndex) {
              row.cells[1].bgColor = FarbeA;
            }
            else {
              row.cells[1].bgColor = "";
            }
          }
        }
      }
    }
  },

  changeNavigation: function(index) {
    for (i = 0; i < this.myNavigation.length; i++) {
      if (this.myNavigation[i] != null) {
        if ((index) != i) {
          var image = this.myNavigation[i][3];
          this.setElementAttribute(image, "src", this.myNavigation[i][4]);
          var menu = this.myNavigation[i][2];
          if (menu != null) {
            menu.style.display = "none";
            menu.style.visibility = "hidden";
          }
        }
        else {
          this._menuIndex = index;
        }
      }
    }
    if (this.myNavigation[index] != null) {
      var menu = this.myNavigation[index][2];
      if (menu != null) {
        menu.style.display = "block";
        menu.style.visibility = "visible";
      }
      var color = this.myNavigation[index][1];
      this.setElementAttribute(this._NavgationTopBar, "bgColor", color);

      var image = this.myNavigation[index][3];
      this.setElementAttribute(image, "src", this.myNavigation[index][5]);

      if (this.myNavigation[index][0] == 1) {
        this.ColorNavigationLinks(-1, "#ff9900", "#FFFFFF");
        this.ColorNavigationGruppe(index - 5, this.myNavigation[index][6]);
      }
      else {
        this.ColorNavigationLinks(index * 2 + 2, color, "#FFFFFF");
        this.ColorNavigationGruppe(-1, null);
      }
    }
    //      this._stopTimer();
    //      this._startTimer();
  },

  updated: function() {
    Polyton.Web.UI.NavigateControl.callBaseMethod(this, 'updated');
    // called after batch updates, this.beginUpdate(), this.endUpdate().
  },

  dispose: function() {
    navigateCtrl = null;
    if (this._pageRequestManager != null) {
      this._pageRequestManager.remove_endRequest(this._endRequestHandlerDelegate);
    }
    // make sure it stopped so we aren't called after disposal
    this._stopTimer();
    // be sure to call base.dispose()
    Polyton.Web.UI.NavigateControl.callBaseMethod(this, 'dispose');
  },

  initialize: function() {
    Polyton.Web.UI.NavigateControl.callBaseMethod(this, 'initialize');

    this.detectBrowser();

    if (this.myNavigation.length == 0) {
      this.myNavigation[0] = [0, "#ff9900", $get("DivMenuAktuelles"), null, "", "", null];
      this.myNavigation[1] = [0, "#ff9900", $get("DivMenuFirmenprofil"), null, "", "", null];
      this.myNavigation[2] = [0, "#ff9900", $get("DivMenuInformationen"), null, "", "", null];
      this.myNavigation[3] = [0, "#ff9900", $get("DivMenuNewsletter"), null, "", "", null];
      this.myNavigation[4] = [0, "#ff9900", $get("DivMenuKontakt"), null, "", "", null];
      this.myNavigation[5] = [0, "#ff0099", $get("DivMenuAdmin"), null, "", "", null];

      //RGB(210,134,7),RGB(31,99,208),RGB(184,78,149),RGB(49,163,24),RGB(213,101,7)
      this.myNavigation[6] = [1, "#ff9900", $get("DivMenuPolyton"), $get("img_title_1"), "images/title_1a.gif", "images/title_1b.gif", "images/1x1_f.gif"];
      this.myNavigation[7] = [1, "#ffd700", $get("DivMenuShopOriginale"), $get("img_title_2"), "images/title_2a.gif", "images/title_2b.gif", "images/1x1_a.gif"];
      this.myNavigation[8] = [1, "#b84e95", $get("DivMenuBuerogeraete"), $get("img_title_3"), "images/title_3a.gif", "images/title_3b.gif", "images/1x1_c.gif"];
      this.myNavigation[9] = [1, "#d56507", $get("DivMenuService"), $get("img_title_5"), "images/title_5a.gif", "images/title_5b.gif", "images/1x1_e.gif"];
      this.myNavigation[10] = [1, "#31a318", $get("DivMenuLeergut"), $get("img_title_4"), "images/title_4a.gif", "images/title_4b.gif", "images/1x1_d.gif"];
    }

    this._NavgationTopBar = $get("NavgationTopBar");
    if (this._NavgationTopBar == null) {
      this._NavgationTopBar = $get("_ctl0_NavgationTopBar");
    }

    this._TableNavigationLinks = $get("TableNavigationLinks");
    if (this._TableNavigationLinks == null) {
      this._TableNavigationLinks = $get("ctl00_SideMenuControl1_TableNavigationLinks");
    }

    this._TableRowNavgationTopBarGruppe = $get("NavgationTopBarGruppe");
    if (this._TableRowNavgationTopBarGruppe == null) {
      this._TableRowNavgationTopBarGruppe = $get("_ctl0_NavgationTopBarGruppe");
    }

    this._TableCellNavigationCorner = $get("NavigationCorner");

    this._endRequestHandlerDelegate = Function.createDelegate(this, this._handleEndRequest);
    if (Sys.WebForms && Sys.WebForms.PageRequestManager) {
      this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
    }
    if (this._pageRequestManager != null) {
      this._pageRequestManager.add_endRequest(this._endRequestHandlerDelegate);
    }

    navigateCtrl = this;

    this.updateMenu();
  },

  get_uniqueID: function() {
    /// <value type="String"></value>
    if (arguments.length != 0) throw Error.parameterCount();
    return this._uniqueID;
  },

  set_uniqueID: function(value) {
    this._uniqueID = value;
  },

  // events
  add_Navigate: function(handler) {
    /// <summary>Adds a event handler for the tick event.</summary>
    /// <param name="handler" type="Function">The handler to add to the event.</param>
    this.get_events().addHandler("Navigate", handler);
  },

  remove_Navigate: function(handler) {
    /// <summary>Removes a event handler for the tick event.</summary>
    /// <param name="handler" type="Function">The handler to remove from the event.</param>
    this.get_events().removeHandler("Navigate", handler);
  },

  _onNavigate: function(eventArgs) {
    var handler = this.get_events().getHandler("Navigate");
    if (handler) {
      handler(this, eventArgs);
    }
  }
}

// JSON object that describes all properties, events, and methods of this component that should
// be addressable through the Sys.TypeDescriptor methods, and addressable via xml-script.
Polyton.Web.UI.NavigateControl.descriptor = {
  properties: [{ name: 'menuindex', type: Number },
               { name: 'menusubindex', type: Number },
               { name: 'uniqueID', type: String}],
  events: [{ name: 'Navigate'}]
}

Polyton.Web.UI.NavigateControl.registerClass('Polyton.Web.UI.NavigateControl', Sys.UI.Control, Sys.IDisposable);

function changeSideMenu(index) {
  if (navigateCtrl != null) {
    navigateCtrl.changeNavigation(index - 1);
  }
}

function changeTopMenu(index) {
  if (navigateCtrl != null) {
    navigateCtrl.changeNavigation(index + 6 - 1);
  }
}

function setImage(control, imageName) {
  var img = $get(control);
  if (img) {
    img.src = imageName;
  }
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();