(function($){
  $.fn.addEventListener = function(eventType, handler, scope, eventData){
    if (arguments.length < 2)
      return;

    var fn = handler;
    if (arguments.length > 2) {
      fn = $.proxy(handler, scope);
    }
    return this.each(function(){
      $(this).bind(eventType, eventData, fn);
    });

  };
})(jQuery);

function checkAll(input) {
    if (input.checked) {
        var div = input.parentNode.parentNode;
        var id = $(div).attr('id');
        $('#' + id + ' input:checked').attr('checked', null);
        $(input).attr('checked', 'checked');
        input.form.submit()
    }
}

function test()
{
    alert('test');
}

$(document).ready(function(){
        // vyhledavaci formy
        var options = {
            serviceUrl: '/cs/live_search',
            autoSubmit: true,
            width: 250
        };

        var forms = {
            search_form: "searh_title",
            sent_form: "stext"
        };

        for (var fname in forms) {
            var form = $('#' + fname);
            var input = form.find('#' + forms[fname]);

            if (input != undefined && input.length > 0) { // naseptavace
                input.autocomplete(options);
            }

            form.submit(function(){
                var input = $(this).find('#' + forms[this.id]);
                var val = input.val();

                if (val == 'vyhledávání' || val == '' || val == undefined) {
                    alert('Prosím vlo\u017ete text pro vyhledání');
                    return false;
                }

                return true;
            });
        }

        // filtrovaci formulare se odesilaji sami
        $('#products-restrictions input, #products-restrictions select').each(function(){
            $(this).addEventListener('change', function() {
                this.form.submit();
            }, false);
        })

        
        $('#products-restrictions .restriction-box').each(function(index, div) {
            var jdiv = $(div);
            var countch = $('#' + jdiv.attr('id') + ' input:checked').length;
            var checked = ' checked="checked"';

            if (countch > 0) {
                checked = '';
            }

            var id = 'restr_check_all_'+ index;
            
            jdiv.append(
                '<span class="check"><label for="'+id+'">Vše</label><input type="checkbox" id="'+id+' "value="0"' + checked + ' onchange="checkAll(this)"></span>'
            )
        });
        
    // gallery
	$("#gallery_belt").easySlider({
		continuous: true
	});

    // bookmarks
    if ($("ul.bm_controllers").tabs) {

        $("ul.bm_controllers").tabs("div.bm_content > div > div > div.page");

        $('ul.bm_controllers li').bind('click', function() {

            $('ul.bm_controllers li a').each(function(){
                $(this).removeClass('active');
            });

            $(this).find('a').addClass('active');

        });
   }

});

