/*
***********************************************************

Name:			main.js
Title:			Main JS file

Author: 		Vahe Hovhannisyan (www.vaheweb.info)
Created: 		12.05.2007
Last modified: 	19.05.2007

***********************************************************
*/

/*
** Root Path
*/
var ROOT_PATH = 'http://'+location.host+'/guitaring/www2/';

/*
** Change page into more readable ones.
*/
function makeMoreReadable()
{
	
	/*
	** Remove the More Readable button
	*/
	$('makeMoreReadableButton').remove();
	
	/*
	** Change bgcolor to white, and text color to black.
	*/
	new Fx.Styles($('main'), {duration:500}).start({    
		'background-color': '#FFF',
		'color': '#000'
	});
	
	/*
	** Change comments style
	*/
	$$('#comments p').each(function(p)
	{
		p.setStyle('background','url('+ROOT_PATH+'images/left_bottom_white.gif) left bottom no-repeat');
	});
	
}

// This function is used in combination with a refresh button to get a new capthca image
// in case the user cannot read the current one. The page is not reloaded to accomplish this.

function getCaptcha()
{
	var d = new Date();
	$('captchaImage').setAttribute('src', ROOT_PATH+'captcha/captcha.php?r='+ d.getTime());
}

// Scroll back up to the top of the page
var scroll = new Fx.Scroll(window);

// Start
function start()
{
	
	/*****************************************************
	**	1. Toggle										**
	*****************************************************/
	if ($('comments_slide'))
	{
		var comments_slide = new Fx.Slide('comments');
		$('comments_slide').addEvent('click', function(){comments_slide.toggle()});
	}
	
	/*****************************************************
	**	2. Add comment									**
	*****************************************************/
	if ($('comments')) {
		
		// Hide new comment div
		if ($('newComment'))
		{
			var newComment = new Fx.Slide('newComment');
			newComment.hide();
		}
		
		$('add_comment').addEvent('submit', function(e) {
			
			new Event(e).stop();
		
			/* Add comment ***********************************************************
			**																		**
			**	1. Get & Filter $_POST values										**
			**	2. CAPTCHA															**
			**	3. Disable form elements											**
			**	4. Create new P element and show indicator							**
			**  5. send() to ajax.php												**
			**								 										**
			*************************************************************************/
			
			/*
		 	** 1. Get & Filter $_POST values
		 	*/
			if (($('add_comment').name.value) && ($('add_comment').comment.value))
			{
				
				/*
		 		** 2. CAPTCHA
		 		*/
				if (Cookie.get("captcha") == $('add_comment').captcha.value)
				{
					
					/*
				 	** 3. Disable submit element
				 	*/
					$('add_comment').submit.disabled = true;
					/*$('add_comment').captcha.disabled = true;
					$('add_comment').comment.disabled = true;
					$('add_comment').email.disabled = true;
					$('add_comment').name.disabled = true;*/
					$('add_comment').submit.value = 'Please wait...';
					//$('add_comment').submit.setStyle('padding', '50px');
					
					if (!$('comments_slide'))
					{
						
						// <h2></h2>
						var h2 = new Element('h2');
						
						// <h2>Comments (1)</h2>
						h2.appendText('Comments (1)');
						
						h2.injectBefore('comments');
					
					}
					
					/*
				 	** 4. Create new P element and show indicator
				 	*/
					
					$('newComment').setStyles({
						'border': '1px solid #3e3e3e',
						'padding': '5px'
					});
					
					/*
				 	** 5. send() to ajax.php
				 	*/
					$('add_comment').send({
						
						update: $('newComment'),
						onComplete: function()
						{
							
							/*
							** Remove the spinning indicator
							*/
							//$('newComment').removeClass('ajax-loading');
							
							/*
							** Empty comment's textarea & Captcha
							*/
							//$('add_comment').comment.value = '';
							//$('add_comment').captcha.value = '';
							
							/*
							** Enable submit element
							*/
							//$('add_comment').submit.disabled = false;
							
							/*
							** Change captcha image
							*/
							//getCaptcha();
							$('add_comment').remove();
							/*
							** Shows the new comment's div
							*/
							newComment.show();
							
							/*
							** Scroll to new comment
							*/
							scroll.toElement($('newComment'));
							
							/*
							** Effects for new Element
							*/
							
							/*
							** Remove add_comments
							*/
							//$('add_comment').remove();
							
						}
					
					});
					
					
				
				}
				else
				{
					/*
					** If captcha is not valid
					*/
					alert('Captcha code is not valid.');
				}
				
			}
			else
			{
				/*
				** If user not entered his name, or comment, alert this
				*/
				alert('Please enter your name and comment.');
			}
			
			/*
			** Prevent the submit event
			*/
			new Event(e).stop();
			
		})
		
	}
	
	/*****************************************************
	**	3. Lightbox										**
	*****************************************************/
	Lightbox.init({showControls: true});
}

window.addEvent('domready', start);