var g_targetname    = '';
//var g_targetname    = 'parent.textbox';
var g_textheadheight = 80;   // tekst cellen hoogte boven de kleine plaatjes
var g_defsmalltext  = ' ';
var g_deffont       = 'Century Gothic';//'Comic Sans MS';
var g_mouseovertext = 'Klik hier voor grotere foto';
var g_defrowheight  = 30;
var g_maxarraysize  = 512;
var g_imagedir      = "./" //"images/";
var g_pictndir      = "";
var g_picdir        = "";
var notehilite      = "#FFCCCC";
var bluecolor       = "#333333";//"#28365B";
var browncolor      = "#B89D67";
var cellcolor       = mixcolorrgb(bluecolor,browncolor, 0.5);
var scrollshadecolor = "#9A9A9A";

//*************************************************************************************************

function check_top()
{
    // do not allow this as top window
    if ((document.URL.substring(0,16) != 'http://localhost' && document.URL.substring(0,4) != 'file') && window.top == window) location.href="http://www.troika.nl";
}

var timerID = null;
var timerRunning = false;
var id,pause=0,position=0;


function txtScroll()
{
  var i,x;

  if (g_scrollmessage != '')
  {
    var message = "       " + g_scrollmessage + "                         ";
    x=(120/message.length)+1;
    for(i=0;i<=x;i++)
    {
      message+=" "+message;
    }
    if (window.status.length == 0 || window.status.length > 40)
    {
      window.status=message.substring(position,position+120);
    }
    if(position++==g_scrollmessage.length)
    {
      position=2;
    }
    id=setTimeout("txtScroll()",1500/10);
  }
}

// set the left frame jheight equal to the div of the content part of the page
// as an onload event of the main window
function setleftframe()
{
  // Set the contents on the left to the same height as the main div.
  var g_DivInhoud = document.getElementById('inhoud');
  var g_DivAllesHeight  = document.getElementById('alles').offsetHeight;
  //g_DivInhoud.style.position = 'relative';
//  alert(g_DivAllesHeight);
  var screenheight = window.screen.availHeight;
//alert(screenheight)
  if (g_DivAllesHeight > screenheight)
  {
    g_DivInhoud.style.height = g_DivAllesHeight + 'px';
  }
  else
  {
    g_DivInhoud.style.height = screenheight + 'px';
  }
}


function action()
{
        if(!pause) {
                clearTimeout(id);
                pause=1; }

        else {
                ticker();
                pause=0; }
}


function array_rgb(inp, to_int)
{
    if (arguments.length > 1 && to_int)
    {
        return inp[0] << 16 | inp[1] << 8 | inp[2];
    }
    else
    {
        if (typeof(inp) == "string" && inp.substr(0,1) == '#')
            inp = parseInt(inp.substr(1), 16)
        return new Array((inp >> 16), (inp & 0x00FF00) >> 8 , inp & 0xFF);
    }
}

// Mix RGB colors
function mixcolor(rgb1, rgb2, rate, mult)
{
    var rgb3 = new Array(4)
    rgb3[3] = 0;
    var n = 65536;
    for (var i=0; i<3; i++) {
//        if (arguments.length > 3)
//            rgb3[i] = Math.round(rgb1[i]*rgb2[i]*rate/256);
//        else
            rgb3[i] = Math.round(rgb1[i]*(1-rate)+rgb2[i]*rate);
        if (rgb3[i] > 255) rgb3[i] = 255;
        rgb3[3] += (n * rgb3[i]);
        n /= 256;
    }
    return rgb3;
}

function d2h(d, pad)
{
    if (arguments.length == 1) pad = 0;
    var hD="0123456789ABCDEF";
    var h = hD.substr(d&15,1);
    while(d>15 || pad >1) {pad--;d>>=4;h=hD.substr(d&15,1)+h;}
    return h;
}

