// JavaScript Document
var Triario = {
	no_voto : -1,
	voto_negativo : 1,
	voto_neutro : 2,
	voto_positivo : 3,
	img_pos : '<img src="/imagenes/flecha-verde1.png" />',
	img_neg : '<img src="/imagenes/flecha-roja1.png" />',
	img_neu : '<img src="/imagenes/flecha-neutro1.png" />',

	Show : {
		Default : {
			standard: function(miObj) {
				$(miObj.handles.result_positivos).update(miObj.Positivos);
				$(miObj.handles.result_positivos_porcentajes).update(Number(miObj.Positivos_Porcentaje).toFixed(2));
			
				$(miObj.handles.result_negativos).update(miObj.Negativos);
				$(miObj.handles.result_negativos_porcentajes).update(Number(miObj.Negativos_Porcentaje).toFixed(2));


				if (miObj.Positivos>miObj.Negativos) {
					$(miObj.handles.result_imagen).update(miObj.images.img_pos);
				}
				else if (miObj.Positivos<miObj.Negativos) {
					
					$(miObj.handles.result_imagen).update(miObj.images.img_neg);
				}
				else {
					$(miObj.handles.result_imagen).update(miObj.images.img_neu);
				}

				Element.hide(miObj.handles.accion);
				Element.show(miObj.handles.result);
			}
		},
		Noticias : {
			standard : function() {
				Triario.Show.Default.standard(this);
			}
		},
		Noticias_Entidad : {
			standard : function() {
				Triario.Show.Default.standard(this);
			}
		}
	},
	
	ShowLast : {
		Default : {
			standard: function(miObj) {
				$(miObj.handles.result_positivos).update(miObj.Positivos);
				$(miObj.handles.result_positivos_porcentajes).update(Number(miObj.Positivos_Porcentaje).toFixed(2));
			
				$(miObj.handles.result_negativos).update(miObj.Negativos);
				$(miObj.handles.result_negativos_porcentajes).update(Number(miObj.Negativos_Porcentaje).toFixed(2));


				if (miObj.Positivos>miObj.Negativos) {
					$(miObj.handles.result_imagen).update(miObj.images.img_pos);
				}
				else if (miObj.Positivos<miObj.Negativos) {
					
					$(miObj.handles.result_imagen).update(miObj.images.img_neg);
				}
				else {
					$(miObj.handles.result_imagen).update(miObj.images.img_neu);
				}
			}
		},
		Noticias : {
			standard : function() {
				Triario.ShowLast.Default.standard(this);
			}
		},
		Noticias_Entidad : {
			standard : function() {
				Triario.ShowLast.Default.standard(this);
			}
		}
	},
	
	Voto : {
		Noticias : function(vote) {
			if (!getCookie(this.cookie)) {
				myAjax = new Ajax.Request(
						this.urls.actualizar, 
						{
							parameters: {
									actual_vote: Triario.no_voto,
									new_vote: vote,
									not_id : this.id_1
							},
							method : 'get',
							onComplete: this.receive.bind(this)
						});
			}
		},
		Noticias_Entidad : function(vote) {
			if (!getCookie(this.cookie)) {
				
				myAjax = new Ajax.Request(
						this.urls.actualizar, 
						{
							parameters: {
									actual_vote: Triario.no_voto,
									new_vote: vote,
									not_id : this.id_1,
									ent_id : this.id_2
							},
							method : 'get',
							onComplete: this.receive.bind(this)
						});
			}
		}
	},

	GetLast : {
		Noticias : function() {
				myAjax = new Ajax.Request(
						this.urls.consultar, 
						{
							parameters: {
									not_id : this.id_1
							},
							method : 'get',
							onComplete: this.receiveLast.bind(this)
						});
		},
		Noticias_Entidad : function(vote) {
				myAjax = new Ajax.Request(
						this.urls.consultar, 
						{
							parameters: {
									not_id : this.id_1,
									ent_id : this.id_2
							},
							method : 'get',
							onComplete: this.receiveLast.bind(this)
						});
		}
	},

	Vote : {
		Noticias : {
			Positivo : function() {
				this.Voto(Triario.voto_positivo);
			},
			Negativo : function() {
				this.Voto(Triario.voto_negativo);
			},
			Neutral : function() {
				this.Voto(Triario.voto_neutro);
			}
		},
		Noticias_Entidad : {
			Positivo : function() {
				this.Voto(Triario.voto_positivo);
			},
			Negativo : function() {
				this.Voto(Triario.voto_negativo);
			},
			Neutral : function() {
				this.Voto(Triario.voto_neutro);
			}
		}
	},
	
	Receive : {
		Standard : function(response, miObj) {
			var aResponse = response.responseText.split('#');

			miObj.Positivos = parseInt(aResponse[0]);
			miObj.Negativos = parseInt(aResponse[1]);
			miObj.Neutrales = parseInt(aResponse[2]);
			miObj.Total = parseInt(aResponse[3]);
			miObj.new_vote = parseInt(aResponse[5]);

			miObj.Positivos_Porcentaje = miObj.Positivos*100/miObj.Total;
			miObj.Negativos_Porcentaje= miObj.Negativos*100/miObj.Total;
			
			setCookie(miObj.cookie, miObj.new_vote, 1, '/','','');
			miObj.show();			
		},
		
		Noticias : function(response) {
			Triario.Receive.Standard(response, this);
		},
		Noticias_Entidad : function(response) {
			Triario.Receive.Standard(response, this);
		}
	},

	ReceiveLast : {
		Standard : function(response, miObj) {
			var aResponse = response.responseText.split('#');

			miObj.Positivos = parseInt(aResponse[0]);
			miObj.Negativos = parseInt(aResponse[1]);
			miObj.Neutrales = parseInt(aResponse[2]);
			miObj.Total = parseInt(aResponse[3]);

			miObj.Positivos_Porcentaje = miObj.Positivos*100/miObj.Total;
			miObj.Negativos_Porcentaje= miObj.Negativos*100/miObj.Total;
			
			miObj.showLast();			
		},
		
		Noticias : function(response) {
			Triario.ReceiveLast.Standard(response, this);
		},
		Noticias_Entidad : function(response) {
			Triario.ReceiveLast.Standard(response, this);
		}
	}
};

