var default_status_message = 'Jan Henderson Original Art';

function initialize()
{
  self.defaultStatus = self.default_status_message;
}

function updateStatusMessage(status_message, bypass_text_addendum, target_window)
{
  if (!Object.isUsable(target_window))
  {
    target_window = self;
  }
  if (String.isUsable(status_message))
  {
    target_window.status = bypass_text_addendum === true ? unescape(status_message) : self.default_status_message + ' - ' + unescape(status_message) + ' Page';
  }
  else
  {
    target_window.status = self.default_status_message;
  }
  return true;
}

function getLocationData()
{
  var location_data = {folder:self.location.href.slice(0, self.location.href.lastIndexOf('/') + 1), file:self.location.href.slice(self.location.href.lastIndexOf('/') + 1)};
  var special_character_indexes = [location_data.file.length, location_data.file.indexOf('#'), location_data.file.indexOf('?')];
  special_character_indexes.sort(function(value_A, value_B){return value_A - value_B;});
  while (special_character_indexes[0] < 0)
  {
    special_character_indexes.shift();
  }
  location_data.file = location_data.file.slice(0, special_character_indexes[0]);
  return location_data;
}

function updateNavigation(menu_location)
{
  var upper_navigation_element = self.document.getElementById('upper_navigation');
  var lower_navigation_element = self.document.getElementById('lower_navigation');
  var menu_code = '';
  var location_data = self.getLocationData(self);
  var page_is_gallery = location_data.folder.indexOf('/gallery_pages/') < 0 ? false : true;
  var page_data = new Array();
  page_data.push({file:'index.html', tip:'Home'});
  page_data.push({file:'questions_and_answers.html', tip:'Q &#x0026; A'});
  page_data.push({file:'new_paintings.html', tip:'New Paintings'});
  page_data.push({file:'classes.html', tip:'Classes'});
  page_data.push({file:'prints.html', tip:'Prints'});
  page_data.push({file:'pricing_and_commissions.html', tip:'Pricing/Commissions'});
  page_data.push({file:'guestbook.html', tip:'Guestbook'});
  page_data.push({file:'site_map.html', tip:'Site Map'});
  page_data.push({file:'contact.html', tip:'Contact'});
  for (var index = 0; index < page_data.length; index++)
  {
    if (page_data[index].file == location_data.file)
    {
      menu_code += '  <span id="activeMenuItem">' + page_data[index].tip + '</span>';
    }
    else
    {
      menu_code += '  <a href="' + (page_is_gallery ? '../' : './') + page_data[index].file + '" title="' + page_data[index].tip + ' Page" onmouseover="return self.parent.updateStatusMessage(\'' + page_data[index].tip + '\');" onmouseout="return self.parent.updateStatusMessage(\'\');">' + page_data[index].tip + '</a>';
    }
    if (index != page_data.length - 1)
    {
      menu_code += ' |';
    }
    menu_code += '\n';
  }
  if (upper_navigation_element !== null)
  {
    upper_navigation_element.innerHTML = menu_code;
  }
  if (lower_navigation_element !== null)
  {
    lower_navigation_element.innerHTML = menu_code;
  }
}

function insertCopyright(target_window)
{
  if (!Object.isUsable(target_window))
  {
    target_window = self;
  }
  target_window.document.write('<p id="copyright">Copyright &#x00A9; Jan Henderson</p>\n');
}

function insertPageHeader(target_window)
{
  if (!Object.isUsable(target_window))
  {
    target_window = self;
  }
  target_window.document.write('<div id="headline">jan henderson original art</div>\n');
  target_window.document.write('<div id="tagLine">extraordinary&#x00A0;&#x00A0;&#x00A0;&#x00A0;style&#x00A0;&#x00A0;&#x00A0;&#x00A0;texture&#x00A0;&#x00A0;&#x00A0;&#x00A0;vibrant&#x00A0;&#x00A0;&#x00A0;&#x00A0;intricate</div>\n');
  target_window.document.write('<br />\n');
}

function insertPageFooter(target_window)
{
  if (!Object.isUsable(target_window))
  {
    target_window = self;
  }
  target_window.document.write('<br />\n');
  self.insertCopyright();
}

