// Assumptions:		Scriptaculous and Prototype libraries have been loaded


/************************************************************************************
*								PRE-LOAD CODE										*
************************************************************************************/

var isSiteLoaderShowing = true;
var isSiteLoaderPlaying = false;

var isSupperWrapperShowing = true;
var isSupperWrapperPlaying = false;

function toggleSiteLoader()
{
	
	if (isSiteLoaderShowing)
	{
		// Must close contact and then show About
		new Effect.Fade('SUPER_LOADER', { 		queue: 'front',
												duration: 0.3,
												beforeSetup:	function()
																{
																	site_loader(true);
																},
												afterUpdate: 	function()
																{
																	isSiteLoaderPlaying = true;
																},
												afterFinish: 	function ()
																{
																	isSiteLoaderPlaying = false;
																	isSiteLoaderShowing = false;
																}
		});
		new Effect.Appear('SUPER_WRAPPER', {	queue:		'end',
												duration:	0.3,
										 		afterUpdate: 	function()
																{
																	isSupperWrapperPlaying = true;
																},
												afterFinish: 	function()
																{
																	isSupperWrapperPlaying = false;
																	isSupperWrapperShowing = true;
																	site_loader(false);
																	// addLoadEvent(prepareInputsForHints);
																	try {
																		prepareInputsForHints();
																	} catch(e) {
																		// Probably not on main blog page
																	}
																}
		});
		
	}
	
	
}


function site_loader(isVisible)
{
	if (isVisible)
	{
		$('site_top_loader').style.display = 'block';
	} else {
		$('site_top_loader').style.display = 'none';
	}
}

function startIEpulsate()
{
	Effect.Pulsate('ie_warning', { pulses: 10, duration: 60, from: 0.5 } );
}

/************************************************************************************
*								MAIN MENU BAR										*
************************************************************************************/

// These booleans will keep track of which forms are open
var 	aboutIsShowing 		= false;
var		contactIsShowing	= false;

// These booleans will keep track of when animations are still playing
var		aboutIsPlaying		= false;
var		contactIsPlaying	= false;

function toggleAbout() {

	// If either animation is playing, ignore the request
	if (aboutIsPlaying || contactIsPlaying)
		return false;

	if (contactIsShowing)
	{
		// Must close contact and then show About
		new Effect.toggle('CONTACT', 'blind', { queue: 'front', 
												afterUpdate: 	function()
																{
																	contactIsPlaying = true;
																},
												afterFinish: 	function ()
																{
																	contactIsPlaying = false;
																	contactIsShowing = false;
																}
		});
		new Effect.toggle('ABOUT', 'blind', {queue: 'end', 		afterUpdate: 	function()
																				{
																					aboutIsPlaying = true;
																				},
																afterFinish: 	function()
																				{
																					aboutIsPlaying = false;
																					aboutIsShowing = true;
																					
																					// Reset the contact view
																					resetContactView();
																				}
		});
		
	} else {
		new Effect.toggle('ABOUT', 'blind', {	afterUpdate:	function ()
																{
																	aboutIsPlaying = true;
																},
												afterFinish: 	function ()
																{
																	aboutIsPlaying = false;
																	aboutIsShowing = !aboutIsShowing;
																}
		});
		
		
	}
}

function toggleContact() {

	// If either animation is playing, ignore the request
	if (aboutIsPlaying || contactIsPlaying)
		return false;

	if (aboutIsShowing)
	{
		// Must close contact and then show About
		new Effect.toggle('ABOUT', 'blind', { 	queue: 'front', 
												afterUpdate: function() 
															{
																aboutIsPlaying = true;
															},
												afterFinish: function()
															{
																aboutIsPlaying = false;
																aboutIsShowing = false;
															}
		});
		
		
		new Effect.toggle('CONTACT', 'blind', {queue: 'end',	afterUpdate:	function()
																				{
																					contactIsPlaying = true;
																				},
																afterFinish:	function()
																				{
																					contactIsPlaying = false;
																					contactIsShowing = true;
																				}
		});
		

	} else {
		new Effect.toggle('CONTACT', 'blind', {	afterUpdate:	function()
																{
																	contactIsPlaying = true;
																},
												afterFinish:	function()
																{
																	contactIsPlaying = false;
																	contactIsShowing = !contactIsShowing;
																	
																	// Reset the contact view
																	resetContactView();
																	
																}
		});
	}
}


