var inme = false;
// var imgs is a global array set by reading pictures from a folder
var nfs, fs;
var pArr = null;
var captions = new Array(); 
var prices = new Array(); 
var full_size = new Array();
var priceArray = new Array();
var sizeArray = ["( 6in x 4in inc mount )", "( 7in x 5in inc mount )", "( 11in x 8in inc mount )", "( 16in x 12in inc mount )", "( 20in x 16in inc mount )"];
var indexSelected=0;  
var my_pn=0; 
var my_label, scd, scd_y, scd_h, pc_name, pc_0,
    pc_1, pc_2, pc_3, pc_4, priceNode, nameNode, sizeNode, seelctNode, 
    pp_open=false,  pp_window=null;

function checkMySize()
{   i = document.getElementById("pic_size").selectedIndex;
    if ( i <= 0 )  
    {  window.alert("Please choose a size.");
       return false;
    }
    else 
    {  return checkWindow();  }
}

function checkSize(pn)
{  // window.alert(pn);
   sizeNode = document.getElementById("size_" + pn);
   // window.alert(sizeNode.value);
   if ( sizeNode.value == "fillIn" )  
   {  window.alert("Please choose a size.");
      return false;
   }
   else 
   {  return checkWindow();  }
}

function checkWindow()
{   if ( pp_open )
    { if ( pp_window == null ) pp_window=window.open('',"paypal");
      if ( pp_window.closed ) {  pp_window=null;  return true; }
      pp_window.focus();
    }
    pp_open=true;
    return true;
}

function wedPrice(pn, i)
{    // window.alert(i);
     priceNode = document.getElementById("price_" + pn);
     sizeNode = document.getElementById("size_" + pn);
     if ( i == 0 )
     {   priceNode.value = "fillIn";
         sizeNode.value = "fillIn";
     }
     else
     {  i--;
        priceNode.value = priceWedArray[i];
        sizeNode.value = sizeWedArray[i];;
     }
}

function setPrice(index)
{   priceNode=document.getElementById('my_price');
    sizeNode=document.getElementById('my_size');
    if ( index > 0 )  
    {  if ( pArr == null )
           priceNode.value=priceArray[index-1];  // default pricing
       else
           priceNode.value=pArr[index-1];   // per image pricing
       sizeNode.value=sizeArray[index-1];
       indexSelected=index;
    }
    else
       indexSelected=0;
}

function init()
{   pc_name=document.getElementById("pic_name");
    pc_0=document.getElementById("Small");
    pc_1=document.getElementById("Medium");
    pc_2=document.getElementById("Large");
    pc_3=document.getElementById("Wall");
    pc_4=document.getElementById("Gallery");
    nfs=document.getElementById("nfs");
    fs=document.getElementById("fs");
    my_label=document.getElementById("picid_0");
    priceNode=document.getElementById('my_price');
    nameNode=document.getElementById('my_name');
    sizeNode=document.getElementById('my_size');
    selectNode=document.getElementById('pic_size');
    scd = document.getElementById("scdiv");
    // pic(imgs, 0);
    piccap(imgs[0]); picprice(imgs[0]);
    setPositions();
    window.onresize=setPositions;
    preload("../img/arrowL-over.gif");
    preload("../img/arrowR-over.gif");
    preload("../img/buyme-over.gif");
    preloadImages(imgs);
}

function setPositions()
{   scd_y = yPosition(scd);  
    scd_h = getInnerHeight() - scd_y;
    // scroll div height set depending on position and window height
    scd.style.height = "" + scd_h + "px";
}

function replaceBox() {   boxBorder(my_label); }

function moveBox(img)
{  if ( img == my_label ) return;
   unbox(my_label);
   boxBorder(img);
}

function box(img)
{  adjustScroll(img);
   boxBorder(img);
}

function boxBorder(img)
{  img.style.borderBottom="2px solid #fc0"; 
   img.style.borderTop="2px solid #fc0"; 
   img.style.borderLeft="2px solid #fc0"; 
   img.style.borderRight="2px solid #fc0"; 
}

function adjustScroll(img)
{  y0 = yPosition(img);  y1 = y0 + img.height;  
   scrollBottom = scd.scrollHeight - scd_h - scd.scrollTop;
   ih = getInnerHeight();
   if ( (y1-scd.scrollTop) > ( ih - 5 ) )
   {   scd.scrollTop = y1 - ih + 10 ;  }
   if ( (y0-scd.scrollTop) <  scd_y )
   {   scd.scrollTop = y0 - scd_y;  }
}

function wedbox(img)
{  img.style.borderBottom="1px solid #fc0"; 
   img.style.borderTop="1px solid #fc0"; 
   img.style.borderLeft="1px solid #fc0"; 
   img.style.borderRight="1px solid #fc0"; 
}

function wedunbox(img)
{  img.style.borderBottom="1px solid #333"; 
   img.style.borderTop="1px solid #333"; 
   img.style.borderLeft="1px solid #333"; 
   img.style.borderRight="1px solid #333"; 
}

