touch.clicks = 0;
touch.doubleClick = false;
document.ondblclick = touch.doubleClick;

touch.loadStyle = function(src, onReady) {
  if (src.indexOf('.css') == -1) src += '.css';
  if (touch.styles[src]) return;

  var style = document.createElement("link")
  style.setAttribute("rel", "stylesheet")
  style.setAttribute("type", "text/css")
  style.setAttribute("href", src)
  document.getElementsByTagName('head')[0].appendChild(style);  
  touch.styles[src] = true;
}

// extra resources
touch.loadStyle('/menu/touch/touch.css');

touch.addIcon = function(icon) {
  var link=document.createElement("link")
  link.setAttribute("rel", "apple-touch-icon")
  link.setAttribute("href", icon)
  document.getElementsByTagName('head')[0].appendChild(link);
}

touch.doubleClick = function() {
  touch.doubleClick = false;
}

touch.addMeta = function(name, content) {
  var meta = document.createElement('meta');
  meta.name = name;
  meta.content = content;
  document.getElementsByTagName('head')[0].appendChild(meta);
}

touch.addIcon("/menu/images/icecap158x158.png");

touch.addMeta("viewport", "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no");
touch.addMeta("apple-mobile-web-app-capable", "yes");
touch.addMeta("apple-mobile-web-app-status-bar-style", "black");
touch.addMeta("format-detection", "telephone=no");

Ext.override(Ext.Panel, {
	afterRender: Ext.Panel.prototype.afterRender.createSequence(function() {
		if (this.getXType() == 'panel') {
			this._getIScrollElement = function() {
				return (this.el.child('.x-panel-body', true));
			}
		}

		//Uncomment below to use iScroll only on mobile devices but use regular scrolling on PCs.
		//if (this.autoScroll && Ext.isMobileDevice) {
			if (this._getIScrollElement) {
				this._updateIScroll();
				this.on('afterlayout', this._updateIScroll);
			}
		//}
	}),

	_ensureIScroll: function() {
		if (!this.iScroll) {
			var el = this._getIScrollElement();
			if (el.children.length > 0) {
				this.iScroll = new iScroll(el);
				this.iScrollTask = new Ext.util.DelayedTask(this._refreshIScroll, this);
			}
		}
	},

	_updateIScroll: function() {
		this._ensureIScroll();
		if (this.iScroll) {
			this.iScrollTask.delay(1000);
    }
	},

	_refreshIScroll: function() {
		this.iScroll.refresh();
		//Refresh one more time.
    this.iScrollTask.delay(1000);
	}
});

Ext.override(Ext.tree.TreePanel, {
	_getIScrollElement: function() {
		return (this.el.child('.x-panel-body', true));
	}
});

Ext.override(Ext.grid.GridPanel, {
	_getIScrollElement: function() {
		return (this.el.child('.x-grid3-scroller', true));
	},

	afterRender: Ext.grid.GridPanel.prototype.afterRender.createSequence(function() {
		//TODO: need to hook into more events and to update iScroll.
		this.view.on('refresh', this._updateIScroll, this);

    var clicks = 0;
    this.on('rowclick', function(grid, rowIndex, e) {
      touch.clicks++;
      if (touch.clicks == 1) {
        setTimeout(function() {
          if (touch.doubleClick) {
            grid.fireEvent('rowcontextmenu', grid, rowIndex, e);
          } else {
            grid.fireEvent('rowdblclick', grid, rowIndex, e);
          }
          touch.clicks = 0;
          touch.doubleClick = false;
        }, 300);
      }
    });
	})

});