/* Funkce pro WENGINE
   (c) Petr Mach <gsl@seznam.cz>, 2005
   Neni volne siritelne a urceno k volnemu pouziti.
   Nejsou povoleny modifikace kodu.

   Modifikace a pouziti v jinem projektu konzultujte s autorem.
*/

//alert(window.navigator.userAgent);
MOZILLA=0
OPERA=0
MSIE=0
if(window.navigator.userAgent.indexOf('MSIE')!= -1){
  MSIE=1;
}
if(window.navigator.userAgent.indexOf('Opera')!= -1){
  MSIE=0;
  OPERA=1;
}
if(window.navigator.userAgent.indexOf('gecko')!= -1){
  MSIE=0;
  OPERA=0;
  MOZILLA=1;
}
if(window.navigator.userAgent.indexOf('Gecko')!= -1){
  MSIE=0;
  OPERA=0;
  MOZILLA=1;
}
//alert(MSIE+' '+OPERA+' '+MOZILLA);

function Cookie(document, name, min, path, domain, secure){
  this.$document=document;
  this.$name=name;
  if(min){
    this.$expires=new Date((new Date()).getTime() + min*60*1000);
  }else{
    this.$expires=null;
  }
  if(path){
    this.$path=path;
  }else{
    this.$path=null;
  }
  if(domain){
    this.$domain=domain;
  }else{
    this.$domain=null;
  }
  if(secure){
    this.$secure=true;
  }else{
    this.$secure=false;
  }
}

Cookie.prototype.debug=function(text){
  alert('CookieDebug - (' + text + ')' + this.$document.cookie);
}

Cookie.prototype.save=function(){
  var cookieVals='';
  for(var prop in this){
    if(prop.charAt(0)=='$' || ((typeof this[prop])=='function')){
      continue;
    }
    if(cookieVals!=''){
      cookieVals+='&';
    }
    cookieVals+=prop+':'+escape(this[prop]);
  }

  var cookie=this.$name+'='+cookieVals;
  if(this.$expires){
    cookie+='; expires='+this.$expires.toGMTString();
  }
  if(this.$path){
    cookie+='; path='+this.$path;
  }
  if(this.$domain){
    cookie+='; domain='+this.$domain;
  }
  if(this.$secure){
    cookie+='; secure';
  }
  this.$document.cookie=cookie;
}

Cookie.prototype.load=function(debug){
  var allCookies=this.$document.cookie;
  if(allCookies==''){
    return false;
  }

  var start=allCookies.indexOf(this.$name+'=');
  if(start==-1){
    return false;
  }
  start+=this.$name.length+1; //+1 = rovnitko
  var end=allCookies.indexOf(';', start);
  if(end==-1){
    end=allCookies.length;
  }
  var cookieVals=allCookies.substring(start, end);
  if(debug) alert(cookieVals);
  var arrayCookieVals=cookieVals.split('&');
  for(var i=0; i<arrayCookieVals.length; i++){
    x=arrayCookieVals[i].split(':');
    this[x[0]]=unescape(x[1]);
  }
  return true;
}

Cookie.prototype.del=function(){
  var cookie;
  for(var prop in this){
    if(prop.charAt(0)=='$' || ((typeof this[prop])=='function')){
      continue;
    }
    this[prop]=null;
  }
    
  cookie=this.$name+'=';
  if(this.$path){
    cookie+='; path='+this.$path;
  }
  if(this.$domain){
    cookie+='; domain='+this.$domain;
  }
  cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT';
  this.$document.cookie=cookie;
}

//pro plovouci menu, ziskani cfg
cook=new Cookie(document, 'AKEDU', (60*24*365));
cook.load(false);
if(!cook.menu){
  cook.menu=0;
  if(OPERA||MOZILLA) cook.menu=1;
}
//alert('COOK:: menu: ' + cook.menu + ', style_color: ' + cook.style_color);
//alert('COOK:: filtr: ' + cook.filtr);

function start(){
  //document.body.focus();

  if(MSIE){// MSIE - fix stupid font size
    size=parseInt(document.body.currentStyle.fontSize);
    if(size>11){
      document.body.style.fontSize='90%';
    }
  }
  if((MOZILLA || OPERA) && cook.menu==1){//plovouci menu, init dle cfg
    check=document.getElementById('idChMenu');
    check.checked=true;
    setMenu(1);
  }
  
  el=document.getElementById(id='idFocus')
  if(el){
    el.focus();
  }

  setFiltr(cook.filtr);
}

