//s2f.js
//Contains all the necessary functions for send-to-friend
MN.TP.sendStart = 0;
MN.TP.sendStop = -1;
MN.TP.sendWindow = null;
MN.TP.sendBox = false;  //is the send to friend form visible?

MN.TP.InitS2F = function(){
	MN.Event.Observe($('mn_send'), 'click', MN.TP.SendClick);
	MN.Event.Observe($('mn_set_start'), 'click', MN.TP.SetSendStart);
	MN.Event.Observe($('mn_set_stop'), 'click', MN.TP.SetSendStop);
	
	//If keyboard controls are enabled, we need to disable them when the user is filling out the 
	//send-to-friend form
	log('keyboard controls')
	MN.Event.Observe($('mn_senders_name_field'), 'focus', function(){MN.TP.allowKeyboardCtrls = false;});
	MN.Event.Observe($('mn_senders_name_field'), 'blur', function(){MN.TP.allowKeyboardCtrls = true;});
	MN.Event.Observe($('mn_senders_email_field'), 'focus', function(){MN.TP.allowKeyboardCtrls = false;});
	MN.Event.Observe($('mn_senders_email_field'), 'blur', function(){MN.TP.allowKeyboardCtrls = true;});
	MN.Event.Observe($('mn_friends_email_field'), 'focus', function(){MN.TP.allowKeyboardCtrls = false;});
	MN.Event.Observe($('mn_friends_email_field'), 'blur', function(){MN.TP.allowKeyboardCtrls = true;});
	MN.Event.Observe($('mn_comments'), 'focus', function(){MN.TP.allowKeyboardCtrls = false;});
	MN.Event.Observe($('mn_comments'), 'blur', function(){MN.TP.allowKeyboardCtrls = true;});
}

MN.TP.OpenSendBox = function(){
	$('mn_playlist_container').className = 'mn_hidden';
	$('mn_send_friend').className = 'mn_visible';
	MN.TP.sendBox = true;
}

MN.TP.CloseSendBox = function(){
	$('mn_playlist_container').className = 'mn_visible';
	$('mn_send_friend').className = 'mn_hidden';
	MN.TP.sendBox = false;
}

MN.TP.SetSendStart = function(name){
	$('mn_share_clip').checked = true;
	var sec = MN.TP.qmp.CurrentPosition();
	$('mn_start').value = MN.ConvertToTimestamp(sec, MN.TP.curChnl.send2Friend.timeFormat);
	MN.TP.sendStart = sec;
}

MN.TP.SetSendStop = function(name){
	var sec = MN.TP.qmp.CurrentPosition();
	$('mn_end').value = MN.ConvertToTimestamp(sec, MN.TP.curChnl.send2Friend.timeFormat);
	MN.TP.sendStop = sec;
}

MN.TP.CheckSendersName = function(val){
	var chunks = val.strip().split(' ');
	for(var i=0; i < chunks.length; i++){
		var chunk = chunks[i];
		if(!MN.Validate.IsAlphaNumeric(chunk))
			return false;
	}
	return true;
}

MN.TP.CheckFriendsEmails = function(val){
	var chunks = val.strip().split(',');
	for(var i=0; i < chunks.length; i++){
		var chunk = chunks[i].strip();
		if(!MN.Validate.IsEmail(chunk))
			return false;
	}
	return true;
}

MN.TP.SendClick = function(){
	var qvt = MN.TP.qmp.CurrentQVT();
	var success = true;
	var check = {'mn_senders_email' : MN.Validate.IsEmail, 'mn_senders_name' : MN.TP.CheckSendersName, 'mn_friends_email' : MN.TP.CheckFriendsEmails};
	var checked = $('mn_share_current').checked ? 'show' : 'clip';
	
	//Get the appropriate start and end positions
	if(checked == 'clip'){
		if(sendStop < sendStart){
			$('mn_send').style.display = 'none';
			$('mn_clip_error').style.display = 'block';
			setTimeout(function(){
				$('mn_send').style.display = 'block'; 
				$('mn_clip_error').style.display = 'none';
			}, 5000);		
			return false;
		}
		else{
		   	$('mn_start').style.className = '';
		   	$('mn_end').style.className = '';
		}
	}
	else{
		var showNum = MN.TP.qmp.CurrentShow();
		if(showNum < 0 || showNum >= qvt.ShowCount())
	        showNum = qvt.ShowCount() - 1;
		
		MN.TP.sendStart = qvt.StartTime(showNum) || 0;
		MN.TP.sendStop = qvt.StopTime(showNum) || -1;
		
	    $('mn_start').style.className = '';
	   	$('mn_end').style.className = '';
	}

	//Check the info form
	var form = $('mn_send_friend');
	for(var item in check){
		var curVal = form[item].value;
		if(check[item](curVal)){
			$(item).style.className = '';
		}
		else{
			$(item).style.className = 'mn_error';
			success = false;
		}
	}

	if(success){
		var params = {
						'show':qvt.PrimaryURL(), 
						'ep': '', 
						'start':MN.TP.sendStart, 
						'end':MN.TP.sendStop, 
						'fromEmail':form.mn_senders_email.value, 
						'fromName':form.mn_senders_name.value, 
						'toEmail':form.mn_friends_email.value, 
						'comments':form.mn_comments.value, 
						'cfg':MN.TP.curChnl.send2Friend.config || MN.TP.curChnl.name
					 };
	    MN.TP.sendWindow = window.open(MN.URL.SetParams("http://gksrv.xlontech.com/scripts/s2f/Send", params), "_blank", "top=500,left=500,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,height=279,width=316");
	    setTimeout(MN.TP.SendWindowPoll, 750);
	}
	else{
		$('mn_send').style.display = 'none';
		$('mn_send_error').style.display = 'block';
		setTimeout(function(){
			$('mn_send').style.display = 'block'; 
			$('mn_send_error').style.display = 'none';
		}, 5000);
		return false;
	}
	
	return false;
}

MN.TP.SendWindowPoll = function(){
    if(MN.TP.sendWindow){
        if(MN.TP.sendWindow.closed)
            MN.TP.CloseSendBox();
        else
            setTimeout(MN.TP.SendWindowPoll, 750);
    }
    else
        return;
}