/************************************************************************************
*								CONTACT FORM										*
************************************************************************************/
function goToEmail()
{
	new Effect.Fade('contact_options', {	queue: 'front',
											duration: 0.5 });
	new Effect.Appear('contact_email', { 	queue: 'end',
											duration: 0.5,
											afterFinish:	function()
														 	{
														 		$('contact_email').innerHTML = "<font size=\"4\"><center><BR><BR>My email address is:<BR><BR><a href=mailto:nick@nickology.com>nick@nickology.com</a><BR><BR>&laquo;  <a href=\"#\" onclick=\"goToContactFromEmail(); return false;\">Go back</a></center></font>";
														 	}
	});
}

function resetContactView()
{
	if (!contactIsShowing)
	{
		$('contact_form').style.display 	= 'none';
		$('contact_email').style.display 	= 'none';
		$('contact_options').style.display 	= 'block';
		$('contact_status_text').innerHTML = "There are two ways to contact me.";
		$('ajax_spinner_contact').style.display = 'none';
		
	}
}

function goToContactFromEmail()
{
	new Effect.Appear('contact_options', {	queue: 'end',
											duration: 0.5 });
	new Effect.Fade('contact_email', { 	queue: 'front',
											duration: 0.5 });

}

function goToContactForm()
{
	new Effect.Fade('contact_options', 	{ 	queue: 'front',
											duration: 0.5 });
	new Effect.Appear('contact_form',	{	queue: 'end',
											duration: 0.5,
											afterFinish: 	function()
															{
																$('email').focus();
															}
											 });

}

function goToContactFromForm(showThankyou)
{
	if (showThankyou)
	{
		$('ajax_spinner_contact').style.visibility = 'hidden';
		$('contact_status_text').innerHTML = "<b>Thank you. Your message has been sent.</b>";
	}
	
	new Effect.Fade('contact_form', { 		queue: 'front',
											duration: 0.5 });

	new Effect.Appear('contact_options', {	queue: 'end',
											duration: 0.5 });
											
	

}

function sendMessage()
{
	
	// check form quickly (only checking email... if text is blank... don't care for now.
	emailval 	= $('email').value;
	if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailval)) )
	{
		$('email').focus();
		return false;
	}
	
	$('submit_button').disabled = true;
	
	$('ajax_spinner_contact').style.visibility = 'visible';
	
	var message	= encodeURIComponent($('emailtext').value);
	var email 	= encodeURIComponent($('email').value);
	var url 	= 'scripts/site_contact.php?email=' + email + '&text=' + message;
	
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess:	function(transport) 
	  				{
	  					if (transport.responseText == "1")
					    {
					    	$('email').value = '';
					    	$('emailtext').value = '';
					    	$('submit_button').disabled = false;
					    	goToContactFromForm(true);
					    } else {
	      					alert('An error occurred while submitting your message. Please send me a direct email.');
	      				}
					}
	});
	
}

/************************************************************************************
*								BLOG CODE											*
************************************************************************************/
/*
Effect.BlindRight = function(element) {
  element = $(element);
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, 100, Object.extend({ 
    scaleContent: false, 
    scaleY: false,
    scaleFrom: 0,
    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: true,
    afterSetup: function(effect) {
      effect.element.makeClipping().setStyle({width: '0px'}).show(); 
    },  
    afterFinishInternal: function(effect) {
      effect.element.undoClipping();
    }
  }, arguments[1] || { }));
};

Effect.BlindLeft = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false, 
      scaleY: false, 
      restoreAfterFinish: true,
      afterFinishInternal: function(effect) {
        effect.element.hide().undoClipping();
      } 
    }, arguments[1] || { })
  );
};
*/

