// 2009, neueDaten* Bochum. neueDaten.de

window.onload=function(){
	//Load Comments:
	loadComments();
	
	/* Comment Form Validation: */
	var commentFormValidation = new Validation('sendCommentForm', {useTitles:true,onFormValidate:commentFormValid});
	function commentFormValid(status) {
		if (status == true){
			sendCommentForm();
		}
	}

	/* Tell A Friend Form Validation: */
	var sendAFriendFormValidation = new Validation('tellAFriendForm', {useTitles:true, onFormValidate:tellAFriendFormValid});
	function tellAFriendFormValid(status) {
		if (status == true){
			sendTellAFriendForm();
		}
	}	

	/* E-Mail: */
	$('sendEmailBox').hide();
	$('sendEmailBox').setOpacity(0);
	$('showSendEmailBoxLink').observe('click', function() {
		$('sendEmailBox').show();
		new Effect.Opacity("sendEmailBox", {duration:1.0, from:0.0, to:1.0});
		$('comments').hide();
		$('showCommentsLinkArea').show();
	});

	/* Send Tell A Friend: */
	function sendTellAFriendForm(){
		new Ajax.Updater('sendEmailBox','/videos/tellAFriend', {
			asynchronous:true, evalScripts:true, parameters:Form.serialize('tellAFriendForm'), onSuccess: sendTellAFriendSuccessed
		});
	}
	function sendTellAFriendSuccessed(){
	}


	/* Comments: */
	$('comments').hide();
	$('comments').setOpacity(0);
	$('writeCommentBox').hide();
	$('writeCommentBox').setOpacity(0);
	// Show Write Comments Link:
	$('showWriteCommentsBoxLink').observe('click', function() {
			if ($('writeCommentBox').visible()){
				$('writeCommentBox').hide();
		    }else {
		    	$('writeCommentBox').show();
		    	new Effect.Opacity("writeCommentBox", {duration:1.0, from:0.0, to:1.0});
		    }
	});
	
	// Show Comments:
	$('showCommentsLink').observe('click', function() {
		$('comments').show();
		new Effect.Opacity("comments", {duration:1.0, from:0.0, to:1.0});
		$('showCommentsLinkArea').hide();
		$('sendEmailBox').hide();
	});
	
	/* Send Comments: */
	function sendCommentForm(){
		new Ajax.Updater('writeCommentBox','/comments/add', {
			asynchronous:true, evalScripts:true, parameters:Form.serialize('sendCommentForm'), onSuccess: saveCommentSuccessed
		});
	}
	function saveCommentSuccessed(transport) {
		loadComments();
	}
	
	/* Load Comments: */
	function loadComments(){
		//alert('loadComments');
		new Ajax.Updater('commentBox', '/comments/view/'+Math.random()*9999, {
			parameters:  { video_id: $('video_id').value },
 			method: 'post'
		});
	}
}