Triario.Objeto = Class.create ({
	id_1 : null,
	id_2 : null,
	
	Positivos : null,
	Positivos_Porcentaje : null,
	Negativos : null,
	Negativos_Porcentaje : null,
	Neutrales : null,
	Neutrales_Porcentaje : null,
	Total : null,

	new_vote : null,
	options : null,
	
	/* functions */
	Voto : null,
	Voto_Positivo : null,
	Voto_Negativo : null,
	Voto_Neutral : null,
	getLast : null,
	show : null,	
	showLast : null,
	receive : null,
	receiveLast : null,
	
	handles : null,
	cookie : null,
	urls : null,
	
	init : function () {
		if (!getCookie(this.cookie)) {
			Element.hide(this.handles.result);	
		}
		else {
			Element.hide(this.handles.accion);
		}
		this.getLast();
	}
});

Triario.Noticia = Class.create ( Triario.Objeto, {
	initialize : function (not_id, options) {
		this.id_1 = not_id;

		this.options  = options || { };

		this.Voto = this.options.Voto || Triario.Voto.Noticias;
		this.Voto_Positivo = this.options.Voto_Positivo || Triario.Vote.Noticias.Positivo;
		this.Voto_Negativo = this.options.Voto_Negativo || Triario.Vote.Noticias.Negativo;
		this.Voto_Neutral  = this.options.Voto_Neutral || Triario.Vote.Noticias.Neutral;
		this.getLast = this.options.getLast || Triario.GetLast.Noticias;
		this.show = this.options.show || Triario.Show.Noticias.standard;
		this.showLast = this.options.showLast || Triario.ShowLast.Noticias.standard;
		this.receive = this.options.receive || Triario.Receive.Noticias;
		this.receiveLast = this.options.receiveLast || Triario.ReceiveLast.Noticias;

		this.images = this.options.images || {
												img_pos : Triario.img_pos,
												img_neg : Triario.img_neg,
												img_neu : Triario.img_neu
											};

		this.handles = this.options.handles || {
													accion : 'not_accion_'+not_id,
													result : 'not_resul_'+not_id,
													result_positivos : 'not_resul_positivos_'+not_id,
													result_positivos_porcentajes : 'not_resul_positivos_porcentajes_'+not_id,
													result_neutrales : 'not_resul_neutrales_'+not_id,
													result_neutrales_porcentajes : 'not_resul_neutrales_porcentajes_'+not_id,
													result_negativos : 'not_resul_negativos_'+not_id,
													result_negativos_porcentajes : 'not_resul_negativos_porcentajes_'+not_id,
													result_imagen : 'not_imagen_'+not_id
									    		};

		this.cookie = this.options.cookie || 'not_tri_'+not_id;
		
		this.urls = this.options.urls || {
									actualizar : '/noticias-categoria/noticia_triarias_actualizar.php',
									consultar : '/noticias-categoria/noticia_triarias_consultar.php'
										};
			
		this.init();		
	}
});

