SosForm = {
	pastel_blue: '#e2ecf4',
	selected_positions: [],
	selected_players: [],

	Init: function(){
		this.form = $('#post-form');
		this.container = $('#sos', this.form);
		this.checkbox = $('#sos-question', this.container);
		this.close_button = $('.close-button', this.container);
		this.header_label = $(this.checkbox).siblings('label');
		this.global_options = $('#sos-options', this.container);
		this.position_select = $('#sos-positions-select', this.container);
		this.positions_list = $('#sos-positions-selected', this.container);
		this.players_input = $('#sos-players-input', this.container);
		this.players_list = $('#sos-players-selected', this.container);
		this.sosMessage = 'Add any additional notes about your sit-or-start question here.';
		this.defaultMessage = 'Ask a question, start a discussion, or microblog your thoughts here.';
		this.expandedOnce = false;
		this.expanded = false;

		// Listeners 
		$(this.checkbox).click(function(){SosForm.ExpandAnimation(this)});
		$(this.close_button).click(function(){SosForm.ContractAnimation(this); return false;});

		$('#sos-positions-add-button').click(function(){
			SosForm.AddPosition();
			return false;
		});

		this.form.find('textarea').autogrow();

		$('#sos-players-add-button').click(function(){
			SosForm.AddPlayer();
			return false;
		});

		$(this.positions_list).click(function(e){
			if ($(e.target).hasClass('remove-button')) {
				SosForm.RemoveItem(e.target, SosForm.selected_positions, SosForm.position_select);
			}

			return false;
		});

		$(this.players_list).click(function(e){
			if($(e.target).hasClass('remove-button')){
				SosForm.RemoveItem(e.target, SosForm.selected_players, SosForm.players_input);
			}
			return false;
		});

		if ($('#PostStream').val() == 'general') {
			$('#PostTagsGeneral').attr('checked', true).parent().addClass('checked');
		} else if ($('#PostStream').val() == 'sit-or-start') {
			$('#PostTagsSos').attr('checked', true).parent().addClass('checked');
			$(this.checkbox).attr('checked', true).parent().addClass('checked');
			SosForm.ExpandAnimation($(this.checkbox))
		} else if ($('#PostStream').val() == 'waiver-wire') {
			$('#PostTagsWaiver').attr('checked', true).parent().addClass('checked');
		} else if ($('#PostStream').val() == 'keeper-leagues') {
			$('#PostTagsKeeper').attr('checked', true).parent().addClass('checked');
		} else if ($('#PostStream').val() == 'draft-talk') {
			$('#PostTagsDraft').attr('checked', true).parent().addClass('checked');
		} else if ($('#PostStream').val() == 'trade-talk') {
			$('#PostTagsTrade').attr('checked', true).parent().addClass('checked');
		} else if ($('#PostStream').val() == 'injury-report') {
			$('#PostTagsInjury').attr('checked', true).parent().addClass('checked');
		} else if ($('#PostStream').val() == 'fsq-fp') {
			$('#PostTagsFsq').attr('checked', true).parent().addClass('checked');
		}
	},

	ExpandAnimation: function(checkbox){
		if($(checkbox).is(':checked')){
			$(this.header_label).animate({paddingLeft: 0}, 500).children('span').animate({left: -25}, 500);
			$(this.close_button).fadeIn();
			$(this.container).animate({borderBottomColor: '#ffffff', borderLeftColor: '#ffffff', borderRightColor: '#ffffff', borderTopColor: '#ffffff'});
			$(this.global_options).slideDown();
			$(this.container).addClass('expanded');

			// If this is the first time sit or start game has been expanded...
			if (!this.expandedOnce) {
				$('#sos-players-input').autocomplete().focus(function() {$(this).val('');});
				this.expandedOnce = true;
			}

			$('#sos-check label').html('Sit-or-Start Question');
			$('#PostTagsSos').attr('checked', true).parent().addClass('checked');
			$('#PostPostTypeId').val(4);
			this.expanded = true;

			if (document.getElementById('PostText').value == this.defaultMessage) {
				document.getElementById('PostText').defaultValue = this.sosMessage;
				document.getElementById('PostText').value = this.sosMessage;
			}
		} 
	},

	ContractAnimation: function(){
		$(this.checkbox).attr('checked', false).parent().removeClass('checked');
		$(this.header_label).animate({paddingLeft: 17}, 500).children('span').animate({left: 0}, 500);
		$(this.close_button).fadeOut();
		$(this.container).animate({borderBottomColor: this.pastel_blue, borderLeftColor: this.pastel_blue, borderRightColor: this.pastel_blue, borderTopColor: this.pastel_blue}).removeClass('expanded');
		$(this.global_options).slideUp();

		$('#sos-check label').html('<span></span>Use this checkbox if you are asking a Sit-or-Start Question.');
		$('#PostTagsSos').attr('checked', false).parent().removeClass('checked');
		$('#PostPostTypeId').val(1);
		this.expanded = false;

		if (document.getElementById('PostText').value == this.sosMessage) {
			document.getElementById('PostText').defaultValue = this.defaultMessage;
			document.getElementById('PostText').value = this.defaultMessage;
		}
	},

	AddPosition: function(){
		var selected = $('option:selected', this.position_select);
		var new_position = this.CreateItem('<item>' + selected.val() + '</item>', selected.text(), this.selected_positions, 'positions');

		if (new_position){
			$(new_position).appendTo(this.positions_list).fadeIn();
		}

		if (this.IsListFull(this.selected_positions, 'positions')) {
			 $(this.position_select).attr('disabled', 'disabled');
		}
	},

	AddPlayer: function(){
		if ($('#sos-players-input').next().val().length > 0) {
			var xml_data = unescape($('#sos-players-input').next().val());
			var player_data = parseXML(xml_data);
			var player_id = $(player_data).find('item id').text();
			var first_name = $(player_data).find('item first_name').text();
			var last_name = $(player_data).find('item last_name').text();
			var player_team = $(player_data).find('item team').text();
			var display_name = first_name.substring(0, 1) + '. ' + last_name;
			var new_player = this.CreateItem(xml_data, display_name + ' (' + player_team + ')', this.selected_players, 'players');

			if (new_player) {
				$(new_player).appendTo(this.players_list).fadeIn();
				this.players_input.next().next().hide();
				this.players_input.next().val('');
				this.players_input.val('');
			}

			if (this.IsListFull(this.selected_players, 'players')){
				 $(this.players_input).attr('disabled','disabled');
			}
		}
	},
	
	CreateItem: function(hash, name, list_array, list_name){
		if (this.IsListFull(list_array, list_name)) {
			return false;
		}

		var index = list_array.length;	

		if (name.length > 0) {
			list_array[index] = hash;
			return '<li id="position-' + index + '">' + name + '<a href="#" class="remove-button">remove</a></li>';
		}
	},

	RemoveItem: function(button, selected_array, input){
		var item = $(button).parent();
		var list = $(button).closest('ul');
		var index = $(item).attr('id').match(/[0-9]+$/);

		selected_array.splice(index, 1);

		item.fadeOut('fast', function(){
			$(this).remove();
			$('li', list).each(function(i){
				$(this).attr('id', 'item-' + i);
			});			
		});
		
		$(input).removeAttr('disabled');
	},

	IsListFull: function(list_array, list_name){
		if(list_name == 'positions'){
			return list_array.length >= 5;
		}

		if(list_name == 'players'){
			return list_array.length >= 10;
		}

		return false;
	}
};

