
var firstLabel;
var firstFile;
var firstHidden;
var firstForm;

function initializeFiles()
{
	
	$('a#journeyPhotosFormShow').click(
		function()
		{
			$('div#journeyPhotosForm').fadeIn(500);
			return true;
		}
	);

	if(!$('form.jsFilesMulti').is('form'))
		return false;
		
	firstLabel = $('form.jsFilesMulti label');
	firstFile = $('form.jsFilesMulti input[@type=file]');
	firstHidden = $('form.jsFilesMulti input[@type=hidden]');
	firstForm = $('form.jsFilesMulti');
	
	firstFile.hide();
	firstLabel.hide();
	addNewForm(firstLabel, firstFile, firstForm);
	
	$('form.jsFilesMulti input#submitButton').before('<input class="button" type="button" id="moreButton" value="Więcej plików" />');
	
	$('form.jsFilesMulti input#moreButton').click(
		function ()
		{
			addNewForm(firstLabel, firstFile, firstForm);
			return false;
		}
	);
		
	$('input#submitButton').click(
		function ()
		{
			submitForms();
			return false;
		}
	);
		
}

var iframesCounter = 0;
var secondForm;
var secondLabel;
var secondHidden;
var secondFile;

var arrForms = [];
var countForms = 0;

function addNewForm(firstLabel, firstFile, firstForm)
{
	secondForm = firstForm.clone();
	secondLabel = firstLabel.clone();
	secondFile = firstFile.clone();
	secondHidden = firstHidden.clone();
	
	//secondForm.attr('action','index.php');
	
	iframesCounter++;
	
	firstForm.before('<iframe frameborder="0" width="300" height="40" src="index_input_file.php" name="jsIfForm'+iframesCounter+'" id="jsIfForm'+iframesCounter+'" class="jsPreForm"></iframe>');
	$('*', secondForm).remove();
	
	secondForm.append(secondLabel);
	secondForm.append(secondFile);
	secondForm.append(secondHidden);
	
	return true;
}

function getInnerForm(iframe)
{

	secondLabel.fadeIn(500);
	secondFile.fadeIn(500);
	//secondForm.attr('action', 'index.php?ajax=1');
	arrForms.push(secondForm);
	return secondForm;
}


function submitForms()
{
	countForms = arrForms.length;
	for(var i = 0; i< arrForms.length; i++)
	{
		arrForms[i].submit();
	}
	arrForms = [];
	return false;
}

// function formSubmited()
// {
// 	countForms--;
// 	if(countForms == 0)
// 	{
// 		document.refresh = 1;
// 	}
// }

$(document).ready(initializeFiles);

