/* Copyright (C) 2005 Regina Fernandes
*/

if (paintings == undefined) {
   var paintings = new Array();
}

if (pGlobal == undefined) {
   var pGlobal = 1;
}

function preloadPaintings() {
  // since a navigation grid is used to display a painting
  // dynamically on a click, stores all the paintings in
  // 'paintings' array into an array of image objects
  // which causes the client to cache them if the function
  // is invoked upon an onLoad event on the body
  var pArray = new Array(paintings.length);
  for (var i = 0; i < paintings.length; i++) {
      pArray[i] = paintings[i][0];
  }
  for (var j = 0; j < pArray.length; j++) {
      var anImage = new Image();
	  anImage.src = pArray[j];
  }
  return
}

function setPointerStyle() {
  // since the numbers in the navigation grid are to be
  // clicked on, change the pointer from normal (text)
  // hairline stile to a pointing hand, for all cells
  var x = 0;
  for (var i = 0; i < paintings.length; i++) {
      x = i+1;
      cell = 'q' + x.toString();
	  document.getElementById(cell).className='pointerHand';
  }
}

function showPainting(p) {
  //
  // 'p' is the cell number in the navigation grid
  // 'index' is used to read from an array of painting data,
  //  it is obtainable from painting by a shift of minus one 
  var index = p-1;
  // (uses a floating frame to generate variable text)
  //
  // opens a document in a floating frame, sets the style of the
  // document, then uses getPaintingDetails to generate the HTML
  // content coresponding to 'painting' which it then writes onto
  // the frame, and finally closes the document
   parent.frames[0].document.open();
   setPrologueMaterial();
   parent.frames[0].document.write(getPaintingDetails(index));
   // prepare the actual parameters for showing the painting itself
   var srcFile = paintings[index][0];
   var paintingTitle = paintings[index][1].replace(/&rsquo;/g,"\\'");
   var cell = 'q' + p.toString();
   showPaintingItself(srcFile,paintingTitle,cell);
   setEpilogueMaterial();
   parent.frames[0].document.close();
   return
}

function setPrologueMaterial() {
   // sets up the document to display painting details in a 
   // floating frame linking it with the proper style sheet
   //
   // (see also setEpilogueMaterial() below)
   // (used by showPaintingDetailsFrame(painting) below)
   // (used by clearPaintingDetailsFrame() below)
   parent.frames[0].document.write('<html>');
   parent.frames[0].document.write('<head>');
   parent.frames[0].document.write('<link href="grey.css" rel="stylesheet" type="text/css">');
   parent.frames[0].document.write('</head>');
   parent.frames[0].document.write('<body class="bodyStyle">');
   return
}

function showPaintingItself(srcFile,paintingTitle,cell) {
   //
   // index is used to read from an array of painting data,
   // cell is an HTML element id value, it prefixes a 'q'
   // to the string value of the corresponding painting number
   // i.e., index+1
   // 
   // shows the painting in the file paintings[index][0] in the element
   // identified by p_file when cell identified by cell in the
   // navigation grid is clicked, and then set the alt and title tags
   // with the title of the painting and changes the colour of
   // the text inside cell using the seenAlready class, which
   // emulates the colour of a:visited in the linked style sheet
   //
   // first prepare the source strings using the actual parameters
   var setSrcLine = 'parent.document.getElementById(\'p_file\').src=\'' + srcFile + '\';' 
   var setAltLine = 'parent.document.getElementById(\'p_file\').alt=\'' + paintingTitle + '\';' 
   var setTitleLine = 'parent.document.getElementById(\'p_file\').title=\'' + paintingTitle + '\';' 
   var setClassNameLine = 'parent.document.getElementById(\'' + cell + '\').className=\'seenAlready\';' 
   // then write out the code
   parent.frames[0].document.write('<script language="JavaScript" type="text/JavaScript">');
   parent.frames[0].document.write(setSrcLine);
   parent.frames[0].document.write(setAltLine);
   parent.frames[0].document.write(setTitleLine);
   parent.frames[0].document.write(setClassNameLine);
   parent.frames[0].document.write('</script>');
   return
}

