var first = new Object;

CmdUtils.CreateCommand({
	names: ["crm"],

	description: "Icrcem custmer search.",
	help: "CRM (query).",
	author: {
		name: "Patrick Woodings",
		email: "patrickwoodings@gmail.com",
		homepage: "http://www.digitaldetritus.com",
	},
	license: "GPL",
	homepage: "http://ubiquity.mozilla.com/",
	icon: "http://www.mozilla.com/favicon.ico",
  
	arguments: [{role: "object",
		nountype: noun_arb_text,
		label: "query"},
		{role: 'source',
		nountype: noun_arb_text,
		label: "type"},
		],
			   
	preview: function(pblock, arguments){
		var type = "tbl_customers_contacts";
		var url = "http://itdb.comtecs.co.uk/json_search.php?q=" + arguments.object.text + "&type="+type;

		$.getJSON(url, function(data){
			var msg = '{for object in objects}{if object}<div>';
			first = data[0];
			msg += '<h5 style="margin:10px 0 0;"><a href="http://itdb.comtecs.co.uk/customers.php?id=${object.id}">${object.title} ${object.firstname} ${object.lastname}</a></h5>';
			
			msg += '<p style="font-size:12px; margin:0;">${object.position}</p>';
			msg += '<p style="font-size:12px; margin:0;">${object.phone}</p>';
			msg += '<p style="font-size:12px; margin:0;">${object.mobile}</p>';
			msg += '<p style="font-size:12px; margin:0;">${object.email}</p>';

			msg += '</div>{/if}{/for}';
			
			pblock.innerHTML = _(msg, {objects: data});
		});
	},

	execute: function(arguments){
  		Utils.openUrlInBrowser("http://itdb.comtecs.co.uk/customers.php?id=" + first.id);
	}
});
