/**
 * JavaScript functionality for Ardtaraig Fine Foods.
 */
 
var $a = function(id) {
  return document.getElementById(id);
};

function checkCart() {
  var cart = $a('cart');
  if (cart) {
    var items = getCartItem(3);
	if (items > 0) {
	  changeOpac(0, "cart");
      cart.style.display = "block";
      doOpacity("cart", 0, 80, 800);
	}
  }
}

function doOpacity(id, opacStart, opacEnd, millisec) {
  var speed = Math.round(millisec / 100);
  var timer = 0;

  if (opacStart > opacEnd) {
    for(var i = opacStart; i >= opacEnd; i--) {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	timer++;
    }
  } 
  else if (opacStart < opacEnd) {
    for (var i = opacStart; i <= opacEnd; i++) {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	timer++;
    }
  }
}

function changeOpac(opacity, id) {
  var object = $a(id).style; 
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
}

/**
 * Switches the selected tab on product pages.
 */
function switchTab(tabIndex) {
  var tabTitles = $a('tab-titles');
  var tabContent = $a('tab-container');
  if (tabTitles && tabContent) {
	var newTabID = "tab" + tabIndex;
	var newContentID = "cont" + tabIndex;
	 
    var allTabs = tabTitles.getElementsByTagName("A");
	for (var i = 0; i < allTabs.length; i++) {
	  allTabs[i].className = allTabs[i].getAttribute("id") == newTabID ? "sel" : "nosel";
	}
	
	var allContent = tabContent.getElementsByTagName("DIV");
	for (var i = 1; i <= 3; i++) {
	  var tab = $a('cont' + i);
	  if (tab) {
	    tab.style.display = (i == tabIndex ? "block" : "none");
	  }
	}
  }
}

/**
 * Opens default mail client.
 */
function email(productName) {
  var script = "I thought you might be interested in " + productName + ".\n\nYou can view it at\n" + window.location;
  var subject = encodeURI(productName + " from Stairway");
  window.location.href = 'mailto:?subject=' + subject + '&body=' + encodeURI(script);
}