function toggleUserArrow(elementId)
{
	var elem 	= document.getElementById(elementId);
	var arrow 	= document.getElementById('arrow_'+elementId);
	var speed 	= 1;
	
	// $('test').morph('margin-left: 150px; width: 32px;');
	
	/* 
	
		new Effect.Morph( arrow, { queue: 'end',
			style: 'bluearrow_rotate',
			duration: 0.1 // Effects
		});	
		
	*/
	
	if (elem.style.width == '32px')
	{
		// Shrinking...
		new Effect.Morph( elem, { beforeStart:  function()
												{ 
													$(arrow).style.webkitTransform='rotate(0deg)';
													$(arrow).style.MozTransform='rotate(0deg)';
												}, // afterFinish
						queue: 'front',
						style: {
							width: '100px',
							marginLeft: '10px'
						}, // CSS
						duration: speed // Effects
		});
		
				
	} else {
	
		// Expanding...
		new Effect.Morph( elem, { beforeStart:  function()
												{ 
													$(arrow).style.webkitTransform='rotate(-180deg)';
													$(arrow).style.MozTransform='rotate(-180deg)';
												},
						queue: 'front',
						style: {
							width: '32px',
							marginLeft: '118px'
						}, // CSS
						duration: speed // Effects
		});
		


		/* 
		-webkit-transform: rotate(45deg);
		-moz-transform: rotate(45deg) scale(1.25, 0.5);
		*/
	}
	return false;
}

// Keep track of which comment parts are open to NOT scroll if not necessary
var commentArray = [];

function toggleComments(id)
{
	var comment_block	= document.getElementById('comments'+id);
	var blog_entry		= document.getElementById('blog_entry_'+id);
	
	/*
	if ( commentArray[id] || commentArray[id] == '1')
	{
		for (i = 1; i < 100; i++)
		{
			alert(i);
			try {
				var arrow = $('comment_'+i);
				new Effect.toggle(arrow,'appear', { queue: 'front', duration: 1.0 } );
			} catch (e) {
				break;
			}
		}
	}
	
	
	if ( !commentArray[id] || commentArray[id] == '0')
	{
		queue_direction 		= 
		queue_arrow_direction	=
	}
	*/
	site_loader(true);
	new Effect.toggle(comment_block,'blind', { 	queue: 'end', duration: 1.0,
												afterFinish: 	function()
																{ 
																	site_loader(false);
																	focusComment(id);
																}
											} );
	
	if ( !commentArray[id] || commentArray[id] == '0')
	{
		// Scroll To effect removed
		// new Effect.ScrollTo(blog_entry, { queue: 'end', offset: -10 } );
		commentArray[id] = '1';
	} else {
		commentArray[id] = '0';
	}
}

function focusComment(id)
{
	// try {
		$('comment_text_area_'+id).focus();
	// } catch(e) { 
	//	alert('Comment error: ' + e.number);
		// Ignore
	// }
}


/*

	THIS WORKS BUT AJAX-LOADED CONTENT COMES ON TOP OF DIVS, NOT IN THEM

function toggleComments(id)
{
	var comment_holder	= document.getElementById('comments_holder_'+id);
	var blog_entry		= document.getElementById('blog_entry_'+id);
	
	// show the spinner
	comment_holder.style.display = 'block';
	
	// blind spinner 
	// new Effect.toggle(comment_holder,'blind', { queue: 'front', duration: 0.4, 
	//											afterFinish: loadComments(id) } );
	
	loadComments(id);
	
}

function loadComments(id)
{
	url = "http://www.nickology.com/index.php/site/comments_by_ajax/" + id + "/";
	new Ajax.Request(url, {		method:'get',
								onSuccess:	function(transport)
											{
												// alert(id);
												var ajaxResult=transport.responseText;
												
												// $('comments_holder_'+id).innerHTML = ajaxResult;
												
												$('comments_holder_'+id).update(ajaxResult);
												
												 new Effect.SlideUp('main_div', {
													afterFinish: 	function () 
																	{
																		$('main_div').update(ajaxResult);
																		new Effect.SlideDown('main_div');
																	}
												});
												
											},
												onFailure: 	function()
															{
																//failure handling
																alert('ajax request failed');
															}
							}
						);
}
*/
