// imaja-slideshow.js
// search for load database

// scriptmaster=greg@imaja.com
// This script is Copyright © 2001-2006 Greg Jalbert
// Copying or using this script is expressly forbidden. 
// For licensing information, contact greg@imaja.com.
// functions: 
//   picscanprefs getparam addAttribute getSearchAsArray loadFrame 
// displayThumbnail displayListview displayFullSize makeEntryForm
// getFormData getformascommaseparatedvalues displayThumbnailTable makeImageInfo
// displayNextPrevLinks
// URL?viewas=list
// URL?viewas=thumbnails
// URL?viewas=entryform

var columns = 14;
var currentImage = 0;
var thumbnailsize = 40;
var paramstr = window.document.location.search;

baseurl = window.document.location.href;
if (baseurl.indexOf("?") > 0)
    baseurl = baseurl.substring(0, baseurl.indexOf("?"));

function picscanprefs(imageDirectory, thumbnailDirectory, thumbnailwidth, thumbnailheight) {
  this.imageDirectory = imageDirectory;
  this.thumbnailDirectory = thumbnailDirectory;
  this.thumbnailwidth = thumbnailwidth;
  this.thumbnailheight = thumbnailheight;
}

var picscanprefs = new picscanprefs("images", "thumbnails", thumbnailsize, thumbnailsize);

function getparam(name) {
  var paramlist = window.document.location.search;
  var pos = paramlist.indexOf(name + "=");
  if (pos > 0) {
    return paramlist.substring(pos+paramlist.length(), paramlist.indexOf('&', pos));
  }
}

function addAttribute(tagstring, attrname, attrvalue) {
  tagstring = tagstring + " " + attrname + "='" + attrvalue + "'";
  return tagstring;
}

function closetag(str) {
  return str + ">";
}

function getSearchAsArray() {
   var minNav3 = (navigator.appName == "Netscape" && 
      parseInt(navigator.appVersion) >= 3)
   var minIE4 = (navigator.appName.indexOf("Microsoft") >= 0 && 
      parseInt(navigator.appVersion) >= 4)
   var minDOM = minNav3 || minIE4   // Baseline DOM required  for this function
   var results = new Array()
   if (minDOM) {
      location
      var input = unescape(location.search.substr(1))
      if (input) {
         var srchArray = input.split("&")
         var tempArray = new Array()
         for (i = 0; i < srchArray.length; i++) {
            tempArray = srchArray[i].split("=")
            results[tempArray[0]] = tempArray[1]
         }
      }
   }
   return results
}

function loadFrame() {
   if (location.search) {
      var srchArray = getSearchAsArray()
      if (srchArray["content"]) {
         self.content.location.href = srchArray["content"]
      }
   }
}

function displayThumbnail(index) {
  var imagestr = "<img";
  imagestr = addAttribute(imagestr, "src", this.thumbnail);
 imagestr = addAttribute(imagestr, "width", picscanprefs.thumbnailwidth);
 imagestr = addAttribute(imagestr, "height", picscanprefs.thumbnailheight);
  imagestr = addAttribute(imagestr, "class", "imageborder");
  imagestr = addAttribute(imagestr, "border", 0);
  imagestr = closetag(imagestr);
  linkurl = baseurl + "?cur=" + index + "&"; //  + "viewas=thumbnails&"
  document.write(imagestr.link(linkurl));
  if (false) {
  	document.write("<br>");
  	// document.write("<br>baseurl=" + baseurl);
 	trunctitle = this.title.substring(0, 13) + "...";
  	document.write(trunctitle.link(linkurl));
  }
}

function displayListview(index) {

  document.write("<tr><td valign='top'>");
  var imagestr = "<img";
  imagestr = addAttribute(imagestr, "src", this.thumbnail);
  imagestr = addAttribute(imagestr, "width", picscanprefs.thumbnailwidth);
  imagestr = addAttribute(imagestr, "height", picscanprefs.thumbnailheight);
  imagestr = addAttribute(imagestr, "class", "imageborder");
  imagestr = addAttribute(imagestr, "border", 0);
  imagestr = closetag(imagestr);
  linkurl = baseurl + "?cur=" + index + "&";
  document.write(parseInt(index)+1);
  document.write("</td><td valign='top'>");

  if (true) { // disable this code
 	 document.write(imagestr.link(linkurl));
  } // end disable
  document.write("</td><td valign='top' width='40%'>");
  if (this.title == "") {
	  document.write(this.image);
  }
  else {
  	document.write(this.title.link(linkurl));
  }
  document.write("</td><td valign='top' width='15%'>");
  document.write(this.size);
  document.write("</td><td valign='top'>");
  document.write(this.price);
  document.write("</td><td valign='top'>");
  document.write(this.medium);
  document.write("</td></tr>");

 // document.write("<br>");
  // document.write("<br>baseurl=" + baseurl);
 // trunctitle = this.title.substring(0, 10) + "...";
//  document.write(trunctitle.link(linkurl));
}

