$(document).ready( function ()
{
	updatePageElements();
	playFeatureLoop();
} );

$(window).load( updatePageElements );



var __malmerPreloadImageCache = new Array();

function preLoad( src )
{
	var len = arguments.length;
	for (var i = len; i--;) 
	{
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		__malmerPreloadImageCache.push(cacheImage);
	}
}
function updatePageElements()
{	
	$('#commentform input').unbind( 'focus', focusInput );
	$('#commentform input').unbind( 'blur', blurInput );
	$('#commentform input').unbind( 'change', blurInput );
	$('#commentform input').bind( 'focus', focusInput );
	$('#commentform input').bind( 'blur', blurInput );
	$('#commentform input').bind( 'change', blurInput );
	$('#commentform input').each( blurInput );
	
	$('a.youtube-thumbnail').click( function (e)
		{
			if( $(this).hasClass('no-play') )
				return;
				
			e.stopPropagation();
			e.preventDefault();
			var code = $(this).attr('href');
			code = code.substring( code.lastIndexOf('?')+3, code.length );
			switchToYoutube( this, code );
		} );
	
	$('#splash .features .feature-large').unbind( 'mouseover', pauseFeatureLoop );
	$('#splash .features .feature-large').unbind( 'mouseout', playFeatureLoop );	
	$('#splash .features .feature-large').bind( 'mouseover', pauseFeatureLoop );
	$('#splash .features .feature-large').bind( 'mouseout', playFeatureLoop );
}

function focusInput( event )
{
	$(this).parent().find('label').hide();
}

function blurInput( event )
{
	var label = $(this).parent().find('label[for='+$(this).attr('id')+']');
	if( label[0] != null )
	{
		if( $(this).val() == '' )
			label.show();
		else
			label.hide();
	}
}

function switchToYoutube( item, code )
{
	$(item).parent().replaceWith( '<iframe width="580" height="465" src="http://www.youtube.com/embed/'+code+'?autoplay=1&hd=1&fmt=18" frameborder="0" allowfullscreen></iframe>' );
}



var __currentFeatureIndex = 0;
var __featureLoopInterval;

function playFeatureLoop()
{
	clearInterval( __featureLoopInterval );
	if( $('#splash .features .feature-large').length != 0 )
		__featureLoopInterval = setInterval( nextFeature, 3000 );
}

function pauseFeatureLoop()
{
	clearInterval( __featureLoopInterval );
}

function nextFeature()
{
	var features = $('#splash .features .feature-large');
	var index0 = __currentFeatureIndex % features.length;
	var index1 = ++__currentFeatureIndex % features.length;
	var feature0 = features.eq( index0 );
	var feature1 = features.eq( index1 );
	
	feature0.css('z-index', 20 );
	feature1.css('z-index', 30 );
	feature1.fadeIn( 400, function () { feature0.hide(); } );
}


