﻿Ext.namespace('Mapping.Ext.Esri');

Mapping.Ext.Esri.CursorManager = function() {

  var map, cursorContainer;
  var initialized = false;
  var defaultClasses = new Array();
  var activeCursors = new Array();

  // Sets the cursorContainer variable to the value of the layers div of the map
  // Note: map._layersDiv is an undocumented property and could potential change
  function setCursorContainer() {
    cursorContainer = Ext.get(map._layersDiv);
  }

  /// Populate the initialClasses array here with the initial classes, then append our default
  /// @param {cursor} string The CSS style of the cursor to set as default
  function setDefaultCursor(cursor) {
    // Test for pre-IE8 and handle those browsers differently
    if(Ext.isIE) {
      
      if(Ext.isIE8){
        defaultClasses = cursorContainer.dom.getAttribute('class').split(' ');
      } else {
        defaultClasses = cursorContainer.dom.getAttributeNode('class').value.split(' ');
      }
    } else {
      defaultClasses = cursorContainer.dom.getAttribute('class').split(' ');
    }
    defaultClasses.push(cursor);
    // Set the default cursor for our application and add it to the div
    cursorContainer.addClass(cursor);
  }

  /// Set the passed cursor as active
  /// @param {cursor} string The CSS style of the cursor to add
  function setActiveCursor(cursor) {
    var currentCursors;
    // Gets nodeList of current cursors
    //var currentCursors = cursorContainer.dom.classList;
    // Test for pre-IE8 and handle those browsers differently
    if(Ext.isIE) {
      
      if(Ext.isIE8){
        currentCursors = cursorContainer.dom.getAttribute('class').split(' ');
      } else {
        currentCursors = cursorContainer.dom.getAttributeNode('class').value.split(' ');
      }
    } else {
      currentCursors = cursorContainer.dom.getAttribute('class').split(' ');
    }    
    
    
    var cursorExists = false;
    for (i = 0; i < currentCursors.length; i++) {

      if (currentCursors[i] == cursor) {
        cursorExists = true;
      }
    }
    if (!cursorExists) {
      cursorContainer.addClass(cursor);
      activeCursors.push(cursor);
    }
  }

  /// Removes the cursor from the container and our internal list. 
  /// This may not even be visible, but we don't care
  /// @param {cursor} string The CSS style of the cursor to remove
  function removeCursor(cursor) {

    cursorContainer.removeClass(cursor);
    activeCursors.remove(cursor);
  }

  /// Set cursor to default and clear any other active cursors
  function resetCursor() {
    // Gets nodeList of current cursors
    var currentCursors = cursorContainer.dom.classList;

    for (var i = 0; i < activeCursors.length; i++) {
      cursorContainer.removeClass(activeCursors[i]);
    }
    activeCursors = new Array();
  }

  return {

    /// @param {cursor} string The CSS style to add for the cursor
    appendCursor: function(cursor) {
      setActiveCursor(cursor);
    },

    /// @param {cursor} string The CSS style to add for the cursor
    removeCursor: function(cursor) {
      removeCursor(cursor);
    },

    /// Set cursor to default and clear any other active cursors
    resetCursor: function() {
      resetCursor();
    },

    /// Replace all existing cursors with this one
    setCursor: function(cursor) {
      resetCursor();
      setActiveCursor(cursor);
    },

    /// @param {mapObj} esri.Map The map this cursor will be associated with    
    /// @param {baseCursor} string The cursor that should be used by default
    init: function(mapObj, baseCursor) {
      if (!initialized) {
        initialized = true;
        map = mapObj;
        setCursorContainer();
        setDefaultCursor(baseCursor);
        initialized = true;
      }
    }
  };

} ();    
