/*
	Copyright (c) 2007-2008 JB Interactive Pty. Ltd.
	All Rights Reserved
	http://www.jbinteractive.com.au/
*/

(function($) {
	
var AdoptionslistForm = {
	id: '#adoptionsListForm',
	textNormal: 'black',
	textGrey: '#aaaaaa',
	
	newTextbook: function() {
		
		var textbooks      = $('#textbooks');
		var textbooksCount = Number(textbooks.val())+1;
		var bookfield = $('#add_textbook').parent().find('.bookfield:last');
		//var row = $(this).parents('.textbook');

		textbooks.val(textbooksCount);

		var newBookfield = bookfield.clone()
						.find('.errorContainer')
						.remove()
						.end()
						.find('div')
						.each(function () {
							$(this).css({display:'none'});
						})
						.end()
						.find('select,input')
						.each(function() {
							if ($(this).attr('type') == 'radio'){
								var name = $(this).attr('name').replace(
											'_'+(textbooksCount-1),
										 	'_'+(Number(textbooksCount))
										 );
								$(this)
									.attr('name', name);
								if ($(this).val() == 1) {
									$(this).attr('checked', false);
								} else {
									$(this).attr('checked', true);
								}
								$(this).parent().parent().children('label').attr('for', name);
							} else {
								var id = $(this).attr('id').replace(
											'_'+(textbooksCount-1),
										 	'_'+(Number(textbooksCount))
										 );
								$(this)
									.attr('id', id)
									.attr('name', id)
									.val('');
								$(this).parent().parent().children('label').attr('for', id);
							}
							
						})
						.end();

		$('#add_textbook').parent().find('.bookfield:last').append('<hr />').after(newBookfield);
		$('.bookfield:last').find('.textbooksErrors').remove();

		newBookfield.find('div').each(function() {
			if (!$(this).hasClass('field_period')){
				$(this).show();//slideDown('slow');
			}
		});	
		
		$('.used_again').bind('change', AdoptionslistForm.toggleShowPeriod);
	},
	
	newReader: function() {
		
		var readers     = $('#readers');
		var readersCount = Number(readers.val())+1;
		var readerfield = $('#add_reader').parent().find('.readerfield:last');

		readers.val(readersCount);

		var newReaderfield = readerfield.clone()
						.find('div')
						.each(function () {
							$(this).css({display:'none'});
						})
						.end()
						.find('select,input')
						.each(function() {
							if ($(this).attr('type') == 'radio'){
								var name = $(this).attr('name').replace(
											'_'+(readersCount-1),
										 	'_'+(Number(readersCount))
										 );
								$(this)
									.attr('name', name);
								if ($(this).val() == 1) {
									$(this).attr('checked', false);
								} else {
									$(this).attr('checked', true);
								}
								$(this).parent().parent().children('label').attr('for', name);
							} else {
								var id = $(this).attr('id').replace(
											'_'+(readersCount-1),
										 	'_'+(Number(readersCount))
										 );
								$(this)
									.attr('id', id)
									.attr('name', id)
									.val('');
								$(this).parent().parent().children('label').attr('for', id);
							}
						})
						.end();

		$('#add_reader').parent().find('.readerfield:last').append('<hr />').after(newReaderfield);
		$('.readerfield:last').find('.readersErrors').remove();
		newReaderfield.find('div').each(function() {
			$(this).show();//slideDown('slow');
		});		
	},
	
	toggleTextbooks: function() {
		
		if ($('#no_textbooks:checked').val() != null){
			textbookStore = $('#textbooks').val();
			$('#textbooks').val(0);
			$('.bookfield').hide();
		} else {
			$('#textbooks').val(textbookStore);
			$('.bookfield').show();		
		}
		setTimeout(function(){
			$.fn.errors.updatePositions();
		}, 50);
	},
	
	toggleReaders: function() {
		
		if ($('#no_readers:checked').val() != null){
			readerStore = $('#readers').val();
			$('#readers').val(0);
			$('.readerfield').hide();
		} else {
			$('#readers').val(readerStore);
			$('.readerfield').show();
		}
		setTimeout(function(){
			$.fn.errors.updatePositions();
		}, 50);
	},
	
	schoolHook: function(){
		if ($('#UnitSchool').val() == '0') {
			if (!$('#unit_school_other').is('*')) {
				AdoptionslistForm.addSpecifyField();
			}
			
			AdoptionslistForm.showSpecifyField();
		} else if ($('#unit_school_other').is('*')) {
			AdoptionslistForm.hideSpecifyField();
		}
	},
	
	styleErrors: function() {
		$('.errors')
			.parents('.field').addClass('input_error').end()
			.parents('.input').addClass('input_error').end();
		
		if ($('.inputTextbooks').children('.errors').is('*')) {
			$('.inputTextbooks').addClass('textbooksRequired');
		}		
	},
	
	addSpecifyField: function() {
		var specify = $('<input type="text" />')
			.attr('size', '34')
			.attr('id', 'unit_school_other')
			.attr('name', 'unit_school_other')
			.attr('value', Specify.msg)
			.css('display', 'none')
			.css('color', AdoptionslistForm.textGrey)
			.bind('focus', function(){
				if ($(this).val() == Specify.msg) {
					$(this)
						.attr('value', '')
						.css('color', AdoptionslistForm.textNormal);
				}
			})
			.bind('blur', function(){
				if ($(this).val() == '') {
					$(this)
						.attr('value', Specify.msg)
						.css('color', AdoptionslistForm.textGrey);
				}
			});
		
		$('#UnitSchool').after(specify);
	},
	
	showSpecifyField: function () {
		$('#unit_school_other').animate({
			marginTop: 10
		}, { duration: 'fast', queue: false }).show('fast');
	},
	
	hideSpecifyField: function () {
		$('#unit_school_other').animate({
			marginTop: 0
		}, { duration: 'fast', queue: false }).hide('fast');
	},
	
	styleBoxes: function() {
		$('.fs-narrow .fs-fields').each(function() {
			var box = $(this);
			box.find('.control:last').each(function() {
				var bottom = $(this).position().top+$(this).height();
				var boxEnd = box.position().top+box.height();
				if (bottom > boxEnd) {
					var newHeight = bottom-boxEnd+box.height()+5;
					$('.fs-narrow .fs-fields').each(function(){
						$(this).height(newHeight);
					});
				}
			});
		});		
	},
	
	hookSubmit: function() {
		// if the previous button is clicked
		$('#adoptionslist_submit button.previous').click(function() {
			$('#previous').val(1);
		});
		$('#adoptionslist_submit button').click(function() {
			$('#loading').show();
			$(this)
				.attr('disabled','true')
				.css('background','#dde29b');
			
			$(AdoptionslistForm.id).submit();
			return false;
		});
	},
	
	toggleShowPeriod: function() {
		if ($(this).val() == 1) {
			$(this).parent().parent().parent().children('.field_period').show();
		} else {
			$(this).parent().parent().parent().children('.field_period').hide();
		}
	}
};

var Specify = {
	msg: 'Please specify here'
};
	
$(document).ready(function(){

	var school = $('#UnitSchool').val();
	var school_other = null;
	var textbookStore;
	var readerStore;

	$('#UnitSchool').bind('change', AdoptionslistForm.schoolHook);
			
	if (school != '' && school == 0) {
		if ($('#unit_school_other').is('*')) {
			school_other = $('#unit_school_other').val();
			$('#unit_school_other').remove();
		}
		
		AdoptionslistForm.addSpecifyField();
		AdoptionslistForm.showSpecifyField();
		
		if (school_other != null)
			$('#unit_school_other').val(school_other);
	}
	
	if($('#no_readers').is(':checked'))
		AdoptionslistForm.toggleReaders();
		
	if($('#no_textbooks').is(':checked'))
		AdoptionslistForm.toggleTextbooks();
	
	// bind the add textbook and reader buttons
	$('#add_textbook').bind('click', AdoptionslistForm.newTextbook);
	$('#add_reader').bind('click', AdoptionslistForm.newReader);
	
	// bind the no textbooks and readers checkboxes
	$('#no_textbooks').bind('click', AdoptionslistForm.toggleTextbooks);
	$('#no_readers').bind('click', AdoptionslistForm.toggleReaders);
	
	//  bind the "use again" radio buttons to hide the additional fields
	$('.used_again').bind('change', AdoptionslistForm.toggleShowPeriod)
					.bind('change', $.fn.errors.updatePositions);
	
	if ($('.errors').is('*')) 
		AdoptionslistForm.styleErrors();
		
	AdoptionslistForm.styleBoxes();	
	
	AdoptionslistForm.hookSubmit();
	
	$('#enrolment_commencing').datepicker({
		dateFormat:'dd/mm/yy',
		mandatory: true,
		currentText: 'Close',
		onSelect: function () {
			$('#enrolment_commencing')
				.css('color', AdoptionslistForm.textNormal);
				
			// Get error associated with date field and remove on select date event
			var dateError = $.fn.errors.getErrorByKey('enrolment_commencing');
			$.fn.errors.removeError(dateError);
		}
	});
});

})(jQuery);
