//crea un arbol de nodos para abrir/cerrar a partir de la ruta a los nodos (li) de una lista (ul)
function prepararArbol(ruta_hijos){
	$$(ruta_hijos).each(function(p){
		p.select('ul').each(function(q){
			q.hide();
		});
		if(p.childElements()[1] != undefined && p.childElements()[1].tagName == 'UL'){
			var tagBoton = '<a class="boton_nodo"><span>+</span></a> ';
			p.insert({top: tagBoton});
			p.select('a.boton_nodo').each(function(r){
				r.onclick = function(){
					var interior = '<span>+</span>';
					this.removeClassName('abierto');
					if (!this.adjacent('ul')[0].visible()){
						interior = '<span>-</span>';
						this.addClassName('abierto');
					}
					this.update(interior);
					this.adjacent('ul').each(function(s){
						Effect.toggle(s,'blind',{duration: 0.3});
					});
				}
			});
		}
		
	});	
}

/**
 * @author ipacheco
 */
navHover = function() {

	var lis = document.getElementById("menu").getElementsByTagName("li");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			//alert('lala:' + this.className)
			this.className+=" over";
		}
		lis[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" over\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", navHover);


// copied from http://www.quirksmode.org/js/cookies.html
// original implementation from http://www.scottandrew.com/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*
* This file contains code to create login in all special domains of ICV
*/

var total_seconds = 0;
var special_domain_requests_counter = 0;

/* 
* Creates cookies for each special domain and wait until each iframe has been loaded with response code
*/
function create_all_special_sessions()
{
  exists_special_forms = false;
  for(var i = 1; (i<= num_special_domains && num_special_domains > 0 ); i++)
  {
    if($('special_form_') + i)
    {
      exists_special_forms = true;
      $('special_form_' + i).login_special.value    = $('session_icv').login.value;
      $('special_form_' + i).password_special.value = $('session_icv').password.value;
      $('special_form_' + i).submit();
    }
  }

  if(exists_special_forms)
  {
    pexecuter_on_iframes = new PeriodicalExecuter(check_sessions_and_submit_to_icv, 1);
  }
}

/* 
* Send petitions for destroy cookies for each special domain
*/
function destroy_all_special_sessions()
{
  exists_destroy_special_forms = false;
  for(var i = 1; (i<= num_special_domains && num_special_domains > 0); i++)
  {
     if($('destroy_special_form_' + i))
     {
       exists_destroy_special_forms = true;
       $('destroy_special_form_' + i).submit();
     }
  }
  if(exists_destroy_special_forms)
  {
    pexecuter_on_iframes = new PeriodicalExecuter(check_sessions_and_submit_to_icv, 1);
  }
}

/*
* Checks if have passed 12 seconds since user clicked on login button or special domain cookies 
* have been created. Then submit request login to ICV domain
*/
function check_sessions_and_submit_to_icv(){
  total_seconds++;

  if ( special_domain_requests_counter >= num_special_domains || total_seconds >= 12  || num_special_domains == 0){
    /* all iframes for special domains has been filled with response of special sessions controller */
    pexecuter_on_iframes.stop();
    total_seconds = 0;
    if ($('session_icv'))
    {
      window.document.title += "login"
      $('session_icv').submit();
    }
    else
    {
      window.document.title += "logout"
      $('close_icv_session').submit();
    }
  }
}

function addEvent(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; 
 } 
}

Event.observe(window, "load", function(){
  /* User isn't logged in yet */
  if($('session_icv'))
  {
    /* When 'Enter' key is pressed in password field... */
    addEvent($('password'), 'keypress', function(event){ 
      if(event.keyCode==13){
        create_all_special_sessions();
      }
    });

    for(var i = 1; (i<= num_special_domains && num_special_domains > 0); i++)
    {
       if($('special_iframe_' + i))
       {
         addEvent($("special_iframe_" + i), 'load', function(){  
           special_domain_requests_counter++; 
           window.document.title += ".";
         });        
       }
    }
  }

  /* User is logged in */
  else{
    for(var i = 1; (i<= num_special_domains && num_special_domains > 0); i++)
    {
       if($('destroy_special_iframe' + i))
       {
         addEvent($('destroy_special_iframe_' + i), 'load', function(){  
           special_domain_requests_counter++; 
           window.document.title += ".";
         });        
       }
    }
  }
});



var Popup = {
              open: function(options)
              {
                this.options = {
                  url: '#',
                  width: screen.width,
                  height: screen.height,
                  name:"_blank",
                  location:"no",
                  menubar:"no",
                  toolbar:"no",
                  status:"no",
                  scrollbars:"no",
                  resizable:"no",
                  left:"",
                  top:""
                }
                Object.extend(this.options, options || {});

                this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
                this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
                var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
                if (this.options.top!="")openoptions+=",top="+this.options.top;
                if (this.options.left!="")openoptions+=",left="+this.options.left;
                window.open(this.options.url, this.options.name,openoptions );
                return false;
              }
            }