var benplayer = "benytplayer";
var params = { allowScriptAccess: "always" };
var atts = { styleclass: "benytplayer" };
var ytplayerOK = false;
var ytplayerLoaded = false;
var currentURL = "";

//swfobject.embedSWF("http://www.youtube.com/e/YGw6DJsY7qg?enablejsapi=1&playerapiid=ytplayer&rel=0&showinfo=0",
//                       benplayer, "640", "390", "8", null, null, params, atts, callBackfn);

function loadytplayer(clipURL)
{   swfobject.embedSWF("http://www.youtube.com/e/"+clipURL+"?enablejsapi=1&playerapiid=ytplayer&rel=0&showinfo=0&autoplay=1",
                      benplayer, ytplayerwidth, ytplayerheight, "8", null, null, params, atts, callBackfn);
    ytplayerLoaded = true;
    currentURL = clipURL;
}

function callBackfn(e)
{   if (e.success)
    {   ytplayer = document.getElementById(e.id);
        ytplayerOK = true
    }
}

function play()
{   if (ytplayerOK)
    {   ytplayer.playVideo();
    }
}

function pause()
{   if (ytplayerOK)
    {   ytplayer.pauseVideo();
    }
}

function stop()
{   if (ytplayerOK)
    {   ytplayer.stopVideo();
    }
}

function mute()
{   if (ytplayerOK)
    {   ytplayer.mute();
    }
}

function unmute()
{   if (ytplayerOK)
    {   ytplayer.unMute();
    }
}

function playerRestart()
{   if (ytplayerOK)
    {   ytplayer.seekTo(0, true);
    }
}

function loadNewVideo(clipURL)
{   if (ytplayerOK)
    {   ytplayer.loadVideoById(clipURL);
    }
}

function hideYtplayer(id)
{   var melem1;
	if (document.getElementById)
	{   pause();
	    melem1 = document.getElementById(id);
		melem1.style.visibility="hidden";
	    melem1 = document.getElementById(benplayer);
		melem1.style.visibility="hidden";
		melem1 = document.getElementById(currentURL);
		melem1.style.display="none";
	}
}

function showYtplayer(id, clipURL)
{   var melem1;
	if (document.getElementById)
	{    if ( !ytplayerLoaded )
	    {   loadytplayer(clipURL);
	    }
	    else
	    {   if (clipURL != currentURL)
	        {   melem1 = document.getElementById(currentURL);
				melem1.style.display="none";
				loadNewVideo(clipURL);
	            currentURL = clipURL;
	            melem1 = document.getElementById(benplayer);
		        melem1.style.visibility="visible";
	        }
	        else
	        {   melem1 = document.getElementById(benplayer);
		        melem1.style.visibility="visible";
		        playerRestart();
		        play();
	        }
	    }
	    melem1 = document.getElementById(id);
		melem1.style.visibility="visible";
	    melem1 = document.getElementById(clipURL);
		melem1.style.display="inline";

	}
}


