/**
 * makeElementComplex
 * @path : string, XPath like with element identifiers and tag name, dot separated
 * @pattern : string, content target replacement, use %S to retreive actual content of target
 **/
function makeElementComplex(path, pattern) {
    var container = document;
    if (path.indexOf('.')!=-1) {
        var items = path.split('.');
        var tag2search = items[items.length-1];
        for(var i=0; i<items.length-1; i++) {
            container = container.getElementById(items[i]);
        }
    } else
        tag2search = path;
    var items = container.getElementsByTagName(tag2search);
    for (var i=0; i<items.length; i++) {
        items[i].innerHTML = pattern.replace("%S", items[i].innerHTML);
    }
}

    /**
     * Toggle visibility status of an identified element
     * @id : string, target identifier
     **/
    function toggleVisible(id) {
        var item = document.getElementById(id);
        if ( item ) {
            if (!item.style.display || item.style.display =='block')
                item.style.display= 'none';
            else
                item.style.display= 'block';
        
        } else
            alert(id+' existe pas !!');
    }
    
  /**
   * Return all childs with specified tag name & CSS class name attribute
   * @tag : string, tag name to get
   * @classname : string, CSS class name associated with tag name
   * @container : optional string, container identifier for target restriction
   *              if not specified, container is set to HTML...
   **/
  function getElementsByTagClass(tag, className) {
    var items = new Array();
    if(getElementsByTagClass.arity==3) {
        // parent by ID
        if(typeof(getElementsByTagClass.arguments[2])==string)
            var container = document.getElementById(getElementsByTagClass.arguments[2]);
        else
            var container = getElementsByTagClass.arguments[2];
    } else {
        // no param 3 : get all from HTML
        var container = document.getElementsByTagName('html')[0];
    }
    var collection = container.getElementsByTagName(tag);
    for (var i=0; i<collection.length; i++) {
        if (collection[i].className == className) {
            items.push(collection[i]);
        }
    }
    if (items.length>0) {
        return items;
    }
    else {
        return false;
    }
  }
  
  function childsOnlyTagName(item, tag) {
    if( item.hasChildNodes() ) {
        var max = item.childNodes.length;
        var n = 0;
        for(var i=0; i<max; i++) {
            var cur = item.childNodes[i];
            if ( cur.nodeName.toLowerCase() == tag) {
                n++;
                break;
            }
        }
        if (n>0)
            return true;
        else
            return false;
    } else
        return false;
  }

  function toggleFullSize(image) {
    var item = document.getElementById('infos_produit');
    img = item.getElementsByTagName('img')[0];
    if( !document.getElementById('baseIllust')) {
        var maxH = img.offsetHeight;
        /*var maxW = img.offsetWidth;
        img.parentNode.style.marginLeft = 'auto';
        img.parentNode.style.marginRight = 'auto';*/
        img.parentNode.style.height = maxH+6+'px';
    }
    if( image!=undefined && document.getElementById(image).nodeName == 'IMG' ) {
        cur = document.getElementById(image);
        target = cur.parentNode.getElementsByTagName('IMG')[0];
        if ( !document.getElementById('baseIllust') )
            img.parentNode.innerHTML += '<input type="hidden" id="baseIllust" value="'+img.src+'"/>';
        var tmp = document.getElementById(image).src.replace("/thumbnails/", "/");
        img.src = tmp;
    } else {
        img.src = document.getElementById('baseIllust').value;
    }
  }

/*****  third party scripts  ******/
/**
 * window.onload manager by http://www.ibilab.net/
 */
function addToStart(fnc){
  if(!window.listStart) window.listStart = new Array();
  window.listStart.push(fnc);
}
function start(){
  var ls = window.listStart;
  if(ls){
    for(i=0; i<ls.length; i++){
      fnc = ls[i];
      if(typeof(fnc) == 'function'){
        fnc();
      } else {
        eval(fnc);
      }
    }
  }
}
window.onload = start;
/*****  /third party scripts  ******/

/***********  Events on load  ***********/

function layoutFix() {
    var Hmax = Math.max(document.getElementById('tools').offsetHeight, document.getElementById('verticalmenu').offsetHeight);
    var Hcontents = document.getElementById('contents').offsetHeight;
    if(Hcontents < Hmax) {
        document.getElementById('contents').style.height = Hmax+20+'px';
    }
}

addToStart(layoutFix);  // fix layout