/* ZMENA MENU */

function toggleMenu(){//pro plovouci menu, prepinani+save do cfg
  check=document.getElementById('idChMenu');
  if(check.checked){
    setMenu(1);
    cook.menu=1;
    cook.save();
  }else{
    setMenu(0);
    cook.menu=0;
    cook.save();
  }

  if(document.body.focus) document.body.focus();
}
function setMenu(on){//nastaveni plovouciho menu
  topMargin='1.5em';
  menu=document.getElementById('idDSloupec');
  if(!menu){
    menu=document.getElementById('idDSloupec2');
  }

  if(on==1){
    menu.style.position='fixed';
    menu.style.top=topMargin;
  }else{
    menu.style.position='';
    menu.style.top='';
  }

  if(document.body.focus) document.body.focus();
}

/* ZMENA ZOBRAZENI NASTAVENI FILTRU */

function toggleFiltr(){//zobrazeni nastaveni filtru, prepinani+save do cfg
  check=document.getElementById('idChFiltr');
  if(check.checked){
    setFiltr(1);
    cook.filtr=1;
    cook.save();
  }else{
    setFiltr(0);
    cook.filtr=0;
    cook.save();
  }

  if(document.body.focus) document.body.focus();
}
function setFiltr(on){//zobrazeni nastaveni filtru
  filtr=document.getElementById('idTFiltr');
  chckb=document.getElementById('idChFiltr');
  if(!filtr) return false;

  if(on==1){
    filtr.style.display='';
    chckb.checked=true;
  }else{
    filtr.style.display='none';
    chckb.checked=false;
  }

  if(document.body.focus) document.body.focus();
}


/* ZMENA STYLU */

function setStyle(group, variant){
  return
  //alert('setstyle: ' + group + ', ' + variant);
  headLinks=document.getElementsByTagName('link');
  for(var i=0; i<headLinks.length; i++){
    if(headLinks[i].id.indexOf('idStylG'+group)==0){
      //alert(headLinks[i].id);
      headLinks[i].disabled=true;
    }
  }
  idStyl='idStylG'+group+'V'+variant;
  //alert(idStyl);
  el=document.getElementById(idStyl)
  //alert(el.rel);
  if(!el){
    alert("Chyba na stránce. Alternativní styl s id '"+idStyl+"' neexistuje.")
  }
  el.disabled=false;
}
function onSetStyle(ev){
  if(!ev){/* BUG in IE */
    ev=event;
  }

  if(ev.target){
    el=ev.target;
  }else{/* BUG in IE */
    el=ev.srcElement;
  }

  id=el.value;
  gIndex=id.indexOf('G');
  vIndex=id.indexOf('V');
  if(gIndex==-1 || vIndex==-1){
    alert('Spatny nazev ID, musi byt idStylGgggVvvv kde ggg je group a vvv je variant.');
    return;
  }
  group=id.substring(gIndex+1, vIndex);
  variant=id.substring(vIndex+1);
  //alert('group: ' + group);
  //alert('variant: ' + variant);
  setStyle(group, variant);
  cook['style_'+group]=variant;
  cook.save();
  //document.body.focus();
}

/* FOTOGALERIE */

function setDatum(idEl){
  el=document.getElementById(idEl);
  datum = new Date;
  el.value=datum.getDate()+'. '+(datum.getMonth()+1)+'. '+datum.getFullYear();
}

function sel2txt(elSel, idTxt, rewrite){
  if(elSel.value==0) return;
  str=new String();
  hodnota=elSel.value;
  hodnota=hodnota.replace(/^ +/, '');
  hodnota=hodnota.replace(/ +$/, '');
  txt=document.getElementById(idTxt);
  ar=txt.value.split(';');
  if(rewrite){
    txt.value=hodnota;
  }else{
    newAr=[]; removeValue=0; j=0;
    for(var i=0; i<ar.length; i++){
      if(!ar[i]){
        continue;
      }
      arVal=ar[i].replace(/^ +/, '');
      arVal=arVal.replace(/ +$/, '');
      if(arVal==hodnota){
        removeValue=1;
        continue;
      }
      newAr[j++]=arVal;
    }
    if(removeValue==0) newAr[j++]=hodnota;
    txt.value=newAr.join('; ');
  }
  elSel.value=0;
}