function mixcolorrgb(rgb1,rgb2, rate,mult)
{
    a = mixcolor(array_rgb(rgb1),array_rgb(rgb2), rate,mult);
    return '#' + d2h(a[3],6);
}

function Imagedata(picture, thumbnail, smalltext, largetext, cx, cy, index)
{
    this.picture = picture;
    this.thumbnail = thumbnail;
    this.smalltext = smalltext;
    this.largetext = largetext;
    this.url = "";
    this.index = index;

    if (typeof(smalltext)=="undefined") smalltext = "";
    var i = smalltext.indexOf('!');
    if (i > 0)
    {
        this.url = this.smalltext.substring(i+1);
        this.smalltext = this.smalltext.substring(0,i);
    }
    this.cx = cx;
    this.cy = cy;
}

var g_index = 0;
var allcount = 0;

function setimagedata(a,picture,thumbnail, smalltext, largetext, cx, cy, index)
{
    if (arguments.length < 3)
    {
        smalltext = '';
        largetext = '';
        cx = 0;
        cy = 0;
    }
    if (typeof(smalltext)=="undefined") smalltext = "";
    if (thumbnail == "" || typeof(thumbnail) == "undefined" || arguments.length < 3)
    {
        if (picture != "")
        {
            thumbnail = picture.replace(/(^.+?\/)?([^\/]+)\.(jpe?g|gif|png)$/, "$1-tn-$2.$3");
        }
        else
            thumbnail = "";
    }
//picture   = g_imagedir + picture;
//thumbnail = g_imagedir + thumbnail;
    // largetext not given
    if (typeof(largetext) == "undefined") largetext = "";

    a[g_index++] = new Imagedata(picture, thumbnail, smalltext, largetext, cx, cy, index);
}

function mouseover(text)
{
    var output = 'onmouseover="window.status=\x27' + text + '\x27; return true;" onmouseout="window.status=\x27\x27; return true;" title="' + text + '" ';
    return output;
}

