/**
 * Ampdraw - functions_product.js
 *
 * Javascript functions to support the product view
 * processes.
 *
 * @author     Shane Archer <futureal@rctech.net>
 * @copyright  2005-2007 RC Tech, LLC
 * @version    svn: $Revision:$ $Date:$
 * @package    AmpdrawPublic
 */

/** @var boolean Make sure we never try to open two images at once (collision control). */
var requestActive = false;

/** @var string The request info. */
var requestInfo = null;

/**
 * Swap the featured product image with a different one.
 */
function swapFeaturedImage(manufacturer,partnumber,index) {

  var js_featuredContainer = document.getElementById("featuredContainer");

  var js_newImage = document.createElement("img");
  js_newImage.src = "/photos/" + manufacturer + "/" + partnumber + "-" + index + "-200.jpg";
  js_newImage.setAttribute("border","0");
  js_newImage.setAttribute("width","200");
  js_newImage.setAttribute("height","200");
  js_newImage.setAttribute("alt","Featured Photo");

  // clear the existing children
  if (js_featuredContainer.hasChildNodes()) {

    // keep removing the first child until there are not
    while (js_featuredContainer.firstChild) {
      js_featuredContainer.removeChild(js_featuredContainer.firstChild);
    }

  }

  // add the new node
  js_featuredContainer.appendChild(js_newImage);

}

/**
 * Begin performing a "zoomed" photo display.
 */
