////////////////////////////////////////
/// Class: PLinkManager              ///
////////////////////////////////////////
PLinkManager = function() {
	this.backend = PEnvironment.linkUrl;
};

PLinkManager.prototype.add = function(link, f) {
	var u = this.backend + '?act=add' + '&link=' + encodeURIComponent(link.url) + '&name=' + encodeURIComponent(link.name);
	f = f ? f : {};
	PAsync2.call(u, f);
};

PLinkManager.prototype.printPDF = function(link, f, parms, template ) {
	template = template ? '&template=' + encodeURIComponent( template ) : '';
	parms = parms ? '&parms=' + encodeURIComponent(parms) : '';
        if( link && f ) {
                var u = this.backend
                        + '?act=print'
                        + '&type=pdf'
                        + '&link=' + encodeURIComponent(link.url)
                        + '&name=' + encodeURIComponent(link.name)
			+ template
			+ parms;
                f = f ? f : {};
                PAsync2.call(u, f);
        }
};

PLinkManager.prototype.printJPG = function(link, f, parms, template ) {
	if( template && parms ) {
	        var u = this.backend 
			+ '?act=print'
			+ '&type=jpeg'
			+ '&template=' + encodeURIComponent( template )
			+ '&link=' + encodeURIComponent(link.url) 
			+ '&name=' + encodeURIComponent(link.name) 
			+ '&parms=' + encodeURIComponent(parms);
        	f = f ? f : {};
	        PAsync2.call(u, f);
	} else alert("Template and parameters must be defined.");
};

PLinkManager.prototype.printPNG = function(link, f, parms, template ) {
	if( template && parms ) {
	        var u = this.backend 
			+ '?act=print'
			+ '&type=png'
			+ '&template=' + encodeURIComponent( template )
			+ '&link=' + encodeURIComponent(link.url) 
			+ '&name=' + encodeURIComponent(link.name) 
			+ '&parms=' + encodeURIComponent(parms);
        	f = f ? f : {};
	        PAsync2.call(u, f);
	} else alert("Template and parameters must be defined.");
};

PLinkManager.prototype.printCSV = function(link, f, template, parms ) {
	if( template ) {
	        var u = this.backend 
			+ '?act=print'
			+ '&type=csv'
			+ '&template=' + encodeURIComponent( template )
			+ '&link=' + encodeURIComponent(link.url) 
			+ '&name=' + encodeURIComponent(link.name) 
			+ '&data=' + encodeURIComponent(link.data)
			+ '&parms=' + encodeURIComponent(parms);
        	f = f ? f : {};
	        PAsync2.call(u, f);
	} else alert("Template and parameters must be defined.");
};

PLinkManager.prototype.embed = function( link, f, parms, template, action ) {
        if( template && parms ) {
                var u = this.backend
                        + '?act=' + action
                        + '&type=jpeg'
                        + '&template=' + encodeURIComponent( template )
                        + '&link=' + encodeURIComponent(link.url)
                        + '&name=' + encodeURIComponent(link.name)
                        + '&parms=' + encodeURIComponent(parms);
                f = f ? f : {};
                PAsync2.call(u, f);
        } else alert("Template and parameters must be defined.");
};

PLinkManager.prototype.emailLink = function( link, f, emails, message, from ) {
	emails = ( emails.length > 0 ) ? '&e=' + encodeURIComponent(emails.join(",")) : ""; 
	message = message ? '&m=' + encodeURIComponent( message ) : "";
	var u = this.backend
		+ '?act=email'
		+ '&from=' + encodeURIComponent(from)
		+ '&link=' + encodeURIComponent(link.url)
		+ '&siteId=' + PEnvironment.siteId
		+ emails
		+ message;
	PAsync2.call( u, f  );
};

PLinkManager.prototype.deleteLink = function(linkId, f) {
	if(linkId) {
		var u = this.backend
			+ '?act=del&id='
			+ linkId;
		f = f ? f : function() {};
		PAsync2.call(u, f);
	}
};

PLinkManager.prototype.renameLink = function(linkId, newName, f) {
	// Used to rename links. They must belong to the current user.
	if(linkId && newName) {
		var u = this.backend
			+ '?act=ren'
			+ '&id=' + linkId
			+ '&name=' + encodeURIComponent(newName);
		f = f ? f : function() {};
		PAsync2.call(u, f);
	}
};

