/* ========================================
  menufn.js
  funkcje generujšce menu
  wersja - 2006-11-20
  Š Piotr Szałkowski, 2007
======================================== */ 

var IE = (navigator.userAgent.indexOf('MSIE') != -1);
var OP = (navigator.userAgent.indexOf('Opera') != -1);
var FF = (navigator.userAgent.indexOf('Firefox') != -1);

var lv = level(page_level); // path for files on folders in level 1,2 ...

function level() {
    var pt = '../';
    var sp = '';
    if ((typeof(page_level) != 'undefined') && (page_level > 0) ) {
        for (var i = 1; i <= page_level; i++) {
            sp = sp + pt;
        }
    }
    return sp
}


//stan lewego menu
var leftmenustatus = GetLMStatus();


function GetLMStatus() {
	var aSrch = document.location.search.substr(1).split('&');
	var ms = null;
	if (aSrch.length > 0) {
		for (var i=0; i < aSrch.length; i++) {
    			var aCrumb = aSrch[i].split('=');
    			if (aCrumb[0]=='lms') {
				// stan menu odczytany z adresu
				// strona wywołana z menu (funkcja gotopage())
				// synchronizuj cookie
				SetInCookie('lms', aCrumb[1])
				ms = aCrumb[1];
			}
		}
	}
	// czy status menu w cookie ?
	if (ms == null) {
		var msfc = GetIntFromCookie('lms');
		ms = (msfc == null) ? 0 : msfc;
	}
	return ms;
}

//alert('leftmenustatus = ' + leftmenustatus + '\n' + 'cookie: ' + document.cookie);


// ==================================================
// funkcje do obsługi cookies

function SetInCookie(cName, cValue) {
  if (navigator.cookieEnabled) {
	document.cookie = 'path=/';
	document.cookie = cName + "=" + cValue;
  }
}

function GetIntFromCookie(cName) {
  if (navigator.cookieEnabled) {
	var cv = GetCookie(cName);
	if (typeof parseInt(cv) == 'number') {
		return cv
	} else {
		return null;
	}
  } else {
	return null;
  }
}

function GetCookie(cName) {
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++) {
    var aCrumb = aCookie[i].split("=");
    if (cName == aCrumb[0]) {
      return aCrumb[1];

	}
  }
  // a cookie with the requested name does not exist
  return null;
}
// ==================================================

function showhidemenu(mID, nId) {
	var oMid = document.getElementById(mID);
	if (oMid.style.display=='none'||oMid.style.display=='') {

		oMid.style.display='block';
		leftmenustatus = SetBitValue(leftmenustatus, nId, 1);
	}
	else { 
		oMid.style.display='none' 
		leftmenustatus = SetBitValue(leftmenustatus, nId, 0);
	}
	// synchronizuj cookie
	SetInCookie('lms', leftmenustatus)
}


function writeCurrentDate() {
   var MonthName = new Array("stycznia","lutego","marca","kwietnia","maja","czerwca","lipca","sierpnia","września","października","listopada","grudnia")
   var DayName = new Array("niedziela","poniedziałek","wtorek","środa","czwartek","piątek","sobota")
   var d = new Date();
   var sD = "<span style='color:darkcyan'>Dzisiaj jest "
   sD += DayName[d.getDay()] + ", "
   sD += d.getDate() + " ";
   sD += MonthName[(d.getMonth())] + " ";
   sD += d.getFullYear() + " r.</span>";
   return sD;
}

