// ---------------------------------------------------------------------
// Browser Detection 
// 
// ---------------------------------------------------------------------
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false; 
isDOM=(document.getElementById)?true:false
isOpera=isOpera5=window.opera && isDOM
isOpera6=isOpera && window.print
isOpera7=isOpera && navigator.userAgent.indexOf("Opera 7") > 0 || navigator.userAgent.indexOf("Opera/7") >= 0
isMSIE=isIE=document.all && document.all.item && !isOpera
isMSIE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false; 
isMSIEmac = ((document.all)&&(isMac)) ? true : false; 
isNC=navigator.appName=="Netscape"
isNC4=isNC && !isDOM
isNC6=isNC && isDOM



// ---------------------------------------------------------------------
// Safe loading into window.onload
//
// Body onload utility (supports multiple onload functions) 
// ---------------------------------------------------------------------
var gSafeOnload = new Array(); 
function SafeAddOnload(f) 
{ 
  if (isMSIEmac && isMSIE4)  // IE 4.5 blows out on testing window.onload 
  { 
    window.onload = SafeOnload; 
    gSafeOnload[gSafeOnload.length] = f; 
  } 
  else if  (window.onload) 
  { 
    if (window.onload != SafeOnload) 
    { 
      gSafeOnload[0] = window.onload; 
      window.onload = SafeOnload; 
    } 
    gSafeOnload[gSafeOnload.length] = f; 
  } 
  else 
    window.onload = f; 
} 
function SafeOnload() 
{ 
  for (var i=0;i<gSafeOnload.length;i++) 
    gSafeOnload[i](); 
} 



// ---------------------------------------------------------------------
// Set a layer opacity
// 
// ---------------------------------------------------------------------
function SetLayerOpacity(layername, opacitylevel) {
  if (!isDOM) {
    return false;
  }
  if(isMSIE) {
    document.getElementById(layername).style.filter='alpha(opacity='+opacitylevel+')';
  }
	if(isNC6) {
    document.getElementById(layername).style.MozOpacity = opacitylevel/100;
  }
}



// ---------------------------------------------------------------------
// Set a cookie
// 
// ---------------------------------------------------------------------
function SetCookie(cookieName, cookieValue, nDays) {
  var today = new Date();
  var expire = new Date();
  if (nDays==null || nDays==0) {
    nDays=1;
  }
   expire.setTime(today.getTime() + 3600000*24*nDays);
   document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}


// ---------------------------------------------------------------------
// Print data in div
//
// ---------------------------------------------------------------------

function PrintDivContent(pDivId) {
  id = document.getElementById(pDivId);
  
  width = 580;
  height = 400;
  var odstepwysokosc  = (screen.height - height) / 2;
  var odstepszerokosc = (screen.width -  width) / 2;
  okno = window.open('', 'name1', 'height='+height+',width='+width+',hotkeys=no,location=no,menubar=no,resizable=yes,screenX='+odstepszerokosc+'+,screenY='+odstepwysokosc+',left='+odstepszerokosc+',top='+odstepwysokosc+',scrollbars=yes,status=yes,titlebar=no,toolbar=no');
  if (isMSIE) {
    okno.print();
  }
  okno.document.write('<html><head><link rel="stylesheet" href="/css/style.css" type="text/css"><title>Wydruk</title></head><body style="margin:10px; background:#FFFFFF">');
  okno.document.write(id.innerHTML);
  if (!isMSIE) {
    okno.document.write('<script>window.print()</script>');
  }
  okno.document.write('</body></html>');
}



// ---------------------------------------------------------------------
// Open new browser window
// 
// Params:
//
//   pWidth - popup width in px
//   pHeight - popup height in px
//   pXpos - x window position or one of: center, left, right
//   pYpos - y window position or one of: center, top, bottom
//   pURL - URL to open in window - optional
//   pWindowName - name of window - optional
//   pFocusAfterLoad - focus window after load
//   pAddon - turn on addons. give what addons you want to enable separated by ,
//            ie: scrollbars,hotkeys,location,menubar,resizable,titlebar,toolbar
// ---------------------------------------------------------------------
function PopupWindow(pWidth, pHeight, pXpos, pYpos, pURL, pWindowName, pFocusAfterLoad, pAddon) {

  // Position X
  if ('center' == pXpos) {
    pXpos = Math.round((screen.width -  pWidth) / 2);
  }
  else if('left' == pXpos) {
    pXpos = 0;
  }
  else if('right' == pXpos) {
    pXpos = screen.width -  pWidth;
  }
  
  // Position Y
  if ('center' == pYpos) {
    pYpos = Math.round((screen.height - pHeight) / 2);
  }
  else if('top' == pYpos) {
    pYpos = 0;
  }
  else if('bottom' == pYpos) {
    pYpos = screen.height - pHeight;
  }
  
  var addons = '' + 
    'hotkeys=no' + 
    ',location=no' + 
    ',menubar=no' + 
    ',resizable=no' + 
    ',scrollbars=no' + 
    ',status=no' + 
    ',titlebar=no' + 
    ',toolbar=no';
    
  // Replace addons to yes - ie. status=no -> status=yes
  if (pAddon) {
    splitaddons = pAddon.split(',');
    
    for(i = 0; i < splitaddons.length; i++) {
      regexp = new RegExp(splitaddons[i] + '=no', 'i');
      addons = addons.replace(regexp, splitaddons[i] + '=yes');
    }
  }
    
  windowhandle = window.open(
    pURL, pWindowName, 
    'height=' + pHeight +
    ',width=' + pWidth +
    ',screenX=' + pXpos + 
    ',screenY=' + pYpos + 
    ',left=' + pXpos +
    ',top=' + pYpos + 
    addons
  );
  
  // Raise window after window load
  if (true == pFocusAfterLoad) {
    windowhandle.focus();
  }
  
  //windowhandle.opener = window;
  
  return windowhandle;
}



// ---------------------------------------------------------------------
// Show image in popup window
// 
// Params:
//
//   pWidth - popup width in px
//   pHeight - popup height in px
//   pXpos - x window position or one of: center, left, right
//   pYpos - y window position or one of: center, top, bottom
// ---------------------------------------------------------------------
function ShowImagePopup(pImage, pWidth, pHeight, pXpos, pYpos) {
  if (!pXpos) {
    pXpos = 'center';  
  }
  if (!pYpos) {
    pYpos = 'center';
  }
  handle = PopupWindow(pWidth, pHeight, pXpos, pYpos, '', 'picture_in_popup', 1, '');
  handle.document.write('' + 
  '<html><body style="margin:0px; padding:0px">' +
  '<a href="javascript: window.close()"><img src="' + pImage + '" border="0" width="' + pWidth + '" height="' + pHeight + '"></a>' +
  '</body></html>'
  );
}

// ---------------------------------------------------------------------
// Add event to object
//
// ---------------------------------------------------------------------
function AddEvent(pObject, pHandler, pFunction){

  if (!document.all && document.getElementById){
    pObject.setAttribute(pHandler, pFunction);
  }

  //workaround for IE 5.x and IE 6
  if (document.all && document.getElementById){
    pObject[pHandler.toLowerCase()] = new Function(pFunction);
  }
}

function $(pId) {
  return document.getElementById(pId);
}


function SwitchDisplay(pElementName) {
  if ($(pElementName).style.display == 'block') {
    $(pElementName).style.display = 'none';
  }
  else {
    $(pElementName).style.display = 'block';
  }
  
}