/*
   application.js
   themes
   
   Created by read: Invalid Path on 2007-02-01.
   Copyright 2007 Infonova Informática e Comunicacions, S,L.. All rights reserved.
*/
function blank(srt) {
  return /^\s*$/.test(srt);
}

function open_close(id){
  element = document.getElementById(id);
  if(element.style.display == "none"){
    Effect.BlindDown(id);
  }else{
    Effect.BlindUp(id);
  }
}

var CurrentEffect = true;

function view_cap(id, active_link)
{
  if(CurrentEffect){
  closeOpenCap("sub_"+id);
  if($("sub_"+id).style.display == "none")
  {
    Effect.BlindDown("sub_"+id);
  }

  setCurrentCap(id, active_link);
  closesubMenus(active_link);
  }
}

function closeOpenCap(current_id)
{
  var i, a;
  var element = $('unidad');

  for(i=0; (a = element.getElementsByTagName("div")[i]); i++) {
    var id = a.id;
    if(current_id != id){
      if(id.match(/sub_/)){
        if(a.style.display != "none"){
          CurrentEffect = false;
          Effect.BlindUp(id, {afterFinish: myCallBackOnFinish});
        }
      }
    }
  }

  return null;
}

function closesubMenus(current_link){
  var i, a;
  var element = $('wrapper-cap');

  for(i=0; (a = element.getElementsByClassName("subMenu")[i]); i++) {
    if(a.style.display != "none" && a.id !="sub_Cap_"+current_link ){
      CurrentEffect = false;
      Effect.BlindUp(a.id, {afterFinish: myCallBackOnFinish});
    }
  }
}

function setCurrentCap(id, active_link){
  var i, item;
  var nav  = $('wrapper-cap');
  var element = $('link_'+active_link);

  for(i = 0; (item = nav.getElementsByTagName("li")[i]); i++) {
    //item.setStyle({color: '#677abd', textDecoration: 'none'});
    item.style.color = "#677abd";
    Element.removeClassName(item, 'active');
  }

  if(element){
    element.style.color = "#900";
    Element.addClassName(element, 'active');
    //element.setStyle({color: '#900'});
  }

  if($("subCap_"+id)){
    $("subCap_"+id).style.color = "#900";
    $("subCap_"+id).addClassName('active');
    //$("subCap_"+id).setStyle({textDecoration: 'underline', color: '#900'});
  }
}

Event.observe(window, 'load', page_loaded, false);

function page_loaded(evt) {
  if($('gl'))
    Event.observe('gl', 'click', item_clicked, false);
  if($('es'))
    Event.observe('es', 'click', item_clicked, false);
  if($('en'))
    Event.observe('en', 'click', item_clicked, false);
}

function item_clicked(evt){
  var link    = Event.element(evt);
  var wrapper = $('wrapper-cap');
  var a       = "";

  if(link.href.match(/unidad/)){
    var active    = $('wrapper').getElementsByClassName('active').first();
    var subActive = $('wrapper').getElementsByClassName('active').last();

    a += "&a="+ active.id.match(/[0-9]{1,2}$/);
    a += "&sa="+ subActive.id.match(/[0-9]{1,2}$/);

    document.location = link.href + a;
    Event.stop(evt);
  }

}

function myCallBackOnFinish(obj){
  CurrentEffect = true;
}

/*______________________________________________________________________________________________*/
/*Añade el formulario de autenticación en la posición que queremos*/
function commentAuth(url, element){
  new Ajax.Request(url, {
    method: 'get',
    onSuccess: function(transport) {
        new Insertion.Top(element, transport.responseText)
    }
  });
}

/*______________________________________________________________________________________________*/
/*Envia a la página de filtrado por fecha*/
function entriesFilter(){
  var date = $F('date');
  var cat  = $F('categories')
  var options = "";

  if(!blank(date)){
    options += "date="+date;
  }

  if(!blank(cat)){
    options += (blank(options) ? "" : "&")+"cat="+cat;
  }

  document.location = $F('action') + (blank(options) ? "" : "?") + options
}

function monthLocate(locale, date){
  var months = [];
  var dateObject = new Date();
  var year  = dateObject.getFullYear();
  var month = dateObject.getMonth();
  var htmlMonts = "<select id=\"date\" name=\"date\"><option value=\"\">&nbsp;</option>";

  switch (locale) {
     case "en":
      months =  ["January", "February", "March" , "April", "May", "Juni", "July", "August", "September", "October", "November", "December"];
        break;
     case "es":
      months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
        break;
     default:
      months = ["Xaneiro", "Febreiro", "Marzo", "Abril", "Maio", "Xuño", "Xulio", "Agosto", "Septembro", "Outubre",  "Novembro", "Decembro"];
        break;
  }

    for(var i = 0; i < 10; i++){

      if(month < 0){
        month = 11;
        year -= 1;
      }

      valueMonth = month+1;
      if(valueMonth < 10){
        valueMonth = '0'+valueMonth;
      }

      value = year+''+valueMonth;
      selected = '';

      if(value == date){
        selected = 'selected=\"selected\"';
      }

      htmlMonts += '<option '+selected+' value=\"'+value+'\"> '+months[month]+' '+ year+'</option>';

      month -=1;
    }

    htmlMonts += "</select>";

    new Insertion.After('action', htmlMonts);
}

/*______________________________________________________________________________________________*/
/*Presentacion de las imágenes*/
var PresentationShow = Class.create();
 PresentationShow.prototype = {
 initialize: function(element, options) {
   this.element = $(element);
   this.options = Object.extend({className: 'presentation', duration: 3}, options);
   this.presentations = $$("."+this.options.className);

   this.preparePresentation();
   this.registerCallback();
 },

 preparePresentation: function() {
   this.currentPresentation = this.presentations.first();
   this.element.style.position = 'relative';

  this.element.style.height = this.presentations.max(function(presentation) {
    var visible = Element.visible(presentation), height;

    if (!visible) {
      Element.setStyle(presentation, {position: 'absolute', left: '15%'});
      Element.show(presentation);
    }

    height = Element.getHeight(presentation);
    if (!visible) Element.hide(presentation);

    return height;
  }).toString() + 'px';
 },

 nextPresentation: function() {
   return this.presentations[(this.presentations.indexOf(this.currentPresentation) + 1) % this.presentations.length];
 },

 registerCallback: function() {
   window.setTimeout(this.tick.bind(this), this.options.duration * 1000);
 },

 tick: function() {
   var currentPresentation = this.currentPresentation, nextPresentation = this.nextPresentation();

   new Effect.Parallel([
     new Effect.Fade(currentPresentation, {sync: true}),
     new Effect.Appear(nextPresentation, {sync: true})
   ], {
     duration: 2,
     afterFinish: (function(effect) {
       this.currentPresentation = nextPresentation;
       this.registerCallback();
     }).bind(this)
   })
 }
}

/*______________________________________________________________________________________________*/ 
/*Formularios roller*/
function fixURL(theForm) {
    if (theForm.url.value != "" &&
        theForm.url.value.indexOf("http://") == -1) { //prepend http://
        theForm.url.value = "http://"+theForm.url.value;
    }
    saveUserInformation(theForm);
}

function saveUserInformation(theForm) {
    if (theForm.rememberInfo.checked) {
        rememberUser(theForm);
    } else {
        forgetUser(theForm);
    }
}