/**
 * Provides functionality for product tabs in Actinic.
 * Requires actiniccore.js for cookie functions.
 *
 * @author Lewis MacKenzie, teclan - http://www.teclan.com/
 * @date 24/11/08
 *
 * @modified 03/02/09
 */

//Shortcut for document.getElementById
var $ = function(id) {
  return document.getElementById(id);
};

//Returns all elements of type 'nodeType' with the class 'className'
var $$ = function(nodeType, className) {
  var ret = new Array();
  var arr = document.getElementsByTagName(nodeType.toUpperCase());
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].className && arr[i].className == className) {
	  ret.push(arr[i]);
	}
  }
  return ret;
};

var currentTab = 0;
var currentSection = 0;

//Hides all tabs and shows the one defined by 'tabIndex'
function toggleTab(tabIndex, sectionID) {
  setLoaderVisible(true);
  getTabContent(tabIndex, sectionID);
  currentTab = tabIndex;
  currentSection = sectionID;
}

//Reinstates the last selected tab after a page bounce e.g. adding to cart
function checkTabAfterBounce() {
  var savedTab = getCookie("TABINDEX_PAGE");
  if (savedTab) {
    var savedTabCrumbs = savedTab.split("::");
	if (savedTabCrumbs[0] && savedTabCrumbs[0] == window.location) {
	  if (savedTabCrumbs[1] && !isNaN(savedTabCrumbs[1]) && savedTabCrumbs[2] && !isNaN(savedTabCrumbs[2])) {
	    toggleTab(savedTabCrumbs[1], savedTabCrumbs[2]);
	  }
	}
  }
}

//The HTTP request object
var request = null;

function getTabContent(tabID, sectionID) {
  //Mozilla
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  //Microsoft
  else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (!request) request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (request) {
    request.onreadystatechange = tabCallBack;
    try {
      request.open("POST", theURL + "TabContent" + sectionID + ".php?", true);
      request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      request.send("tab=" + tabID);
    }
    catch (e) {
      alert(e);
	  setLoaderVisible(false);
    }
  }
  else {
    window.alert("Your browser does not support AJAX!");
	setLoaderVisible(false);
  }
}

function tabCallBack() {
  if (request.readyState == 4) {
    if (request.status == 200) {
	  $('tab-container').innerHTML = request.responseText;
	  redisplayNav();
	  setLoaderVisible(false);
	  window.location.hash = "tabsTop";
	}
  }
}

function setLoaderVisible(visible) {
  var m = document.getElementsByName('loader');
  for (var i = 0; i < m.length; i++) {
    m[i].style.visibility = visible ? "visible" : "hidden";
  }
}

function redisplayNav() {
  var theNavArr = $$('div', 'tab-nav');
  for (var i = 0; i < theNavArr.length; i++) {
    var tabLinks = theNavArr[i].getElementsByTagName("A");
	for (var j = 0; j < tabLinks.length; j++) {
	  tabLinks[j].className = (j == currentTab ? "selected" : "");
	  if (j == currentTab) {
	    //set a cookie to remember the selected tab for page bounces - see below
		//setCookie("TABINDEX_PAGE", getCookie("ACTINIC_REFERRER") + "::" + currentTab + "::" + currentSection);
	  }
	}
  }
}

function tabsGo(directon, sectionID) {
  if (directon <= -1) {
    if (currentTab > 0) {
	  toggleTab(currentTab + directon, sectionID);
	}
  }
  else {
    if (currentTab < maxTabs-1) {
	  toggleTab(currentTab + directon, sectionID);
	}
  }
}

function checkSearchResult() {
  if (window.location.hash) {
    var h = window.location.hash.replace(/#/, '');
	setLoaderVisible(true);
	getTabFromHash(h);
  }
}

if (window.attachEvent) { 
  window.attachEvent("onload", checkSearchResult); 
} 
else {  
  window.addEventListener("load", checkSearchResult, false); 
}

function getTabFromHash(productHash) {
  //Mozilla
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  //Microsoft
  else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (!request) request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (request) {
    request.onreadystatechange = hashCallBack;
    try {
      request.open("POST", theURL + "TabContent" + section + ".php", true);
      request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      request.send("hash=" + productHash);
    }
    catch (e) {
      alert(e);
	  setLoaderVisible(false);
    }
  }
  else {
    window.alert("Your browser does not support AJAX!");
	setLoaderVisible(false);
  }
}

function hashCallBack() {
  if (request.readyState == 4) {
    if (request.status == 200) {
	  var tab = parseInt(request.responseText);
	  if (!isNaN(tab)) {
	    toggleTab(tab, section);
	  }
	  else {
	    setLoaderVisible(false);
	  }
	}
  }
}