var entel = entel || {};
entel.footer = {
	
	/*
	 * init
	 */
	init: function() {
		var $ = function(id) { return document.getElementById(id); }
		
		//Enlaces Sociales
		if ($('footerRSS')) {
			$('footerRSS').onclick = function(){ // - RSS
				window.parent.location.href = 'http://www.entelpcs.cl/noticias/rss.asp';
			};
		}
		if ($('footerYouTube')) {
			$('footerYouTube').onclick = function(){// - YouTube
				window.parent.location.href = 'http://www.youtube.com/entelpcs';
			};
		}
		if ($('footerFacebook')) {
			$('footerFacebook').onclick = function(){// - Facebook
				window.parent.location.href = 'http://www.facebook.com/home.php?#/pages/Entel-PCS/48738614662?sid=2e64647ad345ff8ef778894587b52888&amp;ref=search';
			};
		}
		if ($('footerTwitter')) {
			$('footerTwitter').onclick = function(){// - Twitter
				window.parent.location.href = 'http://twitter.com/entel_pcs';
			};
		}
		
		//Compartir e Informacion
		//Comprobamos si existen los elementos
		if ($('btnCompartirPagina')) {
			
			//Despliege Caja: Compartir Pagina
			$('btnCompartirPagina').onmousedown = function(e){
				$('caja_info_compartir').style.display = 'none';
				
				var compartir = $('caja_compartir');
				if (compartir.style.display == 'none') {
					compartir.style.display = 'block';
					entel.footer.funciones.eventoCerrarCaja(compartir, 'btnCompartirPagina');
				}
				else {
					compartir.style.display = 'none';
				}
				return false;
			};
			
			//Despliegue Caja: "Que es esto?"
			$('btnCompartirPaginaInfo').onmousedown = function(e){
				$('caja_compartir').style.display = 'none';
				
				var info = $('caja_info_compartir');
				if (info.style.display == 'none') {
					info.style.display = 'block';
					entel.footer.funciones.eventoCerrarCaja(info, 'btnCompartirPaginaInfo');
				}
				else {
					info.style.display = 'none';
				}
				return false;
			};
			$('btnCompartirPaginaInfo').onclick = function(){
				return false;
			}
			
			//Tabs Caja Compartir
			$("compartir_tab").onclick = function(){
				entel.footer.funciones.removeClass($('compartir_tab'), 'seleccionado');
				entel.footer.funciones.removeClass($('marca_sociales_tab'), 'seleccionado');
				entel.footer.funciones.addClass(this, 'seleccionado');
				
				$("contenido_compartir").style.display = 'block';
				$("marcadores_sociales").style.display = 'none';
			};
			
			$("marca_sociales_tab").onclick = function(){
				entel.footer.funciones.removeClass($('compartir_tab'), 'seleccionado');
				entel.footer.funciones.removeClass($('marca_sociales_tab'), 'seleccionado');
				entel.footer.funciones.addClass(this, 'seleccionado');
				
				$("contenido_compartir").style.display = 'none';
				$("marcadores_sociales").style.display = 'block';
			};
			
			//Cancelar Formulario Compartir
			/*$('btnCompartirCancelar').onclick = function(){
				$('caja_compartir').style.display = 'none';
				return false;
			};*/
		}
	},
	
	/*
	 * Funciones
	 */
	funciones: {
		hasClass: function(ele,cls) {
			return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
		},

		addClass: function(ele, cls) {
			if(!this.hasClass(ele,cls)) ele.className += " "+cls;
		},
		
		removeClass: function(ele, cls) {
			if(this.hasClass(ele,cls)) {
				var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
				ele.className=ele.className.replace(reg,' ');
			}
		},
		
		isElement: function(t, el) {
			while(t.nodeType == 1) {
				if(t === el) return true;
				t = t.parentNode;
			}
			return false;
		},
		
		eventoCerrarCaja: function(caja, boton) {
			entel.footer.funciones.addEvent(document,'mousedown', function(evt) {		
				var e=evt?evt:window.event?window.event:null;
				var target=e.target?e.target:e.srcElement?e.srcElement:null;
				if(target.nodeType==3) target=target.parentNode; //Safari bug
				
				if(!entel.footer.funciones.isElement(target,caja) &&
				   !entel.footer.funciones.isElement(target,document.getElementById(boton))) {
						caja.style.display = 'none';
						entel.footer.funciones.removeEvent(document,'mousedown');
				}
				return false;
			});
		},
		
		addEvent: function(obj, evType, fn){
			if (obj.addEventListener) {
				obj.addEventListener(evType, fn, false);
				return true;
			} else {
				if (obj.attachEvent) {
					var r = obj.attachEvent("on" + evType, fn);
					return r;
				}
				else 
					return false;
			}
		},
		
		removeEvent: function(obj, evType, fn){
			if (obj.removeEventListener) {
				obj.removeEventListener(evType, fn, false);
				return true;
			} else {
				if (obj.detachEvent) {
					var r = obj.detachEvent("on" + evType);
					return r;
				} else return false;
			}
		}
	},
	
	/*
	 * AJAX
	 */
	ajax: {
		ajaxObj: function() {
			try {
				_ajaxobj = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					_ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					_ajaxobj = false;
				}
			}
		
			if (!_ajaxobj && typeof XMLHttpRequest!='undefined') {
				_ajaxobj = new XMLHttpRequest();
			}
		
			return _ajaxobj;
		},
		
		get: function(url, div) {
			ajax = new this.ajaxObj();
			ajax.open("GET", url, true);
		
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 1)
					document.getElementById(div).innerHTML = "<img src=img/preloader.gif> Cargando...";
						
				if (ajax.readyState == 4) {
					document.getElementById(div).innerHTML = ajax.responseText;
					entel.footer.init();
				}
			}
			ajax.send(null);
		}
	},
	
	/*
	 * DOM Onload
	 */
	cargar: {
		onload: function(func){
			entel.footer.funciones.addEvent(window, 'load', func);
		}
	}
};

entel.footer.init();
