<div id="primary_header">
	<h1><?php print $this->translate('Add Plugins')?></h1>
</div>

<div id="primary_content">
	<div id="primary_content_inner">
		<div id="Flash"></div>
		
		<div id="licence-error" class="default_notice">
		<?php echo $this->translate("Select a plugin from your hard drive to upload. Check out available plugins at") ?> <a href="http://atmail.com/plugins/#plugins" target="_blank">http://atmail.com/plugins/</a>
	    </div>
		
		<table class="settings" id="add_plugins">
			<?php if( isset($this->installResult[1]) && !empty($this->installResult[1]) ): ?>
		    <tr>
		        <td class="label"><label><?php print $this->translate("Post install information")?>:</label></td>
		        <td><?php echo $this->installResult[1] ?></td>
		    </tr>
		    <?php endif; ?>
			<tr>
				<td class="label"><label for="replyTo"><?php print $this->translate('Plugins available for installation')?>:</label></td>
				<td>
<?php 			foreach ($this->availablePluginPackages as $p): ?>
					<a href="#" class="availablePluginPackage" id="<?php echo $p?>" title="<?php echo $this->translate("Click to see plugin info before installing") ?>"><?php echo $p ?></a><br />
<?php 			endforeach; ?>
				</td>
			</tr>
			<tr>
				<td class="label"><label for="replyTo"><?php print $this->translate('Upload a plugin for installation')?>:</label></td>
				<td>
					<div id="back_button"><a href="#"><span class="stronglastword"><?php echo $this->translate('Upload Plugin') ?></span></a></div>
				</td>
			</tr>
		</table>
		<div id="pluginInfo">
		</div>
	</div>
</div>
<script>
$( function() {
	// bind to the form's submit event and cancel legacy submit
	$("#Flash", "#Add_Plugins").hide();
	
	new AjaxUpload( $('#back_button a', '#Add_Plugins'), {
		// Location of the server-side upload script
		action: "<?php echo $this->moduleBaseUrl ?>/plugins/preinstall",
		// File upload name
		name: 'newPlugin',
		// Submit file after selection
		autoSubmit: true,
		onChange: function(file, extension){ },
		onSubmit: function(file, extension) 
		{ 
		
			if( !(extension && /^(tgz)$/i.test(extension)) )
			{

				// extension is not allowed
				$.simpleFlash('Error: invalid file extension', 'error');
				// cancel upload
				return false;

			}

		},
		onComplete: function(file, responseHTML) 
		{
		    
			if (responseHTML.length > 100) 
			{					
			
				$('#pluginInfo').html(responseHTML);
			
			}
			else
			{
			
				//error case with short error response
				//$('#' + fileUniqueId, '.<?php echo $this->composeID ?>').remove()
				$.simpleFlash('Error occurred uploading file: ' + responseHTML, 'error');
			
			}

		}

	});
		
	$('.availablePluginPackage').bind("click", function() 
	{
	
		$("#pluginInfo").load("<?php echo $this->moduleBaseUrl ?>/plugins/preinstall/existing/" + $(this).attr('id'));
	
	});
	
	//thow any errors that might have occured
<?php if( isset($this->installResult) ): ?>
    <?php if( $this->installResult[0] === true ): ?>
	$.simpleFlash('<?php echo addslashes($this->translate('Plugin Successfully Installed')) ?>', 'notice');
    <?php else: ?>
	$.simpleFlash('<?php echo addslashes($this->translate($this->installResult[1])) ?>', 'error');
    <?php endif; ?>
<?php endif; ?>
	
});
</script>
