var gaFiles = new Array();


fnCompileFormInit = function ()
{
	var nForm = document.getElementById('CompileForm');
	var nSelect = nForm.getElementsByTagName('select')[0];
	
	jsCore.addListener( {
		'mElement': nSelect,
		'sType': 'change',
		'fnCallback': fnSetExtraInfo } );
	
	fnSetExtraInfo();
	fnAddCompileFile();
}


fnSetExtraInfo = function( )
{
	var nForm = document.getElementById('CompileForm');
	var nSelect = nForm.getElementsByTagName('select')[0];
	var nSmall = nForm.getElementsByTagName('small')[0];
	
	if ( nSelect.getElementsByTagName('option')[0].selected == true )
	{
		// PHP
		nSmall.innerHTML = "Enter your CSS file names below and click 'Compile' to download PHP Conditional-CSS, or leave the file names blank and edit the downloaded file later.";
	}
	else
	{
		// C
		nSmall.innerHTML = 'Leave the file name empty below to use Conditional-CSS as an interpreter, or enter file names to always include CSS from those files. <a href="/conditional-css/media/src/Conditional-CSS.zip">Download the source</a> to compile for platforms other than Linux x86.';
	}
}



fnAddCompileFile = function( )
{
	for ( var i=0 ; i<gaFiles.length ; i++ )
	{
		gaFiles[i] = document.getElementById('File'+i).value;
	}
	
	gaFiles[gaFiles.length++] = '';
	fnRedraw();
}



fnRemoveCompileFile = function( e )
{
	var nTarget = jsCore.getElementFromEvent( e );
	var sTargetId = nTarget.getAttribute( 'id' );
	var aTmpFiles = new Array();
	
	for ( var i=0 ; i<gaFiles.length ; i++ )
	{
		if ( 'Minus'+i != sTargetId )
		{
			aTmpFiles[aTmpFiles.length++] = document.getElementById('File'+i).value;
		}
	}
	
	gaFiles.splice( 0, gaFiles.length );
	gaFiles = aTmpFiles.slice();
	
	if ( gaFiles.length == 0 )
	{
		gaFiles[gaFiles.length++] = '';
	}
	
	fnRedraw();
}



fnRedraw = function( )
{
	var nDiv;
	var nLabel;
	var nInput;
	var nPlus;
	var nMinus;
	
	/* Delete old divs */
	var nForm = document.getElementById('CompileForm');
	var nDivs = nForm.getElementsByTagName('div');
	
	while( nDivs.length > 2 )
	{
		nDivs[1].parentNode.removeChild( nDivs[1] );
		nDivs = nForm.getElementsByTagName('div');
	}
	
	// Remove old listeners if there are any
	jsCore.removeListener( { 'sLabel': 'Compiler' } );
	
	
	var nSubmit = document.getElementById('CompileSubmit');
	
	for ( var i=0 ; i<gaFiles.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 = gaFiles[i];
		nMinus.src = '/conditional-css/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': fnRemoveCompileFile,
			'sLabel': 'Compiler' } );
		
			nSubmit.parentNode.insertBefore( nDiv, nSubmit );
	}
	
	
	/* 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 = gaFiles[i];
	nPlus.src = '/conditional-css/media/images/plus.jpg';
	
	nDiv.appendChild( nLabel );
	nDiv.appendChild( nInput );
	
	if ( gaFiles.length != 1 )
	{
		nMinus = document.createElement( 'img' );
		nMinus.src = '/conditional-css/media/images/minus.jpg';
		nMinus.setAttribute( 'id', 'Minus'+i );
		nDiv.appendChild( nMinus );
		jsCore.addListener( {
			'mElement': nMinus,
			'sType': 'click',
			'fnCallback': fnRemoveCompileFile,
			'sLabel': 'Compiler' } );
	}
	
	nDiv.appendChild( nPlus );
		
	jsCore.addListener( {
		'mElement': nPlus,
		'sType': 'click',
		'fnCallback': fnAddCompileFile,
		'sLabel': 'Compiler' } );
	
	nSubmit.parentNode.insertBefore( nDiv, nSubmit );
}


/* Init the form as soon as we can */
jsCore.fnOnAvailable( {
	'sId': 'CompileForm',
	'fnCallback': fnCompileFormInit } );