function showImage(picture, cx, cy, smalltext, largetext, number, targetname)
{
   var url = "";

   if (smalltext == "")
       smalltext = g_defsmalltext;
   if (largetext == "")
       largetext = smalltext;

   var img = '<img src=\"' + g_urlpath + picture + '\" name=\"largeimg\" border=0 alt=\'' + smalltext + '\' />\n';

   var msg1 =
       '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n' +
       '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' +
       '<html>\n' +
       '<head>\n' +
       '<title>[' + number + '] ' + smalltext + '<\/title>\n' +
       '<script type=\'text\/javascript\' src=\"' + g_urlpath + 'troika.js\">' +
       '<\/script>\n' +
       '<link rel=\'stylesheet\' type=\'text\/css\' href=\'' + g_urlpath + 'style.css\'\/>\n' +
       '</head>\n' +
//'<scr' + 'ipt> alert(location.href); </scr' + 'ipt>'+
       '<body oncontextmenu="return false" onclick="window.close()" ' +
       (targetname == '' ?  'onBlur="self.focus()"' : '') + '><center>\n' +
       (g_textcell ? '<h3>' +   largetext + '<\/h3>\n<p>\n'  : '') +
       '<table height=\'100%\' width=\'100%\'><tr><td align=\'center\' valign=\'middle\' height=\'100%\'>' +
       (targetname == '' ?  img  : '<a href="#" ' + mouseover("Klik hier om weer terug te gaan naar overzicht") + ' onclick="location.href=location.href;">' + img + '</a>') + '\n' +
       '</td></tr></table>' +

       '';
//alert(msg1);

       if (targetname == '')
       {
/*
            msg1 +=
       '<script type=\'text\/javascript\'>\n' +
       'width  = window.document.largeimg.width;\n' +
       'height = window.document.largeimg.height;\n' +
       '// workaround on bug ???\n' +
       'if (width < 100 || height < 100)\n' +
       '{\n' +
       '    width  = ' + g_defcx + ';\n' +
       '    height = ' + g_defcy + ';\n' +
       '}\n' +
       'width += 50; height += 120;\n' +
       'if (width > window.screen.availWidth)\n' +
       '    width = window.screen.availWidth;\n' +
       'if (height > window.screen.availHeight)\n' +
       '    height = window.screen.availHeight;\n' +
       'x = (window.screen.availWidth - width)/2;\n' +
       'y = (window.screen.availHeight - height)/2;\n' +
       'window.moveTo( x,y);\n' +
       'window.resizeTo( width,height);\n' +
       '<\/script>\n';
*/
       }

       msg1 += '<\/center></body>\n</html>\n';

//  msg1 = showSource(msg1);
    var s = "left=50,top=50,height="+(cy+20)+",width="+(cx+100)+",status=no,location=no,toolbar=no,directories=no,menubar=no,resizable=yes,scrollbars=no";
    var w ='';
//alert(targetname);
    if (targetname == '')
        w = open(url, "newwin", s);
    else
        w = eval(targetname);
    w.document.open("text/html", "replace")
    w.document.write(msg1)
    w.document.close()
    w.focus();
}
/*
function scrollin(divid, left, hspeed, hmax, top, vspeed, vmax, imgname, size, sizespeed, sizemax)
{
    var ll;
    if (hspeed != 0)
    {
        ll = eval("parseInt(document.getElementById('" + divid + "').style.left")) + (hspeed/2);
        eval("document.getElementById('" + divid + "').style.left = '" + ll + "'");
    }
        eval("document.images." + name + ".width = " + size);


    if (vspeed != undefined && vspeed != 0)
    {
        ll = eval("parseInt(document.getElementById('" + divid + "').style.top")) + (vspeed/2);
        eval("document.getElementById('" + divid + "').style.top = '" + ll + "'");
    }

    if (imgname != undefined && imgname != '')
    {
        var size = eval("document.images." + imgname + ".width");
        size += sizespeed;
    }
    if (size > 0) {
        var ll = parseInt(document.getElementById('hello').style.left) + ((decrement)/2);
        eval("document.getElementById('hello').style.left = '" + ll + "'");
        ll = parseInt(document.getElementById('hello').style.top) + ((decrement)/2);
        eval("document.getElementById('hello').style.top = '" + ll + "'");
       eval("document.images." + name + ".width = " + size);
        setTimeout("animatepic(\\"" + name + "\\"," + decrement + "," + timeout +")", timeout);
    }
    else {
        setTimeout("window.close()", timeout);
}}
}

*/
function closeSite(picture, width,height)
{
   var test = 0;
   var url = "";
   var sizeimage=width/2;
   var topimage = 60;
   var leftimage = (width - sizeimage)/2;
   var toptext = height-150;
   var lefttext = 120;
   var timeout = 50;
   var decrement = 5;
   var textwidth = width * 2 / 3;

   var lefttext = (width - textwidth)/2;

// not on local file
//   if (document.URL.substring(0,4) == 'file') return ;
return;
   var msg1 =
       '<html>\n' +
       '<head>\n' +
       '<title>Bedankt</title>\n' +
       '<scr' + 'ipt src=\"troika.js\">' +
       '</scr' + 'ipt>\n' +
        stylesheet() +
       '</head>\n' +
       '<body leftmargin=0 topmargin=0 oncontextmenu="return false">\n' +
       '<p><center>\n' +
       '<p>\n' +
       '<table height=100% width=100% cellpadding=0 cellspacing=0><tr><td width=100% align=center>' +
        "<div style=\"text-algn: center; position:absolute; width:" + textwidth + "; left:" + lefttext + "; top:" + toptext + "; z-index:1;\">\n" +
        '<h1>Bedankt voor Uw bezoek</h1>' +
       '</div>' +
        "<div id=hello style=\"position:absolute; left:" + leftimage + "; top:" + topimage + "; z-index:1;\">\n" +
        '<img src=troika-logo.gif name=logo width=' + sizeimage + '>' +
       '</div>' +
       (picture != '' ? '<img src=\"' + picture + '\" name=\"largeimg\">\n' : '') +
       '</td></tr></table>\n' +
       '<scr' + 'ipt>\n' +
       "function animatepic(name, decrement, timeout) {\n" +
       "    var size = eval(\"document.images.\" + name + \".width\");\n" +
       "    size -= decrement;\n" +
       "    if (size > 0) {\n" +
       "        var ll = parseInt(document.getElementById('hello').style.left) + ((decrement)/2);\n" +
       "        eval(\"document.getElementById('hello').style.left = '\" + ll + \"'\");\n" +
       "        ll = parseInt(document.getElementById('hello').style.top) + ((decrement)/2);\n" +
       "        eval(\"document.getElementById('hello').style.top = '\" + ll + \"'\");\n" +
        "       eval(\"document.images.\" + name + \".width = \" + size);\n" +
       "        setTimeout(\"animatepic(\\\"\" + name + \"\\\",\" + decrement + \",\" + timeout +\")\", timeout);\n" +
       "    }\n" +
       "    else {\n" +
       "        setTimeout(\"window.close()\", timeout);\n" +
       "}}\n" +
       'width  = ' + width + ";\n" +
       'height = ' + height + ";\n" +
       '// workaround on bug ???\n' +
       'if (width > window.screen.availWidth)\n' +
       '    width = window.screen.availWidth;\n' +
       'if (height > window.screen.availHeight)\n' +
       '    height = window.screen.availHeight;\n' +
       'x = (window.screen.availWidth - width)/2;\n' +
       'y = (window.screen.availHeight - height)/2;\n' +
       'window.moveTo( x,y);\n' +
       'window.resizeTo( width,height);\n' +
       'animatepic(\"logo\",' + decrement + ',' + timeout + ');\n' +
       '</s' + 'cript>\n' +
       '</body>\n' +
       '</html>\n';

//alert(msg1);
//if (test)  msg1 = showSource(msg1);

    if (width > window.screen.availWidth)
        width = window.screen.availWidth;
    if (height > window.screen.availHeight)
        height = window.screen.availHeight;
    x = (window.screen.availWidth-width)/2
    y = (window.screen.availHeight-height)/2

    var s = "left=" + x + ",top=" + y +",height="+height+",width="+width+",status=no,titlebar=no,visible=no,location=no,toolbar=no,directories=no,menubar=no,resizable=no,scrollbars=no";
    var w = open(url, "newwin", s);
    w.document.open("text/html", "replace")
    w.document.write(msg1)
    w.document.close()
    w.focus();
}