function imageZoom(manufacturer,partnumber,index) {

  // if a request is active, abort
  if (requestActive) {
    return;
  }

  // close any remaining just in case
  if (imageOpen) {
    closeImageDiv();
  }

  var clientWidth = 0, clientHeight = 0;

  // figure out width and height
  if (typeof(window.innerWidth) == "number") {

    // non-IE
    clientWidth = window.innerWidth;
    clientHeight = window.innerHeight;

  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {

    // IE 6+ in 'standards compliant mode"
    clientWidth = document.documentElement.clientWidth;
    clientHeight = document.documentElement.clientHeight;

  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {

    // IE 4 compatible
    clientWidth = document.body.clientWidth;
    clientHeight = document.body.clientHeight;

  }

  var imageHeight = 0, imageWidth = 0;

  // determine how big of an image we want
  if (clientWidth > 1280 && clientHeight > 1024) {
    imageWidth = 1280;
    imageHeight = 1024;
  } else if (clientWidth > 1024 && clientHeight > 768) {
    imageWidth = 1024;
    imageHeight = 768;
  } else if (clientWidth > 800 && clientHeight > 600) {
    imageWidth = 800;
    imageHeight = 600;
  } else {
    imageWidth = 640;
    imageHeight = 480;
  }

  // set the callback info
  requestInfo = new Array(clientWidth,clientHeight,imageWidth,imageHeight,manufacturer,partnumber,index);
  requestActive = true;

  // get the real values
  checkDimensions(manufacturer,partnumber,index);

}

/**
 * Close an existing image DIV in the DOM.
 */
function closeImageDiv() {

  var js_imageDiv = document.getElementById("imageDiv");
  document.body.removeChild(js_imageDiv);
  imageOpen = false;

}

/**
 * Make the actually AJAX call for the photo dimensions.
 */
function checkDimensions() {

  newAjaxCall();
  http_request.onreadystatechange = handleImageDimensions;

  // note that asset does not use the framework, so we do not need session data
  http_request.open("GET", "/asset.php?action=dimensions&manufacturer=" + requestInfo[4] + "&partnumber=" + requestInfo[5] + "&screen=" + requestInfo[6] + "&width=" + requestInfo[2] + "&height=" + requestInfo[3], true);
  http_request.send(null);

}

/**
 * The response handler, meaning we have received image dimensions and
 * can proceed with the display.
 */
function handleImageDimensions() {

  // only do something if the request is complete
  if (http_request.readyState == 4) {

    // check for HTTP status code 200
    if (http_request.status == 200) {

      if (http_request.responseText == "error") {

        alert("Error: Could not complete request.");
        requestActive = false;

      } else {

        // get the real dimensions
        var realDims = http_request.responseText.split(",");
        var realWidth = realDims[0];
        var realHeight = realDims[1];

        // see how far we've scrolled
        var scrollOffset = getScrollXY();

        // figure out the correct offset
        var offsetWidth = (requestInfo[0] - realWidth) / 2;
        var offsetHeight = ((requestInfo[1] - realHeight) / 2 + scrollOffset[1]);

        // accessing style object directly due to IE not liking setAttribute for class/style changes
        var js_imageDiv = document.createElement("div");
        js_imageDiv.style.zIndex = 10;
        js_imageDiv.style.position = "absolute";
        js_imageDiv.style.top = (Number(offsetHeight)-2) + "px";
        js_imageDiv.style.left = (Number(offsetWidth)-2) + "px";
        js_imageDiv.style.backgroundColor = "gray";
        js_imageDiv.style.border = "0";
        js_imageDiv.style.width = (Number(realWidth)+4) + "px";
        js_imageDiv.style.height = (Number(realHeight)+24) + "px";
        js_imageDiv.setAttribute("id","imageDiv");

        var js_containerDiv = document.createElement("div");
        js_containerDiv.style.position = "relative";
        js_containerDiv.style.top = "2px";
        js_containerDiv.style.left = "2px";
        js_containerDiv.style.backgroundColor = "black";
        js_containerDiv.style.backgroundImage = "url('/graphics/loadingbackground.gif')";
        js_containerDiv.style.backgroundPosition = "center";
        js_containerDiv.style.backgroundRepeat = "no-repeat";
        js_containerDiv.style.border = "0";
        js_containerDiv.style.width = realWidth + "px";
        js_containerDiv.style.height = (Number(realHeight)+20) + "px";

        var js_imageZoom = document.createElement("img");
        js_imageZoom.setAttribute("src","/asset.php?manufacturer=" + requestInfo[4] + "&partnumber=" + requestInfo[5] + "&screen=" + requestInfo[6] + "&width=" + requestInfo[2] + "&height=" + requestInfo[3]);
        js_imageZoom.setAttribute("id","imageZoom");
        js_imageZoom.setAttribute("border","0");
        js_imageZoom.setAttribute("alt","");

        var js_closeDiv = document.createElement("div");
        js_closeDiv.style.fontFamily = "verdana, sans-serif";
        js_closeDiv.style.fontWeight = "bold";
        js_closeDiv.style.fontSize = "10px";
        js_closeDiv.style.color = "white";
        js_closeDiv.style.width = realWidth + "px";
        js_closeDiv.style.position = "absolute";
        js_closeDiv.style.top = (Number(realHeight)+4) + "px";
        js_closeDiv.style.textAlign = "center";
        js_closeDiv.innerHTML = "<a class=\"zoomlink\" href=\"javascript:closeImageDiv();\">Close</a>";

        // add the image and close bars
        js_containerDiv.appendChild(js_imageZoom);
        js_containerDiv.appendChild(js_closeDiv);

        // add the interior to the gray border
        js_imageDiv.appendChild(js_containerDiv);

        // add it to the document
        document.body.appendChild(js_imageDiv);

        imageOpen = true;
        requestActive = false;

      }

    }

  }

}

/**
 * Make the actual call to add a user favorite.
 */
function addUserFavorite(productid) {

  newAjaxCall();
  http_request.onreadystatechange = handleAddUserFavorite;

  // check to see if we need to include session in call
  if (sId != "") {
    var sessionString = "&s=" + sId;
  } else {
    var sessionString = "";
  }

  http_request.open("GET", "/account.php?amp_action=do_ajax_addfavorite&productid=" + productid + sessionString, true);
  http_request.send(null);

  return false;

}

/**
 * The response handler for adding a favorite.
 */
function handleAddUserFavorite() {

  // only do something if the request is complete
  if (http_request.readyState == 4) {

    // check for HTTP status code 200
    if (http_request.status == 200) {

      var responseText = http_request.responseText.split("|");

      if (responseText[0] == "ok") {

        var pId = responseText[1];
        var newText = "You have marked this as a favorite, and will be notified of new stock and pricing changes.";

        var js_flc = document.getElementById("favoriteLinkContainer");
        var js_flc_id = document.getElementById("favoriteLinkContainer_" + pId);
        var js_flc_b = document.getElementById("favoriteLinkContainerBelow");

        if (js_flc) {

          var js_newtext = document.createTextNode(newText);

          js_flc.style.fontStyle = "italic";
          js_flc.replaceChild(js_newtext,js_flc.firstChild);

        }

        if (js_flc_id) {

          var js_newtext_id = document.createTextNode(newText);

          js_flc_id.style.fontStyle = "italic";
          js_flc_id.replaceChild(js_newtext_id,js_flc_id.firstChild);
        }

        if (js_flc_b) {

          var js_newtext_b = document.createTextNode(newText);

          js_flc_b.style.fontStyle = "italic";
          js_flc_b.replaceChild(js_newtext_b,js_flc_b.firstChild);

        }

      } else {

        alert("Error: " + responseText[1]);
        requestActive = false;

      }

    }

  }

}
