/*
  Hack: mms by Luqman Amjad
  Author: Luqman Amjad
  Website: http://www.luqmanamjad.com
  Twitter: http://twitter.com/luqmanamjad
  Required: vbulletin 4.x.x
*/
(function(){

  vB_AJAX_mms_Init = function(e){
    if (AJAX_Compatible && (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax < 2)){
      links = YAHOO.util.Dom.getElementsByClassName('mms', 'a' , 'mms'), i = links.length;
      while(i--){
        link = links[i];
      };
      YAHOO.util.Event.addListener('mms', 'click', vB_AJAX_mms_Click);
      YAHOO.util.Event.addListener('mms_list', 'mouseout', vB_AJAX_mms_Mouseout);
    };
  };

  vB_AJAX_mms_Mouseout = function(e){
    var target = YAHOO.util.Event.getRelatedTarget(e);
    if (YAHOO.util.Dom.isAncestor(this, target) == false){
      var mms_ul = YAHOO.util.Dom.getLastChild(this);
      YAHOO.util.Dom.setStyle(mms_ul, 'display', 'none');
    }
  };
  
  vB_AJAX_mms_Click = function(e){
    var target = YAHOO.util.Event.getTarget(e);    
    this.change = function(){
      if (YAHOO.util.Dom.hasClass(target, 'mms')){
        YAHOO.util.Connect.asyncRequest("POST", "ajax.php?do=mms_update&securitytoken=" + SECURITYTOKEN + "&mms=" + target.title, {
          success: this.handle_ajax_response,
          failure: vBulletin_AJAX_Error_Handler,
          timeout: 15000,
          scope: this
        }, SESSIONURL + "do=mms_update&securitytoken=" + SECURITYTOKEN + "&mms=" + PHP.urlencode(target.title));
      };
    };

    this.handle_ajax_response = function(ajax){
      var mms = YAHOO.util.Dom.get('mms-mine');

      if (mms.nodeType == 1 && /img/i.test(mms.nodeName)){
        mms.setAttribute('src', mms_IMAGES_PATH + '/' + ajax.responseText + '.gif');
        mms.setAttribute('alt', ajax.responseText);
        mms.setAttribute('title', ajax.responseText);
      } else {
        mms.innerHTML = ajax.responseText;
      }
    }

    this.change();
    YAHOO.util.Event.stopEvent(e);
  };

  YAHOO.util.Event.addListener(window, 'load', this.vB_AJAX_mms_Init);
})()