function displayFullSize() {

//  document.write("<table width='100%' cellpadding=4 cellspacing=0 border=0>");
//  document.write("<tr><td valign='top' width='50%'>");

 document.write("<p align='center'>");

  var imagestr = "<img";
  imagestr = addAttribute(imagestr, "src", this.image);
  // imagestr = addAttribute(imagestr, "width", 320);
  // imagestr = addAttribute(imagestr, "height", 320);
  imagestr = addAttribute(imagestr, "border", 0);
  imagestr = addAttribute(imagestr, "align", "center");
  imagestr = addAttribute(imagestr, "class", "imageborder");
  imagestr = closetag(imagestr);
  document.write(imagestr);
  document.write("<br>");

  document.write("<b>" + this.title + "</b> . . . ");
//  document.write(":<br>");
//  document.write(this.description);
//  document.write(": ");
  document.write(this.medium + ", <br>" + this.size + ", ");
   document.write(this.frame + ", "  + this.price );

//   document.write(this.image);
//  document.write("</td><td valign='top' width='50%'>");

  document.write("<br>");
  // this.title.link(this.thumbnail)
  if (false) {
// disable the following
  document.write(this.medium + ", " + this.size + ", $" + this.price + " ");
  document.write("<p>");
document.write('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">');
document.write('<input type="hidden" name="business" value="greg@imaja.com">');
document.write('<input type="hidden" name="return" value="http://www.imaja.com/store/paymentsuccess.html">');
document.write('<input type="hidden" name="undefined_quantity" value="1">');
document.write('<input type="hidden" name="item_name" value="' + this.title + '">');
// document.write('<input type="hidden" name="item_number" value="XXXXX">');
document.write('<input type="hidden" name="amount" value="' + this.price + '">');
// document.write('<input type="hidden" name="no_shipping" value="1">');
document.write('<input type="hidden" name="cancel_return" value="http://www.imaja.com/store/paymentcancelled.html">');
document.write('<input type="hidden" name="image_url" value="https://secure.us.com/imaja/secure/logo-imaja-paypal.jpg">');
document.write('<input type="hidden" name="no_note" value="0">');
document.write('<input type="hidden" name="cn" value="Comments">');
document.write('<input type="image" src="../images/AddToCart.jpg" name="submit" alt="Buy Now">');
document.write('</form>');

document.write('<form name="_xclick" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
document.write('<input type="hidden" name="cmd" value="_cart">');
document.write('<input type="hidden" name="business" value="greg@imaja.com">');
document.write('<input type="image" src="../images/ViewCart.jpg" border="0" name="submit" alt="View Cart">');
document.write('<input type="hidden" name="display" value="1">');
document.write('</form>');
} // end disable

//  document.write("</td></tr></table>");

  document.write("<p>");
}

function makeEntryForm() {
  document.write('<form name="imageInfoEntryForm" action="browser.html" method="Get">');
  document.write('image:<input type="text" name="image" size="50" maxlength="200"><br>');
  document.write('title:<input type="text" name="title" size="50" maxlength="200"><br>');
  document.write('thumbnail:<input type="text" name="image" size="50" maxlength="200"><br>');
  document.write('medium:<input type="text" name="medium" size="50" maxlength="200"><br>');
  document.write('size:<input type="text" name="size" size="50" maxlength="200"><br>');
  document.write('frame:<input type="text" name="frame" size="50" maxlength="200"><br>');
  document.write('price:<input type="text" name="price" size="50" maxlength="200"><br>');
  document.write('description:<textarea name="description" cols="80" rows="8"></textarea><br>');
  document.write('datastatement:<textarea name="datastatement" cols="80" rows="8"></textarea><br>');
  document.write('<input type="submit" value="Submit" name="submit">&nbsp;&nbsp;<input type="reset" value="Reset">');
  document.write('<INPUT type="button" VALUE="comma separated values" onClick="makedatastatement(this.form)">');
  document.write('</form>');
}

// Browser and Document Object Model version variables
var minNav3 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 3)
var minIE4 = (navigator.appName.indexOf("Microsoft") >= 0 && parseInt(navigator.appVersion) >= 4)
var minDOM = minNav3 || minIE4   // baseline DOM required for this function

