Ext.BLANK_IMAGE_URL = '/thirdparty/ext/images/default/s.gif';
var loadingNow = false;
function replyFormToggle(nodeToggler,commentId)
{
	$('reply_form_'+commentId).toggleClassName('hidden');
};

function adminDeleteComment(id,videoId)
{
	
	var a = $('#comment_div_'+id);
	a.fadeOut(250);
	jQuery.get(
				'?remove_id='+id
			);
					loadComments(videoId,1);
	
	
	return false;
};
function submitReply(nodeForm)
{
	if(nodeForm.content.value.length>0)
	{
		var videoId=nodeForm.video_id.value;
		var captchaChallenge=null
		var captchaResponse=null;
		if(nodeForm.recaptcha_challenge_field)
			captchaChallenge=nodeForm.recaptcha_challenge_field.value;
		if(nodeForm.recaptcha_response_field)
			captchaResponse=nodeForm.recaptcha_response_field.value;

 		jQuery.post
		(
			nodeForm.getAttribute('action'),
			{
				video_id:videoId,
				content:nodeForm.content.value,
				recaptcha_challenge_field:captchaChallenge,
				recaptcha_response_field:captchaResponse
			},
			function(responseJson)
			{
				if(typeof Recaptcha == 'object')
					Recaptcha.reload();
				if(responseJson.success)
				{
					nodeForm.content.value="";
					loadComments(videoId,1);
				}
				else
				{
					if(typeof responseJson.message != 'undefined')
					{
						alert(responseJson.message);
					};
				};
			},
			'json'
		);
	}
	else
		alert('Comment must not be blank.');
};

function showCalendar(fieldId)
{
	var cds = new CalendarDateSelect($(fieldId));
};
function showEmbedVideoCode(html)
{
	var winEmbed=new Ext.Window
	(
		{
			autoShow:true,
			height:250,
			width:330,
			autoScroll:false,
			modal:true,
			layout:'fit',
			title:'EMBED THIS VIDEO',
			items:
			[
				{
					xtype:'label',
					text:'Copy the code below and paste it into your website.'
				},
				{
					xtype:'textarea',
					value:html,
					width:'90%',
					height:160,
					name:'message',
					fieldLabel:'Embed Code',
					allowBlank:true,
					hideLabel:true
				}
			]
		}
	);
	winEmbed.show();
}
function showShareVideoUrl(url)
{
	var winShare=new Ext.Window
	(
		{
			id:'share_win',
			autoShow:true,
			height:350,
			width:340,
			autoScroll:false,
			modal:true,
			layout:'fit',
			title:'SHARE THIS VIDEO',
			items:
			[
				{
					xtype:'form',
					id:'share_video_form',
					bodyStyle:'padding:5px',
					url:'/json/sharevideo/',
					items:
					[
						{
							xtype:'label',
							html:'<p><b>Your Friends</b><br />Add in email addresses of fields to send it to.</p>'
						},
						{
							xtype:'hidden',
							name:'url',
							value:url
						},
						{
							xtype:'hidden',
							name:'video_title',
							value:$('#video_title').val()
						},
						{
							xtype:'hidden',
							name:'video_byline',
							value:$('#video_byline').val()
						},
						{
							xtype:'hidden',
							name:'video_desc',
							value:$('#video_desc').val()
						},
						{
							xtype:'hidden',
							name:'video_thumb',
							value:$('#video_thumb').val()
						},
						{
							xtype:'textfield',
							name:'emails[0]',
							fieldLabel:'1',
							vtype:'email',
							allowBlank:true,
							labelSeparator:'',
							labelStyle:'width:20px;text-align:center !important;',
							style:'width:276px;margin-left:-83px;',
							validationEvent:false,
							focusClass:''
						},
						{
							xtype:'textfield',
							name:'emails[1]',
							fieldLabel:'2',
							vtype:'email',
							allowBlank:true,
							labelSeparator:'',
							labelStyle:'width:20px;text-align:center !important;',
							style:'width:276px;margin-left:-83px;',
							validationEvent:false,
							focusClass:''
						},
						{
							xtype:'textfield',
							name:'emails[2]',
							fieldLabel:'3',
							vtype:'email',
							allowBlank:true,
							labelSeparator:'',
							labelStyle:'width:20px;text-align:center !important;',
							style:'width:276px;margin-left:-83px;',
							validationEvent:false,
							focusClass:''
						},
						{
							xtype:'label',
							html:'<br><p><b>Your Personal Message</b></p>'
						},
						{
							xtype:'textarea',
							name:'message',
							fieldLabel:'Message',
							allowBlank:true,
							hideLabel:true,
							anchor:'100%',
							height:100,
							validationEvent:false,
							focusClass:''
						}
					],
					buttons:
					[
						{
							text:'SEND TO FRIENDS',
							handler:function(b,e)
							{
								var f=Ext.getCmp('share_video_form');
								var basicForm=f.getForm();
								if(basicForm.isValid())
									basicForm.submit
									(
										{
											success: function(form, action)
											{
												alert('Video shared!');
												winShare.close();
											},
											failure: function(form, action)
											{
												switch (action.failureType)
												{
													case Ext.form.Action.CLIENT_INVALID:
														alert('Please fill out all fields');
														break;
													case Ext.form.Action.CONNECT_FAILURE:
														alert('Ajax communication failed');
														break;
													case Ext.form.Action.SERVER_INVALID:
														alert(action.result.msg);
												}
											}
										}
									);
								else
									alert('Form invalid!');
							}
						}
					]
				}
			]
		}
	);
	winShare.show();
}
function showForgotPasswordWindow()
{
	var win=new Ext.Window
	(
		{
			title:'RESET PASSWORD',
			autoShow:true,
			height:125,
			width:550,
			autoScroll:false,
			modal:true,
			draggable:false,
			layout:'fit',
			items:
			[
				{
					xtype:'form',
					id:'reset_password_request_form',
					standardSubmit: true,
					url:'/becomeafriend/',
					items:
					[
						{
							xtype:'hidden',
							name:'form_id',
							value:'reset_password_request'
						},
						{
							xtype:'textfield',
							name:'email',
							fieldLabel:'Email Address',
							vtype:'email',
							allowBlank:false,
							width:350
						}
					],
					buttons:
					[
						{
							text:'Submit',
							handler:function(b,e)
							{
								var f=Ext.getCmp('reset_password_request_form');
								var basicForm=f.getForm();
								if(basicForm.isValid())
									basicForm.submit();
							}
						},
						{
							text:'Cancel',
							handler:function(b,e)
							{
								win.close();
							}
						}
					],
					listeners:
					{
						render:function(cmp)
						{
							var basicForm=cmp.getForm();
							basicForm.getEl().dom.action = cmp.url;
						}
					}
				}
			]
		}
	);
	win.show();
};

