var enableDebug = false;
var carrera, hidecount;
function debug(que, reset) {
                if(!enableDebug) return false;
                var debug = document.getElementById('debug');
                if(!debug){
                        debug = document.createElement('pre');
                        debug.setAttribute('id', 'debug');
                        debug.setAttribute('style', 'wdith: 85%; text-align: center; margin: 3px auto%; background: #eee; max-height: 100px; overflow: auto')
                        debug.style.maxHeight = '100px';
                        debug.style.overflow = 'auto';
                        document.body.insertBefore(debug, document.body.firstChild);
                }
                if(reset == true) debug.innerHTML = que;
                else debug.innerHTML = que + '<br />' + debug.innerHTML;
                return false;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function tooltip(id, el, what){
    if(window.hidecount) window.clearTimeout(hidecount);
    var tool = document.getElementById('tooltip');
    if(!tool){
            tool = document.createElement('div');
            tool.setAttribute('id', 'tooltip');
            tool.style.display = 'none';
            document.body.appendChild(tool);
    }
    iframe = $('iefix');
    pos = findPos(el);
//    alert(carrera[id]);
    tool.style.position= 'absolute';
//    tool.style.left = pos[0] + (el.offsetWidth/3) + 'px';
    tool.style.left = pos[0] + 80 + 'px';
    tool.style.top = pos[1] + 'px';
		    frfixw = 85;
		    frfixh = 7;
	switch(what){
		case 'carrera':
		default:
		    tool.innerHTML = carrera[id];
		    tool.style.display = 'block';
		    tool.style.visibility = 'hidden';
		    tool.style.left = pos[0] - (tool.offsetWidth + 15) + 'px';
		    frfixw = 0;
		    frfixh = 0;
			tool.style.display = 'none';
		    tool.style.visibility = 'visible';
		break;
		case 'campus':
 		    tool.innerHTML = Campus[id];
/* 		    tool.innerHTML = "Aqui va la info del campus"; */
		break;
		case 'fechas':
 		    tool.innerHTML = Fecha[id];
/* 		    tool.innerHTML = "Aqui va la info del campus"; */
		break;

	}
	

    
    tool.onmouseover = function(){
        if(hidecount)
            window.clearTimeout(hidecount)    
    }
    tool.onmouseout = prehidetool;
    el.onmouseout = prehidetool;

    tool.style.display = 'block';
    if(!!(window.attachEvent && !window.opera)){//Es Explorer
        pos = findPos(tool);
	    tool.style.visibility = 'hidden';
	    iframe.style.position= 'absolute';
	    iframe.style.left = pos[0] + frfixw + 'px';
    	iframe.style.top = pos[1] + frfixh + 'px';
		iframe.style.zIndex = '499';
		iframe.style.height = tool.offsetHeight + 'px';
		iframe.style.width = tool.offsetWidth + 'px';
	    tool.style.visibility = '';
    	iframe.style.display = 'block';
    }
}

function prehidetool(){
    window.hidecount = window.setTimeout('hidetool()', 2000);
}

function hidetool(){
    var tool = document.getElementById('tooltip');
    if(!tool) return false;
//    Effect.Fade(tool, {queue: 'end'})
    tool.style.display = 'none';
    var iframe = document.getElementById('iefix');
    if(!iframe) return false;
    iframe.style.display = 'none';    
}


function mandar(){
    usname = find('usnombre', 'text');
    mail = find('correo', 'text');
    phone = find('telefono', 'text');
/*    registro.nombre = name.value;
    registro.email = mail.value;
    registro.telefono = phone.value;*/
    registro.flashEnvia(usname.value, phone.value, mail.value);
    return false;
}

function select_carrera(carrera, titulo){
    checked = find('nivel', 'radio');
    debug('Radio ' + checked, true);
    if(!checked)return false;
    if(titulo != checked.value){
        input = find('nivel', 'radio', titulo);
        debug(input)
        input.checked = 'checked';
        change_carrera_select(titulo);
    }
    option = find('carrera', 'select', carrera);
    debug('option ' + option);
    option.selected='selected';
    forma = document.forms[0];
    new Effect.Highlight(forma);
    registro.carrera = carrera;
    return false;
}

function change_carrera_select(nivel){
    select = find('carrera', 'select');
    nivelul = $('nivel_'+nivel);
    debug(nivelul);
    as = nivelul.getElementsByTagName('a');
    num = as.length;
    opts = select.options;
    optnum = opts.length;
    for(i=optnum-1; i>0; i--){
        node = opts[i];
        debug(node + ' ' +i + ' ' +optnum);
        if(node)
            select.removeChild(node);
    }
    for(i = 0; i<num; i++){
        debug(as[i].name + ' ' + as[i].innerHTML);
        el = as[i];
        opt = document.createElement('option');
        opt.name = 'carrera';
        opt.value = el.name;
        opt.innerHTML = el.innerHTML;
/*        if(sel && sel == el.name)
            opt.selected = 'selected';*/
        select.appendChild(opt);
    }
    return true;
}

function find(que, tipo, value){
    if(tipo == 'select'){
                elements = document.getElementsByTagName('select');
                for(i = 0; i < elements.length; i++){
                        elemento = elements[i];
                        debug('name = ' +elemento.name);
                        if(elemento.name == que){
                                debug('encontre a elemento ');
                //                debug('Lo encontre');
                                if(!value)
                                                return elemento;
                                opts = elemento.options;
                                debug(opts);
                                for(i == 0; i < opts.length; i++){
                                                option = opts[i];
                                                if(option.value == value) return option;
                                }
                            }    
                        }
                return false;
    }
    elements = document.getElementsByTagName('input');
//    debug(elements);
    var i;
    for(i = 0; i < elements.length; i++){
        elemento = elements[i];
//        debug(elemento.type + ' ' + elemento.name);
        if(elemento.type == tipo && elemento.name == que){
                debug('encontre a elemento ' + elemento.type);
//                debug('Lo encontre');
            if(tipo == 'radio'){
//                debug('es radio ' + elemento.checked);
                if(elemento.checked == true && !value){
//                                debug(elemento);
                                return elemento;
                }
                if(elemento.value == value) return elemento;
            }else{
                if(!value)
                                return elemento;
                if(elemento.type == 'select'){
//                                debug('es select');
                                opts = elemento.options;
//                                debug(opts);
                                for(i == 0; i < opts.length; i++){
                                                option = opts[i];
                                                if(option.value == value) return option;
                                }
                }
            }    
        }
    }
    return false;
}
function change_campus(campus){
                el =  find('nivel', 'radio');
//                debug(campus + ' '+ el);
                url = "phpajax/handler.php";
                pars = "idcampus="+campus+"&nivel="+el.value;
                option = find('campus_select', 'select', campus);
                debug(option);
                option.selected = true;
                var myAjax = new Ajax.Request(
                                url, 
                                {
                                        method: 'get', 
                                        parameters: pars, 
                                        onComplete: updateCampus
                                }
                );
                registro.campus = campus;
}
var html;
function updateCampus(req){
                xml = req.responseXML;
                data = new Array();
                data.push(
                          new Array('info', $('datos_campus')),
                          new Array('campus', $('lista_campus')),
                          new Array('forma', $('forma_dinamica')),
                          new Array('carreras', $('carreras'))
                );
                
                for(i = 0; i < data.length; i++){
                                debug(i);
                                //debug(data);
                                element = data[i][1];
                                html = get_element(data[i][0]);
                                //alert(html.length);
                                debug(element + ' ' +element.id);
                                element.newData = html;
                                debug(html);
                                new Effect.Fade(
                                                element,
                                                {
                                                                to: 0.01,
                                                                afterFinish: function(ef){
                                                                                ef.element.innerHTML = ef.element.newData;
                                                                                new Effect.Appear(ef.element);
                                                                }          
                                                }
                                );
                }
                
                js = get_element('script');
//                debug(js);
                eval(js);
                
}

function get_element(name){
                els = xml.getElementsByTagName(name);
                if(!els) return false;
                el = els[0];
 /*               if(!el.hasChildNodes) return false;
                return el.childNodes[1].nodeValue+"";
                return " ";*/
                return dameNodo(el);
}

function dameNodo(nodo) {
	
	var pruebaNodo = nodo.childNodes;
	
	if (pruebaNodo.length != 0 ) {
                                        if(nodo.childNodes[0].nodeValue.length > 3)
                                                return nodo.childNodes[0].nodeValue;
                                        else
                                                return nodo.childNodes[1].nodeValue;
				} else {
                                        debug('nada nait');
					return " ";
				}
	
}

function servs_tooltip(el){;
	donthideservs();
	var hide = false;
	var pos = findPos(el);
	left = pos[0];
	eltop = pos[1];
	lawidth = el.offsetWidth;
	laheight = el.offsetHeight;

	div = $('servtool');
//	div = $('tooltip');
//	div.innerHTML = "<img src='http://www.unioportunidades.com.mx/admin/images/iconos_servicios/greenorbcopy.png' /><br /><p>Bleh bleh bleh bleh bleh bleh bleh bleh bleh bleh bleh bleh bleh bleh bleh bleh</p>";
	str = el.id;
	str = str.replace('sub_', '');
	num = new Number(str);
	if(isNaN(num)) return debug("es NaN " + str);
	div.innerHTML = servstool[num];
	div.style.position = 'absolute';
//	div.style.left = (left - width/2) + 'px';
//	div.style.top = (eltop + height) + 'px';
	if(div.offsetWidth <= 2){
		div.style.visibility = 'hidden';
		div.style.display = 'block';
		var hide = true;
	}
	w = div.offsetWidth/2;
	if(hide){
		div.style.display = 'none';
		div.style.visibility = 'visible';	
	}
	lft = (left + lawidth/2 -w);
	tp = (eltop + laheight + 8);
	div.style.left = lft + 'px';
	div.style.top = tp + 'px';
	
	shadow = document.createElement('div');
	shadow.style.top = (tp + 3) + 'px';
	shadow.style.left = (lft + 3) + 'px';
	shadow.style.background = 'black';
//	shadow.style.display = 'none';
	shadow.style.position = "absolute";
	document.body.appendChild(shadow);
	new Effect.Appear(div, {duration: 0.4});
	new Effect.Appear(shadow, {duration: 0.4, to: 0.5});
//	new Effect.Move (div,{ x: (left + lawidth/2 -w), y: (eltop + laheight), mode: 'absolute'});
	div.onmouseover = donthideservs;
	div.onmouseout = prehideservs;
	el.onmouseout = prehideservs;
//	myLightbox.initialize();
}

function donthideservs(){
    if(window.servscount) window.clearTimeout(servscount);
}

function prehideservs(){
    window.servscount = window.setTimeout('hideservs()', 800);
}

function hideservs(){
    var tool = document.getElementById('servtool');
    if(!tool) return false;
//    Effect.Fade(tool, {queue: 'end'})
    tool.style.display = 'none';
}

function servicio_detalles(id){
	over = $('overlay');
	div = $('servicio_detalle');
	var arrayPageSize = getPageSize();
	over.style.top = '0px';
	over.style.left = '0px';
	over.style.width = '100%';
	over.style.height = '100%';
	over.onclick = hide_detalles;
	new Effect.Appear(over, {to: 0.7, duration: 0.2});
	
	div.style.top = "15%";
/*	div.style.left = "20%";
/*	div.style.height = "70%";
	div.style.width = "60%";*/
	div.style.left = (arrayPageSize[0]/2) - 300 + 'px';
/*	div.style.height = "490px";*/
	div.style.width = "600px";

	new Effect.Appear(div, {queue: 'end', duration: 0.2});
	
	div.innerHTML = "<div class='closebtn'><a href='javascript:void(0)' onclick='hide_detalles()'><img src='http://www.unioportunidades.com.mx/universidad/2001/0/imgs/close.jpg' /></a></div>" + detalle[id];
}

function hide_detalles(){
	over = $('overlay');
	div = $('servicio_detalle');
	new Effect.Fade(over, {duration: 0.5});
	new Effect.Fade(div, {queue: 'end', duration: 0.5});
}

function campus_tooltip(id){
}