/*
 * File:        compiler.js
 * CVS:         $Id$
 * Description: Compiler for Conditional-CSS
 * Author:      Allan Jardine
 * Created:     29/06/2008
 * Modified:    $Date$ by $Author$
 * Language:    JS
 * Project:     Conditional-CSS
 * 
 * Copyright 2008 Allan Jardine, all rights reserved.
 */


var oCompiler = {
	
	_aFiles: new Array(),
	
	fnInit: function()
	{
		/* Compiler */
		document.getElementById('CompileForm').innerHTML = '\
			<input type="hidden" id="idType" name="sType" value="PHP_5"> \
			<h3>1. Choose your server platform</h3> \
			<div id="PHP" class="language_option language_selected"> \
				<h4>PHP</h4> \
				<p class="language_description">Very simple to install and portable to any platform that runs PHP - the right option for you if you want to give Conditional-CSS a go.</p> \
				<div id="id_sub_PHP_5" class="sublang_opt_selected">PHP 5</div>\
				<div id="id_sub_PHP_4" class="sublang_opt">PHP 4</div>\
			</div> \
			<div id="Csharp" class="language_option"> \
				<h4>C#</h4> \
				<p class="language_description">Runs under .NET 2.0 in Windows and Mono on many other platforms, use this version if you are using IIS or a .NET based web-server.</p> \
				<div id="id_sub_CS_ASHX" class="sublang_opt">ASHX</div>\
				<div id="id_sub_CS_CGI" class="sublang_opt">CGI</div>\
			</div> \
			<div id="C" class="language_option"> \
				<h4>C</h4> \
				<p class="language_description">Exceedingly fast and will run on just about every *nix platform, this is a little tricker to install, but very useful as a global interpreter.</p> \
				<div id="id_sub_C_CGI" class="sublang_opt">CGI</div>\
			</div> \
			\
			<div class="css_clear"></div> \
			<h3>2. Enter the files you wish to include</h3> \
			<div id="language_optional"></div>\
			<div id="compiler_files"></div>\
			\
			\
			<h3>3. Information <span class="css_small">(optional)</span></h3>\
			<div id="compiler_info">\
				<div>\
					<label>CSS Author:</label>\
					<input type="text" name="sAuthor" value="" class="text" />\
				</div>\
				\
				<div>\
					<label>Copyright:</label>\
					<input type="text" name="sCopyright" value="" class="text" />\
				</div>\
			</div>\
			\
			\
			<div class="css_center css_spacing"> \
				<button type="submit">Go!</button> \
			</div> \
		';
		
		$('#PHP').click( function () {
			var sType = document.getElementById('idType').value;
			if ( sType == 'PHP_5' || sType == 'PHP_4' )
			{
				return;
			}
			document.getElementById('idType').value = "PHP_5";
			oCompiler.fnResetClasses();
			document.getElementById('PHP').className = 'language_option language_selected';
			document.getElementById('id_sub_PHP_5').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		$('#id_sub_PHP_5').click( function () {
			document.getElementById('idType').value = "PHP_5";
			oCompiler.fnResetClasses();
			document.getElementById('PHP').className = 'language_option language_selected';
			document.getElementById('id_sub_PHP_5').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		$('#id_sub_PHP_4').click( function () {
			document.getElementById('idType').value = "PHP_4";
			oCompiler.fnResetClasses();
			document.getElementById('PHP').className = 'language_option language_selected';
			document.getElementById('id_sub_PHP_4').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		
		$('#Csharp').click( function () {
			var sType = document.getElementById('idType').value;
			if ( sType == 'CS_ASHX' || sType == 'CS_CGI' )
			{
				return;
			}
			document.getElementById('idType').value = "CS_ASHX";
			oCompiler.fnResetClasses();
			document.getElementById('Csharp').className = 'language_option language_selected';
			document.getElementById('id_sub_CS_ASHX').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		$('#id_sub_CS_ASHX').click( function () {
			document.getElementById('idType').value = "CS_ASHX";
			oCompiler.fnResetClasses();
			document.getElementById('Csharp').className = 'language_option language_selected';
			document.getElementById('id_sub_CS_ASHX').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		$('#id_sub_CS_CGI').click( function () {
			document.getElementById('idType').value = "CS_CGI";
			oCompiler.fnResetClasses();
			document.getElementById('Csharp').className = 'language_option language_selected';
			document.getElementById('id_sub_CS_CGI').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		
		$('#C').click( function () {
			document.getElementById('idType').value = "C";
			oCompiler.fnResetClasses();
			document.getElementById('C').className = 'language_option language_selected';
			document.getElementById('id_sub_C_CGI').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		$('#id_sub_C_CGI').click( function () {
			document.getElementById('idType').value = "C";
			oCompiler.fnResetClasses();
			document.getElementById('C').className = 'language_option language_selected';
			document.getElementById('id_sub_C_CGI').className = 'sublang_opt_selected';
			oCompiler.fnSetExtraInfo();
		} );
		
		this.fnSetExtraInfo();
		this.fnAddCompileFile();
	},
	
	fnResetClasses: function()
	{
		document.getElementById('PHP').className = 'language_option';
		document.getElementById('id_sub_PHP_5').className = 'sublang_opt';
		document.getElementById('id_sub_PHP_4').className = 'sublang_opt';
		document.getElementById('Csharp').className = 'language_option';
		document.getElementById('id_sub_CS_ASHX').className = 'sublang_opt';
		document.getElementById('id_sub_CS_CGI').className = 'sublang_opt';
		document.getElementById('C').className = 'language_option';
		document.getElementById('id_sub_C_CGI').className = 'sublang_opt';
	},
	
	
	fnSetExtraInfo: function( )
	{
		var sType = document.getElementById('idType').value;
		var nSmall = document.getElementById('language_optional');
		
		// PHP_5 PHP_4 CS_ASHX CS_CGI C
		if ( sType == "PHP_5" )
		{
			nSmall.innerHTML = "This will download a PHP 5 compatible version of Conditional-CSS. Enter your CSS file names below and click 'Compile' to download, or leave the file names blank and edit the downloaded file later.";
		}
		else if ( sType == "PHP_4" )
		{
			nSmall.innerHTML = "This will create a PHP 4 compatible version of Conditional-CSS to run on your server. Enter your CSS file names below and click 'Compile' to download, or leave the file names blank and edit the downloaded file later.";
		}
		else if ( sType == "CS_ASHX" )
		{
			nSmall.innerHTML = "The ASHX handler is perfect for installing quickly and easily on IIS. Enter your CSS files names below and click 'Compile' to download, or leave the file names blank and edit the downloaded file later.";
		}
		else if ( sType == "CS_CGI" )
		{
			nSmall.innerHTML = 'The C# CGI version does not require and files to be included at this time (although you can add some if you wish!). Using this method, you will be able to use Conditional-CSS as an interpreter. For more information please refer to the <a href="usage">usage page</a>.';
		}
		else if ( sType == "C" )
		{
			nSmall.innerHTML = 'You need not include any files when using the C version (note the compiler will create a binary for x86 computers). In this manner you will be able to use Conditional-CSS as an interpreter. For more information please refer to the <a href="usage">usage page</a>.';
		}
		else
		{
			nSmall.innerHTML = 'XXX';
		}
	},
	
	
	fnAddCompileFile: function( )
	{
		for ( var i=0 ; i<this._aFiles.length ; i++ )
		{
			this._aFiles[i] = document.getElementById('File'+i).value;
		}
		
		this._aFiles[this._aFiles.length++] = '';
		this.fnRedraw();
	},
	
	
	fnRemoveCompileFile: function( e )
	{
		var nTarget = jsCore.getElementFromEvent( e );
		var sTargetId = nTarget.getAttribute( 'id' );
		var aTmpFiles = new Array();
		
		for ( var i=0 ; i<this._aFiles.length ; i++ )
		{
			if ( 'Minus'+i != sTargetId )
			{
				aTmpFiles[aTmpFiles.length++] = document.getElementById('File'+i).value;
			}
		}
		
		this._aFiles.splice( 0, this._aFiles.length );
		this._aFiles = aTmpFiles.slice();
		
		if ( this._aFiles.length == 0 )
		{
			this._aFiles[this._aFiles.length++] = '';
		}
		
		this.fnRedraw();
	},
	
	
	fnRedraw: function( )
	{
		var nDiv;
		var nLabel;
		var nInput;
		var nPlus;
		var nMinus;
		
		/* Nuke all the old inputs */
		var nFiles = document.getElementById('compiler_files');
		nFiles.innerHTML = "";
		
		// Remove old listeners if there are any
		jsCore.removeListener( { 'sLabel': 'Compiler' } );
		
		for ( var i=0 ; i<this._aFiles.length-1 ; i++ )
		{
			nDiv = document.createElement( 'div' );
			nLabel = document.createElement( 'label' );
			nInput = document.createElement( 'input' );
			nMinus = document.createElement( 'img' );
			
			nLabel.appendChild( document.createTextNode( 'File '+(i+1)+'.' ) );
			nInput.className = 'text';
			nInput.name = 'sFile'+i;
			nInput.type = 'text';
			nInput.id = 'File'+i;
			nInput.value = this._aFiles[i];
			nMinus.src = 'media/images/minus.jpg';
			nMinus.setAttribute( 'id', 'Minus'+i );
			
			nDiv.appendChild( nLabel );
			nDiv.appendChild( nInput );
			nDiv.appendChild( nMinus );
			jsCore.addListener( {
				'mElement': nMinus,
				'sType': 'click',
				'fnCallback': this.fnRemoveCompileFile,
				'oObj': this,
				'mScope': true,
				'sLabel': 'Compiler' } );
			
				nFiles.appendChild( nDiv );
		}
		
		
		/* The last one is 'special' */
		nDiv = document.createElement( 'div' );
		nLabel = document.createElement( 'label' );
		nInput = document.createElement( 'input' );
		nPlus = document.createElement( 'img' );
		
		nDiv.className = 'no_bottom';
		nLabel.appendChild( document.createTextNode( 'File '+(i+1)+'.' ) );
		nInput.className = 'text';
		nInput.name = 'sFile'+i;
		nInput.type = 'text';
		nInput.id = 'File'+i;
		nInput.value = this._aFiles[i];
		nPlus.src = 'media/images/plus.jpg';
		
		nDiv.appendChild( nLabel );
		nDiv.appendChild( nInput );
		
		if ( this._aFiles.length != 1 )
		{
			nMinus = document.createElement( 'img' );
			nMinus.src = 'media/images/minus.jpg';
			nMinus.setAttribute( 'id', 'Minus'+i );
			nDiv.appendChild( nMinus );
			jsCore.addListener( {
				'mElement': nMinus,
				'sType': 'click',
				'fnCallback': this.fnRemoveCompileFile,
				'oObj': this,
				'mScope': true,
				'sLabel': 'Compiler' } );
		}
		
		nDiv.appendChild( nPlus );
			
		jsCore.addListener( {
			'mElement': nPlus,
			'sType': 'click',
			'fnCallback': this.fnAddCompileFile,
			'oObj': this,
			'mScope': true,
			'sLabel': 'Compiler' } );
		
		nFiles.appendChild( nDiv );
		
		this.fnSetExtraInfo();
	}
}