StreakForm = {
	pastel_blue: '#e2ecf4',

	Init: function(){
		this.form = $('#post-form');
		this.container = $('#streak', this.form);
		this.checkbox = $('#streak-question', this.container);
		this.close_button = $('.close-button', this.container);
		this.header_label = $(this.checkbox).siblings('label');
		this.global_options = $('#streak-options', this.container);
		this.streakMessage = 'Add any additional notes about your fantasy streaker question here.';
		this.defaultMessage = 'Ask a question, start a discussion, or microblog your thoughts here.';
		this.expanded = false;

		// Listeners 
		$(this.checkbox).click(function(){StreakForm.ExpandAnimation(this)});
		$(this.close_button).click(function(){StreakForm.ContractAnimation(this); return false;});
		$('#streak-date-input').datepicker();

		$('#streak-add-option').click(function() {
			var option_count = $('#streak-option-list li').length + 1;
			var option_item = '<li id="streak-option-' + option_count + '-item">' + "\n";

			option_item += "\t" + '<label for="streak-option-' + option_count + '-input">Option ' + option_count + '</label>' + "\n";
			option_item += "\t" + '<input id="streak-option-' + option_count + '-input" type="text" />' + "\n";
			option_item += "\t" + '<a class="close" href="#remove-option">x</a>' + "\n";
			option_item += "</li>\n";

			$('#streak-option-list').append(option_item);
			$('#streak-option-' + option_count + '-item a.close').click(function() {
				$('#streak-option-' + option_count + '-item').remove();
				return false;
			});

			return false;
		});

		this.form.find('textarea').autogrow();
	},

	ExpandAnimation: function(checkbox){
		if($(checkbox).is(':checked')){
			$(this.header_label).animate({paddingLeft: 0}, 500).children('span').animate({left: -25}, 500);
			$(this.close_button).fadeIn();
			$(this.container).animate({borderBottomColor: '#ffffff', borderLeftColor: '#ffffff', borderRightColor: '#ffffff', borderTopColor: '#ffffff'});
			$(this.global_options).slideDown();
			$(this.container).addClass('expanded');

			$('#streak-check label').html('Fantasy Streaker Question');
			$('#PostTagsGeneral').attr('checked', true).parent().addClass('checked');
			$('#PostPostTypeId').val(6);
			this.expanded = true;

			if (document.getElementById('PostText').value == this.defaultMessage) {
				document.getElementById('PostText').defaultValue = this.streakMessage;
				document.getElementById('PostText').value = this.streakMessage;
			}
		} 
	},

	ContractAnimation: function(){
		$(this.checkbox).attr('checked', false).parent().removeClass('checked');
		$(this.header_label).animate({paddingLeft: 17}, 500).children('span').animate({left: 0}, 500);
		$(this.close_button).fadeOut();
		$(this.container).animate({borderBottomColor: this.pastel_blue, borderLeftColor: this.pastel_blue, borderRightColor: this.pastel_blue, borderTopColor: this.pastel_blue}).removeClass('expanded');
		$(this.global_options).slideUp();

		$('#streak-check label').html('<span></span>Use this checkbox if you are asking a Fantasy Streaker Question.');
		$('#PostTagsGeneral').attr('checked', false).parent().removeClass('checked');
		$('#PostPostTypeId').val(1);
		this.expanded = false;

		if (document.getElementById('PostText').value == this.streakMessage) {
			document.getElementById('PostText').defaultValue = this.defaultMessage;
			document.getElementById('PostText').value = this.defaultMessage;
		}
	}
};