function unbox(img)
{  img.style.borderBottom="2px solid #aaa"; 
   img.style.borderTop="2px solid #aaa"; 
   img.style.borderLeft="2px solid #aaa"; 
   img.style.borderRight="2px solid #aaa"; 
}

function pic(imgs, n)
{  target=imgs[n];
   ph = document.getElementById('myphoto');
   // ph.src=target;
   ph.src=full_size[n].src;
   if ( full_size[n].width > 790 )
   {  ph.style.width="550px";  ph.style.height=""; }
   else 
   { if ( full_size[n].height > 650 ) 
     {  ph.style.height="400px"; ph.style.width="";}
     else
     {  ph.style.height=""; ph.style.width="";}
   }
   piccap(target);
   picprice(target);
}

function piccap(target)
{  if ( document.getElementById('caption')  )
   {   if ( captions[target] == undefined )
          document.getElementById('caption').innerHTML="&nbsp;";
       else
          document.getElementById('caption').innerHTML=captions[target];
   }
}

function picprice(target)
{  if ( target.match(/NFS/) )
   {   nfs.style.display = "inline";  fs.style.display="none"  }
   else
   {   nfs.style.display = "none";  fs.style.display="inline"  }
   var pn = picName(target);
   pc_name.innerHTML=pn;
   nameNode.value=pn + " (including shipping and handling)";
   selectNode.selectedIndex=0;
   indexSelected=0;
   if ( prices[target] == undefined ) 
   {   pArr = null;  return;  }   // regular pricing
   // special pricing for this image
   pArr = prices[target];    
   pc_0.innerHTML=sizeArray[0] + " &pound;" + pArr[0];
   pc_1.innerHTML=sizeArray[1] + " &pound;" + pArr[1];
   pc_2.innerHTML=sizeArray[2] + " &pound;" + pArr[2];
   pc_3.innerHTML=sizeArray[3] + " &pound;" + pArr[3];
   pc_4.innerHTML=sizeArray[4] + " &pound;" + pArr[4];
}

function picName(target)
{  var pn = target.substring(0, target.length-4);
   pn = pn.replace(/^[0-9]*_/g,"");
   pn = pn.replace(/_/g," ");
   pn = pn.replace(/NFS/g,"");
   return pn;
}

function picn(imgs, n)
{  if ( n >= imgs.length ) n -= imgs.length;
   if ( n < 0 ) n += imgs.length;
   my_pn=n;   pic(imgs,n);
   unbox(my_label);
   my_label=document.getElementById("picid_" + n);
   box(my_label);
}

function nextpic(imgs) {  picn(imgs, ++my_pn);  }
function previouspic(imgs) {  picn(imgs, --my_pn);  }

function preload(img)
{    var p = new Image();
     p.src=img;
}

function preloadImages(imgs)
{   if (document.images)  
    {   for (var i=0; i < imgs.length; i++)
        {    full_size[i] = new Image();
             full_size[i].src = imgs[i];
        }
    }
}

function wedwin(srcfile)
{    var p = new Image();
     p.src=srcfile;
     imgwin1(p);
}

function imgwin1(img)
{  // picTitle = img.src;
   opt = "resizable=yes,toolbar=no,scrollbars=yes,dependent=yes,width=" 
         + (img.width+24) + ",height=" + (img.height + 24);
   win = window.open(img.src, "Large_Picture", opt);
   win.focus();
}

// function imgwin(n) {  imgwin1(full_size[n]); }
function imgwin(n) 
{  enlargePic(full_size[n].src, picName(imgs[n])); }

function enlargePic(imgSrc, name)
{  var enlarge;
   var ib=document.getElementById("imgbkg");
   ib.style.display="block";
   enlarge=document.getElementById("enlarge");
   enlarge.src=imgSrc;
   var alt= name;
   enlarge.alt = alt;
   enlarge.title=alt;
   document.getElementById("largecap").innerHTML=alt;
   window.scrollTo(0,0);
}

function closelarge()
{  var ib=document.getElementById("imgbkg");
   ib.style.display="none";
}

function yPosition(nd)
{   var y=0;
    while ( nd )
    {  y += nd.offsetTop;
       nd = nd.offsetParent;
    }
    return y;
}

function getInnerHeight() 
{ myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  { //Non-IE
    return window.innerHeight;
  }
  else if( document.documentElement && 
           ( document.documentElement.clientWidth 
             || document.documentElement.clientHeight ) )
  { //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  } 
  else if( document.body && 
           ( document.body.clientWidth || document.body.clientHeight ) )
  { //IE 4 compatible
    return document.body.clientHeight;
  }
}

function getInnerWidth() 
{  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  { //Non-IE
    return window.innerWidth;
  }
  else if( document.documentElement && 
           ( document.documentElement.clientWidth 
             || document.documentElement.clientHeight ) )
  { //IE 6+ in 'standards compliant mode'
     return document.documentElement.clientWidth;
  } 
  else if( document.body && 
           ( document.body.clientWidth || document.body.clientHeight ) )
  { //IE 4 compatible
    return document.body.clientWidth;
  }
}
