// All code copyright by Lars Kristiansson 

// To prevent people from RMB-clicking:
function stop() { 
  if (event.button==2) {
    alert('Intresserad\nav bilderna eller koden?\nKontakta oss!');
  }
  return 0;
}

document.onmousedown = stop;

// if (navigator.appName != "Microsoft Internet Explorer") 
//   alert("Denna sidan fungerar tyv&auml;rr bara med Internet Explorer");

// Write a HTML header including style sheet and script tags
// but without terminating </head> tag to allow extra tags
function writehead(adocument) {
  with(adocument) {
    writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ' + 
	    'Transitional//EN">');
    writeln('<!-- All code copyright by Lars Kristiansson -->');
    writeln('<html>');
    writeln('  <head>');
    writeln('    <meta http-equiv="content-type" content="text/html;' +
	    ' charset=iso-8859-1">');
    writeln('    <meta http-equiv="cache-control" ' + 
	    'content="no cache">');
    writeln('    <meta http-equiv="pragma" content="no cache">');
    writeln
      ('    <link rel=stylesheet href="../work/style.css" type="text/css">');
    writeln
      ('    <script language="javascript" type="text/javascript"');
    writeln('      src="../work/album.js"></script>');
    writeln
      ('    <script language="javascript" type="text/javascript"');
    writeln('      src="../work/script.js"></script>');
  }
  return 0;
}

// Path to the albums
var publicpath = '../bilder/';

// Create a nav button for showonepic
function createnavbutton(album, pic, button, alt) {
  with(document) {
    writeln('      <a nohref onClick="showonepic(' + 
	    album[0][0] + ', ' + pic + ', true, publicpath);">');
    
    writeln('        <img src=../otherpics/bits/' + 
	    button + ' border=0');
    writeln('             alt="' + alt + '"');
    writeln('             align=right></a><br clear=right>');
  }
  return 0;
}

// Used to create a document with one picture
// variable lastpic is used by function lastpicurl()
//var lastpic;
function showonepic(album, pic, buttons, path) {
  var first = 1;
  var prev = adjust(album, pic-1);
  var next = adjust(album, pic+1);
  var last = album.length - 1;
  //lastpic = path + album[pic][0];
  with(document) {
    open();
    writehead(document);
    writeln('  </head>');
    writeln('  <body>');
    writeln('    <table width=100% border=0>');
    writeln('      <tr>');
    writeln('        <td valign=top>');
    writeln('          <center>');
    // This segment goes back in history
    writeln('            <a nohref onClick="window.history.go(-1);">');
    writeln('              <img src=' + path +
	    album[pic][0] + ' alt="Tillbaka" border=0></a>');
	    //album[pic][0] + ' alt="Tillbaka" border=0 heigth=434></a>');
    /* This segment goes to the album on click
    writeln('            <a href=# onClick="initalbum('
	    + album[0][0] + ');">');
    writeln('              <img src=' + path + album[0][0] + '/' +
	    album[pic][0] + ' alt="Till albumet" border=0></a>');
    */
    write('              <pre>' + album[pic][1] + 
	  '          Fotograf: ' + pgr[album[pic][5]] + '          Bild ' + 
	    pic + '/' + (album.length - 1) + '</pre>');
    writeln('        </td>');
    writeln('        <td width=16 valign=top>');

    if(buttons) {
      //createnavbutton(album, first, 'bback.gif', 'F&ouml;rsta bilden');
      createnavbutton(album, prev, 'back.gif', 
		      'F&ouml;reg&aring;ende bild');
      createnavbutton(album, next, 'fwd.gif', 'N&auml;sta bild');
      //createnavbutton(album, last, 'ffwd.gif', 'Sista bilden');
    }
    writeln('        </td>');

    writeln('      </tr>');
    writeln('    </table>');
    writeln('  </body>');
    writeln('</html>');
  }
  return 0;
}

// Slideshow package
var delay = 5000; // 5 sec;

function closeslideshow() {
  close();
  return 0;
}

// Display next slide after timeout
function nextslidetimer(analbum, anindex, cont) {
  if(anindex == 1 && !cont)
    closeslideshow();
  createslideshowpage(anindex, analbum);
  newindex = anindex + 1;
  newindex = newindex >= analbum.length ? 1 : newindex;
  cmd = 'nextslidetimer(' + analbum[0][0] + 
    ', ' + newindex + ', false)';
  window.timer = setTimeout(cmd, delay);
  return 0;
}