Triario.Noticia_Entidad = Class.create ( Triario.Objeto, {
	initialize : function (not_id, ent_id, options) {
		this.id_1 = not_id;
		this.id_2 = ent_id;

		this.options  = options || { };

		this.Voto = this.options.Voto || Triario.Voto.Noticias_Entidad;
		this.Voto_Positivo = this.options.Voto_Positivo || Triario.Vote.Noticias_Entidad.Positivo;
		this.Voto_Negativo = this.options.Voto_Negativo || Triario.Vote.Noticias_Entidad.Negativo;
		this.Voto_Neutral  = this.options.Voto_Neutral || Triario.Vote.Noticias_Entidad.Neutral;
		this.getLast = this.options.getLast || Triario.GetLast.Noticias_Entidad;
		this.show = this.options.show || Triario.Show.Noticias_Entidad.standard;
		this.showLast = this.options.show || Triario.ShowLast.Noticias_Entidad.standard;
		this.receive = this.options.receive || Triario.Receive.Noticias_Entidad;
		this.receiveLast = this.options.receive || Triario.ReceiveLast.Noticias_Entidad;

		this.images = this.options.images || {
												img_pos : Triario.img_pos,
												img_neg : Triario.img_neg,
												img_neu : Triario.img_neu
											};

		this.handles = this.options.handles || {
													accion : 'not_ent_accion_'+not_id+'_'+ent_id,
													result : 'not_ent_resul_'+not_id+'_'+ent_id,
													result_positivos : 'not_ent_resul_positivos_'+not_id+'_'+ent_id,
													result_positivos_porcentajes : 'not_ent_resul_positivos_porcentajes_'+not_id+'_'+ent_id,
													result_neutrales : 'not_ent_resul_neutrales_'+not_id+'_'+ent_id,
													result_neutrales_porcentajes : 'not_ent_resul_neutrales_porcentajes_'+not_id+'_'+ent_id,
													result_negativos : 'not_ent_resul_negativos_'+not_id+'_'+ent_id,
													result_negativos_porcentajes : 'not_ent_resul_negativos_porcentajes_'+not_id+'_'+ent_id,
													result_imagen : 'not_ent_imagen_'+not_id+'_'+ent_id
									    		};

		this.cookie = this.options.cookie || 'not_ent_'+not_id+'_'+ent_id;
		
		this.urls = this.options.urls || {
									actualizar : '/noticias-categoria/noticia_entidades_triarias_actualizar.php',
									consultar : '/noticias-categoria/noticia_entidades_triarias_consultar.php'
										};
			
		this.init();		
	}
});

