window.onload = function() {
	external_links();
  popups();
  pb_read_cookie();
}
window.onunload = function() {
  pb_write_cookie();
}
/** 
 * external links, v.1.0
 * ustawienie \target="_blank"\ dla wszystkich linkow zawierajacych \rel="external"\
*/
function external_links() {
	if (!document.getElementsByTagName) {return false;}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
		if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external")
			anchors[i].target = "_blank";
}

/** 
 * popup windows
 * open a new window with an image or a page
*/
function popups()
{
  if (!document.body.getElementsByTagName) return false;
  var as = document.body.getElementsByTagName('a');
  for(var i=0; i<as.length; i++)
    if ((as[i].className+'').match(/\bpopup\b/))
      as[i].onclick = popup;
  return true;
}
function popup()
{
  var url = this.getAttribute('href');
  var title = this.getAttribute('title');
  if (title) var size = title.match(/\(([0-9]+)x([0-9]+)\)/);
  if (size) title = title.replace(size[0],'');
  if (url.match(/png|gif|jpg|jpe|jpeg/)) var img = true;
  
  /* when size is larger then screen */
  var scrollbars = 0;
  if (size && img) {
    if (size[1] > window.screen.width) {
      size[1] = window.screen.width - 16;
      scrollbars = 1;
    }
    if (size[2] > window.screen.height - 200) {
      size[2] = window.screen.height - 200;
      size[1] = parseInt(size[1]) + 16;
      if (size[1] == window.screen.width) size[1] = window.screen.width - 16;
      scrollbars = 1;
    }
  }  
  if (!size) var size = new Array('', 640, 480);

  /* window options */
  var win_options = 'left=0,top=0,menubar=0,toolbar=0,location=0,status=0,scrollbars='+scrollbars+',resizable=1,dependent=1,width='+size[1]+',height='+size[2];

  /* creates new window */
  if (win = window.open(url,'',win_options)) {
    if (img) {
      win.document.open();
      win.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
      win.document.write('<html xmlns="http://www.w3.org/1999/xhtml">');
      win.document.write('<head><title>'+title+'</title>');
      win.document.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
      win.document.write('<style type="text/css">body {margin:0; padding:0; background:#fff;}</style>');
      win.document.write('</head><body>');
      win.document.write('<img src="'+url+'" alt="'+title+'" onclick="window.close()" />');
      win.document.write('</body></html>');
      win.document.close();
    }
    return false;
  }
  
  /* follows link on failure */
  return true; 
}


var pb_position = 0;
var pb_paused = 0;
var pb_stopped = 0;

function pb_3(v1,v2,v3){
  pb_paused = v1;
  pb_stopped = v2;
  pb_position = v3;
}
function pb_write_cookie() {
  create_cookie('pb_paused',pb_paused,0);
  create_cookie('pb_position',pb_position,0);
  create_cookie('pb_stopped',pb_stopped,0);
}
function pb_read_cookie() {
  pb_paused = readCookie('pb_paused');
  pb_stopped = readCookie('pb_stopped');
  pb_position = readCookie('pb_position');
}
function create_cookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else 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;
}
