// ITL WebHelper starter

WHS_STATES = {
  UNDEFINED:  0,
  SHOW:       1,
  SHOWED:     2,
  HIDE:       3,
  HIDDEN:     4
};

WebHelperStarter = function(args){
  this.debug = false || args.debug;
  this.state = WHS_STATES.UNDEFINED;
  this.container = args.container || null;
  if( !this.container ) throw new Error('container is required');
  
  this.queueName = args.queueName || 'webhelper_starter';
  this.queueContainer = args.queueContainer || window.document;
  if( !this.queueName ) throw new Error('queueName is required');
  if( !this.queueContainer ) throw new Error('queueContainer is required');
  
  this.queueShowHideName = this.queueName + '_' + 'showhide';
  this.queuePositionName = this.queueName + '_' + 'changeposition';
  
  this.eventNamespace = args.eventNamespace || 'webhelper_starter';
  this.eventContainer = args.eventContainer || window;
  if( !this.eventNamespace ) throw new Error('eventNamespace is required');
  if( !this.eventContainer ) throw new Error('eventContainer is required');
  
  this.tickTimer = null;
  this.positionTimer = null;
  this.helperIsOpened = false;
  
  var __log_counter = 0;
  this.log = function(){
    if (this.debug && window.console && typeof console.log === "function" ) {
      __log_counter = ++__log_counter % 1024;
      var args = [];
      for( i = 0, l = arguments.length; i < l; i++ ) {
        args.push( arguments[i] );
      }
      args.unshift( '[' + __log_counter + ']' );
      console.debug.apply( console, args);
    }
  };
  
  return this;
};


WebHelperStarter.prototype.init = function(args){
  // init container
  jQuery(this.container).css(
    {
      "position": "absolute"
    }
  ).hide();
  this.state = WHS_STATES.HIDDEN;
  
  // start ticker-checker
  if( args.autostart ) {
    this.start();
  }
  
  return this;
};

WebHelperStarter.prototype.start = function(){
  //this.log('start() called');
  this.tick();
  return this;
};

WebHelperStarter.prototype.stop = function(){
  //this.log('stop() called');
  if( this.timer ) {
    window.clearTimeout( this.timer );
    this.timer = null;
  }
  return this;
};

WebHelperStarter.prototype.tick = function(){
  //this.log('tick() called');
  // check itl helper showed or hidden
  if( !this.helperIsOpened ) {
    this.show();
  }
  else {
    this.hide();
  }
  // auto-timer
  this.timer = window.setTimeout( function(instance){ return function(){ instance.tick(); } }(this), 5000 );
  return this;
};

WebHelperStarter.prototype.show = function(){
  //this.log('show() called');
  switch( this.state ) {
    case WHS_STATES.HIDDEN:
      this.state = WHS_STATES.SHOW;
      this.startShowQueue(function(instance){ return function(){ instance.state = WHS_STATES.SHOWED } }(this));
      break;
    default:
      break;
  }
  return this;
};

WebHelperStarter.prototype.hide = function(){
  //this.log('hide() called');
  switch( this.state ) {
    case WHS_STATES.SHOWED:
      this.state = WHS_STATES.HIDE;
      this.startHideQueue(function(instance){ return function(){ instance.state = WHS_STATES.HIDDEN } }(this));
      break;
    default:
      break;
  }
  return this;
};


WebHelperStarter.prototype.changePositionHandler = function(){
  //this.log('changePositionHandler() called');
  /*
  if( this.positionTimer ) {
    window.clearTimeout( this.positionTimer );
  }
  this.positionTimer = window.setTimeout( function(instance){ return function(){ instance.changePosition(); instance.positionTimer = null; } }(this), 600 );
*/
/*	
          var height = jQuery.data(this.container, 'originalHeight');
          var clientHeight = parseInt( ( !window.opera && document.body.clientHeight ) || window.innerHeight ) || 0;
          var scrollTop = parseInt( window.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop) || ( window.scrollY && window.scrollY ) ) || 0;
          var documentHeight = parseInt( jQuery(document).height() );
          var top = Math.round( ( clientHeight - height ) / 2 + scrollTop );
          if( documentHeight > 0 ) {
            if( top > ( documentHeight - height ) ) top = documentHeight - height;
          }
          if( top < 0 ) top = 0;
	  
          jQuery(this.container).css(
          {
            "top": top,
            "left": "0px"
          }
        ).show();
*/
	this.changePosition();
  return this;
};