// Create the HTML code for a slide page
function createslideshowpage(slideindex, analbum) {
  with(document) {
    open();
    writehead(document);
    writeln('  <title>Bildspel - ' + analbum[0][1] + '</title>');
    writeln('  </head>');
    writeln('  <body>');
    writeln('    <center><br><br>');
    writeln('    <a nohref onClick="closeslideshow();">');
    writeln('      <img src=' + publicpath +
	    analbum[slideindex][0] +
	    ' alt="St&auml;ng" border=0></a>');
    writeln('    <br>');
    writeln('<pre>' + analbum[slideindex][1] + 
	    '          Fotograf: ' + pgr[analbum[slideindex][5]] + 
	    '          Bild ' + 
	    slideindex + '/' + (analbum.length - 1) + '</pre>');
    writeln('  </body>');
    writeln('</html>');
  }
  return 0;
}

// This functions purpose is to transmit albumname via status
function spawnpicshow() {
  if (navigator.appName != "Microsoft Internet Explorer") {
    alert("Bildspel fungerar tyv&auml;rr bara i Internet Explorer");
    return;
  }
  albumname = window.status;
  var i = 0; // in case any error occur
  for(i = 0; i < albumlist.length; i++) {
    if(albumlist[i][0][0] == albumname) {
      break;
    }
  }
  album = albumlist[i];
  nextslidetimer(album, 1, true);
}

// Cleans a new frame from toolbar, location, status etc.
function cleanframe() {
  with(window) {
    defaultStatus = " ";
    locationbar.visible = false;
    toolbar.visible = false;
    menubar.visible = false;
    personalbar.visible = false;
    statusbar.visible = false;
  }
}

// Make a slideshow in a new clean window
function makeslideshow(analbum) {
  properties = 'toolbar=0,scrollbars=0,menubar=0,resizable=0,' +
    'directories=0,status=0,width=700,height=500,left=20,top=20,' +
    'fullscreen=1';
  test = window.open('picshow.html', 'newwin', properties);
  test.status = analbum[0][0];
  return 0;
}

// Spawn a window with width and height displaying link
function spawnsizedwindow(name, link, width, height) {
  properties = 'toolbar=0,scrollbars=0,menubar=0,resizable=0,' +
    'directories=0,status=0,width=' + width + ',height=' + height + 
    ',left=20,top=20,fullscreen=0';
  _blank = window.open(link, 'Hugin', properties);
 }      

// Spawn one fullsize window displaying link
function spawnwindow(name, link) {
  properties = 'toolbar=0,scrollbars=0,menubar=0,resizable=0,' +
    'directories=0,status=0,width=700,height=500,left=20,top=20,' +
    'fullscreen=1';
  _blank = window.open(link, '_blank', properties);
 }      


// Create a slideshowbutton
function createslideshowbutton(albumname) {
  with(document) {
    writeln('      <a nohref onClick="makeslideshow(' +
	    albumname + ');">');
    writeln('        <img src=../otherpics/bits/screen.gif border=0');
    writeln('             alt="Visa galleriets bilder som bildspel"');
    writeln('             align=right></a>');
  }
  return 0;
}

// Album package

var thumbcount = 8; // Number of thumbs per page in an album
var albumbottom = 1; // Where pictures start in the album Array

// Variables below inited at albums opening (func. initalbum)
var pagestart; // current start of albumpage
var pageend;   // current end of page
var thealbum;  // current album

// Div doesn't exist in js so I make my own...
// Div returns the integer part of a/b
function div(a, b) {
  var ret;
  for(ret = 0;(a -= b) >= 0;)
    ret++;
  return ret;
}

// Create a "previous page" button
function createpagebackbutton() {
  with(document) {
    writeln('      <a nohref onClick="prevpage(' + thealbum[0][0] + 
	    ', ' + pagestart + ', ' + pageend + ');return 0;">');
    writeln('        <img src=../otherpics/bits/pageback.gif border=0');
    writeln('             alt="F&ouml;reg&aring;ende sida"');
    writeln('             align=right></a>');
  }
  return 0;
}