function setEpilogueMaterial() {
   // completes the work done by setPrologueMaterial()
   // and setPaintingItself(index) above
   // (used by showPaintingDetailsFrame(painting) below)
   // (used by clearPaintingDetailsFrame() below)
   parent.frames[0].document.write('</body>');
   parent.frames[0].document.write('</html>');
   return
}

/*
function showPaintingItself(index) {
  //
  // 'index' is used to read from an array of painting data,
  //
  // 'cell' is an HTML element id value, it prefixes a "q" 
  // to the string value of the corresponding painting number
  // i.e., index+1
  var cell = 'q' + (index+1).toString();
  // shows the painting in the file paintings[index][0] in the element 
  // identified by 'p_file' when cell identified by 'cell' in the 
  // navigation grid is clicked, and then set the alt and title tags
  // with the title of the painting and changes the colour of 
  // the text inside 'cell' using the 'seenAlready' class, which
  // emulates the colour of a:visited in the linked styke sheet
  document.getElementById('p_file').src=paintings[index][0];
  document.getElementById('p_file').alt=paintings[index][1];
  document.getElementById('p_file').title=paintings[index][1];
  document.getElementById(cell).className='seenAlready';
  return
}
*/

/*
function clearPaintingDetailsFrame() {
   // (see the function above too)
   //
   // clears the floating frame where painting details are shown
   parent.frames[0].document.open();
   setPrologueMaterial();
   parent.frames[0].document.write('&nbsp;');
   setEpilogueMaterial();
   parent.frames[0].document.close();
   return
}
*/

function getPaintingDetails(index) {
  // 'index' is used to read from an array of painting data,
  //
  // generates a CSS-associated <div> element to be
  // written to the floating frame where painting details
  // are shown
  var num = index + 1 ;
  var f2 = paintings[index][2] == '' ? '' : '&#151;&nbsp;' ;
  var f3 = paintings[index][3] == '' ? '' : '&#151;&nbsp;' ;
  var f4 = paintings[index][4] == '' ? '' : '&#151;&nbsp;' ;
  var f5 = '&pound;';
  if (paintings[index][4] == '') {f5 = ''}
  var f6 = paintings[index][5];
  if (paintings[index][5] == '') {if (document.title != 'illugraphics :: art gallery :: commissions') {f6 = instantiatePayPalButton(index)}};
  return '<div class="textPaintingDetails">' +
          '<font color="#FF9933">' + num + '</font><br>' +
          '<strong>' +
              paintings[index][1] + 
          '</strong>' + '<br>' + f2 +
              paintings[index][2] + '<br>' + f3 +
              paintings[index][3] + '<br>' + f4 +
              f5 + paintings[index][4] + '<br>' +
	        '<font color="#FF9933">' +
			  f6 + 
			'</font><br><br>' +
			  paintings[index][6] +			  
	    '</div>';
}

function instantiatePayPalButton(index){
  var timestamp = ':::' + Date();
  return '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_parent">' +
               '<input type="hidden" name="cmd" value="_xclick">' +
               '<input type="hidden" name="business" value="sales@illugraphics.co.uk">' +
               '<input type="hidden" name="item_name" value="' + paintings[index][1] + ' ' + paintings[index][2] + ' ' + paintings[index][3] + '">' +
               '<input type="hidden" name="item_number" value="' + timestamp + '">' +
               '<input type="hidden" name="amount" value="' + paintings[index][4] + '">' +
               '<input type="hidden" name="page_style" value="Primary">' +
               '<input type="hidden" name="no_shipping" value="2">' +
               '<input type="hidden" name="return" value="http://www.illugraphics.co.uk/thankyou.html">' +
               '<input type="hidden" name="cancel_return" value="http://www.illugraphics.co.uk/cancellation.html">' +
               '<input type="hidden" name="currency_code" value="GBP">' +
               '<input type="hidden" name="lc" value="GB">' +
               '<input type="hidden" name="bn" value="PP-BuyNowBF">' +
               '<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but9.gif" border="0" name="submit" title="Use a debit or credit card to pay through PayPal: fast, free and secure!" alt="Use a debit or credit card to pay through PayPal: fast, free and secure!">' +
               '<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">' +
         '</form>';
}