// Generic function converts form element name-value pairs
// into a URL search string.
function getFormData(form) {
	var searchString = "?"
	var element, onePair
	// Harvest values for each type of form element
	for (var i = 0; i < form.elements.length; i++) {
		element = form.elements[i]
		if (element.type == "text" || element.type == "textarea") {
			onePair = escape(element.name) + "="
			onePair += escape(element.value)
		} else if (element.type.indexOf("select") != -1) {
			onePair = escape(element.name) + "="
			onePair += escape(element.options[element.selectedIndex].value)
		} else if (element.type == "radio" || element.type == "checkbox") {
			if (element.checked) {
				onePair = escape(element.name) + "="
				onePair += escape(element.value)
			} else continue
		} else continue
		searchString += onePair + "&"
	}
	return searchString
}

// Generic function converts form element name-value pairs
// into a URL search string.
function getformascommaseparatedvalues(form) {
	var recordstring = '';
	var element, datavalue
	// Harvest values for each type of form element
	for (var i = 0; i < form.elements.length; i++) {
		element = form.elements[i]
		if (element.name == "datastatement") { 
			continue
		} else if (element.type == "text" || element.type == "textarea") {
			datavalue = element.value
		} else if (element.type.indexOf("select") != -1) {
			datavalue = element.options[element.selectedIndex].value
		} else if (element.type == "radio" || element.type == "checkbox") {
			if (element.checked) {
				datavalue = element.value
			} else continue;
		} else continue;
		recordstring += '"' + datavalue + '"';
		if (form.elements[i+1].name != "datastatement") { 
		  recordstring += ', ';
		}
	}
	// recordstring += '"';
	return recordstring;
}

// Invoked by button to format form data and navigate
// to new page and pass data as URL search string.
function assembleAndGo(form) {
	if (minDOM) {
		var formData = getFormData(form)
		location.href = form.action + formData
	}
}

var lineterminator = "\n";

function makedatastatement(form) {
	if (minDOM) {
		var formData = getformascommaseparatedvalues(form);
// 	form.datastatement.value = form.datastatement.value + "addImageRec( " + formData + " );" + lineterminator + lineterminator;
    form.datastatement.value = "addImageRec( " + formData + " );" + lineterminator + lineterminator;
	}

}

// Create the object
function makeImageInfo(image, title, thumbnail, medium, size, frame, price, description) {
  this.image = picscanprefs.imageDirectory + "/" + image;
  if (title == "")
    title = image;
  if (thumbnail == "" || thumbnail == "xx")
    thumbnail = image;
  this.title = title; 
  this.thumbnail = picscanprefs.thumbnailDirectory + "/" + thumbnail;
  this.medium = medium;
  this.size = size;
  this.frame = frame;
  this.price = price;
  this.description =  description;
  this.displayListview = displayListview;
  this.displayThumbnail = displayThumbnail;
  this.displayFullSize = displayFullSize;
  this.makeEntryForm = makeEntryForm;
}



var gImageRecs = new Array(40);
var gImageRecCount = 0;

function addImageRec(image, title, thumbnail, medium, size, frame, price, description)
{
	gImageRecs[gImageRecCount] = new makeImageInfo(image, title, thumbnail, medium, size, frame, price, description);
	gImageRecCount++;
}

function displayThumbnailTable() {
	var contentIndex = (0 % gImageRecCount);
	var dispCnt = gImageRecCount; // gImageRecCount or 3;
	var columnIndex = 0;

	document.write("<table cellpadding='2'>");
	document.write("<tr>");
	while (dispCnt > 0) {
		if (columnIndex >= columns) {
			columnIndex = 0;
			document.write("</tr><tr>");
		}
		dispCnt--;
		document.write("<td align=center valign='top'");
		// hilite current image
		if (contentIndex == currentImage)
			document.write("bgcolor='#FFFF44'");
		else
			document.write("bgcolor='#DDDDDD'"); 
		document.write(">");
		
		gImageRecs[contentIndex].displayThumbnail(contentIndex);
		document.write("</td>");
		contentIndex = (contentIndex + 1) % gImageRecCount;
		columnIndex ++;
	}
	document.write("</tr></table>");
}

// more fields to add to database: <p>acrylic on plastic, approx. 7.5 x 13 in., unframed, $300</p>

// load database of images
// addImageRec(image, title, thumbnail, medium, size, frame, price, description)

