$(document).ready(function()
{
	var img = $('.menu_item_selected').children('img').eq(0);
	if (img.length > 0) img.attr('src', img.attr('src').replace(/\./, "-active."));
	
	$('.menu_item').add('.menu_item_sub').hover(function(e)
	{
		var img = $(this).children('img').eq(0);
		if (img.length > 0) img.attr('src', img.attr('src').replace(/\./, "-active."));
	}, function(e)
	{
		var img = $(this).children('img').eq(0);
		if (img.length > 0) img.attr('src', img.attr('src').replace(/-active/, ""));
	});
	
	var changeSize = function(size, onComplete)
	{
		$.ajax({
			url: 'accessibility.php',
			type: 'POST',
			data: 'size=' + size,
			complete: function(xhr, status)
			{
				if (onComplete) onComplete.call();
			}
		});
	}
	
	var changeContrast = function(contrast, onComplete)
	{
		$.ajax({
			url: 'accessibility.php',
			type: 'POST',
			data: 'contrast=' + contrast,
			complete: function(xhr, status)
			{
				if (onComplete) onComplete.call();
			}
		});
	}
	
	$('#fontSize1').add($('#fontSize1More')).click(function(e) { changeSize('100', function(e) { $('#layout').css('font-size', '100%'); }); });
	$('#fontSize2').add($('#fontSize2More')).click(function(e) { changeSize('120', function(e) { $('#layout').css('font-size', '120%'); }); });
	$('#fontSize3').add($('#fontSize3More')).click(function(e) { changeSize('140', function(e) { $('#layout').css('font-size', '140%'); }); });
	
	$('#regularContrast').click(function(e) { changeContrast('eeeeee,333333', function(e) { $('#layout').css('background-color', '#eeeeee').css('color', '#333333'); }); });
	$('#highContrast').click(function(e) { changeContrast('000000,FFFF00', function(e) { $('#layout').css('background-color', '#000000').css('color', '#FFFF00'); }); });
	$('#darkBlueOnCreamContrast').click(function(e) { changeContrast('FFF9D2,010066', function(e) { $('#layout').css('background-color', '#FFF9D2').css('color', '#010066'); }); });
	$('#darkBlueOnLightBlueContrast').click(function(e) { changeContrast('EFF8FB,010066', function(e) { $('#layout').css('background-color', '#EFF8FB').css('color', '#010066'); }); });
	
	$('#referOurWebsite').click(function(e)
	{
		$('#referFormMessage').hide();
		$('#referFormText').show();
		return showDialog('referFormDialog', 'center', 'center', '350px', '150px', 0.5, 1000,
			function() { $('#portfolioFrameContent').add($('#homeFrameContent')).hide(); },
			function() { $('#portfolioFrameContent').add($('#homeFrameContent')).show(); });
	});
	
	$('#moreOptions').click(function(e)
	{
		var offset = $(this).offset();
		var x = offset.left + $(this).width() - 300 + 'px';
		var y = offset.top + $(this).height() + 5 + 'px';
		
		return showDialog('moreOptionsDialog', x, y, '300px', '140px', 0, 250,
			function() { $('#moreOptions').addClass('pressed'); },
			function() { $('#moreOptions').removeClass('pressed'); });
	});
	
	$('#caseStudies').onHoverMenu('caseStudiesMenu', 150);
	$('#services').onHoverMenu('servicesMenu', 150);
	
	$('.thumbnailPicture').hover(function(e)
	{
		var img = $(this).children('img').eq(0);
		if (img.length > 0) img.attr('src', img.attr('src').replace(/_off/, ""));
	}, function(e)
	{
		var img = $(this).children('img').eq(0);
		if (img.length > 0) img.attr('src', img.attr('src').replace(/\./, "_off."));
	});
	
	$('.thumbnailPicture').click(function(e)
	{
		var pic = $('#bigPicture').clone();
		var img = $(this).children('img').eq(0);
		$('#bigPicture').replaceWith(pic.attr('src', img.attr('src').replace(/thumb_/, "")).appendTo(''));
	});
	
	$('#referFormSubmit').click(function(e)
	{
		$.ajax({
			url: 'refer.php',
			type: 'POST',
			data: 'from=' + $('#referFormName').val() + '&email=' + $('#referFormEmail').val(),
			complete: function(xhr, status)
			{
				$('#referFormMessage').html(xhr.responseText).show();
				if (xhr.responseText.match("success")) $('#referFormText').hide();
			}
		});
		return false;
	});
	
	$('.contactFormSet').hide();
	$('#projectEnquirySet').show();
	
	$('#enquiryType').change(function(e)
	{
		$('.contactFormSet').hide();
		if ($('#enquiryType').val() == 'I would like to talk about a web project')
			$('#projectEnquirySet').show();
		else
			$('#otherSet').show();
	});
});

// A general purpose fuction to display dialogs
var showDialog = function(element, x, y, width, height, opacity, speed, onShow, onHide)
{
	var dialog = $('#' + element);
	
	if (opacity == null) opacity = 0;
	if (speed == null) speed = 500;
	
	if (dialog.is('.modalDialog'))
	{
		var hide = function(e)
		{
			$('#blackFade')
				.height($(document).height())
				.fadeTo(speed, 0, function(e) { $(this).css('display','none'); });
				
			dialog
				.fadeTo(speed, 0, function(e) { $(this).css('display','none'); });
			
			if (onHide) onHide.call();
			return false;
		};
		
		var show = function(e)
		{
			dialog
				.css('display', 'block')
				.css('width', width)
				.css('height', height);
				
			if (x == 'center')
				x = ($(window).width() - dialog.width()) / 2 + 'px';
				
			if (y == 'center')
				y = ($(window).height() - dialog.height()) / 2 + 'px';
				
			if (opacity > 0)
			{
				$('#blackFade')
					.height($(document).height())
					.css('display', 'block')
					.css('filter', 'alpha(opacity=0)')
					.css("-moz-opacity", "0")
					.css("-khtml-opacity", "0")
					.css("opacity", "0")
					.fadeTo(speed, opacity);
			}
			
			dialog
				.css('filter', 'alpha(opacity=0)')
				.css("-moz-opacity", "0")
				.css("-khtml-opacity", "0")
				.css("opacity", "0")
				.css("left", x)
				.css("top", y)
				.fadeTo(speed, 1);
				
			dialog.children('.close').click(hide);
			
			if (onShow) onShow.call();
			return false;
		}
		
		if (dialog.css('display') == 'block')
			hide();
		else
			show();
	}
	return false;
}

// A general purpose function to display popup menu
$.fn.onHoverMenu = function(element, speed)
{
	var offset = $(this).offset();
	var x = offset.left + 'px';
	var y = offset.top + $(this).height() + 'px';
	
	var menu = $('#' + element);
	
	if (menu.is('.popupMenu'))
	{
		menu.noHover = true;
		menu.slideUpTimeout = function(speed, timeout)
		{
			menu
				.animate({ opacity: 1.0 }, timeout)
				.queue(function(e) { if (menu.noHover) menu.slideUp(speed); $(this).dequeue(); });
		};
		
		menu.contents().find('a').each(function()
		{
			$(this).hover(function(e) { menu.noHover = false; }, function(e) {menu.noHover = true; menu.slideUpTimeout(speed, 100); });
		});
		
		$(this).hover(function(e)
		{
			menu.noHover = false;
			menu
				.css("left", x)
				.css("top", y)
				.slideDown(speed);
		}, function(e)
		{
			menu.noHover = true;
			menu.slideUpTimeout(speed, 100);
		});
	}
	return false;
}
