(function()
{
	$('body').addClass('hasJS');
	
	/**
	 * Menus
	 */
	$('#menu li:not(.contact)').each(function()
	{
		var $a = $(this).find('a');
		
		$(this).prepend('<span></span>');
		$(this).find('span').css({
			'width': $a.outerWidth() + 'px',
			'height': $a.outerHeight() + 'px'
		});
		
		$a.mouseenter(function()
		{
			$(this).prev().css('background-image', $(this).css('background-image')).next().css('background', 'transparent');
			$(this).prev().fadeIn();
			$(this).unbind('mouseenter');
			
			$(this).mouseenter(function()
			{
				$(this).prev().fadeIn('fast');
			}).mouseleave(function()
			{
				$(this).prev().fadeOut('fast');
			});
		});
	});
	
	var $current,
	    timeout_id;
	
	$('#navbar a').hover(function()
	{
		clearTimeout(timeout_id);
		
		var $a = $(this).parent('li').siblings('li').find('a');
		
		$a.each(function()
		{
			if ($(this).hasClass('current'))
			{
				$current = $(this).removeClass('current');
			}
		});
	}, function()
	{
		timeout_id = setTimeout(function()
		{
			$current.addClass('current');
			
			clearTimeout(timeout_id);
		}, 500);
	});
	
	/**
	 * Coverflow
	 */
	var is_ready = false,
	    is_animated = false,
	    prev_item,
	    $references = $('#references'),
	    $items = $references.find('.item').hide();
	
	function updateReferences(id)
	{
		is_animated = true;
		
		$items.fadeOut('normal', function()
		{
			$references.addClass('loading');
			
			var interval_id = setInterval(function()
			{
				$references.removeClass('loading');
				$items.eq(id).fadeIn('normal', function()
				{
					is_animated = false;
				});
				
				clearInterval(interval_id);
			}, 100);
		});
	}
	
	var coverflow = new ContentFlow('coverflow', {
		startItem: 'center',
		reflectionHeight: 0,
		maxItemHeight: 130,
		relativeItemPosition: 'bottom center',
		flowDragFriction: 0,
		scrollWheelSpeed: 0,
		visibleItems: 7,
		calcSize: function(item)
		{
			var pos,
			    height,
			    width;
			
			pos = item.relativePosition;
			height = width = Math.max(1 / (Math.abs(pos) + 1), 0.65);
			
			return {
				height: height,
				width: width
			};
		},
		calcCoordinates: function(item)
		{
			var pos,
			    num,
			    x,
			    y;
			
			pos = item.relativePosition;
			num = this.conf.visibleItems;
			x =  pos / (num / 2);
			y = -1;
			
			return {
				x: x,
				y: y
			};
		},
		onclickActiveItem: function(item)
		{
			return false;
		},
		onMakeActive: function(item)
		{
			var current_src,
			    new_src;
			
			var imglcl = item.content.parentNode.getElementsByTagName("IMG")[0];
			imglcl.src=imglcl.src.replace('.png', '_current.png');
			/*
			current_src = item.content.src;
			new_src = current_src.replace('.png', '_current.png');
			item.content.src = new_src;
			*/
			
			if (!is_ready)
			{
				is_ready = true;
				
				updateReferences(item.index);
			}
		},
		onMakeInactive: function(item)
		{
			var current_src,
			    new_src;
			
			prev_item = coverflow.getActiveItem();
			
			var imglcl = item.content.parentNode.getElementsByTagName("IMG")[0];
			imglcl.src=imglcl.src.replace( '_current.png','.png');

			/*
			current_src = item.content.src;
			new_src = current_src.replace('_current.png', '.png');
			item.content.src = new_src;
			*/
		},
		onMoveTo: function(item)
		{
			if (!is_animated)
			{
				updateReferences(item.index);
			}
			else
			{
				coverflow.moveTo(prev_item.index);
			}
		}
	});
	
	/**
	 * Links
	 */
	var isBottom = false,
	    $scrollToLinks = $('#menu .contact a, #footer h3');
	
	$scrollToLinks.click(function(e)
	{
		e.preventDefault();
		
		if (!isBottom)
		{
			$.scrollTo('#footer', {
				duration: 200
			});
			
			isBottom = true;
		}
		else
		{
			$.scrollTo('#header', {
				duration: 200
			});
			
			isBottom = false;
		}
	});
	
	/**
	 * Form fields
	 */
	$('input:text').each(function()
	{
		var val = $(this).val();
		
		$(this).focus(function()
		{
			if ($(this).val() == val)
			{
				$(this).val('');
			}
		});
	});
	
	/**
	 * Lightboxes
	 */
	$('#references .left a, #references .right a, .ajax').fancybox({
		autoDimensions: false,
		height: 650,
		width: 800,
		padding: 0,
		showNavArrows: false,
		overlayColor: '#000',
		overlayOpacity: 0.7,
		type: 'ajax',
		ajax: {
			dataFilter: function(data)
			{
				return $(data).filter('#content')[0];
			}
		}
	});
	
	$('.inline').fancybox({
		padding: 0,
		showNavArrows: false,
		overlayColor: '#000',
		overlayOpacity: 0.7
	});
	
	
	
	

	$(document).ready(function(){
		$("#mailinglist_form").bind("submit", function() {
			$.ajax({
				type        : "POST",
				cache       : false,
				url         : "/index.php",
				data        : $(this).serializeArray(),
				success: function(data) {
					$.fancybox(data);
					document.getElementById("fancybox-content").style.border="0";
					document.getElementById("fancybox-content").innerHTML='<div><h2>Thank You!</h2><p>You have been sent a confirmation email.</p><p>Please click the link contained in the email to activate your mailing list account.</p><p>Netway</p></div>';
					setTimeout(function(){
						document.getElementById("fancybox-content").style.border="0";
						document.getElementById("fancybox-content").innerHTML='<div><h2>Thank You!</h2><p>You have been sent a confirmation email.</p><p>Please click the link contained in the email to activate your mailing list account.</p><p>Netway</p></div>';
					},20);
					setTimeout(function(){
						document.getElementById("fancybox-content").style.border="0";
						document.getElementById("fancybox-content").innerHTML='<div><h2>Thank You!</h2><p>You have been sent a confirmation email.</p><p>Please click the link contained in the email to activate your mailing list account.</p><p>Netway</p></div>';
					},50);
				}
			});
			return false;
		});
	});

})()
