window.onload = function() {
	external_links();
	popup_init();
  header_pic_title();
  shadowed_imgs();
}
/** 
 * external links
 * sets \target="_blank"\ for all the links with \rel="external"\
*/
function external_links() {
  if (!document.body.getElementsByTagName) return false;
  var as = document.body.getElementsByTagName('a');
  for(var i=0; i<as.length; i++)
    if (as[i].getAttribute('rel') == 'external')
      as[i].target = '_blank';
  return true;
}

/** 
 * popup, v.1.0
 * dla linków z klasą "pupup" otwiera zdjęcie lub stronę w nowym oknie, ustala jego wielkość z tagu "rel"
*/
function popup_init()
{
	if (!document.getElementsByTagName) {return false;}
	var anchors = document.getElementsByTagName('a');
	for(i=0; i<anchors.length; i++)
		if (anchors[i].className == 'popup')
			anchors[i].onclick = popup;
	return true;
}
function popup()
{
	var size = this.getAttribute('rel');
	var url = this.getAttribute('href');
	if (/png|gif|jpg|jpe|jpeg/.exec(this.href)) 
		var img = true;

	/* computes dimensions passed from "rel" tag; format: <width>x<height>, output: width=<width>,height=<height> */
	if (size) {
		size = size.split('x');
		popup_size = 'width='+size[0]+',height='+(parseInt(size[1])+5); /* dodano 5px aby nie pojawialy sie scrollbare w przegladarkach */
	}
	else {
		var popup_size = 'width=640,height=480';
	}

	/* window options */
	var win_options = 'dependent=1,resizable=1,scrollbars=0,location=0,menubar=0,statusbar=0,'+popup_size;

	/* creates new window */
  if (win = window.open(this.href,'_blank',win_options)) {
		if (img) {
			var imgs = this.getElementsByTagName('img');
			if (imgs[0]) {
				var title = imgs[0].getAttribute('title');
				var alt = imgs[0].getAttribute('alt');
			}
			else {
				var title = this.getAttribute('title');
				var alt = title;
			}
			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="'+this.href+'" alt="'+alt+'" title="'+title+'" onclick="window.close()" />');
			win.document.write('</body></html>');
			win.document.close();
		}
		return false;
  }
	
  /* follows link on failure */
	return true; 
}

function header_pic_title() {
  if (!document.getElementById) return false;
  var pic_1 = document.getElementById('header-pic-1');
  var pic_2 = document.getElementById('header-pic-2');
  var pic_title = document.getElementById('header-pic-title');
  pic_title.innerHTML = pic_1.title;
  pic_2.onmouseover = function(){pic_title.style.display = 'block'};
  pic_2.onmouseout = function(){pic_title.style.display = 'none'};
}

/** 
 * rounded corners, v.1.0
 * dodaje cztery otaczajace divy dla diva z klasa "rc",dzieki czemu mozna ustalic w css zaokraglone rogi
*/
function shadowed_imgs() {
	if(!document.getElementById || !document.createElement) {return false;}
  var shadowed_imgs = [];
  /* Take all imgs */
  var imgs = document.getElementsByTagName('img');
  /* Take rounded imgs = with class="left|right" */
  for (var i=0; i<imgs.length; i++) {
    if (/\bleft\b/.exec(imgs[i].className))
      shadowed_imgs[shadowed_imgs.length] = imgs[i];
    if (/\bright\b/.exec(imgs[i].className))
      shadowed_imgs[shadowed_imgs.length] = imgs[i];
  }
  for (var i=0; i<shadowed_imgs.length; i++) {
    var original = shadowed_imgs[i];
    /* Create the outer-most div */
    var tl = document.createElement('div');
    if (original.className == 'right') tl.className = 'img-shadow-tl right';
    if (original.className == 'left') tl.className = 'img-shadow-tl left';
    tl.style.width = (original.width + 20) + 'px';
    tl.style.height = (original.height + 20) + 'px';
    /* Swap out the original (we'll put it back later) */
    original.parentNode.replaceChild(tl, original);
		/* Create the two other inner nodes */
    var tr = document.createElement('div');
    tr.className = 'img-shadow-tr';
    var bl = document.createElement('div');
    bl.className = 'img-shadow-bl';
    var br = document.createElement('div');
    br.className = 'img-shadow-br';
		/* Glue the nodes back in to the document */
    tl.appendChild(tr);
    tr.appendChild(bl);
    bl.appendChild(br);
    br.appendChild(original);
	}
}

