//
// Javascript functions for the ef-radiant CMS.
//

// Slideshow function
// Based on 
// http://wiki.script.aculo.us/scriptaculous/show/Build+your+own+Diaporama+whit+cool+scriptaculoius+effects

function slideShow (num)
{
   new Effect.Fade('img'+id_current, {duration: 1});
   if (id_current < (num-1)) id_current++;
   else id_current = 0;
   new Effect.Appear('img'+id_current, {duration: 1, delay: 1});

 window.setTimeout("slideShow("+num+")",5000);
}

//
// Functions used by link_to_remote 
//
function setActiveLItem(link) {
  var element = $(link).parentNode;
  changeMenuClass(element, "active", "passive");
  changeElementClass(element, "passive", "active")     
}

function setLItemAndClearProduct(link) {
   setActiveLItem(link);
   $("product").innerHTML = "<span class=\"info\">Leyendo los datos de los productos ...</span>"
}


//
// Hide/show menus (uses prototype.js and bejaviour.js)
//

function changeElementClass(node, old_class, new_class) {
  if (Element.hasClassName(node, old_class)) {
    Element.removeClassName(node, old_class);
    Element.addClassName(node, new_class);
  }
}


//
// Left-side navigation
// By default the submenus are hidden. 
// If we are accessing some internal page, the corresponding submenu is shown.
// The specific behaviour rules are given in other Javascript file which depends on
// the specific type of left menu. See left_menu_dropdown.js as example.

function changeMenuClass(element, old_class, new_class) {
  var nodes = $A(element.parentNode.childNodes);
  nodes.each(function(node){
    if (node.nodeType == 1) {
    if (Element.hasClassName(node, old_class)) {
      Element.removeClassName(node, old_class);
      Element.addClassName(node, new_class);
    }
   }
  });
}

//
// Links to external webs. The target attribute is not supported by XHTML Strict,
// so the rel attribute + some Javascirpt is used.
// http://www.sitepoint.com/article/standards-compliant-world
//


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";
 }
}
// window.onload = externalLinks;
