(function($){
if (window.useBrowserHelper == undefined) window.useBrowserHelper = false;

var toggle_Storage = function(username){
  if (!$R.localstorage) { return false; }

  var _storage = $R.localstorage();
  _storage.init('helper_toggle', {'delay': 10});
  return {
    'data': function(data) {
      if (data) {
        var MyData = _storage.data; // Read data from localStorage(JSON Object)
        MyData[username] = data;
        _storage.set(MyData); // Write the data to localStorage
      }

      if (!_storage.get()) { // opera
        return {'toggle': 'show'};
      }
      return _storage.get()[username] || {'toggle': 'show'};
    }
  };
}

var toggle = function(op, aniAtt) {
  var that = {};
  op = op || {};
  
  var state = {'action': 'hide', 'state' : 'finish'}
  
  that.getState = function() {
    return state.action + state.state;
  }
  that.stop = function() {
    state = {'action': state.action, 'state' : 'stop'}
    op.target.stop();
  }
  that.show = function() {
    state = {'action': 'show', 'state' : 'ing'};
    op.target
      .show()
      .stop()
      .animate(
        aniAtt ?
          {'marginTop': typeof op.top == 'function' ? op.top() : op.top}:
          {'top': typeof op.top == 'function' ? op.top() : op.top},
        op.speed || 300,
        'swing', // swing, linear, easeOutBounce
        function() {
          state = {'action': 'show', 'state' : 'finish'};
          op.overlay && op.overlay.show().animate({'opacity': 0.6}, 150, 'swing');
        }
      );
    return false;
  }
  that.hide = function(callback) {
    state = {'action': 'hide', 'state' : 'ing'}

    var _height = (typeof op.height == 'function') ? op.height() : op.height;
    
    op.target
      .stop()
      .animate(
        aniAtt ?
          {'marginTop': (_height + (typeof op.top == 'function' ? op.top() : op.top) + 2) * -1} :
          {'top': (_height + (typeof op.top == 'function' ? op.top() : op.top) + 2) * -1},
        op.speed || 300,
        'swing', // swing, linear, easeOutBounce
        function() {
          op.target.hide();
          if (typeof callback == 'function') callback();
          state = {'action': 'hide', 'state' : 'finish'}
          op.overlay && op.overlay.animate({'opacity': 0}, 150, 'swing', function(){ op.overlay.hide(); });          
        }
      );
    return false;
  }

  return that;
}

var install_helper = function(op) {
  if (!op.target) { return false; }

  $.extend({
    'callbackHide': function() {},
    'checkerNone' : false
  }, op);
  
  if (op.checkerNone) {
    op.target.find('.checkbox, .checker_text').hide();
  }
  
  var that = toggle(op, 'marginTop');
  
  var setHTML = function() {
    $(document).append('');
  }
  
  return {
    'init': function() {
      op.closer && op.closer.click(hide);

      if (op.checker && op.checker.length != 0) {
        op.checker.attr('checked', (_storage.data().toggle != 'show'));          
        op.checker.click(check);
      }

      op.target.show();
      op.target.hide();
      op.overlay.click(hide);
    },
    'show': show,
    'hide': hide,
    'isShow': isShow,
    'getState': that.getState,
    'stop'    : that.stop
  };
  
  function isShow() {
    return op.target.css('display') != 'none';
  }
  function check() {
    _storage.data({'toggle': $(this).attr('checked') ? 'hide' : 'show'});
    $(this).attr('checked') && hide();
  };
  function show() {
    setPos();
    that.show();
  };
  
  function hide(callback) {
    that.hide(callback);
  };
  
  function setPos() {
    var left = ($(window).width() - op.target.width()) / 2;
    op.target.css('left', left);
  };

};

var tell_toolbar = function(op) {
  if (!op.target) { return false; }      
  $.extend({
    'height': 38
  }, op);

  var helper = '', 
      that = toggle(op);
  
  var helperTop = 31;

  return {
    'init': function() {
      helper = install_helper({
        'target' : $('#install_helper'),
        'closer' : $('#install_helper').find('span.close'),
        'checker': $('#install_helper').find('input:checkbox'),
        'overlay': $('#install_helper_overlay'),
        'height' : function() {
          var h = 0;
          $('#install_helper').children().each(function(){
            h += $(this).height();
          });
          return h;
        },
        'top': function(){return helperTop;},
        'speed': 800,
        'callbackHide': that.hide,
        'checkerNone' : true
      });
      helper && helper.init();
      
      !isShowToolbar() && hide();

      $R.browser(window.auth_browser_lang).setText(op.target, {
        'childs': ['version']
      });
      
      op.closer    && op.closer.click(hide);
      op.showlayer && op.showlayer.click(function(){
        if ($.browser.msie && /^6/.test($.browser.version)) {
          $('#install_helper').find('.content:first').find('*').show();
        } else {
          $('#install_helper').find('.content:first').show();  
        }
        
        if (!window.useAcx) {
          $('#install_helper').removeClass('across');
        }
        $('#connector').hide();
        showlayer();
        return false;
      });
      
      if (op.checker) {
        op.checker.attr('checked', (_storage.data().toggle != 'show'));          
        op.checker.click(check);
      }

      if (op.connector.length) {
        op.connector.click(function() {
          
          if ($.browser.msie && /^6/.test($.browser.version)) {
            $('#install_helper').find('.content:first').find('*').hide();
          } else {
            $('#install_helper').find('.content:first').hide();  
          }
          
          if (!window.useAcx) {
            $('#install_helper').addClass('across');
          }
          $('#connector').show();
          showlayer();
        });
      }
      
      if (isShowToolbar()) {
        window.setTimeout(function(){
          that.show();
          op.target.css('line-height', (op.height - 1) + 'px'); 
        }, 500);          
      }
    }// func init end
  };

  function check() {
    _storage.data({'toggle': $(this).attr('checked') ? 'hide' : 'show'});
    $(this).attr('checked') && hide();
  };
    
  function hide() {
    if (!helper) {
      return false;
    }
    helper.isShow() ? helper.hide(that.hide) : that.hide();
    helperTop = -2;
  };

  function showlayer() {
    if (!helper) {
      return false;
    };
    ({
      'showing'    : helper.hide,
      'showstop'   : helper.show,
      'showfinish' : helper.hide,
      'hideing'    : helper.show,
      'hidestop'   : helper.hide,
      'hidefinish' : helper.show
    }[helper.getState()])();
  };
  return false;
};

var _storage;

var isShowToolbar = function(){
  return window.useBrowserHelper && _storage.data().toggle != 'hide';
}
window == parent && $(document).ready(function() {
  if (isSafari2() || checkNotUse()) {
    return false;
  }
  _storage = toggle_Storage('username');
  
  var toolbar = $('#tell_toolbar');

  tell_toolbar({
    'target'    : toolbar,          
    'closer'    : toolbar.find('button.close'),
    'checker'   : toolbar.find('input:checkbox'),
    'showlayer' : toolbar.find('a.advice'),
    'connector' : $('#connectorHelp'),
    'height'    : 31,
    'top'       : 0
  }).init();
  
});

})(jQuery);