function showTab( listId, index) {
      var list = document.getElementById(listId);
      var menuIndex = 0;
      var contentIndex = 0;
      for(var i in list.childNodes)
      {
          var node = list.childNodes[i];
	  if (node.className) {
	  if (node.className.indexOf("tabMenu") != -1) {
		  // Menu item
		  menuIndex++;
		  if (menuIndex == index) {
			  node.className = "tabMenuActive";
		  } else {
			  node.className = "tabMenu";
		  }
	  } else if (node.className.indexOf("tabContent") != -1) {
		  // Content item
		  contentIndex++;
		  if (contentIndex == index) {
			  node.className = "tabContentActive";
		  } else {
			  node.className = "tabContent";
		  }
	  }
	  }
      }
	
}

function displayProduct(prodId, magcoverId){
   getSWF('productViewerTab').displayProduct(prodId, magcoverId);
}

function updateMagCoverInProductViewer(magCoverId, forSale) {
   var swf = getSWF('productViewerTab');
   if (swf) {
	   swf.setMagcover(magCoverId, forSale);
   }
}

function updateProduct(productId) {
   getSWF('productViewerTab').setProduct(productId);
}

function showTabAndProduct(menuListId, tabListId, index, productId) {
   updateProduct(productId);
   showTab(menuListId, index);
   showTab(tabListId, index);
}

function orderProductFromFlash(productId, magCoverId) {
   addToCart(productId, magCoverId);
}

function updateCartNavPanel(serverResponse) {
   // Update the navigation panel
   var navElement = document.getElementById("nav_cart");
   if (navElement) {
      // Class
      var startIndex = serverResponse.indexOf("[");
      if (startIndex > 0) {
         var endIndex = serverResponse.indexOf("]", startIndex);
         if (endIndex > 0) {
	    navElement.className = serverResponse.substring(startIndex+1, endIndex);
	    // HTML
	    startIndex = serverResponse.indexOf("[", endIndex);
	    if (startIndex > 0) {
	       var endIndex = serverResponse.indexOf("]", startIndex);
	       if (endIndex > 0) {
		  navElement.innerHTML = serverResponse.substring(startIndex+1, endIndex);
	       }
	    }
         }
      }
   }
}

function addToCart(productId, magCoverId) {
   var request = "/addToCartAjax.shtml?productId=" + productId + "&magCoverId=" + magCoverId + "&quantity=1";

   new Ajax.Request(request, {

      onSuccess: function(transport) {

         var serverResponse = transport.responseText;
	 
	 // Update the shopping cart
         var elementToFill = document.getElementById("shoppingCart");
	 if (elementToFill) {
            elementToFill.innerHTML = serverResponse;
         }
	 
	 updateCartNavPanel(serverResponse);
      },

      onFailure: function() {
         alert('Failed to update your shopping cart. Please try again later.');
      }

   });
}

function removeFromCart(productId, magCoverId) {
   var request = "/removeFromCartAjax.shtml?productId=" + productId + "&magCoverId=" + magCoverId;

   new Ajax.Request(request, {

      onSuccess: function(transport) {

         var serverResponse = transport.responseText;       
         var elementToFill = document.getElementById("shoppingCart");
	 if (elementToFill) {
            elementToFill.innerHTML = serverResponse;
         }

	 updateCartNavPanel(serverResponse);
      },

      onFailure: function() {
         alert('Failed to update your shopping cart. Please try again later.');
      }

   });
}

function updateCartQuantity(productId, magCoverId, input, digitalProduct) {
   var quantity = parseInt(input.value, 10);
   if (!quantity && input.value != "0") {
      quantity = 1;
   }
   if ( (digitalProduct == true) && (quantity > 1) ) {
      input.value = 1;
      alert("This is a digital product. You can only order 1 copy.");
      return;
   }
   var request = "/updateCartQuantityAjax.shtml?productId=" + productId + "&magCoverId=" + magCoverId + "&quantity=" + quantity;

   new Ajax.Request(request, {

      onSuccess: function(transport) {

         var serverResponse = transport.responseText;       
         var elementToFill = document.getElementById("shoppingCart");
	 if (elementToFill) {
            elementToFill.innerHTML = serverResponse;
         }

	 updateCartNavPanel(serverResponse);
      },

      onFailure: function() {
         alert('Failed to update your shopping cart. Please try again later.');
      }

   });
}

function downloadLowRez(magCoverId) {
   window.location="/saveMagCover.shtml?magCoverId=" + magCoverId;
}

function showLearnMore(productId) {
   if ( (productId == 1) || (productId == 'HD') ) {
      showElement('learnMoreHD');
   } else if ( (productId == 2) || (productId == 'Mag') ) {
      showElement('learnMoreMag');
   } else if ( (productId == 3) || (productId == 'Frame') ) {
      showElement('learnMoreFrame');
   } else if ( productId == 'mugs' ) {
      showElement('learnMoreMugs');
   } else if ( productId == 'clothing' ) {
      showElement('learnMoreClothing');
   } else if ( productId == 'various' ) {
      showElement('learnMoreVarious');
   }
}

function hideLearnMore() {
   hideElement('learnMoreHD');
   hideElement('learnMoreMag');
   hideElement('learnMoreFrame');
   hideElement('learnMoreMugs');
   hideElement('learnMoreClothing');
   hideElement('learnMoreVarious');
}
