/* qlm_quirkleftmenuclass.js contains a class representing dynamic left menu.
 * If .generate() shall work the file qlm_generatemenu.js is required to load 
 * before this one.
 * It's required that the page using it contains a an element with id = 'dynamicLMenuID'
 * and if the menu shall be generated an UL with id = 'staticLMenuID'.
 * Jakub Holy 14.12.2003 AD
 */

//Helper class representing a tag to be generated/used by dynamic menu
function ClassTAG (tagName,className,onclickFunc) {
    this.itag = tagName;
    this.iclass = className;
    if(onclickFunc){this.ionclick=onclickFunc; }
    // initialize the member function references for the class prototype
    if (typeof(_classtag_prototype_called) == 'undefined')
	{
	    _classtag_prototype_called = true;
	    ClassTAG.prototype.belongs = belongs2ThisTAG;
	}//if creating the 1st instance
    function belongs2ThisTAG(tag){ return tag.nodeName==this.itag.toUpperCase() && tag.className==this.iclass;}
}//class ClassTAG

//Class representing the dynamic menu //Uses class ClassTAG ************************************************ QuirkLeftMenu
//@param onClickLabel function to handle click on a label
function QuirkLeftMenu(onClickLabel,onClickLink){
    this.linkTAG    = new ClassTAG('A','link',onClickLink);
    this.labelTAG   = new ClassTAG('H3', 'label',onClickLabel);
    this.submenuTAG = new ClassTAG('DIV','content');
    this.G_COOKIE_NAME = "qlmLinkID";
    this.dynamicLMenuID = 'dynamicLMenuID';//id elementu,v nemz je dyn.menu
    this.staticLMenuID = 'staticLMenuID';//id zdroje

    this.gCurrentItem = {isLink: false, item:null, set:setgCurrentItem};//store the last item clicked
    function setgCurrentItem(itm,isLnk){
	if( this.isLink && this.item ) { setCurrentLinkStyle(this.item,false);/*reset*/ }
	if(isLnk) {setCurrentLinkStyle(itm,true); }
	this.item = itm; this.isLink = isLnk;
    }//func setgCurrentItem
    //Set style info for the link//@param isCurrent if false reset style
    function setCurrentLinkStyle(link, isCurrent){
	link.style.backgroundColor = "rgb(255, 255, 245)"; 
	link.style.color = "rgb(0, 101, 189)";
	if(!isCurrent){/*Reset*/
	    try{link.removeAttribute('style');}catch(e){}
	}//if shall reset
    }//func setCurrentLinkStyle

    this.generate = function gen(){
	if (self.generateLMenu) { return generateLMenu(this);}
	else {error('QuirkLeftMenu.generate: Funkce generateLMenu neni definovana.'); return null;}
    }//this.generate dynamic menu. See func. generateLMenu in qlm_generatemenu.js

    this.error = function errorF (msg){
	if( (typeof DEBUG)!='undefined' && DEBUG) {window.alert("DEBUG:"+msg);}
	else { window.status="CHYBA: "+msg; }
    }//errorF

    //*************************************************************************** Functions operating on menu
    //*************************************************************************** Functions operating on menu

    //@param menu = document.getElementById('dynamicLMenuID') 
    this.closeNav = function closeNav(menu)
    {
	var x = menu.getElementsByTagName( this.submenuTAG.itag );
	for (var i=0;i<x.length;i++)
	{
	    if (x[i].className == this.submenuTAG.iclass)
		x[i].style.display = 'none';
	}
    }

    //********************************************************** item->label->submenu
    //@return submenu for this label/element included in a label
    this.getSubmenu4Label = function getSubmenu4Label( label ){
	var nextSib = label.nextSibling;
	while (nextSib.nodeType != 1) //ignore #TEXT elements
	{ nextSib = nextSib.nextSibling; }
	return nextSib;
    }
    //@return label of submenu the item belongs (1st non-text sibling node of the parent) to or null
    this.getParentLabel = function getParentLabel( item ){
	var prevSib = item.parentNode.previousSibling;
	while (prevSib && prevSib.nodeType != 1) //ignore #TEXT elements
	{ prevSib = prevSib.previousSibling; }
	return ( prevSib && this.labelTAG.belongs(prevSib) )? prevSib : null;
    }//func

    //@return label of which labelSuccessor is a successor (is in it)
    this.getEncapsulatingLabel = function ( labelSuccessor ){
	while ( !this.labelTAG.belongs(labelSuccessor) ){ labelSuccessor = labelSuccessor.parentNode; }
	return labelSuccessor;
    }//getEncapsulatingLabel

    //@return link of which llinkSuccessor is a successor (is in it)
    this.getEncapsulatingLink = function ( linkSuccessor ){
	while ( !this.linkTAG.belongs(linkSuccessor) ){ linkSuccessor = linkSuccessor.parentNode; }
	return linkSuccessor;
    }//getEncapsulatingLabel

    //******************************************************* changing visibility
    //Open/close submenu for the label. //@param makeVisible [optional] set to visible if true
    //@return true if was made visible
    this.toggleSubmenuVisible = function toggleSubmenuVisible( label, makeVisible ){
	var submenu = this.getSubmenu4Label(label);
	submenu.style.display = (makeVisible || submenu.style.display == 'none') ? 'block' : 'none';
	return submenu.style.display == 'block';
    }//toggleSubmenuVisible

    //Exapands menu to make the link visible and perhaps highlights it.
    //@param menu document.getElementById('dynamicLMenuID')
    this.expandCurrentItem =  function expandCurrentItem( item, menu ) {
	this.closeNav(menu);
	var submenu = item.parentNode;
	while (this.submenuTAG.belongs(submenu)) {
	    submenu.style.display = 'block';//make visible
	    submenu = submenu.parentNode;
	}//while = for all submenus the item is in
	var isLink = this.linkTAG.belongs(item);
	if(!isLink){ this.toggleSubmenuVisible(item,true); }//if-else is link
	this.gCurrentItem.set(item,isLink);
	this.setCookie(item.id);
    }//expandCurrentItem
    // *************************************************************************************** cookies handling
    this.setCookie = function setCookie(value){ document.cookie = this.G_COOKIE_NAME+"="+value+"; path=/"; }
    //@return value of the cookie (id of the current link) or null
    this.getCookie = function getCookie()
    {
	var nameEQ = this.G_COOKIE_NAME + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
    }
    // ********************************************** hide + show
    this.showMenu = function(makeVisible){
	var menu = document.getElementById( this.dynamicLMenuID );
	menu.style.display = (makeVisible)? 'block' : 'none';
    };

    this.hide = function(){this.showMenu(false)};
    this.show = function(){this.showMenu(true)};

    //Return true if is supported;
    this.showIfSupported = function(){
	var isSupported = (!(navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1) && 		document.getElementById && document.createElement);
	if( !isSupported) { 
	    this.error('Browser nepodoporuje standard W3C DOM1, dynamické menu nebude zobrazeno.');
	    return false;
	}//if unsupported
	this.show();
	return isSupported;
    }//showIfSupported

}//class QuirkLeftMenu ************************************************************************* /QuirkLeftMenu

