window.addEvent('domready', function(){ new MooJson(); });
function effectIn(){ var myFx = new Fx.Slide('resultados').slideOut().chain(function(){ this.slideIn('vertical'); }); };
var MooJson = new Class({ options: { divLoad: 'loading', injLoad: 'load', injLoad2: 'load-buscador', divError: 'errors', injError: 'noticiero',	injError2: 'resultados', contRes: 'noticia',
		request: { 	loading: 'cargando <img src="images/ajax-loader.gif" title="loading" sytle="padding-left:6px;"/>',
					loading2: '<img src="images/ajax-loader2.gif" title="loading" />', failure: 'Ocurrió un errors durante la carga',
					path: 'includes/', encoding:'utf-8' },
		errors: { '301': 'La página ha sido redireccionada', '302': 'La página ha sido redireccionada','303': 'La página ha sido redireccionada',
			'401': 'Página con acceso restringido','403': 'Página con acceso restringido','404': 'Página no encontrada','500': 'Eror interno del servidor',
			'unknown': 'Error desconocido'}
	},
	initialize: function(options){
		$$('[class*=moojson]').each(function(el){
			this.op = el.get('class').slice(8).split(".");
			if(this.op[0].match(/^\w+:\w+$/)){
				this.temp = this.op[0].split(':');
				this.options.event = this.temp[1];
			}else{ this.options.event = 'click'; }
			el.addEvent(this.options.event, function(){
				this.request(el);
			}.bind(this));
		}.bind(this));
	},
	request: function(el){
		this.data='';
		switch(el.get('tag')){
			case 'a':	this.url = this.options.request.path + el.get('title').substring(1); this.method = 'get'; break;
			case 'input':	this.form = el.getParent('form');
							if(!this.form){
								this.method = 'get';
								this.url = this.options.request.path + el.get('title').substring(1);
							}else{
								this.form.getElements("*").each(function(el) {
									if(el.get('type')=='text' || el.get('type')=='radio' || el.get('type')=='checkbox' || el.get('tag')=='select'){
										if(el.get('type')=='radio' || el.get('type')=='checkbox'){
											if(el.get('checked')) this.data += el.get('name')+'='+el.get('value')+'&';
										}else if(el.get('type')=='text'){
											if(el.get('value')!='') this.data += el.get('name')+'='+el.get('value')+'&';
										}else{ this.data += el.get('name')+'='+el.get('value')+'&';	}}
								}.bind(this));
								this.method = this.form.get('method');
								this.url = this.options.request.path + this.form.get('action').substring(1);
							}break;
		}
		this.myRequest = new Request.JSON({
			method: this.method,
			url: this.url,
			encoding: this.options.request.encoding,
			data: this.data,
			onRequest: function(){
				if(this.method!='post'){
					this.loading = new Element('div', { 'id': this.options.divLoad, 'html': this.options.request.loading });
					this.loading.injectTop(this.options.injLoad);
				}else{ $(this.options.injLoad2).set('html', this.options.request.loading2);	}
			}.bind(this),
			onSuccess: function(respt){
				this.op = el.get('class').slice(8).split(".");
				if(this.op[0].match(/^\w+:\w+$/)){
					this.temp = this.op[0].split(':');
					this.op[0] = this.temp[0];
				}
				$(this.op[0]).empty();
				if($(this.options.divLoad)) $(this.options.divLoad).destroy();
				if($(this.options.injLoad2)) $(this.options.injLoad2).empty();
				if(this.method!='post'){
					for(var i in respt){
						el['conRes'+i] = new Element('div', { 'class': this.options.contRes });
						var res=new Array();
						for(var j in respt[i]){
							res[j] = respt[i][j];
						}
						el['divTit'+i] = new Element('div', { 'class': 'titular' }); el['bTit'+i] = new Element('a', { 'href': res['link0'], 'html': '<b>'+res['titulo0']+'</b><br/>' });
						el['emTit'+i] = new Element('em', { 'html': res['fecha0'] }); el['bTit'+i].injectInside(el['divTit'+i]);
						el['emTit'+i].injectInside(el['divTit'+i]);	el['divTit'+i].injectInside(el['conRes'+i]); el['divBody'+i] = new Element('div', { 'class': 'body' });
						el['divImg'+i] = new Element('div', { 'class': 'img-noticia', 'html': '<img src="images/productos/noticias/'+res['imagen0']+'" title="'+res['titulo0']+'" alt="'+res['titulo0']+'" />' });
						el['divCos'+i] = new Element('div', { 'class': 'txt-noticia', 'html': res['cuerpo0'] }); el['divImg'+i].injectInside(el['divBody'+i]);
						el['divCos'+i].injectInside(el['divBody'+i]); el['divBody'+i].injectInside(el['conRes'+i]);
						el['divBoth'+i] = new Element('div', { 'class': 'both' }); el['divBoth'+i].injectInside(el['conRes'+i]);
						if(typeof res['titulo1']!="undefined"){
							el['divTit'+i] = new Element('div', { 'class': 'titular' }); el['bTit'+i] = new Element('a', { 'href': res['link1'], 'html': '<b>'+res['titulo1']+'</b><br/>' });
							el['emTit'+i] = new Element('em', { 'html': res['fecha1'] }); el['bTit'+i].injectInside(el['divTit'+i]);
							el['emTit'+i].injectInside(el['divTit'+i]); el['divTit'+i].injectInside(el['conRes'+i]);
							el['divBody'+i] = new Element('div', { 'class': 'body' });
							el['divImg'+i] = new Element('div', { 'class': 'img-noticia', 'html': '<img src="images/productos/noticias/'+res['imagen1']+'" title="'+res['titulo1']+'" alt="'+res['titulo1']+'" />' });
							el['divCos'+i] = new Element('div', { 'class': 'txt-noticia', 'html': res['cuerpo1'] });
							el['divImg'+i].injectInside(el['divBody'+i]); el['divCos'+i].injectInside(el['divBody'+i]);
							el['divBody'+i].injectInside(el['conRes'+i]);el['divBoth'+i] = new Element('div', { 'class': 'both' });
							el['divBoth'+i].injectInside(el['conRes'+i]);
						}el['conRes'+i].injectInside(this.op[0]);
					}
				}else{
					for(var i in respt){
						el['conRes'+i] = new Element('div', { 'class': this.options.contRes });
						var res=new Array();
						for(var j in respt[i]){ res[j] = respt[i][j]; }
						if(j=='error'){ el['divTit'+i] = new Element('div', { 'html': respt[i][j]}); el['divTit'+i].injectInside(el['conRes'+i]);
						}else{
							for(var k=0; k<(i.length)/3; k++){
								if(typeof res['nombre_'+k]!="undefined"){
									el['divRes'+k] = new Element('div', { 'class': 'buscCont'}); el['divResImg'+k] = new Element('div', { 'class': 'buscContImg'}); el['divResTxt'+k] = new Element('div', { 'class': 'buscContText'});
									//el['divName'+k] = new Element('a', { 'href': 'productos/'+res['tipo_'+k]+'/'+res['link_'+k],'html': '<b>'+res['nombre_'+k]+'</b>'});
									el['divName'+k] = new Element('a', { 'href': 'productos'+res['tipo_'+k]+'/'+res['link_'+k],'html': '<b>'+res['nombre_'+k]+'</b>'});
									el['divDesc'+k] = new Element('p', { 'html': res['descripcion_'+k]+'...'});
									el['divImg'+k] = new Element('img', { 'src': 'productos/'+res['marca_'+k]+'/'+res['imagen_'+k], 'width':'32px','height':'32px', 'alt': res['nombre_'+k] });
									el['divName'+k].injectInside(el['divResTxt'+k]);el['divDesc'+k].injectInside(el['divResTxt'+k]);
									el['divImg'+k].injectInside(el['divResImg'+k]);el['divResImg'+k].injectInside(el['divRes'+k]);
									el['divResTxt'+k].injectInside(el['divRes'+k]);el['divRes'+k].injectInside(el['conRes'+i]);
							}}
						}el['conRes'+i].injectInside(this.op[0]);
					}effectIn();
				}
			}.bind(this),
			onFailure: function(errors){
				if($(this.options.divError)) $(this.options.divError).destroy();
				if(errors.status==301) this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['301'] });
				else if(errors.status==302) this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['302'] });
				else if(errors.status==303) this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['303'] });
				else if(errors.status==401) this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['401'] });
				else if(errors.status==403) this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['403'] });
				else if(errors.status==404) this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['404'] });
				else if(errors.status==500) this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['500'] });
				else this.error = new Element('div', { 'id': this.options.divError, 'html': this.options.errors['unknown'] });
				if($(this.options.divLoad) && this.method!='post'){ $(this.options.divLoad).destroy();$(this.options.injError).empty();	}
				if($(this.options.injLoad2)) $(this.options.injLoad2).empty();
				if(this.method!='post')this.error.injectInside(this.options.injError);
				else this.error.injectInside(this.options.injError2);
			}.bind(this)
		}).send();
	},
});
