/**
 * @author Edgeward
 */

$(document).ready(function(){
	/**** /project page, file upload animation ****/
	projectSubmit = $('#ft_project_submit');
	projectSubmit.removeAttr('disabled');
	fileNote = $('#filenote');
	fileNote.after('<img src=\"../img/layout/ft-file-loader.gif\" style=\"display:none\" id=\"fileloader\"/>');
	$('#FastrackSummaryForm').submit(function(){
		projectSubmit.attr('disabled','disabled');
		$('.attachment').hide();
		$('#fileloader').show();
		fileNote.text('Uploading files, please wait...');
		return true;
	});
	
	/**** Ajax add/dupe/remove component ****/
	
	compsForm = $('#FastrackComponentsForm');
	compsForm.wrap('<div id="compFormWrapper"></div>');
	
	$('#FastrackComponentsForm .mod_component').live("click", function(){
		obj = $(this);
		showLoader(obj);
		qString = '';
		btn = obj.attr('name') + '=' + obj.attr('value');
		$('#FastrackComponentsForm input, #FastrackComponentsForm select').each(function(){
			if(($(this).attr('value') && $(this).attr('name')) && $(this).attr('type') != 'submit') qString += $(this).attr('name') + '=' + $(this).attr('value') + '&';			
		});
		qString += btn;
		$.ajax({
			type: "POST",
			url: "components",
			data: qString,
			success: function(html) {
				$('#compFormWrapper').html(html);
				hideNotes();
				loadingAnims($('#FastrackComponentsForm'));
			}
		});
		return false;
	});

	
	/**** /components page hide notes ****/
	hideNotes(); //on document ready
	function hideNotes(){
		notesFields = $('.notesfield');
		notesFields.each(function(){
			$(this).data('hidden', true).hide().before('<a href=\"#\" class=\"show_notes\">Show Notes</a>');
		});
		$('a.show_notes').click(function(){
			noteSibbling = $(this).next('.notesfield');
			showButton = $(this);
			if (noteSibbling.data('hidden')) {
				noteSibbling.data('hidden', false).show();
				showButton.text('Hide Notes');
			}
			else {
				noteSibbling.data('hidden', true).hide();
				showButton.text('Show Notes');
			}
			return false;
		});
	}
	
	/**** Show/Hide loading gof ****/
	
	loadingAnims(compsForm); //on document ready
	function loadingAnims(compsForm){
		compsForm.children('fieldset').each(function(){
			$(this).append('<img class="loader" src="../img/ajax-loader-ft.gif" style="width:16px;height:16px;position:absolute;top:12px;left:200px;display:none"/>');
		});
	}
	
	function showLoader(obj){
		obj.parents('fieldset').children('img.loader').show();
	}
	
	function hideLoader(obj){
		obj.parents('fieldset').children('img.loader').hide();
	}
})