SPICEWORKS.plugin.add(     { name:"CDW Search", version:"1.51389", description:"Search CDW from the Spiceworks status bar (North America only). Click configure for more options.", guid:"p-028b77a0-24bf-012c-f2b3-0011856b5722-1250548397", settings:{}, contentAreas: [{"content_type":"image/png","updated_at":"2010/06/15 19:32:27 +0100","id":21,"description":null,"content_name":"cdw_background.png","user_id":null},{"content_type":"image/png","updated_at":"2010/06/15 19:32:27 +0100","id":22,"description":null,"content_name":"cdw_red.png","user_id":null},{"content_type":"text/css","updated_at":"2010/06/15 19:32:27 +0100","id":23,"description":null,"content_name":"cdw_search.css","user_id":null},{"content_type":"image/png","updated_at":"2010/06/15 19:32:27 +0100","id":24,"description":null,"content_name":"cdw_white.png","user_id":null},{"content_type":"image/png","updated_at":"2010/06/15 19:32:27 +0100","id":25,"description":null,"content_name":"cdw_white.png","user_id":null},{"content_type":"image/png","updated_at":"2010/06/15 19:32:27 +0100","id":26,"description":null,"content_name":"find_it.png","user_id":null},{"content_type":"image/png","updated_at":"2010/06/15 19:32:27 +0100","id":27,"description":null,"content_name":"find_it_hover.png","user_id":null},{"content_type":"text/javascript","updated_at":"2010/06/15 19:32:27 +0100","id":28,"description":null,"content_name":"initialize.js","user_id":null}], initialize:function(plugin){ // ==SPICEWORKS-PLUGIN==
// @name          CDW Search
// @description   CDW integrated into the Spiceworks status bar!
// @version       0.90
// ==/SPICEWORKS-PLUGIN==

var FocusReader = Class.create({  
    initialize: function(){  
      this.currentFocus = null;  
      var focusRead = this.focusRead.bindAsEventListener(this);
      if (document.addEventListener) {
        document.addEventListener("focus", focusRead, true);            
      } else {
        /* Also add the listener for IE which does not support capturing */  
        document.attachEvent("onfocusin",focusRead);
      }
    },  

    focusRead: function(e){  
        /* Need to force extending the event since
            it won't be extended by default */  
        e = Event.extend(e);  
        this.currentFocus = e.element();  
    },  

    getCurrentFocus: function(){  
        return this.currentFocus;  
    },  

    getCurrentFocusId: function(){  
    if(this.currentFocus.identify) return this.currentFocus.identify();  
    }  

});

var focusReader = new FocusReader();

// enumerations
var DISPLAY_SUPPORTED_REGIONS = 'Off';
var DISPLAY_ALWAYS = 'On';

var cdw_reload_window = function(e) {
  window.location.reload();
}

plugin.configure({
  settingDefinitions:[
    {name:'country', label:'Search Site', type:'select', options:[['US','com'],['Canada','ca'],['Government','www.cdwg.com']], defaultValue:'com', example:'Looking for CDW.ca?'},
    {name:'display_condition', label:'Show Outside North America', type:'enumeration', defaultValue:DISPLAY_SUPPORTED_REGIONS, options:[DISPLAY_SUPPORTED_REGIONS,DISPLAY_ALWAYS], example:'Want to use CDW Search outside the US?'}
  ],
  onSave: cdw_reload_window
});


// When the app is ready, let's initialize the CDW statusbar panel...
SPICEWORKS.app.ready( function () {
  cc = SPICEWORKS.country.code();
  if ( cc !== null ) {
    cc = cc.toUpperCase();
  } else { cc = ''; }

  var SUPPORTED_REGIONS = ['US','CA','MX'];

  /* only run the plugin if display hold is on,
   or we are in the right country (US) */
  if ( plugin.settings.display_condition ==  DISPLAY_ALWAYS ||
     SUPPORTED_REGIONS.include(cc) )
  {

    plugin.includeStyles();

    statusBarPanel({
      name:'cdw',
      section: 'branded',
      icon: plugin.contentUrl('cdw_red.png'),
      iconOpen: plugin.contentUrl('cdw_white.png'),
      update: updateCDWPanel,
      onShow: focusSearch
    });
  } 
});

// Update the panel with content.  Performed at page load time.
function updateCDWPanel(element, cdwPanel) {
  var baseUrl = null;
  if (/^(com|ca)/.match(plugin.settings.country)) {
    baseUrl = 'www.cdw.' + plugin.settings.country;
  } else {
    baseUrl = plugin.settings.country;
  }

  var content = "<h1><a href='http://" + baseUrl + "/shop/search/browse.aspx?cm_mmc=Spiceworks-_-SmallBusiness-_-Online_Ad-_-CDW_Search_Bar' target='_blank'>Browse All Categories</a>Search CDW</h1>" +
    "<form id='cdw_form' action='http://" + baseUrl + "/shop/search/results.aspx' target='_blank'>" +
    "<input name='key' value='' id='cdw_search_box'/>" +
    "<input type='hidden' name='sr' value='1'/>" +
    "<input type='hidden' name='cm_mmc' value='Spiceworks-_-SmallBusiness-_-Online_Ad-_-CDW_Search_Bar'/>" +
    "<select name='searchscope' id='cdw_search_scope'>" +
      "<option value='All'>All Products</option>" +
      "<option value='Mfgpart'>Mfg Number</option>" +
      "<option value='Edc'>CDW Number</option>" +
      "<option value='Mac'>Mac Only</option>" +
      "<option value='PC'>PC Only  </option>" +
    "</select>" +
  "</form>"

  element.update(content);

  $$('#status_bar div.cdw').first().setStyle({
     background: '#DE0007 top left url(' + plugin.contentUrl('cdw_background.png') + ') repeat-x'
  });

  var button = resource_button(function () {
    setTimeout( incrementCounter, 10 );
    $('cdw_form').submit();
  });

  $('cdw_form').insert(button);
  button.observe('mouseover', cdwPanel.stopHideTimer);
}


// Focus on the searchbox.  Performed each time the panel is shown.
function focusSearch() {
  $('cdw_search_box').focus();
  plugin.stats.record("clicked_on_statusbar_cdw_logo");
}

function incrementCounter() {
  var tempframe = new Element('iframe', {src:'http://community.spiceworks.com/r/371', style: 'position:absolute; top:0; left:0; width:0; height:0; display:none'});
  $(document.body).insert(tempframe);
  setTimeout( function () {
    tempframe.remove();
  }, 2000);
}




// -- GENERIC FRAMEWORK CODE --
// Below is non CDW specific code.

// Help load up a button from the spiceworks plugin resource server...
function resource_button(callback) {
  var btnNormal = plugin.contentUrl('find_it.png');
  var btnHover = plugin.contentUrl('find_it_hover.png');
  var button = new Element('img', {'class':'medium-button', 'src': btnNormal });
  button.observe('mouseover', function (event) {
    Event.stop(event);
    button.src = btnHover;
  });
  button.observe('mouseout', function (event) {
    Event.stop(event);
    button.src = btnNormal;
  });
  button.observe('click', callback);
  return button;
}

// Build out the statusBarPanel from the spec passed...
function statusBarPanel(spec) {
  var link, dd, panel, area, icon, icon_open, hideTimer, self={};

  area = statusAreaSection(spec.section);
  dd = new Element('dd', {'class':'openable ' + spec.name});

  link = new Element('a', {'href':SUI.voidLink});
  icon = new Element('img', {'src':spec.icon, 'class':'icon-closed'});
  iconOpen = new Element('img', {'src':spec.iconOpen, 'class':'icon-open'});
  link.insert(icon).insert(iconOpen);    

  link.observe('click', function () {
    panel.visible() ? hidePanel() : showPanel();
  });

  area.insert(dd);
  dd.insert(link);

  panel = new Element('div', {'class':'status-bar-panel ' + spec.name, 'style':'display:none'});
  panel_inner = new Element('div', {'class':'inner'});

  $('status_bar').insert(panel);
  panel.insert(panel_inner);

  // Hide Logic
  panel.observe('mouseout', startHideTimer);
  panel.observe('mouseover', stopHideTimer);
  dd.observe('mouseout', startHideTimer);
  dd.observe('mouseover', stopHideTimer);


  // FUNCTIONS LOCAL TO statusBarPanel()
  function hidePanel() {
    link.blur();
    dd.removeClassName('open');
    new Effect.BlindUp(panel, {duration:0.25});
  }
  function showPanel() {
    link.blur();
    stopHideTimer();
    dd.addClassName('open');
    panel.clonePosition(dd, {setLeft:true, setTop:false, setWidth:false, setHeight:false});
    panel.style.height='';
    new Effect.BlindDown(panel, {duration:0.25, afterFinish: spec.onShow});
  }

  function startHideTimer() {
    if (panel.visible()) {
      hideTimer = setTimeout(function () {
        var foc = focusReader.getCurrentFocus();
        if (!foc || !foc.descendantOf || !foc.descendantOf(dd) && !foc.descendantOf(panel))
          hidePanel();
        else
          startHideTimer();
      }, 1000);
    }
  }
  function stopHideTimer() {
    if (hideTimer) {
      clearTimeout(hideTimer);
    }
  }

  function statusAreaSection(section) {
    var left, brandedStatusArea = $$('#status_bar .' + section).first();
    if (!brandedStatusArea) {
      left = $$('#status_bar .left').first();
      brandedStatusArea = new Element('dl', {'class': 'status-bar-section ' + section});
      left.insert(brandedStatusArea);
    }
    return brandedStatusArea;
  }

  // Add methods to the object
  self.startHideTimer = startHideTimer;
  self.stopHideTimer = stopHideTimer;
  self.hidePanel = hidePanel;
  self.showPanel = showPanel;

  // Populate the panel!!
  spec.update(panel_inner, self); // populate the panel

  return self;
}



      }
    }
 );
