(function($){
//Config Options Variable QV_config
var QV_config = {  
  previewImagePath: "/v/preview.png",
  spinnerImagePath: "/v/spinner.gif",  
  site: "http://" + window.location.host + "/",
  lastlocation: "",
  buttonXOffset: 34,
  buttonYOffset: 120,
  modalWidth: 680,
  modalHeight: 500,
  hideRelatedProducts: true,
  hideProductHistory: true,
  enableSoftAddToCart: true
}

if(QV_config.enableSoftAddToCart){
  //All these variables and functions are included for soft add to cart functionality
  var global_Config_StoreFolderName = '/';
  var global_Config_ProductPhotosFolder = 'v/vspfiles/photos';
  var global_Current_ProductCode = '01';
  var global_SEOImage = '/PhotoDetails.asp';
  var global_URL_Encode_Current_ProductCode = '01';
  var global_src_part_Config_ProductPhotosFolder = 'v/vspfiles/photos';
  var global_ImageSeed = 'test.jpg';
  global_ImageSeed = global_ImageSeed.substring(global_ImageSeed.indexOf('?')).replace("test.jpg","");
  var global_Config_EnablePhotosPopup = false;
  var global_SEOImage_PhotoGallery = '/PhotoGallery.asp';
  //module variable set from Qty: text box {ENTER} and read from <form> onsubmit event
  var IESuppressFormSubmit = false;
  function QtyEnabledAddToCart_SuppressFormIE() {
    //Only target Internet Explorer
    if (/Internet Explorer/gi.test(navigator.appName)) { return (!IESuppressFormSubmit); }
     return true;
  }
  //Enter Key const
  var ENTER_KEY = 13;
  //determine if the {ENTER} key was pressed from the Qty: text box
  function QtyEnabledAddToCart(e) {
    //Only target Internet Explorer
    if (/Internet Explorer/gi.test(navigator.appName) == false) { return; }
    var evt = (e) ? e : (window.event) ? window.event : null;
    if (!evt) { return; }
    var keycode = evt.keyCode ? evt.keyCode : evt.charCode;
    if (keycode != ENTER_KEY) { return; }
    var el = document.getElementById('btnaddtocart');
    if (!el) { return; }
    //flag key press
    IESuppressFormSubmit = true;
    //suppress immediate form submits and then click "AddToCart"
    window.setTimeout(QtyEnabledAddToCartManualClick, 250);
  }
  function QtyEnabledAddToCartManualClick() {
    IESuppressFormSubmit = false;
    document.getElementById('btnaddtocart').click();
  }
  $.getScript("/a/j/product_details.js");
}


//Start Quickview Functions
getProduct = function(d){
  var url = QV_config.site +'productdetails.asp?productcode=' + d;
  $('.spinner').show();
  $('#modal_wrap').dialog({
    autoOpen:false,
    width:QV_config.modalWidth,
    height:QV_config.modalHeight,
    modal:true,
    close: function(){
      $(unicorns).trigger('mouseleave');
    }
  });

  $('#quick_view_div').load(url + ' #vCSS_mainform', function(){
    var prodName = $('.productnamecolorLARGE').html();
    var prodNameHTML = '<font class="productnamecolorLARGE colors_productname">' + prodName + '</font>';
    $('.vCSS_breadcrumb_td').hide();
    $('.vCSS_breadcrumb_td').replaceWith(prodNameHTML);
    if(QV_config.hideRelatedProducts){
      $('#v65-product-related').hide();
    }
    if(QV_config.hideProductHistory){
      $('#v65-product-history-header').hide();
      $('#v65-product-history-body').hide();
    }
    $('#product_photo').removeAttr('onload');
    $('#product_photo_zoom_url').attr('href', 'javascript:void(0)').css('cursor', 'default');
    $('#product_photo_zoom_url2').hide();
    $('.spinner').hide();
    $('.addthis_toolbox').hide();
    $('#modal_wrap').dialog('open');

    $('.vCSS_input_addtocart').live('click', function(){
      $('#modal_wrap').dialog('close');
    });
    return false;
  });
  return false;
}

function findPosX(a) {
  var b = 0;
  if (a.offsetParent) while (1) {
    b += a.offsetLeft;
    if (!a.offsetParent) break;
    a = a.offsetParent
  } else if (a.x) b += a.x;
  return b
}

function findPosY(a) {
  var b = $(a).offset().top;
  return b;
}

keepVisible = function(a) {
  $('#' + a).show();
}

function removeButton(a) {
  $('#' + a).hide();
}

function drawScreen() {
if($('.v65-productDisplay')){
    unicorns = $('.v65-productDisplay .v65-productPhoto a img');
  }
  else{
    unicorns = $('#MainForm a img');
  }
  var b = QV_config.buttonXOffset;
  var c = QV_config.buttonYOffset;
  buttons = '';
  for (x = 0; x < unicorns.length; x++) {
  var prodPath = $(unicorns[x]).parent().attr('href');
                if(prodPath){
                  var nonLink = prodPath.indexOf('ShoppingCart');
                  if (nonLink < 0){
                    var lastIndexNumber = prodPath.length;
                    var indexOfLastSlash = prodPath.lastIndexOf('/') + 1;
                    var prodCode = prodPath.slice(indexOfLastSlash, lastIndexNumber);
                    var d = prodCode.replace('.htm', '');
                    var e = findPosX(unicorns[x]) + Math.floor((unicorns[x].width - c) / 2);
                    var f = findPosY(unicorns[x]) + unicorns[x].height - b - 5;
      buttons += '<div id="' + d + '" style="z-index:1000;position:absolute;top:' + f + 'px;left:' + e + 'px;display:none;"><div class="quickViewBtn" onclick="getProduct(\'' + d + '\')"><img src="' + QV_config.previewImagePath + '" border="0" style="cursor:pointer" onmouseover="keepVisible(\'' + d + '\')" /></div></div>';
     // var button =  '<div id="' + d + '" style="z-index:5000;position:absolute;top:' + f + 'px;left:' + e + 'px;display:none;"><div class="quickViewBtn" onclick="getProduct(\'' + d + '\')"><img src="' + QV_config.previewImagePath + '" border="0" style="cursor:pointer" onmouseover="keepVisible(\'' + d + '\')" /></div></div>';
     //$(unicorns[x]).parent().append(button);
      unicorns[x].onmouseover = function () {
        if(QV_config.lastlocation!=""){
          $('#' + QV_config.lastlocation).hide();
        }
                  var prodPath = $(this).parent().attr('href');
                  if(prodPath){
                    var lastIndexNumber = prodPath.length;
                    var indexOfLastSlash = prodPath.lastIndexOf('/') + 1;
                    var prodCode = prodPath.slice(indexOfLastSlash, lastIndexNumber);
                    var d = prodCode.replace('.htm', '');
                    $('#' + d).show();
                    QV_config.lastlocation=d;
                  }
      }
    }
}

    $(unicorns[x]).mouseleave(function () {
var prodPath = $(this).parent().attr('href');
                    if(prodPath){
                      var lastIndexNumber = prodPath.length;
                      var indexOfLastSlash = prodPath.lastIndexOf('/') + 1;
                      var prodCode = prodPath.slice(indexOfLastSlash, lastIndexNumber);
                      var d = prodCode.replace('.htm', '');
                      $('#' + d).hide();
                      QV_config.lastlocation=d;
                    }
      $('#' + d).hide();
    });
  }

$('#holder').html(buttons);
}

$(document).ready(function(){
  var htmlChunk = '<div class="spinner" style="display:none"></div><div id="modal_wrap" style="display:none"><div id="quick_view_div"></div></div><div id="holder"></div>';
  $('body').append(htmlChunk);

    $('.spinner').css({
     'width':'32px',
      'height':'32px',
      'position':'absolute',
      'top':'50%',
      'left':'50%',
      'background':'url(' + QV_config.spinnerImagePath + ') center center no-repeat'    
    });
});

$(window).load(function(){
    drawScreen();
    $(window).resize(function(){
      drawScreen();
    });
});
})(jQuery);
