var MAG = Object.extend({}, MAG || {});
MAG.News = {
	Init: function(options)
	{
		this.options = {};
		Object.extend(this.options, options || {});
		if ($('usernewspaginator')) {
			$$('div#usernewspaginator a').each(function(e,i) {
				e.observe('click', MAG.News.showMyNewsByPage.bind(MAG.News, e.id));
			});
		}

	},

	showMyNewsByPage: function(id) {
		page = (id.toString().indexOf('pnpage_') != -1) ? id.toString().replace(/pnpage_/, '') : id.toString().replace(/page_/, '');
		new Ajax.Updater('mynews_container', this.options.url, {
			method: 'get',
			onComplete: this.changedMyNews.bind(this),
			parameters: { method: 'ajax_usernews', page: page }
		});
	},

	changedMyNews: function() {
		if ($('usernewspaginator')) {
			$$('div#usernewspaginator a').each(function(e,i) {
				e.observe('click', MAG.News.showMyNewsByPage.bind(MAG.News, e.id));
			});
		}
	},

	/*initMostReadAndCommented: function() {
		if ($$('a.tabchangers')) {
			$$('a.tabchangers').each(function(e, i) {
				e.observe('click', MAG.News.changeTab.bind(MAG.News, e.id));
			});
		}
	},

	changeTab: function(id) {
		$$('a.tabchangers').each(function(e, i) {
			$(e.id + '_container').toggle();
		});
	},*/

	initMostReadAndCommented: function() {
			$('tab_most_read').observe('click',MAG.News.show_most_read_content);
			$('tab_most_commented').observe('click',MAG.News.show_most_commented_content);

	},

	show_most_read_content: function() {
		$("tab_most_commented_container").hide();
		$("tab_most_read_container").show();

	},

	show_most_commented_content: function() {
		$("tab_most_read_container").hide();
		$("tab_most_commented_container").show();

	},

	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.News.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.News.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.News.calculate();
}

function vote() {
	MAG.News.submit_vote();
}





