// JavaScript Document
if ( ! window.Prototype )
	throw new Exception('Can\'t work without prototype');

if ( ! window.BrowserDetect )
	throw new Exception('Can\'t work without BrowserDetect');
	
//if ( ! window.Scriptaculous )
//	throw new Exception('Can\'t work without Scriptaculous');	

if ( ! window.hensam )
	var hensam = { Projects : { mehrdafon : {} } };
else if ( ! (hensam.Projects ) )
	hensam.Projects = { mehrdafon : {} } ;
else if ( ! hensam.Projects.mehrdafon  )
	hensam.Projects.mehrdafon = {};
else if ( ! hensam.Projects.mehrdafon.AjaxWrapper )
	throw new Exception('dependency on AjaxWrapper');
	
hensam.Projects.mehrdafon.Commenting = Class.create({
	
	postCommentLink : 'postComment',
	ajaxWrapper : null,
	commentStars : 'radio',
	commentValue : null,
	star : null,
	starGrey : '/images/misc/stern_grau.gif',
	
	markStars : function(event) {
		event.stop();
		var element = event.element();
		var id = element.name.replace('star_', '');
		
		for ( var i = 0; i < 5; i++ )
		{
			$('star_'+i).src = (i <= id) ? this.star : this.starGrey;
		}
		
		$(this.commentValue).value = element.value;
	},
	
	replaceRadios : function(element, index) {
		var starElement = new Element('input', {
			'id' : 'star_'+index,
			'name' : 'star_'+index,			
			'src' : this.starGrey,
			'type' : 'image',
			'value' : element.value
		});		
		starElement.observe('click', this.markStars.bindAsEventListener(this));
		element.up().appendChild(starElement);
		element.remove();
	},
	
	formStars : function(event) {
		this.commentStars = this.ajaxWrapper.content.down('#radio');
		if ( !this.commentStars)
		{
			return;
		}
		var inputs =  this.commentStars.select('input[type=radio]');
		var inputsParent = inputs[0].up();
		
		inputs[0].previous('span').update('Bewertung: ');		// change first span
		inputs[0].next('span').remove();																										// remove last span
		
		inputs.each(this.replaceRadios.bind(this));
					
		this.commentValue = new Element('input', {
			'type' : 'hidden',
			'id' : 'commentValue',
			'name' : 'we_ui_commentForm[rating]',
			'value' : '0'
		});
		
		this.commentStars.appendChild(this.commentValue);
	},
	
	postCommentAjaxHandler : function(transport) {
		var data = transport.responseJSON;
		var content = '<span class="headline_box">' + data.text + '</span>';
		this.ajaxWrapper.content.update(content);
		if ( data.status == 'ok' )
		{
			window.setTimeout(function() { window.location.reload() }, 1000);
		}
		else
		{
			window.setTimeout(this.ajaxWrapper.hide.bind(this.ajaxWrapper), 1000);
		}
	},
	
	postNewComment : function(event) {
		event.stop();
		
		var location = window.location.href;
		var params = 'ajax=true&json=true';
		
		if ( -1 < location.indexOf('#') )
		{
			location = location.substr(0, location.indexOf('#'));
		}		
		if ( -1 >= location.indexOf('?') )
		{
			location += '?';
		}
		else
		{
			location += '&';
		}
		location += params;
		
		new Ajax.Request(location, {
			method : 'post',
			postBody : this.ajaxWrapper.content.down('form').serialize(),
			onSuccess : this.postCommentAjaxHandler.bindAsEventListener(this),
			onFailure : this.ajaxWrapper.hide.bindAsEventListener(this.ajaxWrapper)
		});
	},
	
	initializeCommentBox : function(event) {
		this.ajaxWrapper.show();
		var form = this.ajaxWrapper.content.down('form');
		if ( form )
		{
			form.observe('submit', this.postNewComment.bindAsEventListener(this));
		}
	},
	
	showCommentBox : function(event) {
		event.stop();
		var location = window.location.href;
		if ( -1 < location.indexOf('#') ) 
		{
			location = location.substr(0, location.indexOf('#'));
		}
		new Ajax.Updater(this.ajaxWrapper.content, location, {
			parameters : {
				'ajax' : 'true'
			},
			method : 'get',
			onComplete : this.initializeCommentBox.bindAsEventListener(this) 
		});
	},
	
	initializeObjects : function(event) {
		if ( BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7 )
		{
			return;
		}
		else if ( $('comment') ) 
		{ 
			$('comment').remove(); 
		}
		if ( $('feedback') )
		{
			$('feedback').remove();
		}
		this.ajaxWrapper = new hensam.Projects.mehrdafon.AjaxWrapper();
		this.ajaxWrapper.content.setStyle('padding: 0 7px');
		
		this.formStars(null);
		
		this.postCommentLink = $(this.postCommentLink);
		if ( this.postCommentLink )
		{
			this.postCommentLink.observe('click', this.showCommentBox.bindAsEventListener(this));
		}
	},
	
	initialize : function() {
		this.star = this.starGrey.replace('_grau', '');
		Event.observe(document, 'dom:loaded', this.initializeObjects.bindAsEventListener(this));
		Event.observe(document, 'ajaxwrapper:shown', this.formStars.bindAsEventListener(this));
	}
});

new hensam.Projects.mehrdafon.Commenting();

