// Return new array with duplicate values removed
Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

$(document).ready(function(){
  // If no reviews, display appropriate message:
 // if ( !$.trim($("#productReviews").text()) ) {
   // $("#productReviews").html('<p>Submit a review! </p>');
  //}
  
  $("#addToCartForm").submit(function() {
    if ( $("#optionCount1").val() == '' ) {
      return false;
    }
    return true;
  });

  // If JSON pricing data (with variations), set up handling
  if ( priceJson ) {
    // Add any different additional images to clickable thumbnails if specified in the Json
    var images = [];
    $.each(priceJson, function(i, val) {
      images.push(val[1]);
      // add price into each option text...
      var priceVar = val[0];
      var price = parseFloat(priceVar);
      var listpriceVar = val[7];
      var listprice = parseFloat(listpriceVar);
      $("#optionCount1 option[value='"+i+"']").append(' : $' + price.toFixed(2) );
    });
    images = images.unique();
    $.each(images, function(i, val) {
      if ( !$("#additionalImages img.imageThumbnail[src='http://store.aidanceproducts.com/"+val+"']").get(0) ) {
        $("img.imageThumbnail:last").after('<img src="http://store.aidanceproducts.com/'+val+'" width="43" height="58" class="imageThumbnail" alt="" />');
      }
    });
    updateProductOptionJson();
    $("#productOptions select").val('');
    
    $("#productOptions select").change(function() {
      updateProductOptionJson();
    });
  }
});

var guids = [];
function updateProductOptionJson() {
  if ( !guids.length ) {
    // Get the order of guids that the API built; doesn't necessarily match the order
    // of product option choices being rendered in HTML
    var keyStr = '';
    $.each(priceJson, function(i, val) {
      keyStr = i;
      return false;
    });
    if ( keyStr ) {
      var guiAry = keyStr.split(',');
      var id;
      if ( $("#productOptions").children().length ) {
        $.each(guiAry, function(i, val) {
          id = $("option[value='" + val + "']").parent().attr('name');
          guids.push( id.replace('optionCount', '') );
        });
      } else {
        guids.push(guiAry[0]);
      }
    }
  }
  // based on the order of guids, build our hash
  var hash = '';
  
  if ( $("#productOptions").children().length ) {
    $.each(guids, function(i, val) {
      if ( hash ) {
        hash += ',';
      }
      hash += $("select[name='optionCount" + val + "']").val();
    });
  } else {
    hash = guids[0];
  }
  
  if ( priceJson[hash] ) {
    var priceVar = priceJson[hash][0];
    var price = parseFloat(priceVar);
    var listpriceVar = priceJson[hash][7];
    var listprice = parseFloat(listpriceVar);
    if ( listprice > price ) {
      if ( !$("#dynamicProductListPrice").length ) {
        $(".productPrice p").wrapInner('<s/>');
        $("#dynamicProductPrice").text(listprice.toFixed(2));
        $(".productPrice p s").after('<span id="dynamicProductListPrice" style="color:#c00; margin-left:5px;">$'+price.toFixed(2)+'</span>');
      }
    } else {
      $("#dynamicProductPrice").text(price.toFixed(2));
      $("#dynamicProductListPrice").remove();
      $(".productPrice p s #dynamicProductPrice").unwrap();
    }
    $("#dynamicSku").text(priceJson[hash][2]);
    $("#productShotLarge img").attr('src', 'http://store.aidanceproducts.com/'+priceJson[hash][1]);
    if ( $("#productShotLarge img").is(":hidden") ) $("#productShotLarge img").show();
    $("#tab2 p").html(priceJson[hash][6]);
    $("#productShortDescription").html(priceJson[hash][5]);
  }
  
   $('#optionCount1').change(function() {
      
      $("#dynamicProductListPrice").remove();
      $(".productPrice p s #dynamicProductPrice").unwrap();
      
      $("#dynamicProductPrice").text(price.toFixed(2));
     updateProductOptionJson();

   });  
 // $('.productDetails .productPrice').hide();
  
 
}


if($.trim($("#productReviews").text()) != "") {
  $('#reviews a').append('<img src="yellowStar.png" class="first" /><img src="yellowStar.png" /><img src="yellowStar.png" /><img src="yellowStar.png" /><img src="yellowStar.png" />');
}

$('#writeButton').addClass('btnStyle').addClass('rounded').addClass('medWidth');




