window.addEvent('domready', function() {
	var form = $('comment_form');
	var open = $('comment_open');
	var list = $('comment_wrapper');
	var commentForm = $('CommentForm');
	var formStatus = $('form_status');
	//form.set('styles', {display: 'none'});
	//form.set('opacity', 0);


	//var listSlide = new Fx.Slide(list);
	var myVerticalSlide = new Fx.Slide(form);
	
	myVerticalSlide.slideOut();
	
	
	var formStat="close";
	
	var close = function(e) {
		open.set('html', 'Add your comment');
		//form.set('styles',{display:'none'});	
	}
	var Position = function(elementid) {
       var iReturnValue = 0;
        while( elementid != $('content') )
        {
            iReturnValue += elementid.offsetTop;
            elementid = elementid.offsetParent;
        }
        return iReturnValue;
   };
	var toogleForm = function() {
				
		myVerticalSlide.toggle();
		
		if(formStat=='close') {
			formStat='open';
			open.set('html','Close form');
			}
		else {
			formStat='close';
			open.set('html', 'Add your comment');
			}
	}
	
	open.set('html', 'Add your comment');	
	open.addEvent('click', function(e) {
		e.stop();
		toogleForm();
	});
	
	$$('#comment_form input').addEvent('focus', function(){
		this.morph('.input_focus');
	});
	$$('#comment_form input').addEvent('blur', function(){
		this.morph('.input_blur');
	});
	
	$$('#comment_form textarea').addEvent('focus', function(){
		this.morph('.input_focus');
	});
	$$('#comment_form textarea').addEvent('blur', function(){
		this.morph('.input_blur');
	});
	
	//captcha
	$('reloadcaptcha').addEvent('click', function(event) {
		event.stop();
		reloadCaptcha();
	
	});
	var reloadCaptcha = function(e) {
		$('captcha').set('html', '<b>reloading ....</b>');
		$('captcha').load($('reloadcaptcha').get('href'));
	}
	//ajax send
	commentForm.set('send', {
		onSuccess: function(response){
			list.set('html',response);
			//list.tween('opacity',0,1);
			reloadCaptcha();
			
			//scroll to comment div
			var myScrollFx = new Fx.Scroll(document.body);
			myScrollFx.start(0,Position(commentForm) );	
			
			
			formStatus.set('opacity',0);
			formStatus.set('html','');
			formStatus.set('style', {display: 'none'});
			
			if(list.getElementById('commentFlag').get('html')=="ok") {
				$('CommentComment').set('value','');
				toogleForm();
			}
		
		}
	});
	commentForm.addEvent('submit', function(event){
		event.preventDefault();
		
		
		this.send(); 
		
		formStatus.tween('opacity',0,1);
		formStatus.set('html','<b>Sending......</b>');
		formStatus.set('style', {display: 'block'});
		
		
		//close.delay(500, form);
		$('CommentCapctha').set('value','');
		
				
			
	});
	
});
