/* Biblioteka niedużych, samodzielnych skryptów DHTML */

var ui_tools = {

    /* debug */
    init_print: function () {
        $("body").append('<div id="debug" style="position: absolute; left: 0; top: 0; background-color: #000; color: #fff"></div>');
    },
    
    /* debug */
    print: function (msg) {
        $("#debug").append("<p>"+msg+"</p>");
    },
        
    disable_button: function (buttonWrapper) { 
        return false;
    },
    
    enable_button: function (buttonWrapper) { 
        return false;
    },
    
    enable_input: function (input_element) { 
        if (input_element.is('select')) {
            input_element.parent().removeClass('disabled');
            input_element.removeAttr('disabled');
        }
    },
    
    disable_input: function (input_element) {
        if (input_element.is('select')) {
            input_element.parent().addClass('disabled');
            input_element.attr('disabled', 'disabled');
        }
    },

    submit_confirmation_popup: function (form, button, popup_text) {
        var ok_text = jsTexts.delete_popup_ok;
        var cancel_text = jsTexts.delete_popup_cancel;
        button.click(function() {
            Boxy.ask(popup_text, [cancel_text, ok_text], function(val) {
                if (val === ok_text) {
                    form.submit();
                }
            }, {title: jsTexts.confirmation_popup_title});
        });
    },

    set_remove_popup: function (elem, popup_text) {
        $("embed").attr("wmode","opaque"); // zeby przykrylo flash! (movie)
        if (elem === '') {
            elem = $('.deleteLink');
        }
        if (popup_text === '') {
            popup_text = jsTexts.delete_popup_default_text;
        }
        var ok_text = jsTexts.delete_popup_ok;
        var cancel_text = jsTexts.delete_popup_cancel;
        elem.click(function() {
            var link = $(this);
            Boxy.ask(popup_text, [cancel_text, ok_text], function(val) {
                if (val === ok_text) {
                    window.location = link.attr('href');
                }
            }, {title: jsTexts.delete_popup_title});
            return false;
        });
    },
    
    default_text_auto_hide: function(elem, text){
        if(!arguments[0]){
            return false;
        }else{
            //var _this = $(elem);
            var default_text;
            if(arguments[1]){
                default_text = arguments[1];
            }else{
                default_text = $(elem).attr('title');
            }
            if ($(elem).val() === '') {
                $(elem).addClass('defaultText');
                $(elem).val(default_text);
            }
            $(elem).focus(function(){
                if($(this).val() === default_text){
                    $(this).val('');
                    $(this).removeClass('defaultText');
                }
            });
            
            $(elem).blur(function(){
                if($(this).val() === ''){
                    $(this).addClass('defaultText');
                    $(this).val(default_text);
                }
            });
        }
    },
    
    signForIE: function(){
        if($.browser.msie){
            if($.browser.version >= 8){
                $('body').addClass('ie8');
            }else if($.browser.version < 8 && $.browser.version >= 7){
                $('body').addClass('ie7');
            }else{
                $('body').addClass('ie6');
            }
        }
    },
    
    // toole do obslugi odliczania!
    read_time_limit: function (box) {
        /* Funkcja odczytuje czas wyswietlania boxa z klasy o konstrukcji t[liczba sekund] */
        var re_t = /time(\d{1,2})/.test(box.attr('class'));
        return RegExp.$1;
    },
    
    set_time_limit: function (box, new_time_limit) {
        /* Funkcja podmienia czas wyswietlania boxa w klasie o konstrukcji t[liczba sekund] */
        var re_t = /(time\d{1,2})/.test(box.attr('class'));
        var old_class = RegExp.$1;
        box.removeClass(old_class);
        box.addClass('time' + new_time_limit);
    },
    // koniec tooli do obslugi odliczania
    
    userProfileInfoBox:  {
        show: function(e){
            var sourceHtml = $(e).parentsUntil('li').parent().find('.infoBoxSource').html();
            $('#profileInfoBox .profileInfoContent').html(sourceHtml);
            var ePosition = $(e).offset();
            $('#profileInfoBox').css({
                "left": (parseInt(ePosition.left, 10) - 20) + "px",
                "top": (parseInt(ePosition.top, 10) + 15) + "px"
            });
            $('#profileInfoBox').show();
        },
        hide: function(e){
            $(e).parent().hide();
            $(e).parent().find('.profileInfoContent').html('');
        }
    },
    
    update_fake_upload_inputs: function () {
        $(".fakeUploadInput").each(function(){
            $(this).find(".name").html($(this).find("input").val());
        });
    },
    
    init_update_fake_upload_inputs: function () {
        setInterval("ui_tools.update_fake_upload_inputs();", 500);
    },
    
    question_mark_join: function (url, args) {
        /* Zwraca url polaczony url z args sprawdzajac czy trzeba dodac '?' */
       if (url.indexOf('?') > -1) {
           return url + args;
       } else {
           return url + '?' + args;
       }
    }

};



