(function($) {
    $.fn.form_values = function(url) {
        this.each(function() {
            var root = $(this);
            var qstring = $.param(root.find("input:not(:checkbox, :radio, :button, :reset), input:checkbox:checked, input:radio:checked, select, textarea"));
            location.href = url + "?" + qstring;
        });
    };

$.fn.title = function(title, type, initMode, textContainer) {
        $(this).each(function() {
            var root = $(this);
            var k;
            $(document).find('.title').add(root).bind('mouseover click', function() {
                if(k)
                    clearTimeout(k);
            }).bind('mouseout', function() {
               k = setTimeout(function() { $(document).find('.title').fadeOut(); }, 3000);            
            });
            var TitleText = $(this).parent().find(textContainer);
            var popup =
            $("<div>")
            .addClass("title " + type)
            .css('position', 'absolute');

            var popupInner =
            $('<div />')
            .addClass('titleInner')
            .html(title || TitleText.html());

            popup.append(popupInner);
            TitleText.css('display', 'none');

            popup.css('top', root.offset().top - popup.height() - 15 + 'px');
            popup.css('left', root.offset().left + 'px');
            if (root.hasClass('fail')) popup.addClass('error-title');

            var pos = root.parent().attr('position');
            if (pos != 'relative' || pos != 'absolute') {
                root.parent().css('position', 'relative');
                root.parent().css('display', 'block');
            }

            popup.hide();
            var t = null;
            root.bind(initMode || 'mouseover focus', function(e) {
                popup.appendTo($('body'));
                $('body').find('.title').hide();
                if (popup.not(':visible')) {
                    popup.show();
                    popup.css('top', root.offset().top - popup.height() - 35 + 'px').css('left', root.offset().left - popup.width() / 2);
                }

                if (t) clearTimeout(t);
                if (initMode.search(/click/) >= 0) {
                    e.stopPropagation();
                    e.preventDefault();
                }

            });
            root.add(popup).bind('mouseout', function() {
                t = setTimeout(function() { popup.fadeOut(); }, 2000);
            });

            popup.bind('mouseover', function() {
                if (t) clearTimeout(t);
            });
        });
    };

    $.fn.calculate_result = function(checked, scoreInput, titleInput) {
        var root = $(this);
        
        var rtitle = root.find("#regular_title");
        var htitle = root.find(titleInput);
        var rscore = root.find(scoreInput || "#regular_score");  
        
        function updateFields() {
            var arr = new Array(), max = 0, tmpmax = 0, varr = new Array(), tmpmaxv = 0, maxv = 0;


            var active = "";

            var els = root.find('input:checkbox:checked').each(function() {
                if(scoreInput && $(this).parent().attr('title') != '') { 
                    var pointHolder = $(this).parent().attr('title');
                }
                else {
                    var pointHolder = $(this).val();
                }
                
                tmpmax = Math.max(max, parseFloat(pointHolder));

                if (tmpmax > max || (tmpmax == 0 && max == 0)) {
                    var lbl = root.find('label[for=' + $(this).attr('id') + ']');
                    var title = lbl.text();                                       

                    if(scoreInput && $(this).parent().attr('title') != '') {                    
                        rscore.val(pointHolder);
                    }
                    else {
                        rscore.val(pointHolder);
                    }                 
                    max = tmpmax;
                }
                var lbl2 = root.find('label[for=' + $(this).attr('id') + ']');
                if (lbl2) {
                    active += lbl2.text() + " ";
                    rtitle.text(active);
                    if(htitle) htitle.val(active);
                }
            });

            var elsamount = els.length;
            if (elsamount == 0) {
                rtitle.text("-");
                rscore.val("");
                htitle.val("");
            }

        }

        $(this).find('input:checkbox').click(updateFields);
            rscore.val('');
            htitle.val('');
            rtitle.text('-');
           updateFields(); 
            
        jQuery.each(checked.split(' '), function() {
            var value = this
            var el = root.find('input:checkbox[id=' + value + ']');
            
            if (el) {
                el.click();
                el.checked = true;
            }

        });
        $(document).ready(function() {
            updateFields();
        });

    };

})(jQuery);