function addMessage(type, message) {
    $.Growl.show({
        'message': '<p>'+ message +'</p>',
        'icon': type
    });
}

function replaceText(text, textareaId)
{
    var textarea = document.getElementById(textareaId);

    // text range replace (not compatible with IE).
    if (typeof(textarea.selectionStart) != "undefined")
    {
        var begin = textarea.value.substr(0, textarea.selectionStart);
        var end = textarea.value.substr(textarea.selectionEnd);
        var scrollPos = textarea.scrollTop;

        textarea.value = begin + text + end;

        if (textarea.setSelectionRange)
        {
            textarea.focus();
            textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
        }
        textarea.scrollTop = scrollPos;
    }
    //IE
    else
    {
        textarea.focus();

        var selection = document.selection.createRange();
        selection.text = text;
    }
}

$(document).ready( function() {

    // spoiler_tag
	$(".spoiler_head").toggle(
		function() {
            $(this).children().text("Ukryj spoiler");
            //  $lang_common['hide_spoiler']
			$(this).next().show("slow");
		},
		function() {
			$(this).children().text("Pokaż spoiler");
            // $lang_common['show_spoiler']
			$(this).next().hide("slow");
		}
	);

	// zaznaczanie wszystkich postów (moderacja)
	$(".modbuttons input[name=select_all_posts]").toggle(
		function() {
			$(".multidelete input[type=checkbox]").attr("checked","checked");
		},
		function() {
			$(".multidelete input[type=checkbox]").removeAttr("checked");
		}
	);

    // BBCode buttons
    $(".bbcode_buttons input[type=button]").click( function() {
        var open = '['+$(this).attr("name")+']';
        var close = '[/'+$(this).attr("name")+']';
        var msgfield = $(".txtarea .required textarea")[0];

        if (open == '[url]') {
            var adres = prompt('Podaj adres URL','http://');
            if (!adres) {
                return false;
            } else {
                if (adres.length >= 10) {
                    var nazwa = prompt('Podaj tytul strony (opcjonalne)','');
                    if (!nazwa) {
                        open = '[url='+adres+']';
                    } else {
                        open = '[url='+adres+']'+nazwa;
                    }
                }
            }
        }

        if (open == '[img]') {
            var adres = prompt('Podaj adres URL obrazka','http://');
            if (!adres) {
                return false;
            } else {
                if (adres.length >= 11) {
                    open = '[img]'+adres;
                }
            }
        }

        // IE support
        if (document.selection && document.selection.createRange) {
            msgfield.focus();
            sel = document.selection.createRange();
            sel.text = open + sel.text + close;
            msgfield.focus();
        } else if (msgfield.selectionStart || msgfield.selectionStart == '0') {
            // Moz support
            var startPos = msgfield.selectionStart;
            var endPos = msgfield.selectionEnd;

            msgfield.value = msgfield.value.substring(0, startPos) + open + msgfield.value.substring(startPos, endPos) + close + msgfield.value.substring(endPos, msgfield.value.length);
            msgfield.selectionStart = msgfield.selectionEnd = endPos + open.length + close.length;
            msgfield.focus();
        } else {
            // Fallback support for other browsers
            msgfield.value += open + close;
            msgfield.focus();
        }

        return true;
    });

    // emotikony
    $("#smilies_box img").click(function(){
        replaceText(' ' + $(this).attr('title') + ' ', 'message');
    });

    // zwijanie kategorii forum
    $('div[id^=idx] > h2').click(function(){
        $(this).next().toggle();
        $.cookie('bthidden', $('div.blocktable > .box').filter(":hidden").parent().map(function(){
            return this.id;
        }).get().join(','),{expires:60});
    });
    if ($.cookie('bthidden')) {
        $.each($.cookie('bthidden').split(','), function(a,id){
          $('#'+id+' > .box').toggle();
        });
    }
});