function brightness(kolor,prc) {
   if(prc >= -100 && prc <= 100) {
	var R1 = eval("0x"+kolor.substr(1,2));
	var G1 = eval("0x"+kolor.substr(3,2));
	var B1 = eval("0x"+kolor.substr(5,2));

	if(prc>=0 && prc<=100) { //rozjasnianie
		var R2 = parseInt(R1+((255-R1)*prc/100));
		var G2 = parseInt(G1+((255-G1)*prc/100));
		var B2 = parseInt(B1+((255-B1)*prc/100));
	} else {
		var pra = Math.abs(prc);
		var jr = (R1/255);
		var jg = (G1/255);
		var jb = (B1/255);
		var jmax = 0
		jmax = Math.max(jmax,jr)
		jmax = Math.max(jmax,jg)
		jmax = Math.max(jmax,jb)
		var jmin = jmax-(jmax*(pra/100))
		var R2 = Math.ceil(R1*jmin/jmax);
		var G2 = Math.ceil(G1*jmin/jmax);
		var B2 = Math.ceil(B1*jmin/jmax);
	}
	var kolor2 = "#";
	var kolor2 = kolor2 + (R2<16 ? "0" : "") + R2.toString(16);
	var kolor2 = kolor2 + (G2<16 ? "0" : "") + G2.toString(16);
	var kolor2 = kolor2 + (B2<16 ? "0" : "") + B2.toString(16);
	return kolor2;
   }
}

function changecolors(obj, bgcolor, txcolor, borders) {
	obj.style.backgroundColor=bgcolor;
	obj.style.color=txcolor;
	obj.style.borderLeftColor=brightness(bgcolor,borders[0]);
	obj.style.borderTopColor=brightness(bgcolor,borders[1]);
	obj.style.borderRightColor=brightness(bgcolor,borders[2]);
	obj.style.borderBottomColor=brightness(bgcolor,borders[3]);
}

function showctive(obj, bcolor, tcolor) {
	obj.style.backgroundColor=bcolor;
	obj.style.color=tcolor;
  	obj.style.borderStyle='solid';
	obj.style.borderLeftColor=brightness(bcolor,80); //'#ffffff';
	obj.style.borderTopColor=brightness(bcolor,80); //'#ffffff';
	obj.style.borderRightColor=brightness(bcolor,-50); //'#000000';
	obj.style.borderBottomColor=brightness(bcolor,-70); //'#000000';
}

function shownormal(obj, bcolor, tcolor) {
	obj.style.backgroundColor=bcolor;
	obj.style.color=tcolor;
  	obj.style.borderStyle='solid';
	obj.style.borderLeftColor=brightness(bcolor,30); //bcolor;
	obj.style.borderTopColor=brightness(bcolor,40); //'#ffffff';
	obj.style.borderRightColor=brightness(bcolor,-20); //bcolor;
	obj.style.borderBottomColor=brightness(bcolor,-50); //bcolor;
}

function writeaction(act) {
	var a = '';
	var i;
	    //typy plików ładowane domyślnie do bieżącego okna
    var ext_h = new Array('.htm','.html','.shtm','.asp','.aspx','.mspx','.php','.xml');
        //typy plików wyświetlane domyślnie w nowym oknie
    var ext_f = new Array('.pdf','.doc','.rtf','.pps','.ppt','.xls','.txt','.csv','.jpg','.gif','.png','.svg');
        //otwierane w nowym oknie
    var pre = new Array('http:','https:','ftp:','www.');
    
	if(act.length>=3) {
		var n = 0;
		for(i = 0; i < ext_h.length; i++) {
			n += (act.indexOf(ext_h[i]) == -1) ? 0 : 1;
		}
		
		var m = 0;
		for(i = 0; i < ext_f.length; i++) {
			m += (act.indexOf(ext_f[i]) == -1) ? 0 : 1;
		}
		
		var k = 0;
		for(i = 0; i < pre.length; i++) {
			k += (act.indexOf(pre[i]) == 0) ? 1 : 0;
		}
		//czy funkcja
        if (act.indexOf('(')!=-1 && act.indexOf(')')!=-1) {
		    a = act;
        } else {
		    //skrypt
            if (n==0 && m==0 && k==0) {
		        a = act;
            } else {
		        //www lub plik
		        if ((k > 0) || ((m > 0) && (act.length >= 5))) {
		            var ht = (act.indexOf('www.')==0) ? 'http://' : ''
		            a = 'winopen(\'' + ht + act + '\')';
                } else {
		            //link 
		            if ((n > 0) && (act.length >= 5) && (act.indexOf('.',1)!=-1) && (act.indexOf('.href',1)==-1)) { 
				a = 'gotopage(\'' + act + '\')';
				//a = 'location.href=\'' + act + '\'';
		            }
                }
            }
        }
	}
	return a;
}

