// JavaScript Document

AddComment = function() {
	
	var comments = new Ext.form.TextArea({
		fieldLabel: 'Comentario',
		allowBlank: false,
		name: 'text',
		anchor: '95%'
	});	

	var formPanel = new Ext.form.FormPanel({
		baseCls: 'x-plain',
		labelWidth: 70,
		bodyStyle: 'position: relative;',
		labelAlign: 'right',
		autoScroll: true,
		url:'/peliculas/comentarios/agregar/' + mid,
		defaultType: 'textfield',
		items: [comments]
	});

	var ext_window = new Ext.Window({
		title: 'Agregar Comentario',
		modal:true,
		layout: 'fit',
		plain: true,
		bodyStyle: 'padding: 10px;',
		buttonAlign: 'center',
		width: 400,
		height: 160,
		defaultButton: 0,
		constrain: true,
		animateTarget: 'comment_add',
		items: formPanel,
		buttons: [{
			text: '<span style="font-weight: bold">Agregar</span>', 
			handler: function() {
				if (formPanel.form.isValid()) {
					formPanel.form.submit({			      
						waitMsg: 'Enviando',
						failure: function(form, action) {
							ext_window.center();
							Ext.MessageBox.alert('Error al enviar', action.result.errorInfo);
						},
						success: function(form, action) {
							Ext.MessageBox.alert('Env&iacute;o completado', action.result.info, function() {
								window.location = window.location;
							});
							ext_window.hide();
						}
					});                 
				} else{
					Ext.MessageBox.alert('Error al enviar', 'Algunos campos no han sido llenados o son inv&aacute;lidos. ' +
						'Para m&aacute;s detalles, posicione el rat&oacute;n encima de los s&iacute;mbolos de advertencia ' +
						'que encontrar&aacute; a la derecha de los campos con los errores.');
				}             
			}
		},{
			text: 'Cancelar',
			handler: function(){ext_window.hide();}
		}]
	});
	
	ext_window.show();
	
};
	
Ext.onReady(function() {
						
	new Starbox('rating', rating_avg, {
		indicator: '#{average} por #{total} votos',
		identity: mid,
		total: rating_total,
		locked: rating_voted || !uid,
		ghosting: true,
		onRate: function(element, info) {
			Ext.Ajax.request({
				url: '/peliculas/votar',
				params: {identity: info.identity, rated: info.rated},
				method: 'POST',
				success: function(result, request) {
					Ext.MessageBox.show({
						title: 'Voto enviado',
						msg: result.responseText,
						buttons: Ext.Msg.OK,
						animEl: 'rating'
					});				
				},
				failure: function(result, request) {
					Ext.MessageBox.show({
						title: 'Error al votar',
						msg: 'No se ha podido enviar su voto, por favor intente m&aacute;s tarde.',
						buttons: Ext.Msg.OK,
						animEl: 'rating'
					});
				}
			});
		}
	});

	if (rating_voted)
		Ext.get('rating').on('click', rating_voted_click);
		
	if (!uid)
		Ext.get('rating').on('click', rating_nouser_click);

	if (uid)
		Ext.get('comment_add').on('click', AddComment);
	else
		Ext.get('comment_add').on('click', comment_nouser_click);

	function comment_nouser_click() {
		Ext.MessageBox.show({
			title: 'No ha iniciado sesi&oacute;n',
			msg: 'Debe iniciar sesi&oacute;n antes de poder hacer un comentario.',
			buttons: {yes: 'Iniciar sesi&oacute;n', no: 'Nuevo registro', cancel: 'Cancelar'},
			animEl: 'comment_add',
			fn: function(btn) {
				if (btn == 'yes') log_in();
				if (btn == 'no') new_account();
			}
		});
	}

	function rating_nouser_click() {
		Ext.MessageBox.show({
			title: 'No ha iniciado sesi&oacute;n',
			msg: 'Debe iniciar sesi&oacute;n antes de poder votar.',
			buttons: {yes: 'Iniciar sesi&oacute;n', no: 'Nuevo registro', cancel: 'Cancelar'},
			animEl: 'rating',
			fn: function(btn) {
				if (btn == 'yes') log_in();
				if (btn == 'no') new_account();
			}
		});
	}

	function rating_voted_click() {
		Ext.MessageBox.show({
			title: 'Voto ya enviado',
			msg: 'Usted ya ha enviado un voto para esta pel&iacute;cula',
			buttons: Ext.Msg.OK,
			animEl: 'rating'
		});
	}
	
});
