/* Librería Global Front End
 * 
 * Inicia los efectos y contiene la funcion para getear noticias y validar el formulario de ingreso.
 **/

var Vgl = {

	start: function(){
		Vgl.mostrar('#menu li','opacity','0');
		Vgl.mostrar('#lateral li','opacity','0');
	},

	mostrar: function(selector,propiedad,valor){
		var timer = 0;
		var sideblocks = $$(selector);
		var slidefxs = [];
		
		sideblocks.each(function(el, i)
		{
			el.setStyle(propiedad, valor);
			timer += 150;
			if(selector == "#menu li") {
				slidefxs[i] = new Fx.Style(el, propiedad, { duration: 200, transition: Fx.Transitions.backOut, wait: false });
			} 
			else slidefxs[i] = new Fx.Style(el, propiedad, { duration: 200, wait: false });
			slidefxs[i].start.delay(timer, slidefxs[i], 1);
		}, this);
	},
	
	validarFormulario: function(form){
		var div_errores = 'fm_errores';
		var errores = [];
		
		document.getElementById(div_errores).innerHTML = '';
		
		if(!form.login.value) errores.push('No ingreso el usuario');
		if(!form.password.value) errores.push('No ingreso la contrasena');
		
		if(errores.length == 0) {
			form.submit.disabled = true;
			form.submit.value = 'Cargando'

			/*var req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
			req.open('POST', 'test.php', false);
			req.onreadystatechange = procesarLogin;
			req.send();*/
			
			return true;
			
		} else {
			var list = document.createElement('ul');
			
			for(var i=0;i<errores.length;i++) {
				var error = document.createElement('li');
				error.innerHTML = errores[i];
				
				list.appendChild(error);
			}
			document.getElementById(div_errores).appendChild(list);
		}
		
		return false;	
	},
	
	noticiasDigi: function(){
		var req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
		req.open('GET', 'http://www.vgl.cl/wp-content/themes/vgl/inc/digidesign_feed.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
		req.onreadystatechange = function() {
			if(req.readyState == 4) {
				if(req.status == 200) {
					json_object = eval('(' + req.responseText + ')');
					document.getElementById('noticias-aj-news').innerHTML = '';
					document.getElementById('noticias-aj-featured').innerHTML = json_object.featured;
						
					for(var i=0; i<json_object.news.length; i++) {
						var noticia = json_object.news[i];
						var li = document.createElement('li');
						li.innerHTML = noticia;
						
						document.getElementById('noticias-aj-news').appendChild(li);
					}
				} else {
					if(debug) console.log(req);
					alert('Hubo un error al intentar comunicarse con el servidor, por favor intentelo mas tarde.');
				}			
			} 
		}
				
		req.send(null);
	},

	noticiasAvid: function(){
		var req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
		req.open('GET', 'http://190.161.148.83:81/wp-content/themes/vgl/inc/avid_feed.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
		req.onreadystatechange = function() {
			if(req.readyState == 4) {
				if(req.status == 200) {
					json_object = eval('(' + req.responseText + ')');
					document.getElementById('noticias-aj-news').innerHTML = '';
					document.getElementById('noticias-aj-featured').innerHTML = json_object.featured;
						
					for(var i=0; i<json_object.news.length; i++) {
						var noticia = json_object.news[i];
						var li = document.createElement('li');
						li.innerHTML = noticia;
						
						document.getElementById('noticias-aj-news').appendChild(li);
					}
				} else {
					if(Vgl.debug) console.log(req);
					alert('Hubo un error al intentar comunicarse con el servidor, por favor intentelo mas tarde.');
				}			
			} 
		}
				
		req.send();
	}
}

Element.extend({
	visible: function() {
	return this.getStyle('display') != 'none';
	},
	toggle: function() {
		return this[this.visible() ? 'hide' : 'show']();
	},
	hide: function() {
		this.originalDisplay = this.getStyle('display');
		this.setStyle('display','none');
		return this;
	},
	show: function(display) {
		this.setStyle('display',(display || this.originalDisplay || 'block'));
		return this;
	},
	find: function(what) {
		var element = this[what];
		while (element.nodeType != 1) element = element[what];
		return element;
	},
	fadeIn: function(duracion){
		if(!duracion) duracion = 1000;
		if(this.getStyle('display') == 'none') this.setStyles({'opacity':'0','display':'block'});
		this.effect('opacity',{duration: duracion}).start(0,1);
	},
	fadeOut: function(duracion){
		if(this.getStyle('display') == 'none' || this.getStyle('opacity') == '0') return;
		if(!duracion) duracion = 1000;
		this.effect('opacity',{duration: duracion}).start(1,0);
		function ff(){ this.setStyle('display','none'); };
		ff.delay(duracion, this);
	}
});