// Create a "next page" button
function createpagefwdbutton() {
  with(document) {
    writeln('      <a nohref onClick="nextpage(' + thealbum[0][0] + 
	    ', ' + pagestart + ', ' + pageend + ');return 0;">');
    writeln('        <img src=../otherpics/bits/pagefwd.gif border=0');
    writeln('             alt="N&auml;sta sida"');
    writeln('             align=right></a>');
  }
  return 0;
}

// Create a disabled "previous page" button
function createdisabledpagebackbutton() {
  with(document) {
    writeln('      <a nohref>');
    writeln('        <img src=../otherpics/bits/pageback_disabled.gif border=0');
    writeln('             alt="(F&ouml;reg&aring;ende sida)"');
    writeln('             align=right></a>');
  }
  return 0;
}

// Create a disabled "next page" button
function createdisabledpagefwdbutton() {
  with(document) {
    writeln('      <a nohref>');
    writeln('        <img src=../otherpics/bits/pagefwd_disabled.gif border=0');
    writeln('             alt="(N&auml;sta sida)"');
    writeln('             align=right></a>');
  }
  return 0;
}

// Adjusts index n to circulatingly remain inside album array, that is
// range 1 - (length-1)
// This function returns a number inside album given ANY integer n
// except 0 which is header
function adjust(album, n) {
  var header = 1;
  var ret = n - header;
  var mod = album.length - header;
  while(ret < 0)
    ret += mod; // prevent negative numbers
  ret %= mod;
  return ret + header;
}
    
// Create an album from the supplied album array
function createalbum(album) {
  // album contains pictures + a header. When adding thumbcount-1 
  // numpages becomes 1 for picture 1 thru 8 (i.e. 8 thru 15)
  var numpages = div((album.length - 1) + thumbcount - 1, thumbcount);
  var currpage = div(pagestart, thumbcount) + 1;
  var rowcount = 4;
  with(document) {
    open();
    writehead(document);
    writeln('  </head>');
    writeln('  <body>');
    writeln('    <table width=100%>');
    writeln('      <tr>');
    writeln('        <td width=35%>');
    writeln('          <h2>' + album[0][1] + '</h2>');
    writeln('        </td>');
    writeln('        <td width=40%>');
    writeln('          <center>');
    writeln('            Sida ' + currpage + ' av ' + numpages);
    writeln('        </td>');
    writeln('        <td width=30%>');
    //createslideshowbutton(album[0][0]);
    //createbackbutton(); // Exists already in IE...
    if(currpage < numpages)
      createpagefwdbutton();
    else
      createdisabledpagefwdbutton();
    if(currpage > 1)
      createpagebackbutton();
    else
      createdisabledpagebackbutton();
    writeln('        </td>');
    writeln('      </tr>');
    writeln('    </table>');
    writeln('    <hr>');
    writeln('    <center>');
    writeln('    <table border=0 cellpadding=10>');
    writeln('      <tr height=150>');
    for(i = pagestart; i < pageend; i++) {
      writeln('        <td width=160 valign=top>');
      writeln('          <center>');
      write('            <a href="#"');
      writeln(' onClick="showonepic(' + 
	      album[0][0] + ', ' + i + ', true, publicpath);return 0;"');
      writeln('             onMouseOver="window.status=\'Visa bild\';return 0;"');
      writeln('             onMouseOut="window.status=\' \';return 0;">');
      if(album[i][2] == "s")
	writeln('              <img height=150');
      else
	writeln('              <img width=150');
      writeln('                 src="' + publicpath + 
	      //album[0][0] + '/' +
	      "T_" + album[i][0] +'"');
      writeln('                 border=0 hspace=10 vspace=10');
      writeln('                 alt="' + album[i][1] + 
	      '\nFoto: ' + pgr[album[i][5]] + '">');
      writeln('            </a>');
      writeln('        </td>');
      if((i % 4) == 0) {
	writeln('      </tr>');
	writeln('      <tr height=150>');
      }
    }
    writeln('      </tr>');
    writeln('    </table>');
    writeln('  </body>');
    writeln('</html>');
  }
  return 0;
}

