$(document).ready(function(){
    /**
     * Spin components
     */
    $('input.number').spin({max:30, min:10}).css("width", "30px");
    $('input.spin').spin().css("width", "30px");

    /**
     * Date and time components
     */
    $("input.date").datepicker({
        dateFormat: 'yy-mm-dd',
        beforeShow: function(input, inst) {
            inst.dpDiv.css({
                marginTop: '0px', //-input.offsetHeight + 'px',
                marginLeft: (input.offsetWidth - 213) + 'px'//-input.offsetWidth + 'px'
            });
        }
    });
    
    $("input.time").datetimepicker({
        dateFormat: 'yy-mm-dd',
        timeFormat: ' hh:ii:ss',
        beforeShow: function(input, inst) {
            inst.dpDiv.css({
                marginTop: '0px', //-input.offsetHeight + 'px',
                marginLeft: (input.offsetWidth - 213) + 'px'//-input.offsetWidth + 'px'
            });
        }
    });

    /**
     * TableList functionality
     */
    $("input#selectAll").click( function() {
        form = $("form#jForm");
        if ($(this).attr("checked")) {
            form.find("input[type='checkbox']").attr('checked', true);
            form.find("input[type='checkbox']").parent().parent().find("td").addClass("colorSelected");
        } else {
            form.find("input[type='checkbox']").each(function(){
                $(this).attr('checked', false);
                var className = $(this).parent().parent().attr("class");
                $(this).parent().parent().find("td").removeClass();
                $(this).parent().parent().find("td").addClass(className);
            });
        }
    });
    $("a#selectAllLink").click( function() {
        $("form#jForm").find("input[type='checkbox']").attr('checked', true);
        return false;
    });
    $("a#deselectAllLink").click( function() {
        $("form#jForm").find("input[type='checkbox']").attr('checked', false);
        return false;
    });
    
    $("form.groupSelectiveForm table tbody tr").hover(
        function() {
            $(this).find("td").removeClass();
            $(this).find("td").addClass("colorActive");
        }, function() {
            if ($(this).find("td input[type='checkbox']").attr('checked')) {
                $(this).find("td").removeClass();
                $(this).find("td").addClass("colorSelected");
            } else {
                var className = $(this).attr("class");
                $(this).find("td").removeClass();
                $(this).find("td").addClass(className);
            }
        }
    );
    /**/$("form.groupSelectiveForm table tbody tr").click(
        function() {
            checkbox = $(this).find("td input[type='checkbox']");
            if (checkbox.attr('checked')) {
                checkbox.attr('checked', false);
            } else {
                checkbox.attr('checked', true);
            }
        }
    );/**/
    $("form.groupSelectiveForm table tbody tr td input[type='checkbox']").change(function() {
        $(this).attr('checked', !$(this).attr('checked'));
        if ($(this).attr('checked')) {
            $(this).parent().parent().find("td").removeClass();
            $(this).parent().parent().find("td").addClass("colorSelected");
        } else {
            var className = $(this).parent().parent().attr("class");
            $(this).parent().parent().find("td").removeClass();
            $(this).parent().parent().find("td").addClass(className)
        }
        $(this).parent().parent().find("td").removeClass();
        $(this).parent().parent().find("td").addClass("colorActive");
    });
    /**
     * Actions functionality
     */
    $("div.jDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 160,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form#jForm").submit();
            }
        }
    });
    $(".jAction").click(function() {
        var form = $("form#jForm"); //$(this).getForm();
        form.find("input[name='action']").val($(this).attr("href"));
        form.find("input[id='jId']").val($(this).attr("rel"));
        form.submit();

        return false;
    });
    $("a.jDialogAction").click(function() {
        var form = $("form#jForm"); //$(this).getForm();
        form.find("input[name='action']").val($(this).attr("href"));
        form.find("input[id='jId']").val($(this).attr("rel"));
        $("#" + $(this).attr("href") + "Dialog").dialog('open');
        
        return false;
    });
    $("div.jPagenation form a").click(function() {
        form = $(this).getForm();
        page = $(this).attr("href");
        form.find("input[name=page]").val(page);
        form.submit();
        return false;
    });
});
