// these are defaults for gallery
// they can be overriden in code if different settings are more aestetically pleasing
function init_gallery_settings()
{
	var aGallerySettings = new Object();
	aGallerySettings.Descriptions = 0;
	aGallerySettings.ThumbWidth = 100; // width of the thumbnail picture
	aGallerySettings.ImageBorderW = 20; // borderwidth of the big picture
	aGallerySettings.RowNumber = 4; // number of thumbnail rows
	aGallerySettings.InactiveThumbBorderWidth = 5; // border width of thumbnail
	aGallerySettings.ActiveThumbBorderWidth = aGallerySettings.InactiveThumbBorderWidth*2; // border width of thumbnail when mouse hovers over
	aGallerySettings.ActiveThumbSizeRatio = 1.2; // ratio of enlarging thumbnail picture when mouse hovers over
	aGallerySettings.DeltaXstep = Math.floor(aGallerySettings.ThumbWidth/4);
	aGallerySettings.ThumbBorderStyle = 'px solid #666666';
	aGallerySettings.printPrice = 60;
	aGallerySettings.printDescr = "12x18 inch print";
	aGallerySettings.GalleryName = "";
	aGallerySettings.TextCellWidth = 160;
	aGallerySettings.gallery_title = "polyrealism";
	aGallerySettings.gallery_description = "paintings and photography by poul a. costinsky";
	aGallerySettings.keywords = "art paintings painting surrealism oil artist acrylic acrylics photography nudes nude fetish erotic goth burning man desert bellydancing fashion alternative lolita submission domination BDSM teen teens";
	aGallerySettings.DefaultMaterial = "oil on canvas";
	aGallerySettings.DefaultYear = "2007";
	aGallerySettings.DefaultSize = '';
	
	
	return aGallerySettings;
}

function init_gallery_data()
{
	var aGalleryData = new Object();
	aGalleryData.ImageArray = new Array();
	aGalleryData.MaxbigimageH = 500;
	aGalleryData.MaxbigimageW = 0;
	aGalleryData.CurrentPage = -1;
	aGalleryData.PagesArray = new Array();
	aGalleryData.atb = GallerySettings.ActiveThumbBorderWidth   + GallerySettings.ThumbBorderStyle;
	aGalleryData.itb = GallerySettings.InactiveThumbBorderWidth + GallerySettings.ThumbBorderStyle;
	aGalleryData.id = "";
	return aGalleryData;
}

var GallerySettings = init_gallery_settings();
var GalleryData = init_gallery_data();

// end defaults
var temp_tw = 2; // remembered width of the thumbnail picture;
function thumb_mouseover(img)
{
	img.style.border = GalleryData.atb;
	img.style.zIndex = 99;
	temp_tw = img.width;
	img.width = Math.floor(temp_tw*GallerySettings.ActiveThumbSizeRatio);
}

function thumb_mouseout(img)
{
	img.style.border = GalleryData.itb;
	img.style.zIndex = 10;
	img.width = temp_tw;
}

function paging_goto (imageIndex)
{
	// see if we need to show different page
  	var pageinfo = GalleryData.PagesArray[GalleryData.CurrentPage];
	if (imageIndex < pageinfo.first || imageIndex > pageinfo.last)
	{
		for (i = 0; i < GalleryData.PagesArray.length; i++)
		{
			pageinfo = GalleryData.PagesArray[i];
			if (imageIndex >= pageinfo.first && imageIndex <= pageinfo.last)
			{
				goto_page(1000+i);
			}
		}
	} 
   var node = GalleryData.ImageArray[imageIndex];
   // load the image
   document.all["mainimage"].src = node.src;
   document.all["mainimage"].alt = node.alt;
   // load the description
   document.all["maintext"].innerHTML = '<b>"' + node.alt + '"</b>';
   // update nav buttons
   if (imageIndex > 0)
       document.all["previm"].tabIndex = imageIndex - 1;
   else
       document.all["previm"].tabIndex = 0;
   if (imageIndex < GalleryData.ImageArray.length - 1)
       document.all["nextim"].tabIndex = Math.floor(imageIndex) + 1;
   else
       document.all["nextim"].tabIndex = GalleryData.ImageArray.length - 1;

   set_buy_prints_section();
   set_description_section(node);
}

function thumb_mousedown(img)
{
   paging_goto (img.alt);
}

function split_link_text(text)
{
	if (text.length > 3)
	    return '<b>' + text.substring (0,2) + '</b>' + text.substring (2, text.length);
    else
    	return '<b>' + text + '</b>';
}

