$(document).ready(function()
{
	replaceDivs();
});

$(window).scroll(function()
{
	if($('iframe.youtubeplayer.autostart').length>0)  
	{
		autostartVisible();
	}
});

function replaceDivs()
{
	$('div.youtubeplayer').each(function()
	{
    	$(this).replaceWith($('<iframe class="'+$(this).attr('class')+'" id="'+$(this).attr('id')+'" type="text/html" src="'+getTranslatedString('ctxtPath')+'/youtubeplayer.jsp?videoid='+$(this).html()+'" style="'+$(this).attr('style').replace('"', "'")+'" frameborder="0">'));
    });
}

function autostartVisible()
{
	$('iframe.youtubeplayer.autostart').each(function()
	{
		//Not Started & Visible
		if($(window).scrollTop()+$(window).height()>$(this).offset().top+($(this).height()/2)&&$(window).scrollTop()<$(this).offset().top+($(this).height()/2))
		{
			this.contentWindow.initPlayer();
		}
	});
}
