var CSS_CLASS_CURRENT = "current";
var CSS_CLASS_OVER = "over";
var CSS_CLASS_HIDE = "hide";

// OS dependent class
var gCssSectionClass;
var gSelectedSection = 0;
var gIeVersion;

//For instrumentation
var inst_prev_nav_click = 'welcome';
var inst_nav_click = 'welcome';
var inst_path = 'welcome';


// If IE6 explicitly we need to change the background-position of the element 
// as the class name itself doesn't render properly
var gBackgroundPosIe6 = new Array();
gBackgroundPosIe6[0] = {current: "-278px 0px", over: "-139px 0px", none: "0px 0px"};
gBackgroundPosIe6[1] = {current: "-278px -38px", over: "-139px -38px", none: "0px -38px"};
gBackgroundPosIe6[2] = {current: "-278px -76px", over: "-139px -76px", none: "0px -76px"};
gBackgroundPosIe6[3] = {current: "-278px -114px", over: "-139px -114px", none: "0px -114px"};
gBackgroundPosIe6[4] = {current: "-278px -152px", over: "-139px -152px", none: "0px -152px"};
gBackgroundPosIe6[5] = {current: "-278px -190px", over: "-139px -190px", none: "0px -190px"};

function setNavigationState(aState, aObj) {
  var key = aObj.getAttribute("int");
  switch (aState) {
    case CSS_CLASS_CURRENT:
      if (gIeVersion == 6) {
        aObj.style.backgroundPosition = gBackgroundPosIe6[key].current;
      }
      aObj.className = CSS_CLASS_CURRENT;
      break;
    
    case CSS_CLASS_OVER:
      if (gIeVersion == 6) {
        aObj.style.backgroundPosition = gBackgroundPosIe6[key].over;
      }
      aObj.className = CSS_CLASS_OVER;
      break;
      
    default:
      if (gIeVersion == 6) {
        aObj.style.backgroundPosition = gBackgroundPosIe6[key].none;
      }
      aObj.className = "";
      break;
  }
}

function toggleFeature(aGroup, aObj) {
  for (var i = 0; i < aGroup.length; i++) {
    var sectionId = "section"+i;
    var sectionObj = document.getElementById(sectionId);
    var navObj = document.getElementById(sectionObj.getAttribute("parentid"));
    if (sectionId == aObj.getAttribute("sectionid")) {
      setNavigationState(CSS_CLASS_CURRENT, navObj);
      sectionObj.className = gCssSectionClass;
    } else {
      setNavigationState("", navObj);
      sectionObj.className = CSS_CLASS_HIDE;
    }
  }
}

// If nav button is current state, remain current with no hover state
function checkForCurrentState(aObj, aState) {
  return (aObj.className != CSS_CLASS_CURRENT) ? aState : CSS_CLASS_CURRENT;
}

// Init nagivation event handlers for onclick/mouseover/mouseout
function _initFeatures() {
  // Feature navigation
  var els = document.getElementById("featureNav").getElementsByTagName("li");
  for (var i = 0; i < els.length; i++) {
    // Assign the respective content block section/parent variables
    els[i].setAttribute("sectionid", "section"+i);
    els[i].setAttribute("int", i);
    var sectionObj = document.getElementById(els[i].getAttribute("sectionid"));
    sectionObj.setAttribute("parentid", els[i].getAttribute("id"));

    // Assign event handlers
    els[i].onclick = function() {
      inst_prev_nav_click = inst_nav_click;
      inst_nav_click = this.getAttribute('inst');
      gs_inst(inst_nav_click, true);
      setNavigationState(CSS_CLASS_CURRENT, this);
      toggleFeature(els, this);
    }
    els[i].onmouseover = function() {
      setNavigationState(checkForCurrentState(this, CSS_CLASS_OVER), this);
    }
    els[i].onmouseout = function() {
      setNavigationState(checkForCurrentState(this, ""), this);
    }
  }
}

// Differentiate for OS specific product screenshots
function _initUniqueOS() {
  var appVer = navigator.appVersion.toLowerCase();
  if (appVer.indexOf("msie")) {
    gIeVersion = parseInt(appVer.split("msie")[1]);
  }
  if (appVer.indexOf("mac") == -1) {
    gCssSectionClass = "sectionWin";
  } else {
    gCssSectionClass = "sectionMac";
  }
  // Init current section to the proper OS based image
  document.getElementById("section" + gSelectedSection).className = gCssSectionClass;
}

// Diplay upgrade or install welcome page based on qry string
// qry string should include "welcome=upgrade" for Upgrade version
function _initWelcomeDisplay() {
  var upgradeDiv = document.getElementById("section0-upgrade");
  var installDiv = document.getElementById("section0-install");
  if (document.location.search.match(/welcome=upgrade/)) {
    upgradeDiv.setAttribute("id", "section0");
    upgradeDiv.className = "section";
    installDiv.parentNode.removeChild(installDiv);
  } else {
    installDiv.setAttribute("id", "section0");
    installDiv.className = "section";
    upgradeDiv.parentNode.removeChild(upgradeDiv);
  }
}

function _initCSS(aCssPath) {
  var styles = "@import url('" + aCssPath + "');";
  var newSS = document.createElement("link");
  newSS.setAttribute("rel", "stylesheet");
  newSS.setAttribute("type", "text/css");
  newSS.setAttribute("href", aCssPath);
  document.getElementsByTagName("head")[0].appendChild(newSS);
}

function old_gs_inst(item){
	try{
		inst_path += '/' + item;
		//If item equals inst_nav_click, it's a top nav click, so we need previous nav in the tracker
		if(item == inst_nav_click){
			var nav = '/getting-started/nav/' + inst_prev_nav_click + '/click' + item;
		}
		//Else this is a link down inside the tab content, so we need the current tab.
		else{
			var nav = '/getting-started/nav/' + inst_nav_click + '/click' + item;
		}
		var path = '/getting-started/path/' + inst_path;

		urchinTracker(nav);
		urchinTracker(path);

		if(document.location.search.match(/\?test/)){
			str = nav;
			str += '\n';
			str += path;
			alert(str);
		}
	}
	catch(e){ 
		//alert(e); 
	}
}

window.onload = function() {
  _initWelcomeDisplay();
  _initUniqueOS();
  _initFeatures();
  if (gIeVersion == 6) {
    setNavigationState(CSS_CLASS_CURRENT,
                       document.getElementById("congratulations"));
  }
}
