﻿// Store global variable for Products listing
var filterValues;
var productListings;
var productHeader;
var serverHeader;
var rawProducts;
var cleanProducts = [];
var currFilters = "";
var rawFilterInfo;
var cleanFilterInfo = [];
var currProduct;

function initializeVars() {
  filterValues = document.getElementById('FilterValues').getElementsByTagName('li');
  productListings = document.getElementById('ProductMenu').getElementsByTagName('li');
  productHeader = document.getElementById('ProductPrefix');
  serverHeader = document.getElementById('ServerPrefix');
  rawProducts = document.getElementById('ProductContent').getElementsByTagName('div');
  rawFilterInfo = document.getElementById('FilterInfoContainer').getElementsByTagName('div');
  
  for (x = 0; x < rawProducts.length; x++) {
    if (rawProducts[x].className == "product") {
      cleanProducts.push(rawProducts[x]);
      rawProducts[x].style.display = (x == 0) ? "block" : "none";
    }
  }

  for (x = 0; x < productListings.length; x++) {
    productListings[x].firstChild.removeAttribute('href');
  }
  document.getElementById('ProductFilters').style.display = "block";
  
  for (x = 0; x < rawFilterInfo.length; x++) {
    if (rawFilterInfo[x].className == "rhnLinks") {
      cleanFilterInfo.push(rawFilterInfo[x]);
    }
  }

  qs = location.href;
  if (qs.indexOf('?') != -1) {
    qs = qs.substring((qs.indexOf('?') + 6), qs.length) + "-Link";
    showItem(document.getElementById(qs));
  }
}

// Function to show/hide Producs based on Meta Value passed (val)
function filterResults(element) {

  // Turn Below code off for multi select
  for (x = 0; x < filterValues.length; x++) {
    filterValues[x].className = "off";
  }

  // Set On/Off state of filter clicked
  element.className = (element.className == "off") ? "on" : "off";
  if (document.all) {
    productHeader.innerText = element.firstChild.innerText + " ";
    serverHeader.innerText = element.firstChild.innerText + " ";
  } else {
    productHeader.textContent = element.firstChild.textContent + " ";
    serverHeader.textContent = element.firstChild.textContent + " ";
  }
  
  // Turn Below Line on for multi select
  //filterValues[0].className = "off";

  // Hide all Products initially
  for (x = 0; x < productListings.length; x++) {
    productListings[x].className = "off";
    productListings[x].style.display = "none";
  }

  // Check to see what filters are currently selected
  for (i = 0; i < filterValues.length; i++) {
    // Show product if filter's classname is = on
    if (filterValues[i].className == "on") {
      showProduct(filterValues[i].getAttribute('meta'));
    }
  }

  // Set visibility of right rail content
  showFilterInfo(element.getAttribute('meta'));

  showTopProduct();
}

// Manage stored filter values
function showProduct(val) {
  // Loop through rows in list and compare meta data with filter value clicked
  // and Show/Hide.
  for (x = 0; x < productListings.length; x++) {
    // Get Services available to each entry
    currFilters = productListings[x].getAttribute('meta');

    // Show or hide the menu option
    if (currFilters.indexOf(val) != -1) {
      productListings[x].style.display = "block";
    }

    // Reset currFilters for the next row
    currFilters = "";
  }
}

function clearFilters() {
  // Set all filters to off state
  for (x = 0; x < filterValues.length; x++) {
    filterValues[x].className = "off";
  }

  // Set all rows to show
  for (x = 0; x < productListings.length; x++) {
    productListings[x].style.display = "block";
  }

  filterValues[0].className = "on";
  if (document.all) {
    productHeader.innerText = "All ";
    serverHeader.innerText = "All ";
  } else {
    productHeader.textContent = "All ";
    serverHeader.textContent = "All ";
  }
  showFilterInfo("All");

  showTopProduct();
}

function showItem(element) {
  // Get current product description to show
  dispProduct = document.getElementById(element.id.substring(0, (element.id.length - 5)));
  currProduct = element.id.substring(0, (element.id.length - 5));
  buildMailString();
  
  // Hide all product listings
  hideProducts();
  
  // Set all menu items to off
  for (y = 0; y < productListings.length; y++) {
    productListings[y].className = "off";
  }
  // Set On state of menu item clicked
  element.className = "on";

  // Set visibility of product details to block
  dispProduct.style.display = "block";
}

function showFilterInfo(activeFilter) {
  // Hide all info boxes
  for (x = 0; x < cleanFilterInfo.length; x++) {
    cleanFilterInfo[x].style.display = "none";
  }

  document.getElementById(activeFilter).style.display = "block";
}

function hideProducts() {
  for (x = 0; x < cleanProducts.length; x++) {
    cleanProducts[x].style.display = "none";
  }
}

function showTopProduct() {
  // Set visibility for first item in current list of products
  for (x = 0; x < productListings.length; x++) {
    if (productListings[x].style.display == "block") {
      productListings[x].className = "on";
      showItem(productListings[x]);
      return;
    }
  }
}

function showImg(imgName, caption) {
  var imgURL = "/officebusiness/images/screens/large/" + imgName;
  var imgObj = new Image();
  imgObj.src = imgURL;
  
  var imgCont = document.getElementById('ImgContainer');
  var imgWrap = document.getElementById('ImgWrapper');
  var imgID = document.getElementById('FullScreenshot');
  var imgCaption = document.getElementById(caption);
  var imgCaptionContainer = document.getElementById('ImgCaption');
  
  var ieScroll = document.documentElement.scrollTop;
  var fxScroll = window.pageYOffset;
  var sfScroll = window.pageYOffset;
  var position = 0;

  if (navigator.appName == "Microsoft Internet Explorer") {
      imgCont.style.top = "0px";
      imgWrap.style.top = ieScroll + "px";
  }
  else if (navigator.appName == "Mozilla Firefox") {
      imgCont.style.top = "0px";
      imgWrap.style.top = fxScroll + "px";
  }
  else 
  {
      imgCont.style.top = "0px";
      imgWrap.style.top = sfScroll + "px";
  }

  imgCont.style.height = getDocHeight() + "px";
  imgCont.style.display = "block";
  imgWrap.style.display = "block";

  imgCaptionContainer.innerHTML = imgCaption.innerHTML;
  imgID.src = imgObj.src;
}
function getDocHeight() {
  var D = document;
  return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
function closeDIV() {
    document.getElementById('ImgContainer').style.display = "none";
    document.getElementById('ImgWrapper').style.display = "none";
    document.getElementById('ImgCaption').innerHTML = "";
    document.getElementById('FullScreenshot').src = "/officebusiness/images/spacer.gif";
}



//
// BING WEB SEARCH
//

$(document).ready(function() {


    $('.bingWeb').click(
        function() {
            var tbValue = $('.searchTextBox').val();
            //alert(tbValue);

            window.location.href = "http://www.bing.com/search?q=" + tbValue;
        }

    );

    });

    function changeVal() {
        var txtBox = document.getElementById('txtBox');
        if (txtBox.value == "Search Microsoft.com") {
            txtBox.value = "";
            txtBox.style.fontStyle = "normal";
        }
    };
    
    function revertVal() {
        var txtBox = document.getElementById('txtBox');
        if (txtBox.value == "") {
            txtBox.value = "Search Microsoft.com";
            txtBox.style.fontStyle = "italic";
        }
      
    };