var bez = {};
//increase before each commit to invalidate the cache
bez.scriptVersion = 2;
bez.ctl = {};

/* DOKUWIKI:include scripts/thread.js */
/* DOKUWIKI:include scripts/thread_report.js */
/* DOKUWIKI:include scripts/threads.js */
/* DOKUWIKI:include scripts/task_form.js */
/* DOKUWIKI:include scripts/task.js */
/* DOKUWIKI:include scripts/tasks.js */
/* DOKUWIKI:include scripts/projects.js */
/* DOKUWIKI:include scripts/activity_report.js */
/* DOKUWIKI:include scripts/start.js */
/* DOKUWIKI:include scripts/report.js */


jQuery(function () {
    'use strict';

	var getUrlVars = function() {
		var vars = {},
			parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
			function(m,key,value) {
				vars[key] = value;
		});
		return vars;
	};
	var getNparams = function(value) {
		var nparams = [],
			params = value.split(':');
		//lang
		if (params[0] !== 'bez') {
			params.shift();
		}
		for (var i = 0; i < params.length; i += 2) {
            var k = params[i],
				v = params[i+1];
			nparams[k] = v;
		}
		return nparams;
	};

	bez.rich_text_editor = function($textarea, $header) {
		//clone
		var tb = toolbar.filter(function (button) {
			if (button.type === 'autohead' ||
				button.class === 'pk_hl' ||
				button.icon === 'sig.png' ||
				button.icon === 'strike.png') {
				return false;
			}
			return true;
		});
		initToolbar($header, $textarea.attr('id'), tb);
	};

	jQuery.validate({
        form: '.bez_form, .bez_form_blank',
		lang: 'pl'
	});



	var urlParams = getUrlVars();

    //userewrite == '0'
	if ('id' in urlParams) {
		var id = urlParams['id'];
	//userewrite == '1', '2'
	} else {
		var found = window.location.href.match(/([^?\/]*)\??[^?\/]*$/);
		var id = found[1];
	}

	var nparams = getNparams(id),
		ctl = nparams['bez'];

	if (typeof bez.ctl[ctl] === 'function') {
		bez.ctl[ctl].call(ctl);
	}
});

/* =============================================
   NICC - Scrollbar superior sincronizada
   ============================================= */
jQuery(function($) {
    $('.bez_table_wrapper').each(function() {
        var wrapper = $(this);
        var table = wrapper.find('table');
        if (!table.length) return;

        // Crear barra superior
        var scrollTop = $('<div class="bez_scroll_top"><div class="bez_scroll_top_inner"></div></div>');
        wrapper.before(scrollTop);

        // Función para sincronizar ancho del inner con la tabla
        function syncWidth() {
            scrollTop.find('.bez_scroll_top_inner').width(table[0].scrollWidth);
        }
        syncWidth();

        // Sincronizar scroll entre ambas barras
        var syncing = false;
        scrollTop.on('scroll', function() {
            if (!syncing) {
                syncing = true;
                wrapper[0].scrollLeft = scrollTop[0].scrollLeft;
                syncing = false;
            }
        });
        wrapper.on('scroll', function() {
            if (!syncing) {
                syncing = true;
                scrollTop[0].scrollLeft = wrapper[0].scrollLeft;
                syncing = false;
            }
        });

        // Re-sincronizar en resize
        $(window).on('resize', syncWidth);

        // Mostrar barra solo si la tabla es más ancha que el contenedor
        function checkOverflow() {
            if (table[0].scrollWidth > wrapper[0].clientWidth) {
                scrollTop.show();
            } else {
                scrollTop.hide();
            }
        }
        checkOverflow();
        $(window).on('resize', checkOverflow);
    });
});