function loadComments(video,page)
{
	var requestUrl=relroot+'comments/'+video+'/';
	if(page)
		requestUrl+=page+'/';
	jQuery.get
	(
		requestUrl,
		null,
		function(responseHtml)
		{
			$('#comments').html(responseHtml);
		},
		'html'
	);
	return false;
};

function closeReportMenu(elem)
{
	var menuElem=Ext.Element.get(elem.parentNode);
	menuElem.setVisibilityMode(Ext.Element.DISPLAY);
	menuElem.hide();
};
function reportComment(commentId,reportType)
{
	var params={comment_id:commentId};
	if(reportType)
		params['report_type']=reportType;

	Ext.Ajax.request
	(
		{
   			url:relroot+'json/reportcomment/',
			params:params,
			success:function(response, opts)
			{
				var respObj=eval(response.responseText);
				if(respObj && respObj.success)
					alert('Thanks for making us aware of this comment, we will attend to it as soon as possible.');
				else
					alert('Error!');
			},
			failure:function(response, opts)
			{
				alert('Error! '+response.status);
			}
		}
	);
	return false;
};

// *************** Play Live Form begin *************************
var playLiveFormDiv=null;
var playLiveCloseFormDiv=null;
function showPlayLiveForm()
{
	playLiveFormDiv=Ext.Element.get('play_live_div');
	playLiveCloseFormDiv=Ext.Element.get('close_play_live_div');

	playLiveFormDiv.setVisibilityMode(Ext.Element.DISPLAY);
	playLiveCloseFormDiv.setVisibilityMode(Ext.Element.DISPLAY);

	if(playLiveFormDiv.isVisible())//send form
	{
		var f=new Ext.form.BasicForm('play_live_form',{url:'/json/playliveform/'});
		var valid = true;
		$('#play_live_form :input').each(function() {
												if(valid) {
													var message = '';
													var el = $(this);
													if(el.attr('name')!='additional_info' && !el.val()) {
														alert("You need to fill in all the fields!");
														valid = false;
													} else {
														if(el.attr('name')=='email' && !el.val().match(/^.+@.+\..+$/)) {
															alert("You need to enter a valid email address!");
															valid = false;
														}
													}
												}
											});
		if(!valid) {
			return false;
		}
		f.submit
		(
			{
				success:function(form, action)
				{
					alert('Request sent');
					hidePlayLiveForm();
				},
				failure:function(form, action)
				{
					switch (action.failureType)
					{
						case Ext.form.Action.CLIENT_INVALID:
							alert('Form fields may not be submitted with invalid values');
							break;
						case Ext.form.Action.CONNECT_FAILURE:
							alert('Ajax communication failed');
							break;
						case Ext.form.Action.SERVER_INVALID:
							alert(action.result.msg);
							break;
					}
				}
			}
		);
	}
	else//show form
	{
		playLiveFormDiv.show();
		playLiveCloseFormDiv.show();
	};
};
function hidePlayLiveForm()
{
	playLiveFormDiv.hide();
	playLiveCloseFormDiv.hide();
	clearPlayLiveForm();
};
function clearPlayLiveForm()
{
	var formElem=Ext.Element.get('play_live_form');
	if(formElem)
	{
		formElem.dom['name'].value='';
		formElem.dom['email'].value='';
		formElem.dom['event'].value='';
		formElem.dom['additional_info'].value='';
	};
};
// *************** Play Live Form end *************************
// *************** Send Love Form begin *************************
var sendLoveFormDiv=null;
var sendLoveCloseFormDiv=null;
function showSendLoveForm()
{
	sendLoveFormDiv=Ext.Element.get('send_love_div');
	sendLoveCloseFormDiv=Ext.Element.get('close_send_love_div');

	sendLoveFormDiv.setVisibilityMode(Ext.Element.DISPLAY);
	sendLoveCloseFormDiv.setVisibilityMode(Ext.Element.DISPLAY);

	if(sendLoveFormDiv.isVisible())//send form
	{
		var f=new Ext.form.BasicForm('send_love_form',{url:'/json/sendloveform/'});
		f.submit
		(
			{
				success:function(form, action)
				{
					alert('Love sent');
					hideSendLoveForm();
				},
				failure:function(form, action)
				{
					switch (action.failureType)
					{
						case Ext.form.Action.CLIENT_INVALID:
							alert('Form fields may not be submitted with invalid values');
							break;
						case Ext.form.Action.CONNECT_FAILURE:
							alert('Ajax communication failed');
							break;
						case Ext.form.Action.SERVER_INVALID:
							alert(action.result.msg);
							break;
					}
				}
			}
		);
	}
	else//show form
	{
		sendLoveFormDiv.show();
		sendLoveCloseFormDiv.show();
	};
};
function hideSendLoveForm()
{
	sendLoveFormDiv.hide();
	sendLoveCloseFormDiv.hide();
	clearSendLoveForm();
};
function clearSendLoveForm()
{
	var formElem=Ext.Element.get('send_love_form');
	if(formElem)
	{
		formElem.dom['name'].value='';
		formElem.dom['email'].value='';
		formElem.dom['message'].value='';
	};
};

//************************ Tell your friends form begin *********************************************
function sendTellFriendsForm(nodeForm)
{
	var f=new Ext.form.BasicForm(nodeForm);
	f.submit
	(
		{
			success:function(form, action)
			{
				if(action.result.count>0)
					alert('Thanks for telling your friends about Bulls+Arrows!');
				else
					alert('You forgot to enter your friend\'s email addresses.');
				clearTellFriendsForm();
			},
			failure:function(form, action)
			{
				switch (action.failureType)
				{
					case Ext.form.Action.CLIENT_INVALID:
						alert('Form fields may not be submitted with invalid values');
						break;
					case Ext.form.Action.CONNECT_FAILURE:
						alert('Ajax communication failed');
						break;
					case Ext.form.Action.SERVER_INVALID:
						alert(action.result.msg);
						break;
				}
			}
		}
	);
};
function clearTellFriendsForm()
{
	var formElem=Ext.Element.get('tell_friends_form');
	if(formElem)
	{
		formElem.dom['emails[0]'].value='';
		formElem.dom['emails[1]'].value='';
		formElem.dom['emails[2]'].value='';
	};
};
//************************ Tell your friends form end ***********************************************