/*        jeee.js JavaScript    */

/*********************************************
 *              Menu
 *
 *    Harmonikové menu
 **********************************************/
var Menu = {};

Menu.init = function() {
    ///////////// menu /////////////
    $("#menu dl").each( function (i) 
        {
            Menu.hide(this);
        } 
    );
    $("#menu dt").click(Menu.clickAction);
    $("#menu dd a").focusin(Menu.focusAction);
    $("#menu dd input").focusin(Menu.focusAction);
    $("#menu dl#vyuka").each( function (i) 
        {
            Menu.show(this); 
        }
    );
    //////////////// submenu //////////////////////
    $("#menu dd ul").each( function (i) 
        {
            $(this).addClass('subhide');
        }
    );
    $("#menu dd").hover(
        function() {
            $(this).children('dd ul').delay(500).removeClass('subhide');
        },
        function() {
            $(this).children('dd ul').delay(500).addClass('subhide');
        }
    );

    $("#menu dd ul a").focusin( function() 
        {
            $(this).parents('ul').removeClass('subhide');
            
        }
    )
    $("#menu dd ul a").blur( function() 
        {
            $(this).parents('ul').addClass('subhide');
            
        }
    )

}

Menu.clickAction = function(event) {
  //  if ( this._focus) {
        if ( $(this.parentNode).hasClass("hide") ) {
            Menu.show(this.parentNode);
        } else if ( $(this.parentNode).hasClass("show") ) {
            Menu.hide(this.parentNode);
        }
//    } else { 
//        this._focus= false;
//    }
}

Menu.focusAction = function(event) {
    Menu.show($(this).parents('#menu dl')); 
//    this._focus = false;
//    var node = this
    //$(this).delay(100).each(function(){node._focus=true;})
//    alert(event.target.nodeName)
}

Menu.show = function(node) {
    $(node).removeClass('hide');
    $(node).addClass('show');
}

Menu.hide = function(node) {
    $(node).removeClass('show');
    $(node).addClass('hide');
}

$(document).ready(Menu.init);


/*********************************************
 *              Roll
 *
 *    Balení/rozbalování zdrojových kódů
 **********************************************/
var Roll = {};

Roll.init = function() {
    var targets = $(".rolling");
    for (var i=0; i< targets.length;i++) {
        var button = document.createElement("a");
        button.href = "#";
        button.className = "rollbutton";
        buttonText= document.createTextNode("Rozbalit/Zabalit");
        button.appendChild(buttonText);
        targets[i].parentNode.insertBefore(button,targets[i]);
        button.reference = targets[i] ;

        if ( ! $('#zvyuky').length ) {
            targets[i].style.height = "8em";
            $(".rollbutton").click(Roll.open);
        } else {
            $(".rollbutton").click(Roll.close);
        }
    }
}

Roll.close = function(event) {
    Core.preventDefault(event);
    this.reference.style.height = "8em";
    $(this).unbind("click",Roll.close);
    $(this).bind("click",Roll.open);
}

Roll.open = function(event) {
    Core.preventDefault(event);
    this.reference.style.height = "auto";
    $(this).unbind("click",Roll.open);
    $(this).bind("click",Roll.close);
}


$(document).ready(Roll.init);

/*********************************************
 *             JuuTitle 
 *
 * Žůžo zobrazení tutulku
 **********************************************/
var JuuTitle = {};

JuuTitle.init = function() {
    $("*[title]").bind("mouseover",JuuTitle.showListener);
    $("*[title]").bind("focus",JuuTitle.showListener)
//    $("*[title]").bind("mouseout",JuuTitle.hideListener)
//    $("*[title]").bind("blur",JuuTitle.hideListener)
}

JuuTitle.showListener = function(event) {
    if ( ! this._showDisable ) {
        this._showDisable= true;
        var juu = document.createElement("span");
        juu.className="juutitle";
        var juuText = document.createTextNode(this.title);
        juu.appendChild(juuText);
        this.parentNode.insertBefore(juu,this);
        this._spanRef=juu;
        this.title="";
        juu.style.left = event.pageX + "px";
        juu.style.top =  event.pageY + "px";
        $(".juutitle").delay(3000).hide(1000);
        var obj = this;
        setTimeout(function(){JuuTitle.hide(obj);},4100) 
    }
}

JuuTitle.hide = function(obj) {
    obj.title = obj._spanRef.firstChild.nodeValue;
    obj.parentNode.removeChild(obj._spanRef);
    obj._spanRef=null;
    obj._showDisable=null;
}

$(document).ready(JuuTitle.init);

/*********************************************
 *             insert content 
 *
 *vkládá do stránky obsah tvořený z <h2>
 **********************************************/
var InsCont = {}

InsCont.init = function(){
   $('.inscont').each( function() {
            var node = this.nextSibling ;
            var cont = '<ul>';
            var counter = 1;
            var link;
            while ( !(  node == null || node.nodeName == 'h1' )  ) {
                if (node.nodeName == 'h2') {
                   if ( node.id == null || node.id == '') {
                     node.id= counter;
                     link = counter++;
                   } else {
                     link = node.id; 
                   }
                   cont += '<li><a href="#'+link+'">';
                   $(node).contents().filter(function() { 
                       return this.nodeType == 3; 
                      }).each( function(){ cont += this.nodeValue });
                   cont += '</a></li>';


                }
                node = node.nextSibling ;
            }
            cont += '</ul>';
            $(this).after( cont );
            
        })
    
}

$(document).ready(InsCont.init);

