/**
* ACECQA - General site scripts.
*/

$(document).ready(function() 
{
	// UI niceness for input fields.
	$("input.text").attr('initval',  $(this).val())
	.focus(
		function()
		{
			$(this).attr('rel', $(this).val())
					.val('')
					.addClass('active');
		}
	)
	.blur(
		function()
		{
			if ( $(this).val() == '' ) {
				$(this).val($(this).attr('rel')).removeAttr('rel');
			}
			if ( $(this).val() == '' || $(this).val() == $(this).attr('initval') ) {
				$(this).removeClass('active');
			}
		}
	);
	// Replace fancy form fields with UI updates.
	// Radio buttons and checkboxes.
	$('input[type="radio"].fancy').each(
		function()
		{
			var type = $(this).attr('type');
			var parent = $(this).parent();
			var hiddenID = 'input-hidden-'+$(this).attr('name');
			var container = $('<span/>').addClass('fancy-replacement-container')
										.appendTo(parent);
			// Add the replacement element.
			var replacement = $('<a/>').addClass('fancy-replacement '+type)
									   .attr({'href':'#null', 'rel':$(this).attr('name')})
									   .html('<span>'+$(this).val()+'</span>')
									   .click(
											function()
											{
												$('a.fancy-replacement[rel='+$(this).attr('rel')+']').removeClass('checked');
												$(this).addClass('checked');
												$('input#input-hidden-'+$(this).attr('rel')).val($(this).find('span').text());
											}
										)
										.appendTo(container);
			// Add the hidden element with the correct value.
			if ($(this).attr('checked') == true) {
				var hidden = $('<input/>').attr({type:'hidden', id:hiddenID, name:$(this).attr('name')})
										  .val($(this).val())
										  .appendTo(container);
			}
			// Mark the link as checked if necessary.
			if ($(this).attr('checked') == true) {
				replacement.addClass('checked');
			}
			// Remove the original element.
			$(this).remove();
		}
	);
	// Select elements.
	$('select.fancy').each(
		function()
		{
			var parent = $(this).parent();
			var hiddenID = 'input-hidden-'+$(this).attr('name');
			var container = $('<span/>').addClass('fancy-replacement-container select')
										.appendTo(parent);
			// Add the hidden element with the correct value.
			var hidden = $('<input />').attr({type:'hidden', id:hiddenID, name:$(this).attr('name')})
			                          .val($(this).val())
			                          .appendTo(container);
			// Add the replacement drop down elements.
			var placeholder = $('<a/>').attr({'href':'#null'})
			                           .addClass('placeholder')
			                           .html('<span></span>')
			                           .appendTo(container)
			                           .click(function()
                            				{
                            					var list = $(this).parent().find('ul');
                            					var toggleStyle = (list.css('display') == 'block')?'none':'block';
                            					list.css({display:toggleStyle});
                            				}
                            			);
            // Create the unordered list to place the options in.
			var list = $('<ul/>').css({'display':'none'}).appendTo(container).mouseleave(
				function(event)
				{
					if (outsideBounds($(this), event)) {
						$(this).hide();
					}
				}
			);
			// Build the list of options.
			$(this).find('option').each(
				function(i)
				{
					var li = $('<li/>').appendTo(list);
					var a = $('<a/>').attr({'href':'#null', 'rel':$(this).attr('value')}).text($(this).text()).appendTo(li);
					if ($(this).attr('selected') == true) {
						li.addClass('selected');
						placeholder.find('span').text($(this).text());
						$('#'+hiddenID).val($(this).attr('value'));
					}
					a.click(
						function()
						{
							var container = $(this).parent().parent().parent();
							var placeholder = container.find('a.placeholder');
							placeholder.find('span').text($(this).text());
							container.find('input').val($(this).attr('rel'));
							container.find('ul li').removeClass('selected');
							$(this).parent().addClass('selected');
							container.find('ul').hide();
						}
					);
				}
			);
			list.find('li:first').addClass('first');
			list.find('li:last').addClass('last');
			// Remove the original element.
			$(this).remove();
		}
	);
	// Replace the footer links with a more cleanly formatted option.
	var footerLinks = $('<ul/>').addClass('mini-links');
	var numLinks = $('#footer-links a').length;
	$('#footer-links a').each(
		function(i)
		{
			var li = $('<li/>');
			var a = $(this).clone().appendTo(li);
			a.attr('title', a.text());
			li.appendTo(footerLinks);
		}
	);
	footerLinks.find('li:first').addClass('first');
	footerLinks.find('li:last').addClass('last');
	$('#footer-links').html('').append(footerLinks);
});

/**
* Detects if the mouse is outside of the element's bounds.
*/
function outsideBounds( e, event )
{
	var offset = e.offset();
	var box = { top:offset.top, left:offset.left, right:offset.left+e.width(), bottom:offset.top+e.height() };
	if ( event.pageX <= box.left || event.pageX >= box.right || event.pageY <= box.top || event.pageY >= box.bottom ) {
		return true;
	}
	return false;
}