WebHelperStarter.prototype.changePosition = function(){
  //this.log('changePosition() called');
  // replace a queue
  jQuery(this.container).queue(
    this.queuePositionName,
    [
      function(instance){
        return function(next){
          var height = jQuery.data(instance.container, 'originalHeight');
          var clientHeight = parseInt( ( !window.opera && document.body.clientHeight ) || window.innerHeight ) || 0;
          var scrollTop = parseInt( window.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop) || ( window.scrollY && window.scrollY ) ) || 0;
          var documentHeight = parseInt( jQuery(document).height() );
          var top = Math.round( ( clientHeight - height ) / 2 + scrollTop );
          if( documentHeight > 0 ) {
            if( top > ( documentHeight - height ) ) top = documentHeight - height;
          }
          if( top < 0 ) top = 0;
          jQuery(instance.container).animate(
            {
              top: top,
              left:'0px'
            }, {
	      duration: 400, easing: 'easeInOutCubic', queue: false,
              complete: function(next){ return function(){ next(); } }(next)
            }
          );
        };
      }(this)
    ]
  );
  jQuery(this.container).dequeue( this.queuePositionName );
  return this;
};


WebHelperStarter.prototype.startShowQueue = function(callback){
  //this.log('startShowQueue() called');
  jQuery(this.queueContainer).stop(true,true);
  jQuery(this.container).css(
    {
      "z-index": "-1000",
      "overflow-x": "visible"
    }
  ).show();
  jQuery.data(this.container, 'originalWidth', jQuery(this.container).children('div:first').width());
  jQuery.data(this.container, 'originalHeight', jQuery(this.container).children('div:first').height());

  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(instance){
      return function(next){
        jQuery(instance.container).css(
          {
            "width": '0px',
            "opacity": "0",
            "z-index": "1000",
            "overflow-x": "hidden"
          }
        ).show();
        
        jQuery(instance.eventContainer).bind(
          'scroll.' + instance.eventNamespace,
          function(instance){
            return function(){
              instance.changePositionHandler();
            };
          }(instance)
        );
        jQuery(instance.eventContainer).bind(
          'resize.', instance.eventNamespace,
          function(instance){
            return function(){
              instance.changePositionHandler();
            };
          }(instance)
        );
        // and call him once
        instance.changePositionHandler();
        
        next();
      }
    }(this)
  );
  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(instance){
      return function(next){
        jQuery(instance.container).animate(
          {
            "width": jQuery.data(instance.container, 'originalWidth'),
            "opacity": "1"
          },
          1000,
          next
        );
      }
    }(this)
  );
  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(instance){
      return function(next){
        jQuery(instance.container).css(
          {
            "width": jQuery.data(instance.container, 'originalWidth'),
            "opacity": "1",
            "z-index": "1000",
            "overflow-x": "visible"
          }
        ).show();
        instance.helperIsOpened = true;
        next();
      }
    }(this)
  );
  // last item
  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(next){ callback(); next(); }
  );
  jQuery(this.queueContainer).dequeue(this.queueShowHideName);
  return this;
};


WebHelperStarter.prototype.startHideQueue = function(callback){
  //this.log('startHideQueue() called');
  jQuery(this.queueContainer).stop(true,true);
  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(instance){
      return function(next){
        jQuery(instance.container).css(
          {
            "width": jQuery.data(instance.container, 'originalWidth'),
            "overflow-x": "hidden"
          }
        ).show();
        next();
      }
    }(this)
  );
  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(instance){
      return function(next){
        jQuery(instance.container).animate(
          {
            "width": "0px",
            "opacity": "0"
          },
          1000,
          next
        );
      }
    }(this)
  );
  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(instance){
      return function(next){
        jQuery(instance.eventContainer).unbind( 'scroll.' + instance.eventNamespace );
        jQuery(instance.eventContainer).unbind( 'resize.' + instance.eventNamespace );
        
        jQuery(instance.container).css(
          {
            "width": "auto",
            "float": "left",
            "overflow-x": "visible"
          }
        ).show();
        instance.helperIsOpened = false;
        next();
      }
    }(this)
  );
  // last item
  jQuery(this.queueContainer).queue(
    this.queueShowHideName,
    function(next){ callback(); next(); }
  );
  jQuery(this.queueContainer).dequeue(this.queueShowHideName);
  return this;
};
