var checkStartLoop;
var checkEndLoop;
var isComplete = false;

/* Checks for video cue */
function checkEnd()
{
	var isPlaying;

	try
	{
		isPlaying = Math.floor(embeddedPlayerManager.getPlayer().getCurrentTimeEnd());
	}
	catch(err)
	{
		isPlaying = true;	
	}
	
	if (!isPlaying)
	{
		clearInterval(checkEndLoop);
		// Double-check that there is no loop bug
		if (!isComplete)
		{
			isComplete = true;
			nbcu.app.scet.video.onClipCompletedEvent();
		}
	}
}

function startCheck()
{
	checkEndLoop = setInterval("checkEnd()", 500);
}

function checkStart()
{
	try
	{
		embeddedPlayerManager.getPlayer().setRunOnClipStart('startCheck()');
		clearInterval(checkStartLoop);
	}
	catch(err){}
}
	
checkStartLoop = setInterval("checkStart()", 5000);

// BEGIN SEND2FRIEND
function send2Friend(){
	
	var email = encodeURI(document.send2friendSection.email.value); // get the senders email
	var friendemail = encodeURI(document.send2friendSection.friendemail.value); // get the friends email
	var subject = encodeURI(document.send2friendSection.subject.value); // get the subject
	var message = encodeURI(document.send2friendSection.message.value); // get the message
	var url = encodeURI(window.location); // get the url
	var mediaName = encodeURI(document.send2friendSection.mediaName.value); // get the media name
	var mediaDesc = encodeURI(document.send2friendSection.mediaDesc.value); // get the media name
	
	if(validateFriend() == true){// see if the form is valid
		jqN.get("/app/form_mail/send2friend.scet.php?email="+email+"&friendemail="+friendemail+"&subject="+subject+"&message="+message+"&url="+url+"&mediaName="+mediaName+"&mediaDesc="+mediaDesc, function(data)
		{
			confirmIt();
		});
	}
	
}

function confirmIt(){ // confirm the request information

	jqN("#scet_send_confirmation").html("<br /><strong>This video has been sent to " + nbcu.util.common.encodeHtml(document.send2friendSection.friendemail.value) + "!</strong><br />Enter another email to send again.");
	jqN("#scet_send_confirmation").fadeOut("slow");
	jqN("#scet_send_confirmation").fadeIn("slow");
	document.send2friendSection.friendemail.value = ''; // reset the friends email
	nbcu.app.scet.video.logEvent({ event: 'share' });
}

function validateFriend(){ // validation
	if (!validate_email(document.send2friendSection.email.value))
	{
		alert("Please enter a valid e-mail address for yourself.");
		document.send2friendSection.email.focus();
		return false;
	}
	
	if (!validate_email(document.send2friendSection.friendemail.value))
	{
		alert("Please enter a valid friend's e-mail address.");
		document.send2friendSection.friendemail.focus();
		return false;
	}
	
	return true;
}

function validate_email(value)	{
		
	var good = /^([\w\d\.\-_]+)@([\w\d\.\-]+)$/i;
	var evil =  /[^a-z0-9\@_\-\.]+/i;

	if (good.test(value) && !evil.test(value)) {
		return true;
	} else {
		return false;
	}
}
// END SEND2FRIEND

// BEGIN LOOMIA
function keywordFuncLoomia(assetId)
{
	L_VARS.guid = assetId+'VIDEO';
	arrival_ts=(new Date()).getTime();
	action_ts=(new Date()).getTime();

	url='http://assets.loomia.com/jsapi?methodName=addClickstream&onload=1&publisher_key=1552314287&guid='+L_VARS.guid+'&action_ts='+action_ts+'&arrival_ts='+arrival_ts;
	_pingserver(url);
}
/* Generate the timestamps */


/* This is the function that pings the server with the url */
function _pingserver(url)
{
	var img=new Image(1,1);
	img.onload = function () { };
	img.onerror = function () { };
	img.src = url;
}
var _loomia_scripts_loaded=0;
if (typeof L_VARS=='undefined') { L_VARS = new Object() }

// Loomia common settings
L_VARS.publisher_key = '1552314287';
L_VARS.anchor = "recommendations_display";

function _loomia_addScript(url,script_anchor) {
	var anchor=document.getElementById(script_anchor);
	var script= document.createElement("SCRIPT");
	script.src=url;
	// anchor.appendChild(script);
}
function _loomia_addCS() {
	try {_loomia_rec_type = document.getElementById(L_VARS.anchor).className; }
	catch(e) { _loomia_rec_type='' }

	if (!_loomia_scripts_loaded) {
	_loomia_addScript("http://assets.loomia.com/js/clixdom.js", "_loomia_cs_anchor");
	if (_loomia_rec_type.indexOf('socialrecs') != -1)
	_loomia_addScript("http://assets.loomia.com/js/socialrecs.js", "_loomia_script_anchor");
	else if (_loomia_rec_type.indexOf('similarrecs') != -1)
	_loomia_addScript("http://assets.loomia.com/js/simitems.js", "_loomia_script_anchor");
	else if (_loomia_rec_type.indexOf('personalrecs') != -1)
_loomia_addScript("http://assets.loomia.com/js/personalrecs.js", "_loomia_script_anchor");
	else if (_loomia_rec_type.indexOf('checkoutrecs') != -1)
_loomia_addScript("http://assets.loomia.com/js/checkoutrecs.js", "_loomia_script_anchor");
	}
	_loomia_scripts_loaded=1;
}

// Loads Loomia script after page has loaded
if (window.addEventListener) {
	window.addEventListener("DOMContentLoaded", _loomia_addCS, false);
	window.addEventListener("load", _loomia_addCS, false);
} else if (window.attachEvent) window.attachEvent("onload", _loomia_addCS);