// Show next album page
function nextpage(analbum, apagestart, apageend) {

//   alert(analbum);
//   alert(selectedfrompgr);
//   alert(analbum.length);
//   alert(apagestart);
//   alert(apageend);



  thealbum = analbum;
  pagestart = apagestart;
  pageend = apageend;

  if(!((pagestart + thumbcount) >= thealbum.length)) {
    pagestart += thumbcount;
    pageend = min(pagestart + thumbcount, thealbum.length);
  }
  createalbum(thealbum);
  return 0;
}

// Show previous album page
function prevpage(analbum, apagestart, apageend) {
  thealbum = analbum;
  pagestart = apagestart;
  pageend = apageend;

  pagestart -= thumbcount;
  if(pagestart < 1)
    pagestart = albumbottom;
  pageend = min(pagestart + thumbcount, thealbum.length);
  createalbum(thealbum);
  return 0;
}

// Returns the least of two arguments
function min(a, b) {
  return a < b ? a : b;
}

// Init an album on opening
function initalbum(album) {
  pagestart = albumbottom;
  pageend = min(pagestart + thumbcount, album.length);
  thealbum = album;
  createalbum(thealbum);
  return 0;
}

function selectfrompgr(thepgr) {
  ret = new Array(new Array("selectedfrompgr", pgr[thepgr]));
  dummy = 0;
   for(a = 0; a < albumlist.length; a++) {
     analbum = albumlist[a];
     for(p = 1; p < analbum.length; p++) {
       apic = analbum[p];
       if(apic[5] == thepgr) {
	 ret = ret.concat(dummy);
	 ret[ret.length - 1] = apic;
       }
     }
   }
   selectedfrompgr = ret;
   return ret;
}

// Find the thumbnail for the album provided the album
function findthumb(album) {
  ret = 1; // First picture in case there is no thumb mark "T"
  for(x = 1; x < album.length; x++) 
    if(album[x][3] == "T") {
      ret = x;
      break;
    }
  return ret;
}

// Create a link to one album provided the album
function createalbumlink(album) {
  with(document) {
    writeln('          <a href=# onClick="initalbum('
	    + album[0][0] + ');return 0;"');
    writeln('             onMouseOver="window.status=\'Visa galleriet &laquo;'
	    + album[0][1] + '&raquo;\';return 0;"');
    writeln('             onMouseOut="window.status=\' \';return 0;">');
    writeln('            <img src="' + publicpath + 
	    //album[0][0] + '/' + 
	    "T_" + album[findthumb(album)][0] + '"');
    writeln('                 width=100');
    writeln('                 border=0');
    //writeln('                 align=left');
    writeln('                 alt="' + album[0][1] + '"');
    writeln('                 alt="' + album[0][1] + '"><br>');
    //writeln('                 vspace=10');
    //writeln('                 hspace=10><br>');
    writeln('            <font size=1.3>' + album[0][1] + '</font></a>');
    //writeln('            </a>');
  }
  return 0;
}

// Old version 
/* function createalbumlink(album) { */
/*   with(document) { */
/*     writeln('          <a href=# onClick="initalbum(' */
/* 	    + album[0][0] + ');return 0;"'); */
/*     writeln('             onMouseOver="window.status=\'Visa albumet &laquo;' */
/* 	    + album[0][1] + '&raquo;\';return 0;"'); */
/*     writeln('             onMouseOut="window.status=\' \';return 0;">'); */
/*     writeln('            <img src="' + publicpath +  */
/* 	    //album[0][0] + '/' +  */
/* 	    "T_" + album[findthumb(album)][0] + '"'); */
/*     writeln('                 width=100'); */
/*     writeln('                 border=0'); */
/*     writeln('                 align=left'); */
/*     writeln('                 alt="' + album[0][1] + '"'); */
/*     //writeln('                 vspace=10'); */
/*     writeln('                 hspace=10>'); */
/*     writeln('            ' + album[0][1] + '<br clear=left></a><p>'); */
/*     //writeln('            </a>'); */
/*   } */
/*   return 0; */
/* } */

