Event.observe(window, 'load', function()	{
	activate_rating_bar();
	// graph_rating_history();
}, false);

function activate_rating_bar()	{
	var rating_bar = $('rating_bar');
	
	if(!rating_bar)
		return 0;
	
	clean_text_nodes(rating_bar);
	rating_bar.slide_fx = new Fx.Styles(rating_bar.firstChild.firstChild, {duration: 2500, transition: Fx.Transitions.elasticOut});
	rating_bar.firstChild.firstChild.left = '0px';
	
	rating_bar.firstChild.firstChild.style.left = rating_bar.offsetWidth / 2 - rating_bar.firstChild.firstChild.offsetWidth / 2 + 'px';
	
	rating_bar.onclick = function(evt)	{
		var click_left_offset = parseInt(getMousePos(evt).split(',')[0] - this.offsetLeft);
		if(click_left_offset == getMousePos(evt).split(',')[0])
			click_left_offset = parseInt(getMousePos(evt).split(',')[0] - this.parentNode.offsetLeft - 60);
		var rating_bar_image = rating_bar.firstChild.firstChild;
		var rating = parseInt(click_left_offset / this.offsetWidth * 100) + 1;
		current_left_offset = parseInt(rating_bar_image.style.left.substring(0, rating_bar_image.style.left.length - 2));
		rating_bar.slide_fx.clearTimer();
		rating_bar.slide_fx.custom({
			'left' : [current_left_offset, click_left_offset - rating_bar_image.offsetWidth / 2]	
		})
		
		var myAjax = new Ajax.Request(
		'services/process_rating.php', 
		{
			method: 'post', 
			parameters: 'rating=' + rating + '&article_id=' + rating_bar.lastChild.innerHTML, 
			onComplete: function(ajax)	{
				$('rating_bar').lastChild.previousSibling.innerHTML = ajax.responseText;
				$('rating_bar').lastChild.previousSibling.style.visibility = 'visible';
				window.setTimeout(function()	{
					$('rating_bar').lastChild.previousSibling.innerHTML = 'Well?'
					$('rating_bar').lastChild.previousSibling.style.visibility = 'hidden';
				}, 3000);
			}
		});		
	}
	
	rating_bar.onmousemove = function(evt)	{
		var message_container = this.firstChild.nextSibling;	
		var click_left_offset = parseInt(getMousePos(evt).split(',')[0] - this.offsetLeft);
		if(click_left_offset == getMousePos(evt).split(',')[0])
			click_left_offset = parseInt(getMousePos(evt).split(',')[0] - this.parentNode.offsetLeft - 60);
		var rating = parseInt(click_left_offset / this.offsetWidth * 100) + 1;
		
		message_container.innerHTML = 'mirchi meter - ' + parseInt(rating / 10);
		message_container.style.fontVariant = 'small-caps';
		message_container.style.visibility = 'visible';
		message_container.style.textAlign = 'center';
	}
	
	rating_bar.onmouseout = function()	{
		this.firstChild.nextSibling.style.visibility = 'hidden';	
		this.firstChild.nextSibling.style.fontVariant = '';
		this.firstChild.nextSibling.style.textAlign = '';
	};
}

function graph_rating_history()	{
	var rating_history = $('rating_history');
	if(rating_history == null)
		return 0;
	
	// make sure there's rating data
	if($('rating_history_data').innerHTML == '')	{
		rating_history.parentNode.style.textAlign = 'left';
		rating_history.parentNode.innerHTML = '<div>Nobody has rated this article yet.</div><br/>';
		return 0;
	}
	else	{
		var rating_history_data = eval('(' + $('rating_history_data').innerHTML + ')');
		if(rating_history_data.length < 2)	{
			rating_history.parentNode.style.textAlign = 'left';
			rating_history.parentNode.innerHTML = '<div>Not enough people have rated this article for it to have a history.</div><br/>';
		}
		return 0;
	}
	
	var rating_history_data = eval('(' + $('rating_history_data').innerHTML + ')');

	var dataset = {
		'myFirstDataSet' : rating_history_data
	};
	
	var options = {
		padding: {left: 0, right: 0, top: 0, bottom: 20},
		yAxis: [0, 100],
		backgroundColor: '#dbdbdb',
		colorScheme: 'red',
		drawBackground: false,
		drawXAxis: false,
		drawYAxis: false
	};
	
	try	{
		var line = new Plotr.LineChart('rating_history',options);
		line.addDataset(dataset);
		line.render();
/*		
		var bar = new Plotr.BarChart('rating_history',options);
		bar.addDataset(dataset);
		bar.render();		
*/		
	} catch(e)	{
		rating_history.parentNode.innerHTML = 'Your browser doesn\'t support this funktastic feature.';
	}
	
}