function add_navigation_button (text, id, index)
{
    //return '<td><div id="' + id + '" tabIndex=" ' + index + '" onmousedown="paging_goto(this.tabIndex)"><a href="#"><font size="3">' + split_link_text(text) + '</font></a></div></td><td width="4">&nbsp</td>';
	var image = '/gifs/';
	switch (text)
	{
	   case '>': image += 'f'; break;
	   case '>>': image += 'ff'; break;
	   case '>>>': image += 'fff'; break;
	   case '<': image += 'r'; break;
	   case '<<': image += 'rr'; break;
	   case '<<<': image += 'rrr'; break;
	}    
	image += '.gif';
    return '<td width="1"><div id="' + id + '" tabIndex=" ' + index + '" onmousedown="paging_goto(this.tabIndex)"><a href="#"><img border="0" src="' + image + '"/></a></div></td><td width="1">&nbsp</td>';
}

function create_image_list (gallery)
{
    var images; 
    if (document.all)
    	images = document.all.tags("img");
    else
    	images = document.getElementsByTagName("img");
    var theid = gallery.id;
    GalleryData.id = theid;
    // build array of images
    // alert("found images: " + images.length);
    for (i = 0; i < images.length; i++)
    {
	 var node = images[i];
     var oparent = node.parentNode;
	 if (oparent.id == theid)
	 {
	   var imageDesc = new Object();
	   var src = node.src;
	   var slash = src.lastIndexOf('/');
	   if (slash > 0)
	   {
	       src = src.substring(slash+1, src.length);
		   if (GallerySettings.GalleryName.length < 1)
		   {
		   		var path = node.src.substring(0, slash);
		   		slash = path.lastIndexOf('/');
		   		if (slash > 0)
					GallerySettings.GalleryName = path.substring(slash+1, path.length);
		   }
	   }
	   imageDesc.src = src;
	   imageDesc.h = 1.0*node.getAttribute("height",2);
	   imageDesc.w = 1.0*node.getAttribute("width",2);
	   imageDesc.alt = node.getAttribute("alt",2);
	   if (imageDesc.alt == null || imageDesc.alt.length == 0)
	   {
	   	  var text = imageDesc.src;
		  var iii = text.lastIndexOf('.');
		  if (iii > 0)
		  	text = text.substring(0, iii);
	   	  imageDesc.alt = text;
       }
       if (GallerySettings.Descriptions == 1)
       {
       		imageDesc.Material 	= node.getAttribute("controls",2);
       		imageDesc.Size 		= node.getAttribute("datafld",2);
       		imageDesc.Year 		= node.getAttribute("datasrc",2);
       		imageDesc.Price		= node.getAttribute("lang",2);
       }

	   if (GalleryData.MaxbigimageH < imageDesc.h)
	   	  GalleryData.MaxbigimageH  = imageDesc.h;
	   if (GalleryData.MaxbigimageW < imageDesc.w)
	   	  GalleryData.MaxbigimageW = imageDesc.w;
	   
	   GalleryData.ImageArray.push (imageDesc);
	 }
  }
  
}

function goto_page(page)
{
  GalleryData.CurrentPage = page-1000;
  var gallery_object = document.all[GalleryData.id];
  apply_gallery_styles(gallery_object);
}

var page_nav_divider = '<td width="4">&nbsp</td>';
function add_page_navigation (text, nextpage)
{
	text = 1.0+text;
	if (text == GalleryData.CurrentPage+1)
		return '<td><div class="headtext">' + text + '</div></td>' + page_nav_divider;
	else
    	return '<td><div tabIndex="' + (nextpage + 1000) + '" onmousedown="goto_page(this.tabIndex)"><a href="#"><font size="3">' + split_link_text(text) + '</font></a></div></td>' + page_nav_divider;
}

function create_page_navigation()
{
  var html = '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td><span class="headtext"><b>pa</b>ges</span></td><td width="10">&nbsp</td>';
  for (i = 0; i < GalleryData.PagesArray.length; i++)
  {
  	html += add_page_navigation(i, i);
  }
  html += '<td width="99%"></td></tr></table>';
  return html;
}

function init_page_info (i)
{
    var pageinfo = new Object();
    pageinfo.first = i;
    pageinfo.last = -1;
    pageinfo.divh = -1;
    pageinfo.divw = -1;
    pageinfo.thumbs = new Array();
    GalleryData.PagesArray.push(pageinfo);
    return pageinfo;
}