// Create the page with the present albums provided in an Array
function createalbumpage(albumlist) {
  if (navigator.appName != "Microsoft Internet Explorer") {
    dumpall();
    return;
  }
  var trwidth = 230;
  //var rows = (div(albumlist.length - 1, 5));
  with(document) {
    writehead(document);
    writeln('  </head>');
    writeln('  <body>');
    writeln('    <h2>galleri:</h2><hr>');
    
    writeln('    <table border=0 cellpadding=5>');
    writeln('      <tr>');
    
    for(i = 0; i < albumlist.length; i++) {
      writeln('        <td width=' + trwidth + ' valign=bottom>');
      writeln('        <center>');
      createalbumlink(albumlist[i]);
      writeln('        </td>');
      if((i % 5) == 4) {
	writeln('      </tr>');
	writeln('      <tr>');
      }
    }
    
    writeln('      </tr>');
    writeln('    </table>');
    
    writeln('  </body>');
    writeln('</html>');
  }
  return 0;
}

// Old version 
/* function createalbumpage(albumlist) { */
/*   var trwidth = 350; */
/*   var rows = (div(albumlist.length - 1, 2)); */
/*   with(document) { */
/*     writehead(document); */
/*     writeln('  </head>'); */
/*     writeln('  <body>'); */
/*     writeln('    <h2>album:</h2><hr>'); */
    
/*     writeln('    <table border=0 cellpadding=20>'); */
/*     writeln('      <tr>'); */
/*     writeln('        <td width=' + trwidth + ' valign=top>'); */
    
/*     for(i = 0; i < albumlist.length; i++) { */
/*       createalbumlink(albumlist[i]); */
      
/* 	if(i == rows) { */
/* 	writeln('        </td>'); */
/* 	writeln('        <td valign=top>'); */
/* 	} */
      
/*     } */
    
/*     writeln('        </td>'); */
/*     writeln('    </table>'); */
    
/*     writeln('  </body>'); */
/*     writeln('</html>'); */
/*   } */
/*   return 0; */
/* } */

// For non internet explorer browsers
function dumpall() {
  with(document) {
    writehead(document);
    writeln('  </head>');
    writeln('  <body>');
    writeln('    <font size=2>Denna presentation av v&aring;ra album f&aring;r du se');
    writeln('d&auml;rf&ouml;r att din browser tyv&auml;rr inte st&ouml;ds av v&aring;r sida.<br>');
    writeln('Sidan ser &auml;n s&aring; l&auml;nge b&auml;st ut i Internet Explorer...<br>');
    writeln('(Sidan håller på att få ett nytt utseende)</font><hr>');
    for(i = 0; i < albumlist.length; i++) {
      album = albumlist[i];
      //alert(album);
      writeln('    <h2>' + album[0][1] + ':</h2><hr>');      
      writeln('    <center>');
      writeln('    <table border=0 cellpadding=10>');
      writeln('      <tr height=150>');
      for(j = 1; j < album.length; j++) {
	writeln('        <td width=160 valign=top>');
	writeln('          <center>');
	writeln('            <a href="../bilder/' + album[j][0] +
	      '" target=_dumpall>');
	if(album[j][2] == "s")
	  writeln('              <img height=150');
	else
	  writeln('              <img width=150');
	writeln('                 src="' + publicpath + 
		//album[0][0] + '/' +
		"T_" + album[j][0] +'"');
	writeln('                 border=0 hspace=10 vspace=10');
	writeln('                 alt="' + album[j][1] + 
		'\nFoto: ' + 
		pgr[album[j][5]] + '">');
	writeln('            </a>');
	writeln('        </td>');
	if((j % 4) == 0) {
	  writeln('      </tr>');
	  writeln('      <tr height=150>');
	}
      }
      writeln('        </center>');
      writeln('      </tr>');
      writeln('    </table>');
    }
    writeln('  </body>');
    writeln('</html>');
  }
}

// Allocating the monthlist
var monthpics = new Array(
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty"),
  new Array(0,2,"empty")
 );

// Establish the monthlist
function createmonthpics() {
  for(i = 0; i < albumlist.length; i++) {
    album = albumlist[i];
    for(j = 1; j < album.length; j++) {
      month = album[j][4];
      if(month != -1)
	monthpics[month] = new Array
	  (i, j, monthlist[month]);
    }
  }
}