/* DHTML MENU */


weMenu_Objects = []; // seznam DOM objektů DIV / prave zobrazena menu


// HANDLER - inicializace menu
function weMenu_init(ev){

    menu = document.getElementById('idMenuItems');
    menuitems = menu.getElementsByTagName("a");
    for(var i=0; i<menuitems.length; i++){
        // registrace handleru pro polozky menu
        menuitems[i].addEventListener("mouseover", weMenu_overExpand, false);
        menuitems[i].addEventListener("mouseover", weMenu_overSelect, false);
        //weDebug(["+over 0out", menuitems[i].innerHTML]);
    }
}
// registrace handleru
// zaridi inicializaci menu po nahrani stranky
window.addEventListener("load", weMenu_init, false);


// HANDLER - kurzor je nad polozkou menu ktera ma expandovat do podmenu
function weMenu_overExpand(e){
    var el = e.currentTarget; // el == anchor
    // preklopeni handleru, aby se menu nevytvarelo opakovane
    // a zachytila se udalost opusteni polozky
    el.removeEventListener("mouseover", weMenu_overExpand, false);
    el.addEventListener(   "mouseout",  weMenu_outExpand,  false);

    // priprava dat a spusteni funkce pro vytvoreni menu
    var id = el.id;
    var po = weMenu_getPosition(el);
    var of = [0, 0]
    if(weMenu_Objects.length==0){ 
    // u prvniho podmenu musime zjistit offset hlavniho menu 
    // tedy scroll posunuti vuci zacatku stranky
        var of = weMenu_getOffset();
    }
    var x  = po[0]+of[0] + el.offsetWidth - 20;
    var y  = po[1]+of[1] - 0;
    weMenu_make(id, x, y, el);
}

// HANDLER - kurzor je nad polozkou menu, oznacit ji jako select
function weMenu_overSelect(e){
    var a = e.currentTarget; // el == anchor
    var anchors = a.parentNode.getElementsByTagName('a');
    for(var i=0; i<anchors.length; i++){
        weMenu_removeSelected(anchors[i]);
    }
    if(a.className==""){
        a.className="selected";
        //weDebug([0, a.innerHTML, a.className]);
    }else{
        a.className=a.className + " selected";
        //weDebug([1, a.innerHTML, a.className]);
    }
    a.removeEventListener("mouseover", weMenu_overSelect, false);
    a.addEventListener("mouseout",    weMenu_outSelect,  false);
    //weDebug(["-over +out", a.innerHTML]);
}

// zrusit selected polozku v hlavnim menu
//vola se pri zavreni vsech podmenu
function weMenu_removeMainSelected(){
    if(window.weMenu_Objects.length==0){
        var anchors = document.getElementById('idMenuItems').getElementsByTagName('a');
        for(var i=0; i<anchors.length; i++){
            weMenu_removeSelected(anchors[i]);
        }
    }
}
function weMenu_outSelect(e){
    var a = e.currentTarget; // el == anchor
    // kdyz kurzor sjede z hlavniho polozky menu a neni na nejakem
    // otevrenem menu, zrusit selected polozku v hlavnim menu
    // ... moment
    // staci vedet, ze zadne menu neni otevreno a vim, ze kurzor
    // je jinde
    if(window.weMenu_Objects.length==0)
        weMenu_removeMainSelected();
    a.removeEventListener("mouseout",  weMenu_outSelect,  false);
    a.addEventListener("mouseover",     weMenu_overSelect, false);
    //weDebug(["+over -out", a.innerHTML]);
}



// HANDLER - kurzor opustil menu nebo jeho polozku
function weMenu_outExpand(e){
    // ruseni menu od posledniho k prvnimu, je-li kurzor mimo
    // jinak k tomu menu, nad kterym je kurzor
    var r = e.relatedTarget;
    for(var i=weMenu_Objects.length-1; i>=0; i--){
        var mnu = weMenu_Objects[i];
        if(!weMenu_isChild(r, mnu)){
            document.body.removeChild(mnu);
            window.weMenu_Objects.pop();
            //weDebug(["remove", weMenu_Objects.length]);
            if(window.weMenu_Objects.length==0){
                // zadne podmenu, odstranit selected polozku z hlavniho menu
                weMenu_removeMainSelected();
            }
            // preklopeni handleru, aby kurzor nad polozkou mohl
            // opet vytvorit menu
            mnu.odkaz.removeEventListener("mouseout", weMenu_outExpand, false);
            mnu.odkaz.addEventListener("mouseover", weMenu_overExpand, false);
            continue;
        }
        return;
    }
}