function add_thumb_to_page (pageinfo, src, image_number, thumbwidth, offsetx, offsety)
{
	var html = '<img src="t/' + src + '" alt="' + image_number + '" width="' + thumbwidth + '" style="border:' + GallerySettings.InactiveThumbBorderWidth + 'px solid #666666; position: absolute; top:' + offsety + ' ;left:' + offsetx + '; z-index: 10" onmouseover="thumb_mouseover(this)" onmouseout="thumb_mouseout(this)" onmousedown="thumb_mousedown(this)" />';
	html = '<a href="' + src + '">' + html + '</a>' + html;
	var thumbinfo = new Object();
	thumbinfo.number = image_number;
	thumbinfo.html = html;
	pageinfo.thumbs.push(thumbinfo);
}

function pre_calculate_thumbs()
{
  var count = 0;
  var offsetx = 0;
  var offsety = 0;
  var smalldeltay = Math.floor(GallerySettings.DeltaXstep*0.5);
  var divw = 0;
  var divh = 0;
  var rowheight = 0;
  var maxrowwidth = GallerySettings.ThumbWidth*GallerySettings.RowNumber - 
  	GallerySettings.DeltaXstep*(GallerySettings.RowNumber-1) + 2*GallerySettings.InactiveThumbBorderWidth; 
  var pageinfo = init_page_info(0);
  for (i = 0; i < GalleryData.ImageArray.length; i++)
  {
	var node = GalleryData.ImageArray[i];
	// add image to left pane gallery
    var src = node.src;
    var h1 = node.h;
    var w1 = node.w;
    var thumbwidth = GallerySettings.ThumbWidth;
    var thumbheight = Math.floor((GallerySettings.ThumbWidth*h1)/w1);
    if (w1 > h1)
    {
    	thumbheight = GallerySettings.ThumbWidth;
    	thumbwidth = Math.floor((GallerySettings.ThumbWidth*w1)/h1);
    	
    }
	var deltax = Math.floor(thumbwidth - GallerySettings.DeltaXstep);
	var deltay = Math.floor(rowheight + smalldeltay + GallerySettings.InactiveThumbBorderWidth);		
	if (offsetx + thumbwidth > maxrowwidth)
	{
      offsetx = 0;
      offsety += (deltay - (count - 1)*smalldeltay);
      offsety = Math.floor(offsety);
      count = 0;
      rowheight = 0;
	}
    if (thumbheight > rowheight)
    	rowheight = thumbheight; 
	if (offsety + thumbheight > GalleryData.MaxbigimageH)
	{
	  // enough images, let's add link to other page(s)
	    pageinfo.divh = divh; 
	    pageinfo.divw = divw;
	    pageinfo.last = i - 1;
	    pageinfo = init_page_info(i);
	    offsety = 0;
	    offsetx = 0;
		divw = 0;
		divh = 0;
		rowheight = 0;
		count = 0;
	}
	
	if (offsetx + thumbwidth > divw)
	    { divw = offsetx + thumbwidth; }
	if (divh < offsety + thumbheight)
		{ divh = thumbheight + offsety; }	
	if (i == GalleryData.ImageArray.length - 1)
	{
	    pageinfo.divh = divh; 
	    pageinfo.divw = divw;
	    pageinfo.last = i;
	}
	
	add_thumb_to_page (pageinfo, src, i, thumbwidth, offsetx, offsety);
	count++;
    if (count == GallerySettings.RowNumber)
    {
      offsetx = 0;
      offsety += deltay - (count - 2)*smalldeltay;
      count = 0;
      rowheight = 0;
    }
    else
    {
      offsetx += deltax;
      offsety += smalldeltay;
    }
  }	
}