// Create an opening page with pictures 
// monthly picked from monthlist
function createhome() {
  createmonthpics();
  thedoc = document;
  date = new Date();
  month = date.getMonth(); // Range 0-11
  thepictureref = monthpics[month];
  thealbum = albumlist[thepictureref[0]];
  thepictureindex = thepictureref[1];
  thename = thealbum[thepictureindex][0];
  fullname = publicpath + thename;
  alttext = thepictureref[2] + ": " + 
    thealbum[thepictureindex][1];
  with(thedoc) {
    open();
    writehead(thedoc);
    writeln('    <base target="work">');
    writeln('  </head>');
    writeln('  <body>');
    writeln('    <p>');
    writeln('    <center>');
    writeln('    <a href="album.html"');
    writeln('       onMouseOver="window.status=' + 
	    "'Visa alla album'" + ';return 0;"');
    writeln('       onMouseOut="window.status=' + 
	    "' '" + ';return 0;">');
    writeln('      <img width=600');
    writeln('           src="' + fullname + '"');
    writeln('           border=0');
    writeln('           alt="' + alttext + '">');
    writeln('    </a>');
    writeln('  </body>');
    writeln('</html>');
  }
  return 0;
}

// Create a calendar showing the monthly pictures
function createcalendar() {
  createmonthpics();
  with(document) {
    open();
    writehead(document);
    writeln("  </head>");
    writeln("  <body>");
    //createbackbutton();
    writeln("    <h2>kalenderbilder :</h2><hr>");
    for(i = 0; i < monthpics.length; i++) {
      album = albumlist[monthpics[i][0]];
      picindex = monthpics[i][1];
      monthname =  monthpics[i][2];
      pic = album[picindex];
      picname = "T_" + pic[0];
      picalt = monthname + ": " + pic[1];
      fullpath = publicpath + picname;
      writeln('    <a href="#"');
      writeln('       onMouseOver="window.status=' + 
	      "'" + picalt + "'" + ';return 0;"');
      writeln(' onMouseOut="window.status=' + "' '" + ';return 0;"');
      writeln(' onClick="showonepic(' + 
	      album[0][0] + ', ' + picindex + ', false, publicpath);">');
      writeln('<img src="' +
	      fullpath + '" height=100 border=0 alt="' +
	      picalt + '"></a>');
      if(((i + 1) % 4) == 0)
	writeln('    <p>');
    }
    writeln("  </body>");
    writeln("</html>");
  }
  return 0;
}

// Section for news:

// Writes the header of a news frame
function write_newshead() {
  with(document) {
    close();
    open();
    writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">');
    writeln('<!-- All code copyright by Lars Kristiansson -->');
    writeln('<html>');
    writeln('  <head>');
    writeln('    <title>NATURBILDEN - Nyheter</title>');
    writeln('    <link rel=stylesheet href="style.css" type="text/css">');
    writeln('    <script language="javascript" type="text/javascript"');
    writeln('      src="news.js"></script>');
    writeln('    <script language="javascript" type="text/javascript"');
    writeln('      src="script.js"></script>');
    writeln('    <style type="text/css">a {text-decoration: underline}</style>');
    writeln('  </head>');
    writeln('  <body>');
    writeln('    <h2>nyheter:</h2>');
    writeln('    <hr>');
  }
}

// Under construction!!!
// Returns the album provided the name as string
// ... as used below the definition of albumlist is void
function findalbumbyname(text) {
  //alert('Hej 2!');
  for(t = 0; t < albumlist.length; t++)
    if(albumlist[t][0][0] == text)
      return albumlist[t];
}

// Writes n news
function show_n_news(n) {
  with(document) {
    for(t = 0; t < n; t++) {
      writeln('    <font size=1><i>' + thenews[t][0] + 
	      '</i><p></font>');
      writeln('    <h3>' + thenews[t][1] + '</h3>');
      for(l = 2; l < thenews[t].length; l++) {
	a = thenews[t][l];
	if(a[0] == 'alnk') {
	  // findalbumbyname used here... called from news.html
	  //alert('Hej 1!');
	  createalbumlink(findalbumbyname(a[1]));
	} else if(a[0] == 'jlnk') {
	  writeln('    <a href=# onClick=">');
	} else if(a[0] == 'hlnk') {
	  //alert('Hej 0!');
	  writeln('    <a href="' + a[2] + '"');
	  writeln('       onMouseOver="window.status=\''
		  + a[1] + '\';return 0;"');
	  writeln('       onMouseOut="window.status=\' \';return 0;"');
	  writeln('       target=_blank>' + a[1] + '</a>');
	} else if(a[0] == 'txt') {
	  writeln(a[1]);
	} else if(a[0] == 'html') {
	  writeln('    <' + a[1] + '>');
	} else if(a[0] == 'image') {
	  writeln('    <img src="../bilder/' + a[1] + '" hspace=20>');
	} else {
	  alert('Array format error!');
	}
      }
      writeln('    <hr>');
    }
  }
}