// projde vsechny anchory a zadanemu nastavi className="selected"
function weMenu_removeSelected(a){
    var names = a.className.split(/[, ]+/);
    var newNames = Array();
    for(var i=0; i<names.length; i++){
        if(names[i]=="selected") continue;
        newNames.push(names[i]);
    }
    newNames = newNames.join(" ");
    a.className = newNames;
}

// vytvori objekt menu, zobrazi ho a vlozi do seznamu
function weMenu_make(id, x, y, odkaz){
    var listItems = weMENU[id][3];
    if(listItems.length==0){
        return;
    }

    // vytvoreni kontejneru
    var mnu = document.createElement("div");
    mnu.className="clPopMenu";
    mnu.style.top  = y+"px";
    mnu.style.left = x+"px";
    mnu.odkaz = odkaz;
    mnu.addEventListener("mouseout", weMenu_outExpand, false);

    // vytvareni jednotlivych polozek menu
    for(var i=0; i<listItems.length; i++){
        var iName = listItems[i];
        var item  = weMENU[iName];

        var cName = "";
        if(i == 0) 
            cName = "prvni";
        if(i == listItems.length-1) 
            cName = "posledni";
        if(0 == listItems.length-1) 
            cName = "prvni posledni";

        var expand = "";
        if(item[3].length>0){
            //expand char je definovan v _komponenty.php kvuli problemum s kodovanim
            expand = "<span class='clExpandChar'>"+weMenu_ExpandChar+"</span>"; 
        }

        var a = document.createElement("a");
        a.id        = iName;
        a.href      = item[1];
        //a.title     = item[2]; prekryva se s menu
        a.className = cName;
        a.innerHTML = expand + item[0];
        a.addEventListener("mouseover", weMenu_overExpand, false);
        a.addEventListener("mouseover", weMenu_overSelect, false);
        mnu.appendChild(a);

    }

    // zobrazeni a vlozeni do seznamu
    document.body.appendChild(mnu);
    window.weMenu_Objects.push(mnu);
    //weDebug(["add", weMenu_Objects.length]);
}

// vypocita souradnice elementu vuci body
function weMenu_getPosition(el){
    if(el.nodeName.toLowerCase()=="body"){
        return [0, 0];
    }
    if(el.offsetParent==null){
        return [el.offsetLeft, el.offsetTop];
    }

    if(el.offsetParent.nodeName.toLowerCase()=="body"){
        return [el.offsetLeft, el.offsetTop];
    }else{
        var pos = weMenu_getPosition(el.offsetParent);
        return [el.offsetLeft+pos[0], el.offsetTop+pos[1]];
    }
}

// vypocita offset menu
function weMenu_getOffset(){
    // zjistuje pozici menu vuci zacatku stranky
    // to se meni podle toho, zda je menu fixed nebo ne
    menu=document.getElementById('idDSloupec');
    if(!menu){
        menu=document.getElementById('idDSloupec2');
    }
    if(menu.style.position == 'fixed'){
        return [pageXOffset, pageYOffset];
    }
    return [0, 0];


}

// kontrola zda 'child' je 'base' nebo jeho potomek v DOM strukture
function weMenu_isChild(child, base){
    var par = child;
    while(par != null){
        if(par == base) return true;
        par = par.parentNode;
    }
    return false;
}

    //// POMOCNE FUNKCE ////

    weDebugMsg="";
    function weDebug(msg){
        if(typeof msg == "string" && msg.indexOf("START")==0){
            weDebugMsg=msg+"<br />";
        }else{
            if(typeof msg == "string"){
                msg=msg.replace("<", "&lt;");
            }
            weDebugMsg+=msg+"<br />";
        }
        document.getElementById('idMSG').innerHTML=weDebugMsg;
    }


