function trunc_str(value, len){
  var new_value = value.substring(0, Math.min(len, value.length));
  if(new_value.length != value.length) { new_value += '...'; }
  return new_value;
}

function fix_menu(){
    $('dl.navigation > dd').mouseover(function(){
		$('.mm_menu.open').removeClass("open");
		var current_menu = $('dl.navigation').children('dd.active').attr('id');
		if ($(this).attr('id') != current_menu) 
			$(this).addClass("open");
	});
}

function slide_desc(){
    // default hidden
    $('.img span.opis').each(function(){
        var size = $(this).height() + 13;
        $(this).css('bottom', '-' + size + 'px');
    });
    $('a.gallery, a.nogallery').bind("mouseenter", function(){
        var opis = $("span.opis", this);
        var size = $(opis).height() + 13;
        $(opis).animate({
            'bottom': '+=' + size
        }, 'slow');
    }).bind("mouseleave", function(){
        var opis = $("span.opis", this);
        var size = $(opis).height() + 13;
        $(opis).animate({
            'bottom': '-=' + size
        }, 'slow');
    });
}

function tab_event(){
    $('.bTab li').click(function(){
        $('a', this).removeClass().addClass("on");
        $(this).siblings().each(function(){
            $('a', this).removeClass().addClass("off");
        });
        
        var index = $(this).index();
        $(this).parents(".bTab").siblings().each(function(i){
            if (i == index) {
                $(this).show();
            }
            else {
                $(this).hide();
            }
        });
        return false;
    });
}

function pretty_scroll(){
    $('.scroll-pane').each(function(){
        var hide_after_scroll_add = false;
        if ($(this).parent().parent().parent().parent().css('display') == 'none') {
            $(this).parent().parent().parent().parent().show();
            hide_after_scroll_add = true;
        }
        $(this).jScrollPane({dragMinHeight: 30, dragMaxHeight: 30, animateInterval: 10, animateStep: 1 });
		if (hide_after_scroll_add) {
            $(this).parent().parent().parent().parent().hide();
        }
    });
}

function pb_search(){
    $('#ecom0').hide();
    $('#ecom2').hide();
    $('#ecom3').hide();
    
	$('#ecom1:submit').click(function() {
		$('#znajdz_produkt').submit();
		return false;
	});
	
	$('#ecom2:submit').click(function() {
		$('#znajdz_firme').submit();
		return false;
	});
	
    $('#znajdz_produkt').submit(function(){
        var params = [];
        params.push('');
		
		var prod_str = $('#znajdz_produkt input#ecom1_product').val();
		if (prod_str == 'wpisz nazwę produktu') {
			params.push('');
		} else {
	        params.push(prod_str);
		}
		
		var comp_str = $('#znajdz_produkt input#ecom1_company').val();
		if (comp_str == 'wpisz nazwę firmy') {
			params.push('');
		} else {
	        params.push(comp_str);
		}
        params.push(0);
        params.push(0);
        params.push(0);
        
        var path = $(this).attr('action');
        var href = path + 'filter_' + params.join(';');
        window.open(href);
        return false;
    });
	
	$('#znajdz_firme').submit(function() {
		if ($('#ecom2_company').val() == 'wpisz nazwę firmy') {
			$('#ecom2_company').val('');
		}
		
		if ($('#ecom2_product').val() == 'wpisz nazwę produktu') {
			$('#ecom2_product').val('');
		}
	});
}

$(document.body).ready(function(){
    fix_menu();
    slide_desc();
    tab_event();
    pretty_scroll();
    pb_search();
})

function onsortchange(queryargs){
	document.location = "?order="+$('#id_order').val()+"&"+queryargs;
}

function onsortclick(queryargs, type){
	document.location = "?order="+type+"&"+queryargs;
	return false;
}

function ScrollToElement(theElement){
  var selectedPosX = 0;
  var selectedPosY = 0;
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
 window.scrollTo(selectedPosX,selectedPosY);
}

function truncatesmart(value, limit) {
    var limit = parseInt(limit);
	if (value.length <= limit)
		return value;
		
	var value = value.slice(0, limit);
	var words = value.split(' ').slice(0, -1);
	return words.join(' ') + '...'
}

