$(document).ready(function() {
  var url = location.pathname;
  //var url = document.URL;
  //url = url.replace('http://www.capsasolutions.com', '');
  pixelsilk.getListItems('/products/', 'sort1=SortOrder,asc', 'filter1=Categories,contains,'+url+'&filterrel=filter1', 1000, 1, function(json) {
    if ( json.length ) {
      var tmp = [], cats = [];
      for ( i = 0; i < json.length; i++ ) {
        cats = json[i].Categories.split(',');
        for ( j = 0; j < cats.length; j++ ) {
          if ( cats[j] == url ) {
            tmp.push(json[i]);
          }
        }
      }
      json = tmp;
      // select 2 random products from this
      var p1 = Math.floor(Math.random() * json.length);
      if ( json.length > 1 ) {
        var p2 = Math.floor(Math.random() * json.length);
        while ( p2 == p1 ) {
          p2 = Math.floor(Math.random() * json.length);
        }
      } else {
        p2 = -1;
      }
      var html;
      html = '<div class="rollover" id="productRollover1">';
      if (json[p1].Link != '') {
        html += '<a href="'+json[p1].Link+'">';
      } else {
        html += '<a href="javascript:void(0);">';
      }
      html += '<img src="' + json[p1].Image + '" alt="' + json[p1].Title + '" />';
      html += '</a>';
      html += '<div id="productRolloverText1">';
      html += '<strong>[</strong> ' + json[p1].Title + '&nbsp;<strong>]</strong><br />';
      html += json[p1].BriefDescription;
      html += '</div>';
      html += '</div>';
      $("#productRollovers .images").append(html);
      $("#productRollover1 a").hover(
        function() {
          $("#productRolloverText1").show();
          $("#column1").height($("#column1").height() + $("#productRolloverText1").height());
        },
        function() {
          $("#column1").height($("#column1").height() - $("#productRolloverText1").height());
          $("#productRolloverText1").hide();
        }
      );
      if ( p2 > -1 ) {
        html  = '<div  class="rollover" id="productRollover2">';
        if (json[p2].Link != '') {
          html += '<a href="'+json[p2].Link+'">';
        } else {
          html += '<a href="javascript:void(0);">';
        }
        html += '<img src="' + json[p2].Image + '" alt="' + json[p2].Title + '" />';
        html += '</a>';
        html += '<div id="productRolloverText2">';
        html += '<strong>[</strong> ' + json[p2].Title + '&nbsp;<strong>]</strong><br />';
        html += json[p2].BriefDescription;
        html += '</div>';
        html += '</div>';
        $("#productRollovers .images").append(html);
        $("#productRollover2 a").hover(
          function() {
            $("#productRolloverText2").show();
            $("#column1").height($("#column1").height() + $("#productRolloverText2").height());
          },
          function() {
            $("#column1").height($("#column1").height() - $("#productRolloverText2").height());
            $("#productRolloverText2").hide();
          }
        );
      }
    }
  });
});
