window.onload = function() {
  external_links();
  popup_init();
}

/** 
 * 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";
}

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');
  var img = true;

	var scrollbars = 0;

/* computes dimensions passed from "rel" tag; format: <width>x<height>, output: width=<width>,height=<height> */
	if (size) {
		size = size.split('x');
		if (img) {
			if (size[0] > screen.width) {
				size[0] = screen.width - 16;
				scrollbars = 1;
			}
			if (size[1] > screen.height - 150) {
				size[1] = screen.height - 150;
				size[0] = parseInt(size[0]) + 16;
				scrollbars = 1;
			}
			if (size[0] == 1000 && scrollbars == 1){
				size[0] = 1016;
			}

			popup_size = 'width='+size[0]+',height='+size[1];
		}
		else {
			popup_size = 'width='+size[0]+',height='+size[1];
		}
	}
	else {
		var popup_size = 'width=640,height=480';
	}

	/* window options */
	var win_options = 'left=0,top=0,dependent=1,resizable=1,scrollbars='+scrollbars+',location=0,menubar=0,statusbar=0,'+popup_size;

	/* creates new window */
  if (win = window.open(url,'_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;} img{display:block}</style>');
			win.document.write('</head><body>');
      win.document.write('<img src="'+url+'" alt="'+alt+'" title="'+title+'" onclick="window.close()" />');
			win.document.write('</body></html>');
			win.document.close();
		}
		return false;
  }
	
  /* follows link on failure */
	return true; 
}

function popup_order(){
  var _POPUP_FEATURES = 'scrollbars=no,location=0,statusbar=0,menubar=0,width=540,height=600';
  var theWindow = window.open('popup_order.php', '_blank', _POPUP_FEATURES);
  theWindow.focus();
  return theWindow;
}

function popup2(url,size,title)
{
  size = size.split('x');
  popup_size = 'width='+size[0]+',height='+size[1];
  /* window options */
  var win_options = 'left=0,top=0,dependent=1,resizable=1,scrollbars=0,location=0,menubar=0,statusbar=0,'+popup_size;

  /* creates new window */
  if (win = window.open(url,'_blank',win_options)) {
      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;} img{display:block}</style>');
      win.document.write('</head><body>');
      win.document.write('<img src="'+url+'" alt="" onclick="window.close()" />');
      win.document.write('</body></html>');
      win.document.close();
    return false;
  }
  
  /* follows link on failure */
  return true; 
}