function $(id) {
	return document.getElementById(id);
}
function stopEvent(evt)
{
    var e = evt ? evt : window.event;
    if(e.preventDefault)
        e.preventDefault();
	else
        e.returnValue = false;

	if(e.stopPropagation)
        e.stopPropagation();
	else
        e.cancelBubble = true;
}

function addEvent( el, evType, handle )
{
	if( el.addEventListener ) el.addEventListener( evType, handle, false );
	else if( el.attachEvent )
	{
		el["e" + evType + handle] = handle;
		el[evType + handle] = function( )
		{
			el["e" + evType + handle](window.event)
		}
		el.attachEvent("on" + evType, el[evType + handle]);
    }
} 

function _removeEvent( el, evType, handle )
{
	if( el.removeEventListener ) el.removeEventListener( evType, handle, false );
	else if( el.detachEvent )
	{
		el.detachEvent( "on" + evType, el[evType + handle] )
		el[evType + handle] = null
		el["e" + evType + handle] = null;
	}
} 
function _externalLinks() 
{
	if (!document.getElementsByTagName) 
		return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i < anchors.length; i++) 
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}
function $random(min, max)
{
	return Math.floor(Math.random() * (max - min + 1) + min);
}

function StringToXML(text)
{
	if (window.ActiveXObject)
	{
		var doc = new ActiveXObject('Microsoft.XMLDOM');
		doc.async = 'false';
		doc.loadXML(text);
	}
	else
	{
		var parser = new DOMParser();
		var doc = parser.parseFromString(text,'text/xml');
	}
	return doc;
}

function in_array( item, obj ) 
{
	for ( var i = 0; i < obj.length; i++ )
	{
		if ( obj[i].innerHTML.indexOf(item) > 0 )
		{
			return true;
		}
	}
	return false;
}


var net = new Object();
net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;

net.CargadorContenidos = function(url, funcion, funcionError)
{
	this.url = url;
	this.req = null;
	this.onload = funcion;
	this.onerror = (funcionError) ? funcionError : this.defaultError;
	this.cargaContenidoXML(url);
}
net.CargadorContenidos.prototype = {
	cargaContenidoXML: function(url) 
	{
		if (window.XMLHttpRequest)
		{
			this.req = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(this.req)
		{
			try
			{
				var loader = this;
				this.req.onreadystatechange = function() 
				{
					loader.onReadyState.call(loader);
				}
				this.req.open('GET', url, true);
				this.req.send(null);
			} 
			catch (err) 
			{
				this.onerror.call(this);
			}
		}
	},
	onReadyState: function()
	{
		var req = this.req;
		var ready = req.readyState;
		if (ready == net.READY_STATE_COMPLETE)
		{
			var httpStatus = req.status;
			if ( httpStatus == 200 || httpStatus == 0 )
			{
				this.onload.call(this);
			}
			else
			{
				this.onerror.call(this);
			}
		}
	},

	defaultError: function()
	{
		alert("Se ha producido un error al obtener los datos"
		+ "\n\nreadyState:" + this.req.readyState
		+ "\nstatus: " + this.req.status
		+ "\nheaders: " + this.req.getAllResponseHeaders());
	}
}
function cargaContenidos( url, callback ) 
{
	var cargador = new net.CargadorContenidos(url, callback );
}

function StartList()
{
	if ( $("menu") )
	{
		if ( document.all && document.getElementById ) 
		{
			navRoot = $("menu");
			for (i=0; i<navRoot.childNodes.length; i++) 
			{
				node = navRoot.childNodes[i];
				if ( node.nodeName == "LI" ) 
				{
					node.onmouseover = function()
					{
						this.className += " over";
					}
					node.onmouseout = function() 
					{
						this.className=this.className.replace(" over", "");
					}
				}
			}
		 }
	}
}


var Site = {
	start: function()
	{
		if ($('logo')) Swf.banner("images/logo.swf", "logo", "377", "107", "#991F36" );

		lista_banner = ['banner_facs', 'banner_face', 'banner_faat', 'banner_facsjh'];
		bn_file = lista_banner[$random(0,3)];
		if ($('banner')) Swf.banner("images/" + bn_file + ".swf", "banner", "557", "167", "#E5E5E5" );
		if ($('i-news')) Swf.banner("images/news.swf", "i-news", "338", "159", "#FFFFFF" );

		if ($('lst-carreras'))
		{
			addEvent($('lst-carreras'), 'change', function()												  
			{
				if ( $('lst-carreras').value != 0 ) location.href= $('lst-carreras').value + '.php';
			});			
		}
		if ( $('menu') )
		{
			el = $("menu").getElementsByTagName('a');
			for ( i = 0; i < el.length; i++ ) 
			{
				if ( el[i].getAttribute('href').indexOf('#') != -1 )
				{
					addEvent(el[i], 'click', function(e)
					{
						stopEvent(e);
					});			
				}
			}
		}

		if ($('btn-atras'))
		{
			addEvent($('btn-atras'), 'click', function(e)												  
			{
				stopEvent(e);
				window.history.back();
			});			
		}
		if ( $('selector-calendario') )
		{
		  Calendar.setup({
			inputField: "fecha",
			ifFormat:   "%d/%m/%Y",
			button:     "selector-calendario"
		  });		
		}		
		_externalLinks();
	}
};

var Swf = {
	banner: function( swf, target, w, h, c )
	{
		var oSWF = new SWFObject( swf, "swf" + target, w, h, "7", c );
		oSWF.useExpressInstall('images/expressinstall.swf');
		oSWF.addParam("scale", "noscale");
		oSWF.addParam("wmode", "transparent");
		oSWF.write(target);		
	}
}
addEvent(window, 'load', Site.start);
addEvent(window, 'load', StartList);

