plugin.loadLang();
plugin.loadMainCSS();
if(browser.isKonqueror)
	plugin.loadCSS("konqueror");

plugin.recentTrackers = {};

theWebUI.checkCreate = function()
{
	theDialogManager.hide('tcreate');
       	var arr = $('#trackers').val().split("\n");
	var trk = '';
	for( var i in arr )
		trk+=($.trim(arr[i])+'\r');
        this.startConsoleTask( "create", plugin.name, 
	{ 
		"piece_size" : $('#piece_size').val(), 
		"trackers" : trk,
		"path_edit" : $.trim($("#path_edit").val()), 
		"comment" : $.trim($("#comment").val()),
		"source" : $.trim($("#source").val()),
		"private" : $('#private').prop('checked') ? 1 : 0,
		"start_seeding" : $('#start_seeding').prop('checked') ? 1 : 0		
	},
	{
	       	noclose: true
	});
}

plugin.onTaskFinished = function(task,fromBackground)
{
	if(!fromBackground)
	{
		$("#xtaskno").val(task.no);
		if(!task.status)
			$('#xcsave').show();
	}		
	theWebUI.request('?action=rtget',[plugin.getRecentTrackers, plugin]);
}

rTorrentStub.prototype.rtget = function()
{
	this.content = "cmd=rtget";
        this.contentType = "application/x-www-form-urlencoded";	
	this.mountPoint = "plugins/create/action.php";
	this.dataType = "json";
}

theWebUI.showCreate = function()
{
	$('#start_seeding').prop('disabled',!theWebUI.systemInfo.rTorrent.started);
	if(theWebUI.systemInfo.rTorrent.started)
		$('#lbl_start_seeding').removeClass('disabled');
	else		
		$('#lbl_start_seeding').addClass('disabled');		
	theDialogManager.show('tcreate');
}

plugin.getRecentTrackers = function( data )
{
	plugin.recentTrackers = data;
	if(propsCount(plugin.recentTrackers))
		$("#recentTrackers").removeClass("disabled");
        else
	        $("#recentTrackers").addClass("disabled");
}

theWebUI.addTrackerToBox = function(ann)
{
	var val = $('#trackers').val();
	if(val.length)
		val+='\r\n';
	$('#trackers').val( val+ann );
	$('#trackers').focus();
}

theWebUI.showRecentTrackers = function()
{
	if(propsCount(plugin.recentTrackers))
	{
		theContextMenu.clear();
		for( var domain in plugin.recentTrackers )
			theContextMenu.add([domain,"theWebUI.addTrackerToBox('"+addslashes(plugin.recentTrackers[domain])+"')"]);
		var offs = $("#recentTrackers").offset();
		theContextMenu.show(offs.left,offs.top-theContextMenu.obj.height()-5);
	}
}

plugin.onLangLoaded = function()
{
	var plg = thePlugins.get("_task");
	if(!plg.allStuffLoaded)
		setTimeout(arguments.callee,1000);
	else
	{
		theWebUI.request('?action=rtget',[plugin.getRecentTrackers, plugin]);
		$('#tsk_btns').prepend(
			"<input type='button' class='Button' id='xcsave' value='"+theUILang.torrentSave+"'>"
			 );
		plugin.addButtonToToolbar("create",theUILang.mnu_create,"theWebUI.showCreate()","remove");
		plugin.addSeparatorToToolbar("remove");
		var pieceSize = 
			"<label>"+theUILang.PieceSize+": </label>"+
			"<select id='piece_size' name='piece_size'>"+
				"<option value=\"32\">32"+theUILang.KB+"</option>"+
				"<option value=\"64\">64"+theUILang.KB+"</option>"+
				"<option value=\"128\">128"+theUILang.KB+"</option>"+
				"<option value=\"256\" selected=\"selected\">256"+theUILang.KB+"</option>"+
				"<option value=\"512\">512"+theUILang.KB+"</option>"+
				"<option value=\"1024\">1"+theUILang.MB+"</option>"+
				"<option value=\"2048\">2"+theUILang.MB+"</option>"+
				"<option value=\"4096\">4"+theUILang.MB+"</option>"+
				"<option value=\"8192\">8"+theUILang.MB+"</option>"+
				"<option value=\"16384\">16"+theUILang.MB+"</option>"+
				"</select>";
		if(plugin.hidePieceSize)
			pieceSize = "";	

		theDialogManager.make("tcreate",theUILang.CreateNewTorrent,
			"<div class='cont fxcaret'>"+
				"<fieldset>"+
					"<legend>"+theUILang.SelectSource+"</legend>"+
					"<input type='text' id='path_edit' name='path_edit' class='TextboxLarge' autocomplete='off'/>"+
					"<input type=button value='...' id='browse_path' class='Button'><br/>"+
				"</fieldset>"+
				"<fieldset>"+
					"<legend>"+theUILang.TorrentProperties+"</legend>"+
                       	               "	<label>"+theUILang.Trackers+": </label>"+
					"<textarea id='trackers' name='trackers'></textarea><br/>"+
        	                       	       "<label>"+theUILang.Comment+": </label>"+
        		               	"<input type='text' id='comment' name='comment' class='TextboxLarge'/><br/>"+
        	                       	       "<label>" + theUILang.source + ": </label>"+
        		               	"<input type='text' id='source' name='source' class='TextboxLarge'/><br/>"+
					pieceSize+	
				"</fieldset>"+
				"<fieldset>"+
					"<legend>"+theUILang.Other+"</legend>"+
					"<label for='start_seeding' id='lbl_start_seeding' class='nomargin'><input type='checkbox' name='start_seeding' id='start_seeding'/>"+theUILang.StartSeeding+"</label>"+
					"<label class='nomargin'><input type='checkbox' name='private' id='private'/>"+theUILang.PrivateTorrent+"</label><br/>"+
				"</fieldset>"+
			"</div>"+
			"<div class='aright buttons-list'><input type='button' id='recentTrackers' value='"+theUILang.recentTrackers+"...' class='Button menuitem' onclick='theWebUI.showRecentTrackers()'/><input type='button' id='torrentCreate' value='"+theUILang.torrentCreate+"' class='OK Button' onclick='theWebUI.checkCreate()'/><input type='button' class='Cancel Button' value='"+theUILang.Cancel+"'/></div>",true);		
		$(document.body).append($("<iframe name='xcreatefrm'/>").css({visibility: "hidden"}).attr( { name: "xcreatefrm", id: "xcreatefrm" } ).width(0).height(0));
		$(document.body).append(
			$('<form action="plugins/create/action.php" id="xgetfile" method="post" target="xcreatefrm">'+
				'<input type="hidden" name="cmd" value="getfile">'+
				'<input type="hidden" name="no" id="xtaskno" value="0">'+
			'</form>').width(0).height(0));
		$("#xcsave").click( function()
		{
			$('#xgetfile').submit();
		});
		if(thePlugins.isInstalled("_getdir"))
		{
			plugin.btn = new theWebUI.rDirBrowser( 'tcreate', 'path_edit', 'browse_path', null, true );
			theDialogManager.setHandler('tcreate','afterHide',function()
			{
				plugin.btn.hide();
			});
		}
		else
			$('#browse_path').remove();
		theDialogManager.setHandler('tskConsole','beforeShow',function()
		{
			$('#xcsave').hide();
		});
		plugin.markLoaded();		
	}
};

plugin.onRemove = function()
{
	plugin.removeSeparatorFromToolbar("remove");
	plugin.removeButtonFromToolbar("create");
}

plugin.langLoaded = function() 
{
	if(plugin.enabled)
		plugin.onLangLoaded();
}