$().ready(function() {
	SosForm.Init();
	StreakForm.Init();

	$('#post-form .submit button').click(function() {
		if (SosForm.expanded) {
			if (SosForm.selected_positions.length < 1) {
				alert('Please select at least one position.');
				return false;
			} else if (SosForm.selected_players.length <  SosForm.selected_positions.length + 1) {
				alert('Please select at least one more player than eligible positions.');
				return false;
			} else {
				if (!$(this).hasClass('disabled')) {
					$(this).addClass('disabled');
				} else {
					return false;
				}

				$('#PostData').val(
					'<data>' +
						'<options>' +
							'<ppr>' + ($('#sos-league-options-ppr').parent().is('.checked') ? 'true' : 'false') + '</ppr>' + 
							'<return_yards>' + ($('#sos-league-options-return-yards').parent().is('.checked') ? 'true' : 'false') + '</return_yards>' +
							'<td_heavy>' + ($('#sos-league-options-td-heavy').parent().is('.checked') ? 'true' : 'false') + '</td_heavy>' + 
						'</options>' +

						'<positions>' + SosForm.selected_positions.join('') + '</positions>' + 
						'<players>' + SosForm.selected_players.join('') + '</players>' +
						'<text></text>' +
					'</data>'
				);

				var tag_list = new Array();
				var player_data = parseXML('<data>' + SosForm.selected_players.join('') + '</data>');

				$(player_data).find('item').each(function() {
					tag_list.push('#' +
						$(this).find('team').text().substring(0, 2).toLowerCase() +
						$(this).find('first_name').text().substring(0, 2).toLowerCase() +
						$(this).find('last_name').text().substring(0, 2).toLowerCase()
					);
				});

				tag_list.push('#sos');
				$('#PostTagList').val(tag_list.join(', '));

				if ($('#PostText').val() == document.getElementById('PostText').defaultValue) {
					$('#PostText').val('');
				}
			}
		} else if (StreakForm.expanded) {
			if (($('#streak-question-input').val() == '') || ($('#streak-option-1-input').val() == '') || ($('#streak-option-2-input').val() == '')) {
				alert('Please write a streaker question and include at least two options.');
				return false;
			} else if ($('#streak-date-input').val() == '') {
				alert('Please include an expiration date and time for this question.');
				return false;
			} else {
				if (!$(this).hasClass('disabled')) {
					$(this).addClass('disabled');
				} else {
					return false;
				}

				var options = '';

				$('#streak-option-list li').each(function() {
					options += '<option><![CDATA[' + $(this).find('input').val() + ']]></option>';
				});

				$('#PostData').val(
					'<data>' +
						'<question><![CDATA[' + $('#streak-question-input').val() + ']]></question>' +
						'<date>' + $('#streak-date-input').val() + ' ' + $('#streak-date-hour').val() + ':' + $('#streak-date-minute').val() + '</date>' +
						'<options>' + options + '</options>' +
						'<text></text>' +
					'</data>'
				);

				var tag_list = new Array();

				if ($('#PostText').val() == document.getElementById('PostText').defaultValue) {
					$('#PostText').val('');
				}
			}
		} else {
			if ($('#PostText').val() == document.getElementById('PostText').defaultValue) {
				alert('Please write some content before submitting your post.');
				return false;
			}
		}

		if ($('#post-form div.checkbox input:checked').length < 1) {
			alert('Please tag your post to at least one forum category.');
			return false;
		}
	});
});
