/**
 * Funkcja dodaje strone do ulubionych (tylko IE)
 *
 * @param		string		sURL						- adres strony
 * @param		string		sName						- nazwa strony
 */
function addToBookmarks(sURL, sName) {
	sNav = navigator.userAgent.toLowerCase();
	if (sNav.indexOf('msie') != -1 && sNav.indexOf('opera') == -1) {
		window.external.AddFavorite(sURL, sName);
	}
	else {
		alert ('Dostepne tylko dla przeglądarki Microsoft Internet Explorer');
	}
}

/**
 * Funkcja ustawia strone jako startowa (tylko IE)
 *
 * @param		string		sURL						- adres strony
 */
function setHomePage(oA, sURL) {
	sNav = navigator.userAgent.toLowerCase();
	if (sNav.indexOf('msie') != -1 && sNav.indexOf('opera') == -1) {
		oA.style.behavior='url(#default#homepage)';
		oA.setHomePage(sURL);
	}
	else {
		alert ('Dostepne tylko dla przeglądarki Microsoft Internet Explorer');
	}
}

function showPopup(sURL, sImgSrc, iImgWidth, iImgHeight, sImgAlt) {
  var iWindowWidth  = iImgWidth;
  var iWindowHeight = iImgHeight;
  var sParameters   = '';
  var sWindowName   = '';
  var sScrollBars   = '';
  iWindowWidth += 20;
  iWindowHeight += 20;
  if (iWindowHeight > screen.availHeight || iWindowWidth > screen.availWidth) {
    sScrollBars = 'scrollbars=yes';
    if (iWindowHeight > screen.availHeight) {
      iWindowHeight = screen.availHeight - 20;
    }
    if (iWindowWidth > screen.availWidth) {
      iWindowWidth = screen.availWidth - 20;
    }
  }
  else {
    sScrollBars = 'scrollbars=no';
  }
  iLeft = eval((screen.availWidth - iWindowWidth) / 2);
  iTop = eval((screen.availHeight - iWindowHeight) / 2);
  
  sParameters = "width=" + iWindowWidth + ",height=" + iWindowHeight + ", toolbar=no, location=no, directories=no, status=no, menubar=no, resizable=no, left=" + iLeft + ", top=" + iTop + ", " + sScrollBars;
  sURL += '?img=' + sImgSrc + '&w=' + iImgWidth + '&h=' + iImgHeight +
    '&alt=' + sImgAlt;
  sImgSrc = sImgSrc.replace(/\//gi, '_');
  sImgSrc = sImgSrc.replace(/\\/gi, '_');
  sImgSrc = sImgSrc.replace(/\./gi, '_');
  window.open(sURL, sImgSrc, sParameters);
}

function showPrintPopup(sURL, iImgWidth, iImgHeight) {
  var iWindowWidth  = iImgWidth;
  var iWindowHeight = iImgHeight;
  var sParameters   = '';
  var sWindowName   = 'print';
  var sScrollBars   = 'scrollbars=yes';
  if (iWindowHeight > screen.availHeight || iWindowWidth > screen.availWidth) {
    if (iWindowHeight > screen.availHeight) {
      iWindowHeight = screen.availHeight;
      if ((screen.availWidth - iWindowWidth) > 20) {
        iWindowWidth += 20;
      }
      else {
        iWindowWidth = screen.availWidth;
      }
    }
    if (iWindowWidth > screen.availWidth) {
      iWindowWidth = screen.availWidth;
      if ((screen.availHeight - iWindowHeight) > 20) {
        iWindowHeight += 20;
      }
      else {
        iWindowHeight = screen.availHeight;
      }
    }
  }
  
  sParameters = "width=" + iWindowWidth + ",height=" + iWindowHeight + ", toolbar=no, location=no, directories=no, status=no, menubar=no, resizable=no, left=" + eval((screen.width - iWindowWidth) / 2) + ", top=" + eval((screen.height - iWindowHeight) / 2) + ", " + sScrollBars;
  
  sWindowName = sURL.replace(/\W/g, '');
  window.open(sURL, sWindowName, sParameters);
}

function ShowMessageBox(sPrefix, sMsg, sPostfix, bError) {
	alert(sPrefix + "\n" + sMsg + sPostfix);
	return !bError;
}

function getObject(sID, oDoc) {
  if (!oDoc) {
  	oDoc = document;
  }
  if(document.layers) {
    if(oDoc.layers[sID]) {
    	return oDoc.layers[sID];
    }
    else {
      for(var x = 0, y; !y && x < oDoc.layers.length; x++) {
        y = getObject(sID, oDoc.layers[x].document);
      }
      return y;
    }
  }
  if(document.getElementById) {
  	return oDoc.getElementById(sID);
  }
  if(document.all) {
  	return oDoc.all[sID];
  }
  return document[sID];
}

function resizeToContent(sObject, iMargin) {
	var x = window;
	
	// okreslenie rozmiarow obiektu do ktorego bedzie dopasowywany rozmiar okna
	var oObj = getObject(sObject, x.document);
	if (!oObj) {return false;}
  var oW = oObj.style.width ? parseInt(oObj.style.width) : (oObj.offsetWidth ? oObj.offsetWidth : oObj.clip.width);
  if(!oW) {return false;}
  oW += 2 * iMargin;
  var oH = oObj.offsetHeight ? oObj.offsetHeight : oObj.clip.height;
  if(!oH) {return false;}
  oH += 2 * iMargin;
  
  // okreslenie aktualnych rozmiarow okna
  var myW = 0;
  var myH = 0;
  var d = x.document.documentElement;
  var b = x.document.body;
  if (x.innerWidth ) {
  	myW = x.innerWidth;
  	myH = x.innerHeight;
  }
  else if (d && d.clientWidth) {
  	myW = d.clientWidth;
  	myH = d.clientHeight;
  }
  else if (b && b.clientWidth) {
  	myW = b.clientWidth;
  	myH = b.clientHeight;
  }
  if(window.opera && !document.childNodes) {
  	myW += 16;
  }
  
  //alert(myW + ' => ' + myH);
  x.resizeBy(oW - myW, oH - myH);
  
  return false;
}

function toggleActivity(oCheckbox, fieldPrefix, disabledClass, enabledClass) {
	var oForm = oCheckbox.form;
	
	for (var i = 0; i < oForm.elements.length; i++) {
		if (oForm.elements[i].name.indexOf(fieldPrefix) == 0) {
			oForm.elements[i].disabled = !oCheckbox.checked;
			oForm.elements[i].className = (oCheckbox.checked ? enabledClass : disabledClass);
			getObject(oForm.elements[i].name + '_label').style.display = (oCheckbox.checked ? 'inline' : 'none');
		}
	}
}

function showHide(oLink, sShow, sHide) {
	oDiv = getObject('d_' + oLink.id);
	if (oDiv.style.display == 'none') {
		oDiv.style.display = 'block';
		oLink.innerHTML = sHide;
		oLink.className = 'hideLink';
	}
	else {
		oDiv.style.display = 'none';
		oLink.innerHTML = sShow;
		oLink.className = 'showLink';
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = 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;
}

function setFontSize(sId) {
	iSize = readCookie('fontSize');
	if (iSize != null) {
		oObj = getObject(sId);
		if (oObj != null) {
			oObj.style.fontSize = iSize + 'px';
		}
	}
}

function decreaseFont(sId) {
	oObj = getObject(sId);
	if (oObj != null) {
		if (oObj.currentStyle) {
			iSize = parseInt(oObj.currentStyle.fontSize);
	  }
		else {
			iSize = parseInt(window.getComputedStyle(oObj, null).fontSize);
		}
		if (iSize > 10) {
			iSize -= 2;
			oObj.style.fontSize = iSize + 'px';
			createCookie('fontSize', iSize, 365);
		}
	}
}
function increaseFont(sId) {
	oObj = getObject(sId);
	if (oObj != null) {
		if (oObj.currentStyle) {
			iSize = parseInt(oObj.currentStyle.fontSize);
	  }
		else {
			iSize = parseInt(window.getComputedStyle(oObj, null).fontSize);
		}
		if (iSize < 18) {
			iSize += 2;
			oObj.style.fontSize = iSize + 'px';
			createCookie('fontSize', iSize, 365);
		}
	}
}
function changeIssue(oSelect) {
	var sLocation = 'http://' + window.location.host + window.location.pathname + '?mod=m_wydania';
	if (oSelect.selectedIndex > 0) {
		sLocation += '&ino=' + oSelect.options[oSelect.selectedIndex].value;
	}
	window.location.href = sLocation;
}