function revealArtworkLayers()
{
  var artwork_layer;
  var total_artwork_layers = 5;
  var id_prefix = 'artwork';
  var id_as_string = '';
  for (var index = 1; index <= total_artwork_layers; index++)
  {
    id_as_string = index.toString();
    while (id_as_string.length < 2)
    {
      id_as_string = '0' + id_as_string;
    }
    artwork_layer = self.document.getElementById(id_prefix + id_as_string);
    if (artwork_layer.innerHTML.indexOf('placeholder.gif') < 0)
    {
      artwork_layer.style.display = 'inline';
    }
  }
}

function revealLayers(str_layer_prefix, num_total_layers)
{
  var layer;
  var id_as_string = '';
  if (!String.isUsable(str_layer_prefix))
  {
    str_layer_prefix = 'artwork';
  }
  if (!Number.isUsable(num_total_layers))
  {
    num_total_layers = 5;
  }
  for (var index = 1; index <= num_total_layers; index++)
  {
    id_as_string = index.toString();
    while (id_as_string.length < 2)
    {
      id_as_string = '0' + id_as_string;
    }
    layer = self.document.getElementById(str_layer_prefix + id_as_string);
    if (layer.innerHTML.indexOf('placeholder.gif') < 0)
    {
      layer.style.display = 'inline';
    }
	else
	{
      layer.style.display = 'none';
	}
  }
}

function announceAlternatives()
{
  var announcement_window = self.open((self.location.href.indexOf('gallery_pages') >= 0 ? '../' : './') + 'purchase_alternatives.html', 'AnnouncementWindow', 'width=400,height=300,left=50,top=50,menubar=no,scrollbars=no,toolbar=no,location=no');
  announcement_window.focus();
}

function updateShoppingCart(product_id)
{
  if (String.isUsable(product_id))
  {
    var artwork = self.obj_artwork[product_id];
    var purchasing_information = self.document.PurchasingInformation;
    if (Object.isUsable(artwork) && Object.isUsable(purchasing_information))
    {
      switch (artwork.type)
      {
        case 'limited edition' :
          if (confirm('The standard shipping cost for this artwork is $15.00 (tube).\n\nClick Cancel if you would rather have me ship the artwork flat for $35.00.'))
          {
            purchasing_information.shipping.value = '35.00';
          }
          else
          {
            purchasing_information.shipping.value = '15.00';
          }
          break;
        case 'open edition' :
          alert('The standard shipping cost for this artwork is $15.00 (tube).\n\nSend me an e-mail using the Contact page if you are interested in other shipping options.');
          purchasing_information.shipping.value = '15.00';
          break;
        default :
          purchasing_information.shipping.value = artwork.standardShipping;
      }
      purchasing_information.item_number.value = product_id;
      purchasing_information.item_name.value = artwork.name;
      purchasing_information.amount.value = artwork.price;
      purchasing_information.shipping2.value = artwork.additionalShipping;
      purchasing_information.submit();
      purchasing_information.item_number.value = '';
      purchasing_information.item_name.value = '';
      purchasing_information.amount.value = '';
      purchasing_information.shipping.value = '';
      purchasing_information.shipping2.value = '';
    }
  }
}

function handlePurchaseRequest(product_id)
{
  self.updateStatusMessage(null, null, null);
  if (!String.isUsable(product_id) || product_id.match(/JH\d{3}/i) === null)
  {
    self.announceAlternatives();
  }
  else
  {
    self.updateShoppingCart(product_id);
  }
}

function openShowcaseWindow(image_element)
{
  if (Object.isUsable(image_element) && String.isUsable(image_element.href))
  {
    Window_Code.show(image_element.href, 'ShowcaseWindow', {location:'no', menubar:'no', resizable:'no', status:'no', toolbar:'no'}, true);
  }
  return false;
}

function adjustWindow(target_window, width_value, height_value, left_value, top_value)
{
  if (Object.isUsable(target_window))
  {
    var padding_value = 100;
    width_value = width_value || 320;
    height_value = height_value || 240;
    left_value = left_value || 0;
    top_value = top_value || 0;
    target_window.resizeTo(width_value + padding_value, height_value + padding_value * 2);
    target_window.moveTo(left_value, top_value);
  }
}

String.isUsable = function(postulant_data)
{
  return typeof postulant_data == 'string' && postulant_data.length > 0;
};

Number.isUsable = function(postulant_data)
{
  return typeof postulant_data == 'number' && isFinite(postulant_data);
};

Object.isUsable = function(postulant_data)
{
  return !(postulant_data === null || typeof postulant_data == 'undefined');
};

self.initialize();
