$(function(){
	$('#product_rr div:last').addClass('last');
	$("#proddetinfobox tr:nth-child(odd)").addClass("odd");
	});

// Dimension Select
var dimensionsSelect = function(lmnt, stage, totalStages, prodRef, offerCode) {
  //alert("dimensionsSelect");
  // dimension selected

  dimSelection = lmnt.value;
//  alert('dimSelection = ' + dimSelection + "\nstage=" + stage + "\ntotalStages=" + totalStages + "\nprodRef=" + prodRef + "\nofferCode=" + offerCode);

  if (dimSelection != '') {
    // a valid dimension selected
    if (stage < (totalStages - 1)) {
      // start with the common parms
      var parms = "&dim=" + (stage + 1) + "&refid=" + prodRef;
      // add any label text parms
      for (var i = 0; i <= stage; i++) {
        var labeltext = $("#labeldimension" + i + prodRef).text();
        if (labeltext != null && labeltext != '' && labeltext != undefined) {
          parms += "&dim" + i + "=" + labeltext;
        }
      }
      // add any select value parms
      for (var i = 0; i <= stage; i++) {
        var selecttext = $("#selectdimension" + i + prodRef).val();
        if (selecttext != null && selecttext != '' && selecttext != undefined) {
          parms += "&dim" + i + "=" + selecttext;
        }
      }
      if (offerCode != null && offerCode != '') {
        parms += "&offerCode=" + offerCode;
      }
      //$("#proddimension" + (stage + 1)).html('<div class="loading"></div>' + $("#proddimension" + (stage + 1)).html());
      $('#dimensionsloadimg' + prodRef).css('display','inline');
      $("#proddimension" + (stage + 1) + prodRef).load("/za/HNT/product/includes/selection-dimensions.jsp", parms, function(){
//        alert('here');
        $('#dimensionsloadimg' + prodRef).css('display','none');

//        alert('stage='+stage);
        var nextSel = document.getElementById("selectdimension"+(stage+1) + prodRef);
        if (nextSel != null) {
  //        alert('nextSel='+nextSel);
          var whatToDo = String(nextSel.getAttribute("onchange"));
  //        alert('whatToDo='+whatToDo);
          var one = whatToDo.split(",")[1];
  //        alert('one='+one);
          var two = whatToDo.split(",")[2];
  //        alert('two='+two);
  //        alert('prodRef='+prodRef);

          dimensionsSelect(nextSel,one,two,prodRef,offerCode);
        }
        
      });



    } 
    else {
      var skuRefId = lmnt.options[lmnt.selectedIndex].getAttribute("skuref");
      updateSkuInfo(prodRef,skuRefId,offerCode, null);

    }
  }
}


var updateSkuInfo = function(prodRef, skuRef, offerCode, singlesku) {
  
//  alert('updateSkuInfo = ' + "\nprodRef=" + prodRef + "\nskuRef=" + skuRef + "\nofferCode=" + offerCode + "\nsinglesku=" + singlesku);

  var refId = skuRef;
  //alert("refId:"+refId);
  var pimSrc = document.getElementById('proddetprodimgfile' + prodRef).src;
  var imgDir = "/";
  if (pimSrc.indexOf("quick") > 0 ) {
    imgDir = "/images/product/assets/quick";
  }
  else {
    imgDir = "/images/product/assets/detail";
  }


  document.getElementById('proddetprodimgfile' + prodRef).src = imgDir+"/" + refId.toLowerCase() + ".jpg";

  //show inventory
  var qoInventoryMsg = $("#proddimensions" + prodRef + " select:last option:selected").attr("inventory_message");
  if(qoInventoryMsg) {
    $('#availdate' + prodRef).html('<div class="msg">' + $("#proddimensions" + prodRef + " select:last option:selected").attr("inventory_message") + '</div>');
  }
  else {
    $('#availdate' + prodRef).empty();
  }
  //show price
  var price = $("#proddimensions" + prodRef + " select:last option:selected").attr("price");
  var price2 = $("#proddimensions" + prodRef + " select:last option:selected").attr("price2");
  var priceformat = $("#proddimensions" + prodRef + " select:last option:selected").attr("priceformat");


  if (singlesku != null && singlesku == 'true') {
    price = $("#singleskuinfo" + prodRef).attr("price");
    price2 = $("#singleskuinfo" + prodRef).attr("price2");
    priceformat = $("#singleskuinfo" + prodRef).attr("priceformat");
    qoInventoryMsg = $("#singleskuinfo" + prodRef).attr("inventoryMessage");
    if (qoInventoryMsg) {
      $('#availdate' + prodRef).html('<div class="msg">' + qoInventoryMsg + '</div>');
    }
  }

//  alert('prices = ' + "\nprice=" + price + "\nprice2=" + price2 + "\npriceformat=" + priceformat);

  if (priceformat != null && priceformat != '') {
    if (priceformat == 'special') {
      $('#price' + prodRef).html('<del>' + price + '</del>' + ' <span>' + price2 + '</span>' );
    } else if (priceformat == 'discount') {
      $('#price' + prodRef).html(price + ' <span>' + price2 + '</span>' );
    }
  } else {
    $('#price' + prodRef).html(price);
  }

}

