var jquery_identify_salt = 'rf_unid_';
var jquery_identify_counter = 0;

/** function to create a unique id for an element
 * @param {Object} $
 */
(function($){
    $.fn.identify = function() {
        return this.each(function() {
            var obj = $(this);
            var tag = obj.get(0).tagName.toLowerCase()+'_';
            var hasID = obj.attr('id');
            if(!hasID || hasID.length === 0) {
                var newID = jquery_identify_salt+tag+jquery_identify_counter;
                jquery_identify_counter++;
                obj.attr('id',newID);
            }
            return obj.attr('id');
        });
    };
})(jQuery);

// delay it
var delayA = {};
(function($){
    $.fn.delay = function(ms,func){
        this.each(function(){
            var id = $(this).identify();
            console.log(id)
            try {
                clearTimeout(delayA[id]);
            } catch(e) {}
            delayA[id] = setTimeout(func,ms);
        });
        return $(this);
    };
})(jQuery);


var rf_delay_timer = null;
/** method to delay a function
 * will prevent double-click if function has not been fired yet
 * @param {String} func string used as first parameter in setTimout
 * @param {Integer} delay delay in milliseconds
 */
function delay(func,delay) {
    // reset existing timer
    try{clearTimeout(rf_delay_timer)}catch(e){}
    rf_delay_timer = setTimeout(func,delay);
}


function FormDataValidate(form) {
    var invalid_inputs = [];
    $(form).find('[class="required"]').not('label').each(function(element){
        if($(this).val() == '') {
            invalid_inputs.push($(this));
        }
    });
    if(invalid_inputs.length > 0) {
        for(i in invalid_inputs){
            //{ fontSize:"24px" } , 1000
            console.log($(invalid_inputs[i]));
            $(invalid_inputs[i]).css('background-color','#310');
        }
        return false;
    } else {
        return true;
    }
}

$(document).ready(function() { 
    $("table.sortable").each(function(){
        $(this).tablesorter( { sortList: [[$(this).find("th.sortable_presort").prevAll().length,$(this).find("th.sortable_descsort").length]]} );
    });
    
    $(".ToggleDisplayForNextElement").each(function(){
        $(this).css('cursor','pointer');
        $(this).bind('mouseover',function(){
            $(this).css('background-color','#666');
        });
        $(this).bind('mouseout',function(){
            $(this).css('background-color','');
        });
        $(this).bind('click',function(){
            $(this).next().slideToggle();
        });
    })
    
    $(document).ready(function() {
        $("a[href^='/images']").fancybox({
            'zoomSpeedIn': 0, 
            'zoomSpeedOut': 0, 
            'zoomOpacity':true
        });
    });
    
    $(document).ready(function() {
        $("form").find('input').each(function(){
            $(this).change(function(){
                $(this).closest('input[type=submit]').find('ins.formfeedback').remove();
                $(this).closest('input[type=submit]').append('<ins class=\"formfeedback fleft\">Es gibt ungespeicherte Änderungen in diesem Formular</ins>');
                $(this).closest('input[type=submit]').find('ins.formfeedback').css('color','#f00').animate({ color: "#888888" }, 1000);
            });
        });
    });
}); 
