/*******************************************************************************
 *E subCatMenu.js . . . . . . . . . . . . . . . . . . . . . . . . .  starts here
 **
 ******************************************************************************/

var loadedcolor='#FDC558' ;       // PROGRESS BAR COLOR
var unloadedcolor='white';        // BGCOLOR OF UNLOADED AREA
var barheight=17;                 // HEIGHT OF PROGRESS BAR IN PIXELS
var barwidth=202;                 // WIDTH OF THE BAR IN PIXELS
var bordercolor='#910029';        // COLOR OF THE BORDER
var textColor='black';            // COLOR OF TEXT IN LOADING BAR
var textSize='10px';              // SIZE OF TEXT IN LOADING BAR
var textFont='verdana';           // FONT FAMILY OF TEXT IN LOADING BAR
var sessionName='XTCsid'          // Name of the SessionId

/*******************************************************************************
 **
 *F getFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  getFile
 **
 ******************************************************************************/
function getFile(datafile) {
  var http;
  if (window.XMLHttpRequest) {
    http = new XMLHttpRequest();
  } else {
    // Internet Explorer 6 and older
    try {
      http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      http = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }

  var content;
  var result;
  http.open('get',datafile,false);
  http.send(null);
  var type=http.getResponseHeader('Content-Type');
  if (type != 'text/plain' && type !='text/csv' && type != '') {
    alert("datafile: "+datafile+"\ntype: '"+type+"'");
    throw "bad file";
  }
  content=http.responseText;
  result=content.split(/\n/);
  return result;
}

/*******************************************************************************
 **
 *F getCsv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  getCsv
 **
 ******************************************************************************/
function getCsv(datafile) {
  var file=getFile(datafile);
  var result=new Array();
  for (var i=0; i<file.length; ++i) {
    // remove tailing separator from line
    if (file[i].length != 0) {
      //try {
        var parts=file[i].match(/(;|([^"][^;]*|"([^"]|"")*");)/g);
        for (var j=0; j<parts.length; ++j) {
          parts[j]=parts[j].substr(0,parts[j].length-1);
          if (parts[j].substr(0,1)=='"') {
            parts[j]=parts[j].substr(1,parts[j].length-2);
            parts[j]=parts[j].replace(/""/g,'"');
          }
        }
        result[result.length]=parts;
      //}
      //catch(e) {
      //  alert(e);
      //  alert('bad file format: '+datafile);
      //}
    }
  }
  return result;
}

/*******************************************************************************
 **
 *F readSubCats . . . . . . . . . . . . . . . . . . . . . . . read SubCategories
 **
 ******************************************************************************/
function readSubCats(catId) {
  var csv=getCsv('csv/getSubCats.csv.php?catId='+catId+sid);
  var obj=new Array();

  // first colum contains the id for the line
  for(i=0; i<csv.length; ++i) {
    obj[csv[i][0]]=csv[i];
  }

  return obj;
}

/*******************************************************************************
 **
 *F sgml2utf . . . . . . . . . . . . . . . . . . . . . . . . . convert sgml=>utf
 **
 ******************************************************************************/
function sgml2utf(text) {
  text=text.replace(/&Auml;/g,"\u00c4");
  text=text.replace(/&Ouml;/g,"\u00d6");
  text=text.replace(/&Uuml;/g,"\u00dc");
  text=text.replace(/&szlig;/g,"\u00df");
  text=text.replace(/&auml;/g,"\u00e4");
  text=text.replace(/&ouml;/g,"\u00f6");
  text=text.replace(/&uuml;/g,"\u00fc");
  text=text.replace(/&quot;/g,"\u0022");
  text=text.replace(/&Euml;/g,"\u00cb"); 
  

  return text;
}

/*******************************************************************************
 **
 *F setDisplay . . . . . . . . . . . . . . . . . . . . . .  set Display for type
 **
 ******************************************************************************/
function setDisplay(type, item, child) {

  // search for the image to change:
  var img=item.parentNode.getElementsByTagName('IMG');
  if(img.length>0) {
    image = img[0];
    if (image.className == "plus" || image.className == "minus") {
      image.className = type=='hide' ? 'plus' : 'minus';
    }
  }

  if (child) {
    if (type == "hide") {
      child.style.display="none";
    } else {
      child.style.display="block";
    }
  }
}

/*******************************************************************************
 **
 *F getCat . . . . . . . . . . . . . . . . . . . . . . . get CategoryID from URL
 **
 ******************************************************************************/
function getCat(url, deep) {
  var catId=null;
  var cats=url.replace(/.*:::(.*)\.html.*/, '$1').split('_');
  if (cats.length > 0) {
    if (typeof(deep) != 'undefined') {
      if(cats.length = deep) {
        catId = cats[cats.length-1];
      }
    } else {
      catId = cats[cats.length-1];
    }
  }
  return catId;
}

/*******************************************************************************
 **
 *F switchDisplay . . . . . . . . . . . . . . . . . . . . . . . . show/hide item
 **
 ******************************************************************************/
function switchDisplay(item, type, pct) {
  // check for existing sublist
  var ul=item.parentNode.getElementsByTagName('UL');
  if (ul.length == 0) {
    if (type!='hide') {

      if (typeof(type) != 'undefined' && isGreyOut == false) {
        isGreyOut=true;
        greyOut(true);
        // bring this back to the array and return...
        var todo=new Array();
        todo[0]=item;
        todo[1]=pct;
        aTodo.unshift(todo);
        return false;
      }
      // get the submenu list from system
      var catId=getCat(item.href);
      cats[catId]=readSubCats(catId);
      hasRead=true;

      // build the sublist
      var ul=document.createElement('ul');
      ul.className='subCat';
      var keys=new Array();

      // we need the reverse order...
      for(var subCatId in cats[catId]) {
        keys.unshift(subCatId);
      }

      var prev=false;
      for(var i=0; i<keys.length; i++) {
        var subCatId = keys[i];
        var li=document.createElement('li');
        var a=document.createElement('a');
        a.href=cats[catId][subCatId][7];
     
        var img=document.createElement('img');
        img.src='templates/ersatzteilmarkt/img/dot.gif';
        if (cats[catId][subCatId][6]>0) {
          a.onclick=function() { switchDisplay(this); return false; }
          img.className='plus';
          img.alt='+';
        } else {
          if (prev) {
            img.className='item';
          }
          img.alt='';
        }
        img.width=9;
        img.height=9;
        a.appendChild(img);
        a.appendChild(document.createTextNode(sgml2utf(cats[catId][subCatId][1])));

        li.appendChild(a);

        if (!prev) {
          li.className='end';
          ul.appendChild(li);
        } else {
          ul.insertBefore(li, prev);
        }
        prev=li;
      }
      if (prev) {
        item.parentNode.appendChild(ul);
      }
      setDisplay('show', item);
      ul=item.parentNode.getElementsByTagName('UL');
    }
  } else {
    if (typeof(type) == 'undefined' && (ul[0].style.display == '' || ul[0].style.display == 'block') || type=='hide') {
      setDisplay('hide', item, ul[0]);
    } else {
      setDisplay('show', item, ul[0]);
    }
  }
  if (typeof(type) != 'undefined' && ul.length > 0) {
    var LIs=ul[0].childNodes;
    for (var i=LIs.length-1; i>=0; i--) {
      var IMGs=LIs[i].getElementsByTagName('IMG');
      if (IMGs.length > 0 && (IMGs[0].className=='plus' || IMGs[0].className=='minus')) {
        var a=LIs[i].getElementsByTagName('A');
        if (a.length > 0) {
          //switchDisplay(a[0], type);
          var todo=new Array();
          todo[0]=a[0];
          todo[1]=(1/(LIs.length+1))*pct;
          aTodo.unshift(todo);
        }
      }      
    }
  }
  return true;
}

/*******************************************************************************
 **
 *F switchAll . . . . . . . . . . . . . . . . . . . . . . .  show/hide all times
 **
 ******************************************************************************/
function switchAll(type, item) {
  isGreyOut=false
  //greyOut(true);
  window.status='loading...';
  stopLoad=false; 
  sAi=item;
  aTodo=new Array();

  if (typeof(item) == 'undefined') {
    catId=getCat(document.location.href);

    if (cats[catId]) {
      var len=0;
      for(var subCatId in cats[catId]) {
        len++;
      }
      
      for(var subCatId in cats[catId]) {
        var a=document.getElementById('cat'+subCatId).getElementsByTagName('A');
        if (a && a.length > 0) {
          var todo = new Array();
          todo[0]=a[0];
          todo[1]=(1/len);
          aTodo.push(todo);
        }
      }
    }
  } else {
    var todo=new Array();
    todo[0]=item;
    todo[1]=1;
    aTodo.push(todo);
  }

  window.setTimeout("processAll('"+type+"')", 50);
}

/*******************************************************************************
 **
 *F processAll . . . . . . . . . . . . . . . . . . . . . . . . . . .  processAll
 **
 ******************************************************************************/
function processAll(type) {
  var readAtOnce = 1;
  if (!stopLoad && aTodo.length > 0) {
    hasRead=false;
    while (readAtOnce > 0 && aTodo.length > 0) {
      var item=aTodo.shift();
      var result = switchDisplay(item[0], type, item[1]);
      if (!result) {
        readAtOnce==0;
      } else if (hasRead) {
        readAtOnce--;

        var done = 1;
        for (var i in aTodo) {
          done -= aTodo[i][1];
        }
        perdone.style.width=((barwidth-2)*done)+'px';
        done *= 100;
        textdone.replaceChild(document.createTextNode(done.toString().replace(/\..*$/,'')), textdone.firstChild);
        updateGreyOut();
      }
    }

    window.setTimeout("processAll('"+type+"')", 10);
  } else {
    if (isGreyOut) {
      greyOut(false);
      isGreyOut==null;
    }
    window.status=null;
  }
 }

/*******************************************************************************
 **
 *F setFloat . . . . . . . . . . . . . . . . . . . . . . . . . . . . need for IE
 **
 ******************************************************************************/
function setFloat(item, value) {
  if (typeof(item.style.styleFloat) != 'undefined') {
    // this is for our friend the IE
    item.style.styleFloat = value;
  } else {
    item.style.cssFloat = value;
  }
}

/*******************************************************************************
 **
 *F showSubCats . . . . . . . . . . . . . . . . . . . . . . . .  onload funktion
 **
 ******************************************************************************/
function showSubCats() {

  // substract the SessionId from url
  sid=null;
  var sids=document.location.href.match(/.*?.*(XTCsid=[a-zA-Z0-9]*)/);
  if (sids && sids.length > 0) {
    sid='&'+sids[1];
  }

  // get the main Category from URL
  catId=getCat(document.location.href, 1);
  if (catId) {

    // read the needed SubCats
    cats = new Array();
    cats[catId]=readSubCats(catId);

    // get the container to check:
    var body=document.getElementById('topCats');

    if (typeof(cats) != 'undefined' && typeof(body) != 'undefined') {

      for(var i=0; i<body.childNodes.length; i++) {
        if (typeof(body.childNodes[i].style) != 'undefined') {
        body.childNodes[i].style.display = 'block';
        }
      }

      var ul=body.getElementsByTagName('UL')[0];
      var LIs=ul.getElementsByTagName('LI');
      for(var j=0; j<LIs.length; j++) {
        var li=LIs[j];
        var a=li.getElementsByTagName('A')[0];
        var subCatId=getCat(a.href);
        li.id='cat'+subCatId;
        if (cats[catId][subCatId] && cats[catId][subCatId][6] > 0) {
          var firstChild = a.firstChild;
          a.onclick=function() { switchDisplay(this); return false; };
          var img = document.createElement('img');
          img.src='templates/ersatzteilmarkt/img/dot.gif';
          img.className="plus";
          img.alt="+";
          img.width=9;
          img.height=9;
          a.insertBefore(img, firstChild);
          setFloat(a, 'left');

          // add the minus for this categorie:
          var a1 = document.createElement('a');
          a1.href=a.href;
          a1.onclick=function() { switchAll('hide', this); return false; };
          a1.className='showall';
          setFloat(a1, 'right');
          var img = document.createElement('img');
          img.src='templates/ersatzteilmarkt/img/dot.gif';
          img.className='minus';
          img.alt='-';
          img.width=15;
          img.height=15;
          a1.appendChild(img);
          li.appendChild(a1);

          // add the plus for this categorie:
          var a1 = document.createElement('a');
          a1.href=a.href;
          a1.onclick=function() { switchAll('show', this); return false; };
          a1.className='showall';
          setFloat(a1, 'right');
          var img = document.createElement('img');
          img.src='templates/ersatzteilmarkt/img/dot.gif';
          img.className='plus';
          img.alt='+';
          img.width=15;
          img.height=15;
          a1.appendChild(img);
          li.appendChild(a1);

          if (navigator.appName.search(/Internet Explorer/i) == -1) {
            var br = document.createElement('br');
            br.style.clear='both';
            li.appendChild(br);
          }
        }
		// Edit by Sausen normalerweise auskommentiert
        //li.style.clear='both';
        li.style.clear='both';		
      }

      // display the all show/hide buttons:
      //var div=document.createElement('div');
      // <a href="#" style="float:left; background-color: red;">asdf</a><a href="#" style="float:right; background-color: red;">asdf</a><br style="clear:both" />
    
      var a=document.createElement('a');
      a.href='#';
      setFloat(a, 'left');
      a.onclick=function() { switchAll('show'); return false; }
      a.appendChild(document.createTextNode('Alle '));
      var img=document.createElement('img');
      img.src='templates/ersatzteilmarkt/img/plus.gif';
      img.alt='+';
      a.appendChild(img);
      a.appendChild(document.createTextNode('Aufklappen'));
      body.insertBefore(a,ul);
      //div.appendChild(a);

      var a=document.createElement('a');
      a.href='#';
      setFloat(a, 'right');
      a.onclick=function() { switchAll('hide'); return false; }
      a.appendChild(document.createTextNode('Alle '));
      var img=document.createElement('img');
      img.src='templates/ersatzteilmarkt/img/minus.gif';
      img.alt='-';
      a.appendChild(img);
      a.appendChild(document.createTextNode('Zuklappen'));
      body.insertBefore(a,ul);

      ul.style.clear='both';

      //div.appendChild(a);
      //body.insertBefore(div, ul);
    }
  }
}

/*******************************************************************************
 *F updateGreyOut . . . . . . . . . . . . . . . . . . . . . . . .  updateGreyOut
 ******************************************************************************/
function updateGreyOut() {
  var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
  var dsocleft=document.all? iebody.scrollLeft : window.pageXOffset;
  var dsoctop=document.all? iebody.scrollTop : window.pageYOffset;

  // Calculate the page width and height 
  if( iebody.scrollWidth || iebody.scrollHeight ) {
    var pageWidth = iebody.scrollWidth+'px';
    var pageHeight = iebody.scrollHeight+'px';
  } else if( iebody.offsetWidth ) {
    var pageWidth = iebody.offsetWidth+'px';
    var pageHeight = iebody.offsetHeight+'px';
  } else {
    var pageWidth='100%';
    var pageHeight='100%';
  }   

  var displayHeight = (window.innerHeight) ? window.innerHeight : iebody.clientHeight;
  var displayWidth = (window.innerWidth) ? window.innerWidth : iebody.clientWidth

    //alert("left: "+dsocleft+" top: "+dsoctop+" width: "+pageWidth+' display: '+displayWidth+'x'+displayHeight);
  loadInfo.style.top=(dsoctop+displayHeight*.2)+'px';

  dark.style.width= pageWidth;
  dark.style.height= pageHeight;
  
}

/*******************************************************************************
 *F greyOut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  greyOut
 ******************************************************************************/
function greyOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 25;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page

    loadInfo = document.createElement('div');
    loadInfo.id='loadInfo';
    loadInfo.style.position = 'absolute';
    loadInfo.style.top='20%';
    loadInfo.style.left=0;
    loadInfo.style.width='100%';
    loadInfo.style.zIndex=100;
    loadInfo.style.textAlign='center';
    loadInfo.style.lineHeight='0';
    tbody.appendChild(loadInfo);
    var tnode2 = document.createElement('div');
    tnode2.style.position='relative';
    tnode2.style.fontSize='12px';
    tnode2.style.backgroundColor='#FFFFFF';
    tnode2.style.borderWidth='2px';
    tnode2.style.borderStyle='solid';
    tnode2.style.borderColor='#000000';
    tnode2.style.color='#000000';
    tnode2.style.width='250px';
    tnode2.style.height='120px';
    tnode2.style.margin='0 auto';
    tnode2.style.lineHeight='25px';
    tnode2.style.textAlign='center';
    tnode2.appendChild(document.createTextNode('Lade Informationen, bitte haben Sie einen Moment Geduld.'));

    // Ladebalken:
    perouter = document.createElement('div');
    perouter.id='perouter';
    perouter.style.position='relative';
    perouter.style.backgroundColor=bordercolor;
    perouter.style.width=barwidth+'px';
    perouter.style.margin='10px auto 0px auto';
    perouter.style.height=barheight+'px';
    perouter.style.lineHeight='normal';
    var tnode3 = document.createElement('div');
    tnode3.style.position='absolute';
    tnode3.style.top='1px';
    tnode3.style.left='1px';
    tnode3.style.width=(barwidth-2)+'px';
    tnode3.style.height=(barheight-2)+'px';
    tnode3.style.backgroundColor=unloadedcolor;
    tnode3.style.fontSize='1px';
    perouter.appendChild(tnode3);
    perdone = document.createElement('div');
    perdone.id='perdone';
    perdone.style.position='absolute';
    perdone.style.top='1px';
    perdone.style.left='1px';
    perdone.style.width='0px';
    perdone.style.height=(barheight-2)+'px';
    perdone.style.backgroundColor=loadedcolor;
    perdone.style.fontSize='1px';
    perouter.appendChild(perdone);
    textdone = document.createElement('div');
    textdone.style.position='absolute';
    textdone.style.top='2px';
    textdone.style.left='1px';
    textdone.style.width=(barwidth-2)+'px';
    textdone.style.height=(barheight-2)+'px';
    textdone.style.color=textColor;
    textdone.style.fontSize=textSize;
    textdone.style.fontFamily=textFont;
    textdone.style.textAlign='center';
    textdone.style.cursor='default';
    textdone.appendChild(document.createTextNode('0'));
    textdone.appendChild(document.createTextNode('% geladen'));
    perouter.appendChild(textdone);
    perouter.style.marginBottom='10px';

    tnode2.appendChild(perouter);

    // abbruch button
    var tAbort = document.createElement('a');
    tAbort.href='#';
    tAbort.onclick=function() { stopLoad=true; return false; }
    tAbort.style.border='1px solid black';
    tAbort.style.padding='2px';
    tAbort.appendChild(document.createTextNode('Abbruch'));
    tnode2.appendChild(tAbort);

    loadInfo.appendChild(tnode2);
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  } else {
    loadInfo=document.getElementById('loadInfo');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    //dark.style.width= pageWidth;
    //dark.style.height= pageHeight;
    updateGreyOut();
    dark.style.display='block';                          
    perdone.style.width='0px';
    textdone.replaceChild(document.createTextNode('0'), textdone.firstChild);
    loadInfo.style.display='block';
  } else {
     dark.style.display='none';
     loadInfo.style.display='none';
  }
}

if (window.addEventListener)
  window.addEventListener("load", showSubCats, false);
else if (window.attachEvent)
  window.attachEvent("onload", showSubCats);
else if (document.getElementById)
  window.onload=showSubCats;

/*******************************************************************************
 **
 *E subCatMenu.js . . . . . . . . . . . . . . . . . . . . . . . . . .  ends here
 **
 ** Local Variables:
 ** mode:outline-minor
 ** outline-regexp:"^ *\\*[A-Z] "
 ** tab-width: 2
 ** indent-tabs-mode: nil
 ** End:
 ******************************************************************************/