var allDimensionsSelected = function(prodRefId) {
  //alert("allDimensionsSelected");
  var status = true;
  $("#proddimensions" + prodRefId + " select").each(function(i) {
//     alert(i + ":" + this.value);
    if (this.value == '') {
       alert("error at " + i)
      status = false;

      // trigger .each exit
      return false;
    }
  });

  return status;
}
var addtocart = function(prodRefId) {

  var outofstockmsg = "We're sorry this item is currently out of stock. Please check back for availability.";

  if (prodRefId == null || prodRefId == '') {
    alert('addtocart: no prodRefId passed in');
    return false;
  }

//  alert('addtocart: prodRefId = '+prodRefId);

  //check if we have a single label sku as the last selection
  var skuidfromsingleskulabel = $("#singleskulabel" + prodRefId).text();
  if (skuidfromsingleskulabel != null && skuidfromsingleskulabel != '') {
//    alert('singleskulabel = ' + singleskulabel);
    var invtemp = $("#singleskulabel" + prodRefId).attr("inventory_status");
//    alert('invtemp = '+ invtemp);
    if ($("#singleskulabel" + prodRefId).attr("inventory_status") == "OUTOFSTOCK") {
      alert(outofstockmsg);
      return false;
    }
    $('#skuid' + prodRefId).val(skuidfromsingleskulabel);
    return true;
  } else if ($("#proddimensions" + prodRefId + " select:last").length == 0) {//check if we have no picklists at all.. (if so, the skuid in the form is taken care of by the jsp)
    // no selects
//    alert('no selects');
    if ($('#skuid' + prodRefId).attr("inventory_status") == "OUTOFSTOCK") {
      alert(outofstockmsg);
      return false;
    }
    return true;
  } else {
    var skuid = $("#proddimensions" + prodRefId + " select:last option:selected").val();
//    alert('addtocart(): skuid = '+skuid);

    if (skuid == '') {
      // first option (nothing) selected
      alert('Please make your selections');
      return false;
    } else {
      // at the last dimension
      if (allDimensionsSelected(prodRefId)) {
        // all dimensions selected

        if ($("#proddimensions" + prodRefId + " select:last option:selected").attr("inventory_status") == "OUTOFSTOCK") {
          alert(outofstockmsg);
          return false;
        }

        $('#skuid' + prodRefId).val(skuid);
        return true;
      }
      else {
        alert("Please make your selections");
      }
    }
  }

  return false;
}

/**************************************
*                                      *
*      Category Page JS                *
*                                      *
**************************************/

var viewAllRecent = function(){ 
  recText = $('#recentviewall').html();
  recText = recText.replace(/View All/,'View Less');
  recHeight = $('#categoryrecent').css('height');
  if(recHeight == '155px'){  
    $('#categoryrecent').css('height','auto');
    recText = recText.replace(/View All/,'View Less');
    $('#recentviewall').html(recText);
  }
  else { 
    $('#categoryrecent').css('height','155px');
    recText = recText.replace(/View Less/,'View All');
    $('#recentviewall').html(recText);
  }
}


var showAllTestimonial = function() {
  $('#testimtext').html($('#testimtextfull').html());	
}
var showTruncTestimonial = function() {
  $('#testimtext').html($('#testimtexttrunc').html());	
}