// Number of news to show at start
var numnews = 5;

// Wrapping for the news
function create_some_news(n) {
  with(document) {
    write_newshead();
    show_n_news(n);
    if(n == numnews) {
      writeln('    <a href="#"');
      writeln('       onClick="create_all_news();window.status=\' \';return 0;"');
      writeln('       onMouseOver="window.status=\'Alla nyheter till b&ouml;rjan\';return 0;"');
      writeln('       onMouseOut="window.status=\' \';return 0">Visa alla nyheter</a>');
    }
    writeln('  </body>');
    writeln('</html>');
  }
}

// Creates the start page
function create_news() {
  create_some_news(numnews);
}

// Follows the link "Visa alla nyheter"
function create_all_news() {
  create_some_news(thenews.length);
}

// Bubble-sort array of arrays x ordering by element p
function sort(x, p) {
  for(t = 0; t < (x.length - 1); t++)
    for(u = t + 1; u < x.length; u++)
      if(x[t][p] > x[u][p]) {
	temp = x[t];
	x[t] = x[u];
	x[u] = temp;
      }
}

// Create a statistic table over photographers activity
function create_stats() {
  stat = new Array(albumlist.length + 1);
  for(album = 0; album < albumlist.length; album++) {
    stat[album] = new Array(
      albumlist[album][0][1], // albumname
      0, 0, 0, 0, 0)          // anna, annap, lasse, lassep, total
  }
  stat[albumlist.length] = new Array("totalt", 0, 0, 0, 0, 0);
  for(album = 0; album < albumlist.length; album++) {
    for(pic = 1; pic < albumlist[album].length; pic++) {
	stat[album][5]++;
	stat[albumlist.length][5]++;
      if(albumlist[album][pic][5] == 0) {
	stat[album][1]++;
	stat[albumlist.length][1]++;
      }
      if(albumlist[album][pic][5] == 1) {
	stat[album][3]++;
	stat[albumlist.length][3]++;
      }
    }
    stat[album][2] = Math.round(100*stat[album][1]/stat[album][5]);
    stat[album][4] = Math.round(100*stat[album][3]/stat[album][5]);
  }
  stat[albumlist.length][2] = 
    Math.round(100*stat[albumlist.length][1]/stat[album][5]);
  stat[albumlist.length][4] = 
    Math.round(100*stat[albumlist.length][3]/stat[album][5]);
  sort(stat, 4);
  for(t = 0; t < (stat.length - 1); t++)
    if(stat[t][0] == "totalt") {
      temp = stat[t];
      stat[t] = stat[t + 1];
      stat[t + 1] = temp;
    }
  with(document) {
    writehead(document);
    writeln('  </head>');
    writeln("  <body>");
    writeln("    <h2>bildstatistik :</h2><hr>");
    writeln("    <table cellspacing=10>");
    for(album = 0; album < albumlist.length + 1; album++) {
      writeln("      <tr>");
      if(album == albumlist.length) {
	writeln("          <td>");
	writeln("            <hr>");
	writeln("          </td>");
	writeln("        </tr>");
	writeln("        <tr>");
      }
      writeln("        <td>");
//       writeln(stat[album][4] + "%  <b>" + stat[album][0] + "<b> (" + 
// 	      stat[album][5] + " bilder) : ");
      writeln("<b>" + stat[album][0] + "<b> (" + 
	      stat[album][5] + " bilder) : ");
      writeln("        </td>");
      writeln("        <td align=right>");
      writeln(pgr[0] + ": " + stat[album][1] + " st.");
      writeln("        </td>");
      writeln("        <td>");
      writeln("= " + stat[album][2] + "%");
      writeln("        </td>");
      writeln("        <td align=right>");
      writeln(pgr[1] + ": " + stat[album][3] + " st.");
      writeln("        </td>");
      writeln("        <td>");
      writeln("= " + stat[album][4] + "%");
      writeln("        </td>");
      writeln("      </tr>");
    }
    writeln("    </table>");
    writeln("  </body>");
    writeln("</html>");
  }
}
