jQuery(document).ready(function(){
    jQuery.noConflict();
		products_list_margin();
		//category_list_margin();
        filterListCollapse(9);
        liToggle();

});

jQuery(window).resize(function(){
		products_list_margin();
		//category_list_margin();
});

function liToggle() {
    jQuery('.li-toggle').click(function() {
            jQuery(this).parent().children().show();
            jQuery(this).hide();

            return false;
    });
}

function filterListCollapse(elementPos) {
   // try {
        jQuery('ul.filter-link-ul').children(':nth-child(9)').after('<a href="" class="li-toggle">...</a>');
            jQuery('ul.filter-link-ul').each(function(index) {
                jQuery(this).children().each(function(index) {
                    if (index > elementPos) {
                        jQuery(this).hide();
                    }
                });
            });
   // }
   // catch(e) {
   // console.log(e.name);
   // }
}


function category_list_margin(){
		// set min margin for item
		min_margin = 10;
		// set item block width
		block = jQuery(".products .item").width()+min_margin*2;
		// set item parent block width
		width = jQuery(".products").width();
		// count blocks in parent
		count_blocks = Math.floor(width/block);
		// get free space in parent block with blocks
		free_space = width - block*count_blocks;

		if(free_space<(count_blocks*2)){

			ncount = count_blocks-1;

			free_space = width - block*ncount;

			margin = Math.floor(free_space/(count_blocks*2))-3;

		}else{

			margin = Math.floor(free_space/(count_blocks*2))-3+min_margin;

		}
		if((margin*(count_blocks*2)+block*count_blocks) > width){
			margin = margin - 1;
		}

		jQuery(".products .item").css("margin","10px "+margin+"px");
}


function products_list_margin(){
  //  try {
		// set min margin for item
		min_margin = 10;
		// set item block width
		block = jQuery(".good_wrapper").width()+min_margin*2;
		// set item parent block width
		width = jQuery("#center_content_block").width();
		// count blocks in parent
		count_blocks = Math.floor(width/block);
		// get free space in parent block with blocks
		free_space = width - block*count_blocks;

		if(free_space<(count_blocks*2)){

			ncount = count_blocks-1;

			free_space = width - block*ncount;

			margin = Math.floor(free_space/(count_blocks*2))-3;

		}else{

			margin = Math.floor(free_space/(count_blocks*2))-3;

		}

		if((margin*(count_blocks*2)+block*count_blocks) > width){
			margin = margin - 1;
		}

		jQuery(".good_wrapper").css("margin","10px "+margin+"px");
//}
//catch(e) {
//    console.log(e.name);
//    }
}

