/**
 * i2h.de Firefox Jetpack Plugin <http://i2h.de/pages/widgets#firefox-jetpack-plugin>
 *
 * shortens an url with just one click via the http://i2h.de short url service
 * the short url gets copied to your clipboard
 *
 * @filename jetpack.js
 * @version 0.5
 *
 * @author (c) 2009-2010 http://i2h.de
 * @author i2h
 *
 * @history v0.5  28.01.2010 - added contextmenu shorten function 
 * @history v0.4  23.01.2010 - added submission of title 
 * @history v0.3  15.01.2010 - embeded icons, added regular expression checks
 * @history v0.2  12.09.2009 - updated clipboardfunctions to work w/ jetpack 0.4
 * @history v0.1  07.09.2009 - initial release
 */

var manifest = {firstRunPage: '<p>Das <a href="http://i2h.de">i2h.de</a> Plugin wurde installiert!</p>'};
jetpack.future.import('clipboard');
jetpack.future.import('menu');

var i2h = {
  version: '0.5',
  icon : 'data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAABMLAAATCwAAAAAAAAAAAAAAAAAAW1tb/1tbW/9bW1v/W1tb/1tbW/9bW1v/W1tb/1tbW/9bW1v/W1tb/1tbW/9bW1v/W1tb/1tbW/8AAAAAW1tb/1tbW//o6Oj//////////////////////////////////////////////////////+jo6P9bW1v/W1tb/1tbW//o6Oj//////0Oa//9Dmv//Qpn//0OZ//9Dmv//Q5r//0KZ//9Cmf//Qpn//0Oa////////6Ojo/1tbW/9bW1v//////0Wa/f9Fl/f/RJXz/0SU8/9CkvH/Q5Hv/0OS8P9ElfP/RJT0/0OT8v9Dk/L/QpTy//b29v9aWlr/W1tb//////9Jmvj/RI/m/0CH2/9CiN3/QIbY/z+E1f9Bit7/RI/m/0KL4f9Ahtn/QIbZ/0CF2P/j4+P/WFhY/1tbW///////rND7//////+avOX/////////////////wdn0/6rN9f//////s83r//////+/1O7/2tra/1dXV/9bW1v//////67R+///////m73l/9ro+P/n8Pv/U5DY/0qM3f+szvX//////7TN6///////wNXu/9ra2v9XV1f/W1tb//////+w0vv//////52+5f9xpuX//////8Ta9P9KidT/rMvv//////+2zuv//////8LW7v/a2tr/V1dX/1tbW///////stT8//////+iw+v/U5Th/7zX9///////ibHi/6zJ6///////t8/r///////D1u7/29vb/1dXV/9bW1v//////7XV/f//////psjx/1WU3v9fmd3/9Pj8/9Hg8/+uyuv//////7jP6v//////xdnx/+bm5v9ZWVn/W1tb//////9kqfv/X6Du/53C7///////kLjo/93p9//S4vb/s9Dz///////p8Pr//////6rM9v/39/f/Wlpa/1tbW///////udj+//////+tz/n/4O39///////1+f7/hLf1/7jX+///////rc/5/2ap9/9prP3//////1tbW/9bW1v//////22v//9sr///bK7//22v//9tr///ba///22v//9tr///ba///22u//9tr///ba////////9bW1v/W1tb/+jo6P//////b7D//2+w//9vsP//b7D//2+w//9vsP//b7D//2+w//9vsP//b7D////////o6Oj/W1tb/1tbW/9bW1v/6Ojo///////////////////////////////////////////////////////o6Oj/W1tb/1tbW/8AAAAAW1tb/1tbW/9bW1v/W1tb/1tbW/9bW1v/W1tb/1tbW/9bW1v/W1tb/1tbW/9bW1v/W1tb/1tbW/8AAAAAgAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAEAAA==',
  shrink: function(link, title) {
    // check if current url starts with http or https
    if(/^https?:\/\//i.test(link)) {      
      $.getJSON('http://api.i2h.de/v1/?appid=jetpack-plugin/' + this.version + '&format=json&url=' + escape(link) + '&title=' + escape(title), function(data) {
        if(data.error) {
          // build error message
          var message = unescape(data.error).replace(/\+/g, ' ');
        }
        else {
          // build notification message
          var message = 'Der Link ' + data.links.toString() + ' wurde in die Zwischenablage kopiert!';
          // copy short url to clipboard
          jetpack.clipboard.set(data.links.toString());
        }

        // show notification
        jetpack.notifications.show({
          title: 'i2h.de',
          body: message,
          icon: this.icon
        });
      });
    }
    else {
      // raise error, if the current url does not start with http or https
      jetpack.notifications.show({
        title: 'i2h.de',
        body: 'Fehler: Eine Url muß mit http:// oder https:// beginnen!',
        icon: this.icon
      });
    }
  }
  // shrink
};

// trigger stausbar icon clicks
jetpack.statusBar.append({
  html: '<img src="' + i2h.icon + '" width="16" height="16" title="Click to shorten current url!" />',
  width: 16,
  help: 'Click to shorten current url via i2h.de short url service!',
  onReady: function(doc){
  /*
  dblclick(function() {
      jetpack.tabs.open('http://i2h.de');
    }).
    */
    $('img', doc).click(function() {
      i2h.shrink(jetpack.tabs.focused.url, jetpack.tabs.focused.contentDocument.title);
    });
  }
});


// trigger menu item clicks
jetpack.menu.context.page.on('a').add(function(context) {
  // "context" is an object describing the context in which the menu was invoked. "context.node" is the node the user clicked.
  // return a new menuitem
  return {
    label: 'i2h.de - Url kürzen',
    icon: i2h.icon,
    command: function() {
      i2h.shrink(context.node.href, context.node.text);
    }
  };
});