function set_buy_prints_section()
{
	if (GallerySettings.printPrice > 0)
	{
		var image = document.all["mainimage"];
		var itemname = "'"+image.alt+"'";
		var itemid = GallerySettings.GalleryName + ' - ' + itemname + ' - ' + GallerySettings.printDescr + ' - ' + '$' + GallerySettings.printPrice;
		var itemprice = '$' + GallerySettings.printPrice;
		var html = '<table><tr><td width="20">&nbsp</td><td><span class="headtext">buy prints...</span></td><td height="10">';
		html += 		'<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">';
		html += 		'<input type="hidden" name="add" value="1">';
		html += 		'<input type="hidden" name="cmd" value="_cart">';
		html += 		'<input type="hidden" name="business" value="poulcost@hotmail.com">';
		html += 		'<input type="hidden" name="item_name" value="' + itemname + '">';
		html += 		'<input type="hidden" name="item_number" value="' + itemid + '">';
		html += 		'<input type="hidden" name="handling_cart" value="5">';
		html += 		'<input type="hidden" name="amount" value="' + GallerySettings.printPrice + '">';
		html += 		'<input type="hidden" name="no_note" value="1">';
		html += 		'<input type="hidden" name="currency_code" value="USD">';
		html += 		'<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" width="68" height="23" border="0" name="submit"></form>';
		html += '</td></tr></table>';
		document.all["buyprints"].innerHTML = html;
	}
}

function preload_big_images()
{
	var html = "";
  	for (i = 0; i < GalleryData.ImageArray.length; i++)
  	{
  		html += '<img src="' + GalleryData.ImageArray + '" width="1" height="1" style="position: absolute; top:-1000 ;left:-1000;" />'; 
  	}
	return html;	
}

function set_buy_original (node)
{
	var html =  '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">';
		html += '<input type="hidden" name="add" value="1">';
		html += '<input type="hidden" name="cmd" value="_cart">';
		html += '<input type="hidden" name="business" value="poulcost@hotmail.com">';
		html += '<input type="hidden" name="item_name" value="' + node.alt + '">';
		html += '<input type="hidden" name="item_number" value="' + node.alt + ',' + default_string (node.Material , GallerySettings.DefaultMaterial) + ',' + node.Size + ',' + default_string (node.Year, GallerySettings.DefaultYear) + '">';
		html += '<input type="hidden" name="handling_cart" value="5">';
		html += '<input type="hidden" name="amount" value="' + node.Price + '">';
		html += '<input type="hidden" name="no_note" value="0">';
		html += '<input type="hidden" name="currency_code" value="USD">';
		html += '<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" width="68" height="23" border="0" name="submit">';
		html += '</form>';
	return html;
}

function default_string (str, defaultstr)
{
	if (str == null || str.length < 1)
		return defaultstr;
	return str;
}

function set_description_section(node)
{
  if (GallerySettings.Descriptions == 1)
  {
	document.all["material"].innerHTML = default_string (node.Material , GallerySettings.DefaultMaterial);
	document.all["theyear"].innerHTML = default_string (node.Year, GallerySettings.DefaultYear);
	document.all["thesize"].innerHTML = default_string (node.Size, GallerySettings.DefaultSize);
	if (node.Price == null || node.Price.length < 1)
	{
		document.all["theprice"].innerHTML = 'SOLD';
		document.all["buyoriginal"].innerHTML = '&nbsp';
	}
	else
	{
		document.all["theprice"].innerHTML = "$"+node.Price;
		document.all["buyoriginal"].innerHTML = set_buy_original (node);
	}
  }	
}

function CreateDescriptionTable()
{
	var html = 	'<table border="0" cellpadding="0" cellspacing="0" width="100%">' +
				'<tr><td align="left" nowrap="yes"><div id="material" class="subheadtext" >oil on canvas</div></td></tr>' +
				'<tr><td><table border="0" cellpadding="0" cellspacing="0"><tr>' +
				'<td width="10" align="left"><div id="theyear" class="subheadtext ">2006</div></td>' + 
				'<td width="50">&nbsp</td>' +
				'<td align="left"><div id="thesize" class="subheadtext ">12x18"</div></td>' + 
				'</tr></table></td></tr>' +
				'<tr><td align="left"><div id="theprice" class="headtext">$1800</div></td></tr>' +
				'<tr><td align="left"><div id="buyoriginal">&nbsp</div></td></tr>' +
				'</table>';
				
	return html;
}

