// global.js for Dahlhaus
// site-wide functionality
// code by CN

function isDefined(property) {
  return (typeof property != 'undefined');
}

/**********************************************************************
   Page initialization
 *********************************************************************/

function pageInit() {
   if (isDefined(document.getElementsByTagName)) {
      
      // links
      var links = document.getElementsByTagName('a');
      for (var linkIndex = links.length - 1; linkIndex >= 0; linkIndex--) {
         var linkElement = links[linkIndex];
         
         // external links
         if (linkElement.className.indexOf('external') > -1) {
            linkElement.onclick = function() {
               openExternalLink(this.href);
               return false;
            };
         }
         
         // hdr-link links
         if (linkElement.className.indexOf('hdr-link') > -1) {
            linkElement.onmouseover = function() {
               document.getElementById('hdr-plus-' + this.firstChild.alt).style.display = 'block';
            };
            linkElement.onmouseout = function() {
               document.getElementById('hdr-plus-' + this.firstChild.alt).style.display = 'none';
            };
         }
         
         // photo-nav-item and filmstrip-item links
         if (linkElement.className.indexOf('photo-nav-item') > -1 || linkElement.className.indexOf('filmstrip-item') > -1) {
            linkElement.onmouseover = function() {
               if (this.parentNode.className != 'current') {
                  this.parentNode.className = 'hover';
               }
            };
            linkElement.onmouseout = function() {
               if (this.parentNode.className != 'current') {
                  this.parentNode.className = '';
               }
            };
         }

         // show-product-detail links
         if (linkElement.className.indexOf('show-product-detail') > -1) {
            linkElement.onclick = showProductLinkOnclick;
            linkElement.onmouseover = showProductLinkOnmouseover;
            linkElement.onmouseout = showProductLinkOnmouseout;
            (new (Image)).src = 'img/btn_plus.png';
            (new (Image)).src = 'img/btn_plus_over.png';
            (new (Image)).src = 'img/btn_minus.png';
            (new (Image)).src = 'img/btn_minus_over.png';
         }
      }
      
      // images
      var images = document.getElementsByTagName('img');
      for (var imageIndex = images.length - 1; imageIndex >= 0; imageIndex--) {
         var imageElement = images[imageIndex];
         
         // film-strip buttons
         if (imageElement.className.indexOf('filmstrip-button') > -1) {
            imageElement.onclick = filmstripButtonOnclick;
         }
      }
   }
}

function showProductLinkOnclick() {
   this.firstChild.src = 'img/btn_minus.png';
   document.getElementById('item-details').style.display = 'none';
   document.getElementById('item-nav').style.display = 'none';
   document.getElementById('product-detail').style.display = 'block';
   
   this.onclick = function() {
      this.firstChild.src = 'img/btn_plus.png';
      document.getElementById('item-details').style.display = 'block';
      document.getElementById('item-nav').style.display = 'block';
      document.getElementById('product-detail').style.display = 'none';
      
      this.onclick = showProductLinkOnclick;
      this.onmouseover = showProductLinkOnmouseover;
      this.onmouseout = showProductLinkOnmouseout;
      
      return false;
   };
   this.onmouseover = function() {
      this.firstChild.src = 'img/btn_minus_over.png';
   };
   this.onmouseout = function() {
      this.firstChild.src = 'img/btn_minus.png';
   };
   
   return false;
}

function showProductLinkOnmouseover() {
   this.firstChild.src = 'img/btn_plus_over.png';
}

function showProductLinkOnmouseout() {
   this.firstChild.src = 'img/btn_plus.png';
}

function filmstripButtonOnclick() {
   if ((this.id == 'filmstrip-back' && currentFilmstrip == 1) || 
       (this.id == 'filmstrip-forward' && currentFilmstrip == numFilmstrips)) {
      return;
   }
   
   // show new filmstrip
   document.getElementById('filmstrip' + currentFilmstrip).style.display = 'none';
   currentFilmstrip += (this.id == 'filmstrip-back') ? -1 : 1;
   document.getElementById('filmstrip' + currentFilmstrip).style.display = 'block';
   
   // change button images
   document.getElementById('filmstrip-back').src = (currentFilmstrip == 1) ? 'img/btn_back_inactive.png' : 'img/btn_back_active.png'
   document.getElementById('filmstrip-back').className = 'filmstrip-button' + (currentFilmstrip != 1 ? ' clickable' : '');
   document.getElementById('filmstrip-forward').src = (currentFilmstrip == numFilmstrips) ? 'img/btn_forward_inactive.png' : 'img/btn_forward_active.png'
   document.getElementById('filmstrip-forward').className = 'filmstrip-button' + (currentFilmstrip != numFilmstrips ? ' clickable' : '');
}

// call pageInit when document finishes loading
if (isDefined(window.addEventListener)) {
   window.addEventListener('load', pageInit, false);
}
else if (isDefined(window.attachEvent)) {
   window.attachEvent('onload', pageInit);
}


/**********************************************************************
   Popups and New Windows
 *********************************************************************/

// openExternalLink(url) - use to open all off-site links
// ie: <a href="http://url.here" onclick="openExternalLink(this.href); return false;">
function openExternalLink(url) {
   return window.open(url, 'external', 'location,menubar,resizable,scrollbars,status,toolbar');
}
