$(document).ready(function(){

    $('#select-producer').hide();

    $("#browsable").scrollable({
        circular: true,
        speed: 1500
    }).navigator().autoscroll({
        autoplay: true,
        interval: 5000
    });

    search_default_text = $('#search-key').val();

    $('#search-key').focus(function(){
        if($(this).val() == search_default_text)$(this).val('');
    }).blur(function(){
        if($(this).val().length == 0)$(this).val(search_default_text)
    })

    

    $('.increment').click(function(e){
        $v = $(this).prev();
        $v.val(parseInt($v.val())+1);
    });

    $('.decrement').click(function(e){
        $v = $(this).next();
        if (parseInt($v.val()) > 0) $v.val(parseInt($v.val())-1);
    });

    var invoiceDisabled;
    if($('#get-invoice').is(':checked')) invoiceDisabled = false;
    else invoiceDisabled = true;

    $("a[rel=lightbox],a.lightbox").fancybox({
        'zoomSpeedIn'	:	500,
        'zoomSpeedOut' :	500,
        'hideOnContentClick': false
    });

    $('.autosubmit').change(function(){
        $(this).closest('form').submit();
    });

    $('input[type="submit"].autosubmit').hide();

    $('#get-invoice').change(function(){
        $('.invoice input').toggleClass('disabled');
        if (invoiceDisabled) {
            $('.invoice input').attr('disabled','')
        } else {
            $('.invoice input').attr('disabled','disabled')
        }
        invoiceDisabled = !invoiceDisabled;
    });

   if (invoiceDisabled) $('.invoice input').attr('disabled','disabled').addClass('disabled');

    $("table.sortable").tablesorter({
        widgets: ['zebra']
    });

    $('#key').keyup(function(e){
        if($(this).val().length < 2)$('.filterable tr').show();
        else $('.filterable td.name').each(filterTable);
        zebra();
    });

    $('body').filter(':not(#select-label)').filter(':not(#producers)').click(function(){
        if($('#select-producer').is(':visible')) $('#select-label').trigger('click');
    });

    $('#select-producer a').click(function(){
        $('#select-producer').hide();
    })   

    var count = Math.round($('#select-producer li').length / 2);
    $('ul#left-list').after('<ul id="right-list" class="right"></ul>');
    $('ul#left-list li').slice(count).appendTo('#right-list');
    

    $('#select-label').toggle(function(e){
        e.stopPropagation();

        var count = $('#select-producer li').length;
        if (count % 2) var newHeight = (count * 22 / 2);
        else var newHeight = (count * 21 / 2);
        var newTop = '-'+ (newHeight+5)+'px';
        newHeight += 'px';

        $('#select-producer').show().animate({
            top: newTop,
            height: newHeight
        },300);
    },function(e){
        e.stopPropagation();
        $('#select-producer').animate({
            height: 0,
            top: -5
        },300,function(){
            $('#select-producer').hide();
        });

    });
    
    $('#registration-form, #contact-form,#order-form').submit(function(e){
        $('div.errors').remove();
        $('input.required,textarea.required').each(function(){
            if($(this).val().length == 0)$(this).addClass('error');
            else $(this).removeClass('error');
        });

        if($('input.error').length > 0){
            e.preventDefault();
            $(this).prepend('<div class="errors">Prosimy poprawić pola oznaczone na czerowono</div>');
        }

        email = $('#email').val();
        if(!validateEmail(email)){
            e.preventDefault();
            $('#email').addClass('error');
            $(this).prepend('<div class="errors">Podany adres email jest niepoprawny</div>');
        } else {
            $('#email').removeClass('error');
        }

        if($('#rules').length && !$('#rules').is(':checked')){
            e.preventDefault();
            $(this).prepend('<div class="errors">Prosimy zaakceptować regulamin sklepu</div>');
            $('#rules-field').addClass('error');
        }

    });

    try{

        $('#nivoslider').nivoSlider({
            effect:'sliceDown', //Specify sets like: 'fold,fade,sliceDown'
            slices:20,
            animSpeed:1000, //Slide transition speed
            pauseTime:7000,
            directionNav:false, //Next & Prev
            directionNavHide:true, //Only show on hover
            controlNav:false //1,2,3...
        });
    }
    catch(err){
    }


});

function validateEmail(email){
    var regEx = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
    return (!regEx.test(email)) ? 0 : 1;
}

filterTable = function(){
    key = $(this).text().toLowerCase();
    val = $('#key').val().toLowerCase();
    if(key.indexOf(val) > -1)$(this).parent().show();
    else $(this).parent().hide();
}

