// Jquery slider-gallery code.
$(window).ready(function () {

  $('.sliderItems').each(function() {
      bindImageChangeEvent(this.id);
  });

  $(".carousel").jCarouselLite({
      btnNext: ".next",
      btnPrev: ".prev",
      circular: false,
      visible: 6
  });

  $(".external").click(function () {

      // If we don't have the manufacture id then we don't do anything.
      if (manufacturerId == '') {
          return true;
      }

      // Send an jax request to log the click.
      $.ajax({
          type  : "POST",
          url   : statslogUrl,
          data  : 'data[ExternalLinkStatistic][link]=' + this.href + '&data[ExternalLinkStatistic][model_id]=' + manufacturerId + '&data[ExternalLinkStatistic][model_name]=Manufacturer',
          async : false
      });

      return true;
  });

});

// Function to attach the image change event.
function bindImageChangeEvent(productId)
{
    // If the user clicks on a product image in the carousel ..
    $('#' + productId).click(function()
    {
        // Get the product id
        var prodId = productId.replace('product_','');

        // Change the main image source so it appears
        $('#mainImage').attr('src', $('#'+ productId).attr('href'));

        // Display the content in the div from at alt attrib
        $('#manufactureprofile_pic_short_desc').html($('#image_'+ prodId).attr('alt'));

        // If the hidden field has no valye i.e for details link simply hide it.
        if ($('#productLink_'+ prodId).val() == '') {
            $('#productLink').hide();
        } else {
            // Get the link url.
            var linkUrl = $('#productLink_'+ prodId).val();

            // Set the href attrib
            $('#productLink').attr('href', linkUrl);

            // It can be hidden due to last click show it anyway
            $('#productLink').show();

            // Since we change the url of the 'Details>>' link we need to set / unset the attributes
            var extra = $('#productLink_'+ prodId).attr('extra');

            // If its a url remove the chickbox class
            if (extra == 'url') {
                $('#productLink').unbind('click');
                $('#productLink').attr('target', '_blank');
            } else if (extra == 'thickbox') {
                // populate the long description if any
                var longDesc = $('#productLink_'+ prodId).attr('longdesc');
                $('#product_long_desc').html('<p>' + longDesc + '</p>');

                // Its the long description
                $('#productLink').removeAttr('target');
                tb_init('#productLink');
            } else {
                // Its the pdf do not have any attributes
                $('#productLink').removeAttr('target');
                $('#productLink').unbind('click');
            }
        }

        $.get(baseUrl + 'products/log_access/' + prodId, function(){});

        return false;
    });
}