function writeswfpic(picture, width, height, bgcolor)
{
    if (arguments.length > 3 && bgcolor != '')
        bgcolor = "    <param name=bgcolor value=" + bgcolor +">\n";
    else
        bgcolor = "    <param name=\"wmode\" value=\"transparent\">\n";

    pic =
    " <!--NOWRAP-->\n" +
    " <object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n" +
    "    codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\"\n" +
    "    width=" + width + " height=" + height + ",>\n" +
    "    <param name=movie value=\"" + picture + "\">\n" +
    "    <param name=quality value=high>\n" +
    bgcolor +
    "    <embed src=\"" + picture + "\" quality=high type=\"application/x-shockwave-flash\"  width=" + width + " height=" + height + ",\n" +
    "        pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n" +
    "    </embed>\n" +
    " </object>\n" +
    " <!--/NOWRAP-->\n";
    return pic;
}

function writeThumbnail(picture, thumbnail, smalltext, largetext, cx, cy, url, index)
{
    if (cx == 0 || cx == "") cx = g_defcx;
    if (cy == 0 || cy == "") cy = g_defcy;
    output = '';
    if (thumbnail == "")
    {
        output += ("<td align=center valign=top>&nbsp;</td>\n");
    }
    else
    {
        var number = picture.replace(/(^.+?\/)?([^\/]+)\.(jpe?g|gif|png)$/,"$2");
        output += ("<td align=center valign=top>\n" +
                       "  <table" + (g_showdata ? " class='showdata'" : "")  + ">\n");

        if (g_textcell)
        {
            output += ("  <tr height='" + g_textheadheight + "'>\n");
            output += ("    <td align='center' class='tdthumbnail' valign='bottom' height='" + g_defrowheight + "'>\n");
            if (g_showdata)
            {
                output += ("<span class='showdata'>" + ++allcount + ":[" +  picture + "]<br />\n" +
                               "[" +  largetext + "]</span><br />\n");
            }


            if (smalltext != "")
            {
                if (url != "")
                    output += ("<a href=\"http://" + url + "\" target=\"main\">" + smalltext + "</a>\n");
                else
                    output += (smalltext + "\n");
            }

            if (g_showdata)
                output += ("    <\/span>");
            output += ("  <\/td>\n<\/tr>\n");
        }
        //!! HIER extra knop voor verwijderen en aanvinkding
        if (g_admin)
        {
          var picturebase = picture.replace(/^.+\/([^\/]+)$/, '$1');
           output += ("<tr><td>"
                      + picturebase + "<br /><a href='javascript:location.href=\"?a="
                        + g_page + "&amp;" + g_imageindex + "=" + index + "\"'>Verander foto</a>\n"
                      + "&nbsp;<a href='javascript:location.href=\"?a=" + g_page + "&amp;move=" + index + "&amp;moveup=1\"'>&lt;&lt;</a>\n"
                      + "&nbsp;<a href='javascript:location.href=\"?a=" + g_page + "&amp;move=" + index + "\"'>&gt;&gt;</a>\n"
                      + "</td></tr>\n");
        }
        output += ("  <tr>\n" +
                       "    <td align=center valign=top class=tdthumbnail  height=" + g_defcellheight + " width=" + g_defcellwidth + ">\n" +
                         (  picture != "" ?
                       "      <a href=\"javascript:showImage(\'" + picture + "\'," +
                               cx +"," + cy + ",\'" + smalltext + "\',\'" + largetext + "\','" + number + "','" + g_targetname + "');\"\n" +
                       "      onmouseover=\"window.status=\'" + g_mouseovertext + "\'; return true;\"\n" +
                       "      onmouseout=\"window.status=\'\'; return true;\">\n" +
                       "      <p><img src=\"" + thumbnail + "\" border=\"0\">\n" +
                       "      <\/a>\n" :   "      <P><IMG SRC=\"" + thumbnail + "\" BORDER=\"0\">\n" ) +
                       "    <\/td>\n" +
                       "  <\/tr>\n");
        output += ("  <\/table>\n" +
                       "<\/td>\n");
    }
//alert(output);
    document.write(output);
}

myarray = new Array (g_maxarraysize);

function writeTable(myarray, ncolumns,size)
{
    var i=0;
    if (arguments.length < 3)
        size = g_index;
    document.write("<p><table width=100%>\n<tr>\n");
    for (i=0; i<size; i++)
    {
        /*
        if (ncolumns == 3)
        {
            if (size % ncolumns == 1)
            {
          */
        writeThumbnail(g_picdir + myarray[i].picture, g_pictndir + myarray[i].thumbnail, myarray[i].smalltext,
            myarray[i].largetext, myarray[i].cx, myarray[i].cy, myarray[i].url, myarray[i].index);
        if ((i+1) % ncolumns == 0)
        {
            document.write("</tr>\n<tr>\n");
        }
    }
    document.write("</tr>\n</table>\n");
}


function showSource(x)
{
  x = x.replace(/&/g, "&amp;");
  x = x.replace(/</g, "&lt;");
  x = x.replace(/>/g, "&gt;\n");
  return '<pre>' + x + '</pre>';
}