function add_comment(article_id)	{

	comment_form = document.createElement('div');
	
	comment_form.innerHTML = '<h1>Say Something</h1>' +
							'<form enctype="multipart/form-data" method="post" action="services/process_comment.php">' +
							'<span>What do you think?</span>' +
							'<textarea name="comment" rows=8></textarea>' +
							'<input name="action" type="hidden" value="add_comment" style="display: none"/>' +
							'<input name="article_id" type="hidden" value="' + 
							article_id + 
							'" style="display: none"/>' +
							'<span>' +
							'<input type="button" value="comment">' +
							'<input type="button" value="cancel">' +
							'</span>' +
							'</form>';
							
	
	comment_form.lastChild.lastChild.firstChild.onclick = function()	{
		var comment = this.parentNode.parentNode.getElementsByTagName('textarea')[0];
		
		if(comment.value.strip() == '')	{
			comment.focus();
			alert('Please enter a comment.');
			return 0;
		}
		
		this.parentNode.parentNode.submit();
	};
	
	comment_form.lastChild.lastChild.lastChild.onclick = function()	{
		hide_popup(this.parentNode.parentNode.parentNode);
	};

	show_popup(comment_form);	
}

function send_article(article_id)	{
	var email_form = document.createElement('div');
	
	email_form.innerHTML = '<h1>Send This Article</h1>' +
							'<form enctype="multipart/form-data" method="post" action="">' +
							'<span>Your Name</span>' +
							'<input name="sender_name" type="text"/><br/>' +
							'<span>Recipients (separate multiple e-mails with a comma)</span>' +
							'<input name="target_email" type="text"/><br/>' +
							'<span>Include a Message</span>' +
							'<textarea name="sender_message" rows=5></textarea>' + 
							'<input name="action" type="hidden" value="email" style="display: none"/>' +
							'<input name="article_id" type="hidden" value="' +
							parseInt(article_id) + 
							'" style="display: none"/>' +
							'<br/>' +
							'<span>' +
							'<input type="button" value="e-mail">' +
							'<input type="button" value="cancel">' +
							'</span>' +
							'</form>';

	email_form.lastChild.lastChild.lastChild.onclick = function()	{
		hide_popup(this.parentNode.parentNode.parentNode);
	};
	email_form.lastChild.lastChild.firstChild.onclick = function()	{
		if(this.parentNode.parentNode.getElementsByTagName('input')[0].value.strip() == '' || this.parentNode.parentNode.getElementsByTagName('input')[1].value.strip() == '')	{
			alert('Please enter your name and the e-mail of the recipient.');
			this.parentNode.parentNode.getElementsByTagName('input')[0].focus();
			return false;
		}
		
		// all checked out - send ajax request
		var myAjax = new Ajax.Request(
		'services/json.process_email.php', 
		{
			method: 'post', 
			parameters: Form.serialize(this.parentNode.parentNode), 
			onComplete: function(ajax)	{
				$('email_container').innerHTML = ajax.responseText;
			}
		});	
		
		hide_popup(this.parentNode.parentNode.parentNode);
		
		$('email_container').innerHTML = '<img src="images/ajax-loader.gif"/>';		
	};	
	
	show_popup(email_form);
	
	// email_form.getElementsByTagName('input')[0].focus();	
}

function bump_article(article_id)	{
		var myAjax = new Ajax.Request(
		'services/process_rating.php', 
		{
			method: 'post', 
			parameters: 'rating=99&article_id=' + article_id, 
			onComplete: function(ajax)	{
				$('full_rating_info').innerHTML = ajax.responseText;
			}
		});		
	
}