var MAG = Object.extend({}, MAG || {});
MAG.Movies = {
	Init: function(options)
	{
		if (typeof(this.options) == 'undefined') {
			this.options = {};
		}
		Object.extend(this.options, options || {});
	},

	setEpisodeLabel: function(label, options) {
		if (typeof(this.options) == 'undefined') {
			this.options = {};
			Object.extend(this.options, options || {});
		}
		this.options.episode_label = label;
	},

	initSeasonEpisodes: function() {
		if($('season_id') && $('episode_id')) {
			$('season_id').observe('change', MAG.Movies.changeSeason.bind(MAG.Movies));
			$('episode_id').observe('change', MAG.Movies.changeEpisode.bind(MAG.Movies));
		}
	},

	changeSeason: function() {
		new Ajax.Request(this.options.url, {
			method: 'get',
			onComplete: this.changedSeason.bind(this),
			parameters: { method: 'ajax_get_episodes', id: $('season_id').options[$('season_id').selectedIndex].value}
		});
	},

	changedSeason: function(transport) {
		var json = transport.responseText.evalJSON();
		var selectbox = $('episode_id');
		selectbox.length = 0;

		if (json.episodes.length == 0) {
			$('episode_id').innerHTML = '';
			$('episode_id').options.add (new Option ("------------------", "0"));
		} else {
			$('episode_id').innerHTML = '';

			json.episodes.each(function(e, i){e['number']
				selectbox[i] = new Option(e['number'], e['id']);

			});
		}
		$('episode_title').update(json.title);
		$('episode_description').update(json.description);
		if($('episodes_content') && $('episodes_track')) {
			var scrollbar = new Control.ScrollBar('episodes_content','episodes_track', {proportional: true});
		}
	},

	changeEpisode: function() {
		new Ajax.Updater('episode_container', this.options.url, {
			method: 'get',
			onComplete: this.changedEpisode.bind(this),
			parameters: { method: 'ajax_get_episode_by_id', id: $('episode_id').options[$('episode_id').selectedIndex].value}
		});
	},

	changedEpisode: function() {
		if($('episodes_content') && $('episodes_track')) {
			var scrollbar = new Control.ScrollBar('episodes_content','episodes_track', {proportional: true});
		}
	},

	submit_vote : function() {
		var poll_id = $F('poll_id');
		var section_id = $F('section_id');

		$$('.radio').each(function(e,i){
			if(e.checked ) {
				var answer = $F(e);
				new Ajax.Updater('results',  MAG.Movies.options.url, {
					method: 'get',
					parameters: {method: 'ajax_vote', poll_answer_id: answer, id:poll_id, section_id:section_id},
					insertion: Insertion.Bottom
				})
				$('poll_form').hide();
			}

		});


	},

	calculate: function()
	{
		var poll_id = $F('poll_id');
		var section_id = $F('section_id');
		new Ajax.Updater('results',  MAG.Movies.options.url, {
			method: 'get',
			parameters: {method: 'calculate_poll_results', id: poll_id, section_id: section_id},
			insertion: Insertion.Bottom
		})

		$('poll_form').hide();
	}

}

function results(){

	MAG.Movies.calculate();
}

function vote() {

	MAG.Movies.submit_vote();
}