function gotopage(page) {
	if (leftmenustatus >= 0) {
		location.href=page + '?lms=' + leftmenustatus;
	} else {
		location.href=page;
	}
}

function setstatus(stat) {
    self.status=stat;
    return true;
}

function allowspecchar(str) {
    var rs = str;
    var re = /(\")(.*)\1/g;
    rs = rs.replace(re, '\&bdquo;$2\&rdquo;');
    var re = /(\')(.*)\1/g;
    rs = rs.replace(re, '\&lsquo;$2\&rsquo;');
    var re = /\"/g;
    rs = rs.replace(re, '&ldquo;');
    var re = /\'/g;
    rs = rs.replace(re, '&rsquo;');
    var re = /---/g;
    rs = rs.replace(re, '&mdash;');
    var re = /--/g;
    rs = rs.replace(re, '&ndash;');

    return rs
}

function winopen(url) {
    window.open(url,'_blank')
}

function opendialog(url,w,h,resize) {
    var wd = 200;
    var hd = 150;
    var res = 'no';
    if (w > 200) wd = w;
    if (h > 150) hd = h;
    res = (resize > 0 ? 'yes' : 'no');
    if (wd > screen.availWidth) {wd = screen.availWidth - 16; res='yes';}
    if (hd > screen.availHeight) {hd = screen.availHeight - 16; res='yes';}
    var winfeat = 'left=0, top=0, height=' + hd + ',width=' + wd + ',status=no,toolbar=no,menubar=no,location=no,resizable=' + res;
    window.open(url,'_blank',winfeat);
}

function writemmenu() {
    var mw = 0;
    var i;
    for (i = 0; i < mm_w.length; i++) {
        mw = mw + mm_w[i];
    }
    document.write('<table width="' + mw + '" border="0" cellspacing="0" cellpadding="0"><tr>');
    for (i = 0; i < mm_items; i++) {
        document.write('<td width="' + mm_w[i] + '" class="mainmenuitem" onmouseover="changecolors(this, mm_bgcolor_a, mm_txcolor_a, mm_borders_a);setstatus(\'' + allowspecchar(stat_mm[i]) + '\')" onmouseout="changecolors(this, mm_bgcolor_n, mm_txcolor_n, mm_borders_n);setstatus(\' \')" onclick="' + writeaction(actn_mm[i]) + '">' + text_mm[i] + '</td>');
    }
    document.write('</tr></table>');
}

function writeleftmenu(smId) {
    var i;
    var btc;
    var smc;
    btc = '';
    btc = btc + '<td class="buttmenu" ';
    //btc = btc + 'style="background-image:url(\'' + bt_image_n[smId] + '\')"; ';
    btc = btc + 'style="background:' + bt_bgcolor_n + ' url(\'' + bt_image_n[smId] + '\')"; ';
    //btc = btc + 'onmouseover="this.style.backgroundImage=\'url(' + bt_image_a[smId] + ')\'';
    btc = btc + 'onmouseover="this.style.background=\'' + bt_bgcolor_a + ' url(' + bt_image_a[smId] + ')\'';
    btc = btc + ';this.style.color=\'' + bt_txcolor_a + '\' ';
    btc = btc + ';this.style.fontWeight=\'bold\' ';
    btc = btc + ';setstatus(\'' + allowspecchar(stat_bt[smId]) + '\')';
    btc = btc + '" ';
    //btc = btc + 'onmouseout="this.style.backgroundImage=\'url(' + bt_image_n[smId] + ')\' ';
    btc = btc + 'onmouseout="this.style.background=\'' + bt_bgcolor_n + ' url(' + bt_image_n[smId] + ')\' ';
    btc = btc + ';this.style.color=\'' + bt_txcolor_n + '\' ';
    btc = btc + ';this.style.fontWeight=\'normal\' ';
    btc = btc + ';setstatus(\' \')';
    btc = btc + '" '
    if (sm_items[smId] > 0) {
        btc = btc + 'onclick="showhidemenu(\'SubMenu' + smId + '\',' + smId + ')"';
    } else {
        btc = btc + 'onclick="' + writeaction(actn_bt[smId]) + '"';
    }
    btc = btc + '>'
    btc = btc + text_bt[smId];
    btc = btc + '</td>';
    
    //button
    document.write('<table width="' + bt_w + '" border="0" cellspacing="0" cellpadding="0"><tr>');
    document.write(btc);
    //document.write('<td class="buttmenu" style="background-image:url(\'' + bt_image_n[smId] + '\')"; onclick="showhidemenu(\'SubMenu' + smId + '\')" onmouseover="this.style.backgroundImage=\'' + bt_image_a[smId] + '\';this.style.color=\'' + bt_txcolor_a + '\';this.style.fontWeight=\'bold\';setstatus(allowspecchar(\'' + stat_bt[smId] + '\'))"; onmouseout="this.style.backgroundImage=\'' + bt_image_n[smId] + '\';this.style.color=\'' + bt_txcolor_n + '\';this.style.fontWeight=\'normal\';setstatus(\' \')">' + text_bt[smId] + '</td>');
    document.write('</tr></table>');

    //submenu
    if (sm_items[smId] > 0) {
        document.write('<div id="SubMenu' + smId + '" class="submenu">');
        document.write('<table width="' + sm_w + '" border="0" cellspacing="0" cellpadding="0">');
        for (i = 0; i < sm_items[smId]; i++) {
            smc = '';
            smc = smc + '<tr><td class="submenuitem" ';
            smc = smc + 'onmouseover="changecolors(this, sm_bgcolor_a, sm_txcolor_a, sm_borders_a);';
            smc = smc + 'setstatus(\'' + allowspecchar(stat_sm[smId][i]) + '\')';
            smc = smc + '" ';
            smc = smc + 'onmouseout="changecolors(this, sm_bgcolor_n, sm_txcolor_n, sm_borders_n);';
            smc = smc + 'setstatus(\' \')';
            smc = smc + '" ';
            smc = smc + 'onclick="' + writeaction(actn_sm[smId][i]) + '"';
            smc = smc + '>';
            smc = smc + text_sm[smId][i];
            smc = smc + '</td></tr>';
            document.write(smc);
        }
        document.write('</table></div>');
	
	var oName = 'SubMenu' + smId;
	var oMid = document.getElementById(oName);
	if (GetBitValue(leftmenustatus, smId)) {
		oMid.style.display = 'block';
	}
 
    }
}

function writeadv(m) {
	if (m != undefined) {
		mask = m;
	} else {
		mask = advmask;
	}
	var i,k;
	for (i = 0; i < advitems; i++) {
		k = Math.pow(2,i);
		if ((k & mask) == k) {
			document.write('<div id="Ad' + (i) + '" style="' + advstyle[i] + '" onclick="' + writeaction(advact[i]) + '">');
			document.write(advhtml[i]);
			document.write('</div>');
		}
	}
}

function powieksz(objName) {
/*
  Powiększa wielkosc czcionki o 1px w elemencie o nazwie objName
  oraz proporcjanalnie we wszystkich elementach dziedziczonych.
  Powiększa proporcjonalnie odstępy między wierszami.
*/
	var obj = document.getElementById(objName);
	var styl = IE ? obj.currentStyle : window.getComputedStyle(obj,'');
	var fs = parseInt(styl.fontSize);
	var lh = parseInt(styl.lineHeight);
	var fsc;
	var lhc;
	var ch_styl;
	var ch_obj;
	var msg = '';
	if (fs <= 24) {
		var pfs = fs;
		var plh = lh;
		fs = fs + 1;
		lh = Math.round(fs*plh/pfs);
		fs = fs + 'px';
		lh = lh + 'px';
		var n = obj.childNodes.length;
		for (var i = 0; i < n; i++) {
			if (obj.childNodes[i].tagName != undefined && obj.childNodes[i].innerHTML !='') {
				ch_obj = obj.childNodes[i];
				ch_styl = IE ? ch_obj.currentStyle : window.getComputedStyle(ch_obj,'');
				if (obj.childNodes[i].tagName != 'TABLE') {
					fsc = parseInt(ch_styl.fontSize);
					lhc = parseInt(ch_styl.lineHeight);
					pfsc = fsc;
					plhc = lhc;
					fsc = fsc + Math.round(fsc/pfs);
					lhc = lhc + Math.round(lhc/plh);
					fsc = fsc + 'px';
					lhc = lhc + 'px';
					if (pfsc <=28) {
						ch_obj.style.fontSize = fsc;
						ch_obj.style.lineHeight = lhc;
					}
				}
			}
		}
		obj.style.fontSize = fs;
		obj.style.lineHeight = lh;

	}
}

function pomniejsz(objName) {
/*
  Zmniejsza wielkosc czcionki o 1px w elemencie o nazwie objName
  oraz proporcjanalnie we wszystkich elementach dziedziczonych.
  Zmniejsza proporcjonalnie odstępy między wierszami.
*/
	var obj = document.getElementById(objName);
	var styl = IE ? obj.currentStyle : window.getComputedStyle(obj,'');
	var fs = parseInt(styl.fontSize);
	var lh = parseInt(styl.lineHeight);
	var fsc;
	var lhc;
	var ch_styl;
	var ch_obj;
	var msg = '';
	if (fs > 9) {
		var pfs = fs;
		var plh = lh;
		fs = fs - 1;
		lh = Math.round(fs*plh/pfs);
		fs = fs + 'px';
		lh = lh + 'px';
		var n = obj.childNodes.length;
		for (var i = 0; i < n; i++) {
			if (obj.childNodes[i].tagName != undefined && obj.childNodes[i].innerHTML !='') {
				ch_obj = obj.childNodes[i];
				ch_styl = IE ? ch_obj.currentStyle : window.getComputedStyle(ch_obj,'');
				if (obj.childNodes[i].tagName != 'TABLE') {
					fsc = parseInt(ch_styl.fontSize);
					lhc = parseInt(ch_styl.lineHeight);
					pfsc = fsc;
					plhc = lhc;
					fsc = fsc - Math.round(fsc/pfs);
					lhc = lhc - Math.round(lhc/plh);
					fsc = fsc + 'px';
					lhc = lhc + 'px';
					ch_obj.style.fontSize = fsc;
					ch_obj.style.lineHeight = lhc;
			  	}
			}
		}
		obj.style.fontSize = fs;
		obj.style.lineHeight = lh;
	}
}

function changeFontSize(block) {
	document.writeln("<div style=\"margin-left:3px; margin-top:10px; margin-bottom:10px; padding:2px; width:126; height:16; background-color:#FDF9EA; border:solid 1px #77BBFF\">");
	document.writeln("Rozmiar tekstu:");
	document.writeln("<a href=\"javascript:powieksz('" + block + "')\" title=\"Powiększ czcionkę\" onmouseover=\"self.status='Powiększ czcionkę';return true\" onmouseout=\"self.status=\'\';return true\"><span style=\"font-size:16px; line-height:16px\">A</span></a>");
	document.writeln("<a href=\"javascript:pomniejsz('" + block + "')\" title=\"Pomniejsz czcionkę\" onmouseover=\"self.status='Pomniejsz czcionkę';return true\" onmouseout=\"self.status=\'\';return true\"><span style=\"font-size:11px; line-height:11px\">A</span></a><br />");
	document.writeln("</div>");
}


function GetBitValue(number, bit) {
//zwraca wartoć n-tego bitu z liczby naturalnej 
	var bitval = Math.pow(2, bit);
	return (number & bitval);
}

function SetBitValue(number, bit, val) {
//ustawia wartoć val n-tego bitu w liczbie
	var bitval = Math.pow(2, bit);
	if (val) { // ustaw 1
		return (number | bitval);
	} else { // ustaw 0
		if ((number & bitval) == bitval) {
			return (number - bitval);
		} else {
			return number;
		}
	}

}