/**
 * Przełącznie widoczności produktów na liście popularnych (między: widoczne 3 -
 * widoczne wszystkie, z danej kategorii)
 * 
 * @param category_id
 *          ID kategorii
 */
function toggleMoreLessProducts(category_id) {
  var _this = jQuery('#product_toggle_button_' + category_id);

  if (jQuery('.product_row_' + category_id).length >= jQuery(
      '#product_count_' + category_id).val()) {
    toggleAllProductsInCategory(category_id);
  } else {

    jQuery('#ajax_loading_products_' + category_id).show();

    jQuery.get(_this.attr('href'), function(data) {

      var page = _this.attr('href').match('&page=.+');
      if (!page) {
        _this.attr('href', _this.attr('href') + '&page=1');
      } else {
        page = _this.attr('href').match('&page=(.+)');
        if (page[1]) {
          page = parseInt(page[1]) + 1;
        }
        _this.attr('href', _this.attr('href').replace(/page=.+/,
            'page=' + page.toString()));
      }

      jQuery('#ajax_loading_products_' + category_id).hide();
      jQuery(data)
          .insertBefore(jQuery('#ajax_loading_products_' + category_id));

      if (jQuery('.product_row_' + category_id).length >= jQuery(
          '#product_count_' + category_id).val()) {
        _this.hide();
      }

      jQuery('#hide2_' + category_id).show();

      setTimeout("updateVisibleProductsCount();", 235);
    });
  }
}

/**
 * Przełączanie widoczności produktów na liście popularnych (między: widoczne, a
 * niewidoczne)
 * 
 * @param category_id
 *          ID kategorii
 */
function toggleAllProductsInCategory(category_id, event) {
  if (event == undefined) {
    event = false;
  }
  var category = jQuery('#products_in_category_' + category_id);

  if (category.length > 0) {
    var element = jQuery('#hide_' + category_id);

    if (element.length > 0) {
      if (element.hasClass('category_toggle_products_visible')) {

        element.removeClass('category_toggle_products_visible');
        element.addClass('category_toggle_products_hidden');
        element.text(jQuery('#pokaz_hidden').val());
        element.attr('title', jQuery('#pokaz_hidden').val());
        if (event) {
          /*jQuery
              .scrollTo($('#hide_' + category_id).position().top
                  - event.clientY
                  + (event.clientY
                      + (document.documentElement.scrollTop ? document.documentElement.scrollTop
                          : document.body.scrollTop) - $(
                      '#hide2_' + category_id).position().top));    */
        }

      } else if (element.hasClass('category_toggle_products_hidden')) {

        element.removeClass('category_toggle_products_hidden');
        element.addClass('category_toggle_products_visible');
        element.text(jQuery('#schowaj_hidden').val());
        element.attr('title', jQuery('#schowaj_hidden').val());
      }
    }
    category.toggle();
  }

  return false;
}
function updateVisibleProductsCount() {
  jQuery('.product_listing').each(function() {
    var id = jQuery(this).attr('id').replace('product_listing_', '');

    var product_count = jQuery('.product_row_' + id).length;
    if (jQuery('#visible_products_count_' + id).length > 0) {
      jQuery('#visible_products_count_' + id).html(product_count.toString());
    }
  });
}

jQuery(document).ready(function() {
  jQuery('#filter_dropdown').change(function(e) {
    var _this = jQuery(this);
    var uri = window.location.href;
    if(_this.val()==-1)
    {
      uri = window.location.href.replace(/\??manufacturers_id=\d+&?/, '');
    }
    else
    {
      if(window.location.href.match(/manufacturers_id=/))
      {
        uri = window.location.href.replace(/manufacturers_id=\d+(&?)/, 'manufacturers_id='+_this.val()+'$1');  
      }
      else
      {
        if(!window.location.href.match(/\?/))
        {
          uri = window.location.href + '?manufacturers_id=' + _this.val();
        }
        else
        {
          uri = window.location.href + '&manufacturers_id=' + _this.val();
        }
      }
    }
    
    window.location.href = uri;
  });
  
  jQuery('#sort_menu').change(function(e){
    var _this = jQuery(this);
    var uri = window.location.href;
    if(_this.val()==-1)
    {
      uri = window.location.href.replace(/\??sortuj=[^&]+&?/, '');
    }
    else
    {
      if(window.location.href.match(/sortuj=/))
      {
        uri = window.location.href.replace(/sortuj=[^&]+(&?)/, 'sortuj='+_this.val()+'$1');  
      }
      else
      {
        if(!window.location.href.match(/\?/))
        {
          uri = window.location.href + '?sortuj=' + _this.val();
        }
        else
        {
          uri = window.location.href + '&sortuj=' + _this.val();
        }
      }
    }
    
    window.location.href = uri;
  });
});

