var img_cnt=0;
var navigation_yn='Y';
var img_src_array=new Array();
var img_caption_array=new Array();
var wd;

function test() {
   alert('test!');
}

function addImage(img_src,img_caption) {
   img_cnt = img_cnt + 1;
   img_src_array[img_cnt]=img_src;

   if (typeof(img_caption) == 'undefined') {
      img_caption_array[img_cnt]='';
   }
   else {
      img_caption_array[img_cnt]=img_caption;
   }
}

function popupwindow(image_index,nav) {
   var o;
   var window_str;

   if (typeof(nav) != 'undefined') {
      navigation_yn = nav;
   }


   o="toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=1,width=0,height=0";
//   wd=window.open("","",o,true);
//   wd.name="TWCSSlideshow";

   if(wd){
      wd=window.open("","TWCSSlideshow",o,true);
      pw=wd;
   }
   else{
      wd=window.open("","",o,true);
      wd.name="TWCSSlideshow";
   }

   wd.document.open();

   window_str = '<html><head><title>'+img_caption_array[image_index]+'</title>';
   window_str+= '<link rel="stylesheet" href="besa_style.css" type="text/css">';
   window_str+= '<script>var cur=' + image_index +  ';</script>';
   window_str+= '</head>';
   window_str+='<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>';


   window_str+='<center>'
   window_str+='<div id="aDiv" style="left=0px; top=0px; position:absolute; padding:0px; z-index:100">'
   window_str+='</div>';
   window_str+='</body></html>';

   bigimg=new Image();
   bigimg.src=img_src_array[image_index]

   wd.document.writeln(window_str);
   wd.document.close();

   showImage(image_index);

}

function showImage(image_index) {

   var div_str;
   var next_image;
   var prev_image;

   var bigimg
   var aDiv = wd.document.getElementById('aDiv'); 

   if (!aDiv)  return;

   if (image_index == 1) {
     prev_image = img_cnt;
   }
   else {
     prev_image = image_index - 1;
   }

   if (image_index == img_cnt) {
     next_image = 1;
   }
   else {
     next_image = image_index + 1;
   }

   aDiv.innerHTML = '<nobr>Loading Image...</nobr>';
   wd.resizeTo(aDiv.clientWidth + 10 ,aDiv.clientHeight + 70);

   bigimg=new Image();
   bigimg.onload=function(evt){

      div_str ='<table border=0>'

      if (navigation_yn == 'Y') {
         div_str+='   <tr>'
         div_str+='      <td align="left">'
         div_str+= '        <a href="javascript:window.opener.showImage(' + prev_image + ')"><img src="images/prev.gif" border=0 alt="Previous photo"></a>';
         div_str+= '        <a href="javascript:window.opener.showImage(' + next_image + ')"><img src="images/next.gif" border=0 alt="Next photo"></a>';
         div_str+='      </td>'
         div_str+='   </tr>'
      }

      div_str+='   <tr>'
      div_str+='      <td align="center">'
      div_str+= '        <img src="' + img_src_array[image_index] + '">';
      div_str+='      </td>'
      div_str+='   </tr>'

      if (img_caption_array[image_index] != '') {
         div_str+='   <tr>'
         div_str+='      <td align="center">'
         div_str+= '        ' + img_caption_array[image_index];
         div_str+='      </td>'
         div_str+='   </tr>'
      }

      div_str+='</table>'

      aDiv.innerHTML = div_str;
      wd.resizeTo(aDiv.clientWidth + 10 ,aDiv.clientHeight + 70);
      wd.document.title = img_caption_array[image_index];
   //   wd.document.title = 'TEST';
      wd.focus();

   } 

   bigimg.src=img_src_array[image_index];

}