function apply_gallery_styles(gallery)
{
  var topright = document.all["top_right"];
  var toprigthsrc = topright.src;
  //topright.src = "/gifs/loading.gif";
  // none of this will work if images don't have fully specified height and width!
  var big_images_section = "";
  //alert("current page: " + GalleryData.CurrentPage);

  if (GalleryData.CurrentPage < 0)
  {
    create_image_list (gallery);
    pre_calculate_thumbs();
    GalleryData.CurrentPage = 0;
    big_images_section = preload_big_images();
  }
  
  var pageinfo = GalleryData.PagesArray[GalleryData.CurrentPage];
  var totalpages = GalleryData.PagesArray.length;
  var tdh = Math.floor(1.0*pageinfo.divh + 3*GallerySettings.InactiveThumbBorderWidth);
  var divh1 = tdh;
  var divw1 = (Math.floor(pageinfo.divw) + 10);
  var tdhmin = 2*GallerySettings.ImageBorderW+ 1*GalleryData.MaxbigimageH + 60;
  if (tdh < tdhmin )
  	tdh = tdhmin ;
  var pagebigimagew = 0;
  for (i = pageinfo.first; i <= pageinfo.last; i++)
  {
    if (pagebigimagew < GalleryData.ImageArray[i].w)
    	pagebigimagew = GalleryData.ImageArray[i].w;
  }
  var html = '<table border="0" cellpadding="0" cellspacing="0" width="100%" ><tr height="' + tdh + '">';
  html += '<td width="10" height="' + tdh + '">&nbsp</td>';
  html += '<td valign ="top" width="' + divw1 + '" height="' + tdh + '">';  
  html += '<div style="position:relative; left:0px; top:0px; height:' + divh1 + 'px; width:' + divw1 + 'px">';
  
  for (i = 0; i < pageinfo.thumbs.length; i++)
  	html += pageinfo.thumbs[i].html;
  //html += html1;
  html += '</div>';
  if (GalleryData.PagesArray.length > 1)
      html += '<div>' + create_page_navigation() + '</div>'; 
      
  // right pane - image and title
  var big_image_index = pageinfo.first;
  var text = GalleryData.ImageArray[big_image_index].alt;
  html += '</td><td width="20">&nbsp</td><td valign="top" width="99%"><table border="0" cellpadding="0" cellspacing="0" width="100%">';
  // text
  html += '<tr><td>';
  html += '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>'; 
  html += add_navigation_button ("<<<", "firstim", 0);
  if (totalpages > 1)
  {
    var p1 = 0;
    if (GalleryData.CurrentPage > 0)
    	p1 = GalleryData.PagesArray [GalleryData.CurrentPage-1].first;
  	html += add_navigation_button ("<<", "prevpim" , p1);
  }
  html += add_navigation_button ("<", "previm" , 0);
  html += add_navigation_button (">", "nextim" , 1);
  if (totalpages > 1)
  {
    var p1 = GalleryData.ImageArray.length - 1;
    if (GalleryData.CurrentPage < totalpages - 1)
    	p1 = GalleryData.PagesArray [GalleryData.CurrentPage+1].first
  	html += add_navigation_button (">>", "nextpim" , p1);
  }
  html += add_navigation_button (">>>", "lastim" , GalleryData.ImageArray.length - 1);
  html += '<td width="10"></td><td width="' + GallerySettings.TextCellWidth + '" align=center><div id="maintext" class="headtext"><b>"' + text + '"</b></div></td>';
  html += '<td><div id="buyprints"></div>';
  html += '</tr></table>';
  html += '</td></tr>';
  // end text
  // spacer
  html +=  '<tr><td height="10"></td></tr>';
  // image
  html += '<tr><td width="' + pagebigimagew + '">';
  if (GallerySettings.Descriptions == 1)
  {
  	html += '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td width="10">';
  }
  html += '<img id="mainimage" src="';
  html += GalleryData.ImageArray[big_image_index].src + '" ';
  html += ' alt="' + GalleryData.ImageArray[big_image_index].alt + '" ';
  if (GallerySettings.ImageBorderW> 0)
  {
    html += 'style="border: ' + GallerySettings.ImageBorderW+ 'px solid #000000" ';
  }
  html += '>';
  if (GallerySettings.Descriptions == 1)
  {
    html += '</td><td width="40">&nbsp</td><td valigh="center">';
    html += CreateDescriptionTable();
  	html += '</td></tr></table>';
  }
  html += '</td></tr>';
  // end image
  // spacer
  html +=  '<tr><td height="20"></td></tr>';
  // text
  html += '</table></td><td width="10"></td></tr></table>';
  //html += big_images_section;
  gallery.innerHTML = html;
  gallery.style.height = tdh + 'px';
  gallery.style.width = (1*divw1 + 1*pagebigimagew + 20 + 2*GallerySettings.ImageBorderW) + 'px';
  set_buy_prints_section();
  if (GallerySettings.Descriptions == 1)
  {
    set_description_section(GalleryData.ImageArray[big_image_index]);
  }
  topright.src = toprigthsrc ; 
}		


