﻿
var noun_type_url = {
  _name: "url",

  suggest: function( text, html ) {
  
	var location = String(Application.activeWindow.activeTab.document.location);
    var suggestions  = [];
	suggestions.push( CmdUtils.makeSugg(location) );
	suggestions.push( CmdUtils.makeSugg( CmdUtils.getSelection() ) );
	
	return suggestions;
	
  }
}


CmdUtils.CreateCommand({
	name: "add-to-delicious",
	
	homepage: "http://parentno.de/ubiquity",
	author: { name: "Andreas Trost", email: "ndrs(dot)trst(at)gmail(dot)com"},
	license: "MPL",
	description: "Saves URL to del.icio.us",
	icon : "http://static.delicious.com/img/delicious.small.gif",

	takes: {"url": noun_type_url},
  
	preview: function( pblock, url ) {
		if (url.length < 1) url = 'a url';
		var out = '';
		out += 'Add <strong>' + url.text + '</strong> to del.icio.us';
		
		pblock.innerHTML = out;
	},
	  
	execute: function( url ) {
		var deliciousUrl = "http://delicious.com/save?";
		var params = {
			title: context.focusedWindow.document.title,
			url: url.text
		};
		Utils.openUrlInBrowser( deliciousUrl + jQuery.param(params) );
	}
})



CmdUtils.CreateCommand({
	name: "get-delicious",
	
	homepage: "http://parentno.de/ubiquity",
	author: { name: "Andreas Trost", email: "ndrs(dot)trst(at)gmail(dot)com"},
	license: "MPL",
	description: "Gets bookmarks from del.icio.us",
	help: "Insert user name and optional a tag. The bookmarks will show up in the preview, enter will get you directly to del.icio.us",
	icon : "http://static.delicious.com/img/delicious.small.gif",

	takes: {"user": noun_arb_text},
	modifiers: {tag: noun_arb_text},
	
  
	preview: function( pblock, user, mods) {
		pblock.innerHTML = "Will get bookmarks from del.icio.us";
	
		var baseUrl = "http://feeds.delicious.com/html/" + user.text + "/" + mods.tag.text;
		
		var params = "";
		jQuery.get( baseUrl, params, function( answer ) {
			if (answer.length <= 1) return;
			if (mods.tag.text == undefined) mods.tag.text = "";
			answer = answer.substr(0, answer.indexOf('<a href="http://feeds.delicious.com/v2/rss/'));
			out = '<h3 style="font-weight: normal; line-height: 200%">' + user.text + '\'s ' + mods.tag.text + ' bookmarks';
			out += '<span style="font-family: verdana, sans-serif; font-size: 11px; line-height: 150%">';
			out += answer + "</span>" ;
			pblock.innerHTML = out;
	    })
	},
	  
	execute: function( user, mods ) {
		if (mods.tag.text == undefined) mods.tag.text = "";
		var baseUrl = "http://delicious.com/" + user.text + "/" + mods.tag.text;
		Utils.openUrlInBrowser(baseUrl);
	}
})
