SPICEWORKS.plugin.add(     { name:"LogMeIn Rescue", version:"1.46656", description:"Launch the LogMeIn Rescue Technician Console right from the Help Desk or device view.", guid:"p-931942c0-c5ba-012c-642f-00237de05916-1261422981", settings:{}, contentAreas: [{"content_type":"text/javascript","updated_at":"2010/06/15 19:32:24 +0100","id":20,"description":null,"content_name":"initialize.js","user_id":null}], initialize:function(plugin){ 
// Copyright (C) 2009-10 Spiceworks, Inc.  All Rights Reserved.  http://www.spiceworks.com
// LogMeIn Rescue remote support.

plugin.configure({
  settingDefinitions: [{
    name: 'email',
    label: 'Email:',
    type: 'text'
  },{
    name: 'password',
    label: 'Password:',
    type: 'password'
  }]});
  
var IMAGERY = {
  LOGMEIN: 'http://resources.spiceworks.com/banners/spiceworks/plugin/logmein/logmein.png'
};

function getTools() {
  var tools = $$$('.sui-overview div.tools ul');

  if (!tools) {
    var x = $$$('.sui-overview');
    if (x) {
      x.insert('      <div class="section tools">        <h3>Tools</h3>        <ul class="properties content"></ul>      </div>      ');    
    }
    tools = $$$('.sui-overview div.tools ul');
  }
  return tools;
}

var ssoURL = '/utility/get_logmein_session.json';
var existingURL = 'http://community.spiceworks.com/r/416?url=https://secure.logmeinrescue.com/US/TechConsole/Console.aspx?do=run';
var trialURL = 'http://community.spiceworks.com/r/415';
var loginPanel = new Element('div', {'class':'login_panel'}).insert(new Element('div', {'class': 'intro'})).insert(new Element('div', {style: 'display:none', 'class': 'highlight'}).insert('Incorrect email or password'));
var loginURL = 'https://secure.logmeinrescue.com/API/Login.aspx';
var loginSpec = [{name: 'email', label: 'Email:', type: 'string', defaultValue: '', 'class': 'email'},
                 {name: 'password', label: 'Password:', type: 'password', defaultValue: '', 'class': 'password'}];
var accountConfig = {type: 'logmein', description: 'LogMeIn user', login: '', password: ''};
var fieldSet = SPICEWORKS.field.createFieldSet(loginPanel, loginSpec);

var pinDiv = new Element('div').insert(new Element('div').insert('PIN Code: ').insert(new Element('span', {'class': 'logmein pin'}))).insert(new Element('div').insert('Your customer can enter this PIN code at http://www.logmein123.com to automatically enter your Private Queue. This PIN code will remain valid for the next 20 minutes.')).insert(new Element('div').insert('Click here to launch LogMeIn Rescue'));

function logMeIn(email, password, x, y) {
  new Ajax.Request(ssoURL, {method: 'post', parameters:{email: email, password:password}, onComplete:function(request) {
    var success = request.responseJSON;
    if (success && success.pin) {
      var iframe = new Element('iframe', {name: 'logmein_frame', id: 'logmein_frame', style: 'display:none'});
      $('primary').insert(iframe);
      var hiddenForm = new Element('form', {target: 'logmein_frame', method:'post', action:loginURL}).insert(new Element('input', {type: 'hidden', name: 'email', value:email})).insert(new Element('input', {type: 'hidden', name: 'pwd', value:password}));
      $('primary').insert(hiddenForm);
      iframe.observe('load', function(event) {
        window.open(existingURL, '_blank');
        hiddenForm.remove();
        iframe.remove();
      });
      hiddenForm.submit();
    } else {
      loginPrompt(true, x, y);
    }
  }});
}

function loginPrompt(wrong, x, y) {
  var popup = new Popup('logmein_popup', 'LogMeIn Rescue Credentials', loginPanel);
  var el = $('logmein_popup');
  el.style.left = (x - el.getWidth()) + 'px';
  el.style.top = (y - el.getHeight()) + 'px';
  el.style.width = '320px';
  if (wrong) {
    el.down('div.highlight').show();
  } else {
    el.down('div.highlight').hide();
  }
  fieldSet.set(plugin.settings);
  var p = loginPanel.down('p');
  if (!p) {
    var btn = new Element('input', {type:'image', src:'/images/forms/buttons/small/save.gif', 'class': 'save'});
    p = new Element('p').insert(btn);
    loginPanel.insert(p);
    var triala = new Element('a', {'target':'_blank', 'href': trialURL}).insert('Sign up for a free trial.');
    loginPanel.insert(new Element('div', {'class': 'intro'}).insert('<span class="bold">Not yet a LogMeIn user?</span> ').insert(triala));
    var closePopup = function(event) {
      popup.element.down('.close').fire('click');
    };
    triala.observe('click', closePopup);
    btn.observe('click', function(event) {
      // Save credentials
      var email = loginPanel.down('div.email').down('input').value;
      var password = loginPanel.down('div.password').down('input').value;
      plugin.storeSettings({email: email, password: password});
      logMeIn(email, password, x, y);
    });
    btn.observe('click', closePopup);
  }
}

// records a stat if the stats API exists, this is to be backwards compatible with older versions of Spiceworks since the API was added post-4.5 but this plugin was released with 4.5
function safeStat(statName){ if (plugin['stats'] && typeof(plugin.stats['record']) == 'function') plugin.stats.record(statName); };

var init = function () {
    var tools = getTools();
    if (!tools) return;
    tools.insert(new Element('ul', {id: 'logmein_link', 'class': 'tools_link properties logmein'}).insert(
      new Element('li', {'class': 'logmein', id: 'logmein_image', title: 'Launch the LogMeIn Rescue Technician Console'}).update('LogMeIn Rescue')));
    tools.down('ul.logmein').observe('click', function(event) {
      safeStat('click'); // record stat so that conversions can be tracked
      var email = plugin.settings.email;
      var password = plugin.settings.password;
      var x = tools.viewportOffset().left;
      var y = tools.viewportOffset().top;
      if (!(email && password)) {
        loginPrompt(false, x, y);
      } else {
        logMeIn(email, password, x, y);
      }
    });
};

// Show this tool for inventory item details and ticket details
SPICEWORKS.app.inventory.group.item.ready(init);
SPICEWORKS.app.helpdesk.ticket.ready(init);

SPICEWORKS.utils.addStyle(
    'div.tools ul.properties > li.logmein{ background-image:url(' + IMAGERY.LOGMEIN + '); } ' +
    'div.login_panel span.bold{font-weight:bold}' +
  'div.login_panel div.intro{padding-bottom:3px; text-align:center;}' +
    'div.login_panel .save{margin-left:83px;margin-bottom:12px}' +
    'div.tools ul.tools_link:hover { text-decoration:underline }' +
    'div.tools ul.tools_link { cursor:pointer }' +
    'div#logmein_popup_content input.text{ width:180px }' +
    'div#logmein_popup_content input.password{ width:180px }' +
'div#logmein_popup h3.title span {' +
'-moz-background-clip:border;' +
'-moz-background-inline-policy:continuous;' +
'-moz-background-origin:padding;' +
'background:transparent url(' + IMAGERY.LOGMEIN + '?2) no-repeat scroll 0 0;' +
'padding:0 0 2px 18px}'
    );

      }
    }
 );
