XmlForm.CopyDays = function(form) {
	this.init(form);
}

XmlForm.CopyDays.prototype = {
	_form: null,
	_totalDays: null,
	_assignments: null,
	_init: false,

	init: function(form) {
		this._form = form;
		form.addListener(this);
		var self = this;
		this._assignments = [];
		setTimeout(function(){self.notifyChange();}, 500);
		
		$('.questionForm').submit(function(event){
			$("select[@name = 'copyFrom[]']").each(function(currentInstance) {
				var value = $(this).val() - 1;
				if (value) {
					var copyInstance = value - 1;
					$('#group10_inst' + currentInstance + ' .groupBody').html($('#group10_inst' + currentInstance + ' .groupBody'));
				}
			});
		});
	},

	notifyChange: function() {
		var totalDays = null;
		if (this._form.getInstance('iDuration')){
			totalDays = this._form.getInstance('iDuration').getValue();
		}

		// check if days have changed
		if (this._totalDays != totalDays) {
			this._totalDays = totalDays;
			this.update();
		}
	},

	update: function() {
		var self = this;
		var days = [];

		$('.group').each(function(i){
			if (this.id.indexOf('group10_inst') === 0) {
				// get any exsiting value
				var currentVal = $('.copyFrom select', this).val();
				if ((!self._init || self._assignments.length <= days.length) && !currentVal && days.length) {
					currentVal = '1';
				}
				self._assignments[days.length] = currentVal;
				days.push(this);
			}					
		});

		this._init = true;

		$(days).each(function(i){
			var currentVal = self._assignments[i];

			// remove element if exists
			$('.copyFrom', this).remove();

			if (self._totalDays > 1) {

				// add element
				$('> legend', this).after('<span class="copyFrom"><label>Copy from <select name="copyFrom[]"></select></label></span>');
				
				// assign callback
				$('.copyFrom select', this).change(function(){
					var value = $(this).val();
					self._assignments[this.inst] = value;
					setTimeout(function(){self.update()}, 100);
					if (!value) {
						$('> div', this.parentNode.parentNode.parentNode).slideDown('slow');
						this.parentNode.parentNode.parentNode.instance._collapsed = false;
					}	
				})[0].inst = i;

				// add options
				var optionsHtml = '<option value="">None</option>';
				for (var j=0; j<self._totalDays; j++) {
					if (j != i && !self._assignments[j]) {
						optionsHtml += '<option value="' + (j+1) + '">Day ' + (j+1) + '</option>';
					}
				}
				var select = $('.copyFrom select', this);
				select.html(optionsHtml);

				// set initial value
				select.val(currentVal);
				var value = select.val();

				var collapseElement = $('> div', this);
				var link = $('> legend a', this);
				$('> legend span', this).remove();
				if (value) {
					// force always collapsed
					collapseElement.slideUp('slow');
					this.instance._collapsed = true; // slight hack - should be private
					link.hide();
					$('> legend', this).append('<span>' + link.html() + '</span>');
				} else {
					// make uncollapsable again
					link.show();
				}
			}
		});
	}
}