addImageRec( "Glenn-McKay-SM07-0272.jpg", "Untitled 0272 : San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0448.jpg", "Untitled 0448: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0452.jpg", "Untitled 0452: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0453.jpg", "Untitled 0453: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0454.jpg", "Untitled 0454: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0455.jpg", "Untitled 0455: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0456.jpg", "Untitled 0456: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0204.jpg", "Untitled 0204: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0457.jpg", "Untitled 0457: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0458.jpg", "Untitled 0458: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0459.jpg", "Untitled 0459: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0460.jpg", "Untitled 0460: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0461.jpg", "Untitled 0461: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0462.jpg", "Untitled 0462: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0463.jpg", "Untitled 0463: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0464.jpg", "Untitled 0464: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0467.jpg", "Untitled 0467: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0466.jpg", "Untitled 0466: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 

addImageRec( "Glenn-McKay-SM07-0468.jpg", "Untitled 0468: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0469.jpg", "Untitled 0469: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0470.jpg", "Untitled 0470: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0471.jpg", "Untitled 0471: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0472.jpg", "Untitled 0472: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
// addImageRec( "Glenn-McKay-SM07-0473.jpg", "Untitled 0473: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0474.jpg", "Untitled 0474: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0476.jpg", "Untitled 0476: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 30 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0477.jpg", "Untitled 0477: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 15 in.", "unframed", "$1500, includes shipping", "" ); 
addImageRec( "Glenn-McKay-SM07-0479.jpg", "Untitled 0479: San Miguel 07", "xx", "acrylic, colored pencil and graphite, 640 gram Fabriano rag paper", "23 x 15 in.", "unframed", "$1500, includes shipping", "" ); 


// addImageRec( "xxx", "xxx", "xxx", "xxx", "xxx in.", "frame", "200", "xxx.");

function displayNextPrevLinks() {
  // create next and previous links
  var previndex = parseInt(currentImage)-1;
  if (previndex < 0)
    previndex = 0;
  var linkurl = baseurl + "?cur=" + previndex + "&";
  var str = "previous";
  document.write(str.link(linkurl) + "  | ");

  var nextindex = parseInt(currentImage)+1;
  if (nextindex > gImageRecCount-1)
    nextindex = gImageRecCount-1;
  linkurl = baseurl + "?cur=" + nextindex + "&";
  str = "next";
  document.write(str.link(linkurl));
  document.write(" | " + (parseInt(currentImage)+1) + " of " + gImageRecCount + " images");

  if (true) {
  linkurl = baseurl + "?viewas=thumbnails&cur=" + currentImage + "&";
  str="thumbnails";
  document.write(" | " + str.link(linkurl));
  }
  
  linkurl = baseurl + "?viewas=list&";
  str="list";
  document.write(" | " + str.link(linkurl));
}

function viewaslist() {
  var contentIndex = (0 % gImageRecCount);
  var dispCnt = gImageRecCount; // gImageRecCount or 3;

  document.write("<table border='0' cellspacing='0' cellpadding='1' width='550'>");
  while (dispCnt > 0) {
     dispCnt--;
     gImageRecs[contentIndex].displayListview(contentIndex);
     contentIndex = (contentIndex + 1) % gImageRecCount;
    //  document.write("<br>");
  }
  document.write("</table>");
}

pos = paramstr.indexOf("cur=");
if (pos > 0)
  currentImage = paramstr.substring(pos+4, paramstr.indexOf('&', pos));

//    currentImage = getparam("cur");

pos = paramstr.indexOf("thumbnailsize=");
if (pos > 0)
  thumbnailsize = paramstr.substring(pos+14, paramstr.indexOf('&', pos));

// thumbnailsize = getparam("thumbnailsize");

picscanprefs.thumbnailwidth = 30;
picscanprefs.thumbnailheight = 40;


document.write("<p align='center'>");
displayNextPrevLinks();
document.write("<p>");

if (paramstr.indexOf("viewas=list") > 0)  {
  document.write("<p align='left'>");
  viewaslist();
}
else if (paramstr.indexOf("viewas=thumbnails") > 0)  {
  displayThumbnailTable();
  document.write("<p>");
  document.write("<center>");
  gImageRecs[currentImage].displayFullSize();
  document.write("</center>");

}
else if (paramstr.indexOf("viewas=entryform") > 0)  {
  gImageRecs[currentImage].makeEntryForm();
  document.write("<p>");
  document.write("<center>");
  // gImageRecs[currentImage].displayFullSize();
  document.write("</center>");

}
else {
  document.write("<center>");
  gImageRecs[currentImage].displayFullSize();
  document.write("</center>");
}