$(document).ready(function(){
    function disselectActionRow(){
    	$('.action_select').each(function(){
            $(this).removeClass("action_select");
        });
    }

    $('html').click(function(){
       disselectActionRow();
    });

    $('#actionbar ul li .quickpageaction').each(function(){
        $(this).button().click(function() {

        })
        .next()
        .button({
                text: false,
                icons: {
                        primary: "ui-icon-triangle-1-s"
                }
        }).click(function(){
            $('.page_action').qtip('show');
        })
        .parent()
        .buttonset();

    });


    $('.page_action').each(function(){
        $(this).qtip({
            content:
            {
                ajax:
                {
                   method: 'get',
                   url: $(this).attr("rel")
                }
            },
            position: {
               my: 'top right',
               at: 'bottom center',
               adjust: {screen: true}
            },
            style:
            {
                tip:
                {
                    corner: true,
                    size: {x: 20, y: 8}
                },
                classes: 'ui-tooltip-dark action_qtip'
            },
            show: 'click',
            hide: 'unfocus'
        });
    });

    $('.table_action').button({
        icons: {
                primary: "ui-icon-triangle-1-s"
        }
    });

    function updateTooltip(){
        var qtip = $(this).attr('id');
        var cid = qtip;
        if(cid.length == 0){
            cid = 'choose';
        }

        cid = cid.replace('ui-tooltip-', '');
        var selected = $('input[name*="'+ cid + '"]:checked').size();

        $('#' + qtip + ' .action_info_box').css('display', 'none');
        $('#' + qtip + ' .action_info_none').css('display', 'none');
        $('#' + qtip + ' .action_info_multiple').css('display', 'none');

        if(selected == 0)
        {
            $('#' + qtip + ' .action_info_box').css('display', 'block');
            $('#' + qtip + ' .action_info_none').css('display', 'block');
            $('#' + qtip + ' a[href!=""]').each(function(){
                $(this).attr('rel', $(this).attr('href'));
                $(this).removeAttr('href', null);
                $(this).addClass('inactive');
            });
        }
        else
        {
            $('#' + qtip + ' a[rel!=""]').each(function(){
                $(this).attr('href', $(this).attr('rel'));
                $(this).removeAttr('rel', null);
                $(this).removeClass('inactive');
            });

            if(selected >= 2)
            {
               //alert('Is multiple');
               $('#' + qtip + ' .action_info_box').css('display', 'block');
               $('#' + qtip + ' .action_info_multiple').css('display', 'block');
               $('#' + qtip + ' .single_action a[href!=""]').each(function(){
                    $(this).attr('rel', $(this).attr('href'));
                    $(this).removeAttr('href', null);
                    $(this).addClass('inactive');
                });
            }
            else
            {
                //alert('Is single');
                $('#' + qtip + ' .single_action a[rel!=""]').each(function(){
                    $(this).attr('href', $(this).attr('rel'));
                    $(this).removeAttr('rel', null);
                    $(this).removeClass('inactive');
                });
            }
        }
    }

    function createEventTooltips(){
        $('#ui-tooltip-' + $(this).attr('id') + '-title').children('.tooltip').each(function(){
            $(this).qtip({
               content:
               {
                   title: $(this).attr('title'),
                   ajax:
                   {
                      method: 'get',
                      url: $(this).attr("rel")
                   }
               },
               position: {
                  my: 'top right',
                  at: 'bottom right',
                  adjust: {screen: true}
               },
               style:
               {
                   tip:
                   {
                       corner: true,
                       size: {x: 20, y: 8}
                   },
                   classes: 'ui-tooltip-dark action_qtip ontop'
               },
               show: '',
               hide: ''
           });

           $(this).mouseover(function(){
               $(this).qtip('show');
            });

            $(this).mouseout(function(){
               $(this).qtip('hide');
            });

            $(this).focusout(function(){
               $(this).qtip('hide');
            });
       });
    }

    $('.table_action').each(function(){
        $(this).qtip({
            prerender: true,
            id: $(this).attr('ref'),
            content:
            {
                ajax:
                {
                   method: 'get',
                   url: 'index.php?target=ajax',
                   data: $(this).attr("rel")/*,
                   success: updateTooltip*/
                }
            },
            position:
            {
               my: 'bottom left',
               at: 'top left',
               adjust:
               {
                   x: 15,
                   screen: true
               }
            },
            events:
            {
                show: updateTooltip
            },
            style:
            {
                tip:
                {
                    corner: true,
                    size: {x: 20, y: 8}
                },
                classes: 'ui-tooltip-dark action_qtip'
            },
           show: 'click',
           hide: 'click unfocus'
        });
    });

    $('.table_row_action').each(function(){
        $(this).qtip({
        content:
        {
            ajax:
            {
               method: 'get',
               url: 'index.php?target=ajax',
               data: $(this).attr("rel")
            }
        },
        position:
        {
           my: 'top right',
           at: 'bottom right',
           adjust:
           {
               screen: true
           }
        },
        style:
        {
            classes: 'ui-tooltip-dark action_qtip'
        },
       show: 'click',
       hide: 'click unfocus'
    });
    });

    $('.event_action').each(function(){
        $(this).click(function(event){
            // events aus dem Kalender haben ein anderes parent
            if($(this).parents('.tt-eventborder').val() != undefined)
            {
                // entweder mit der Klasse tt-eventborder (Event)
                parent = '.tt-eventborder';
            }
            else
            {
                // oder mit der Klasse tt-col (Tag)
                parent = '.tt-col';
            }
            $('input#selected_row').val($(this).parents(parent).attr("id"));
            $(this).parent().parent().parent().addClass('action_select');
            event.stopPropagation();
        });

        $(this).qtip({
            content:
            {
                title: $(this).parent('ul').attr('rel'),
                ajax:
                {
                   method: 'get',
                   url: 'index.php?target=ajax',
                   data: $(this).attr("rel"),
                   success: createEventTooltips
                }
            },
            position:
            {
               my: 'top right',
               at: 'bottom right',
               adjust:
               {
                   screen: true
               }
            },
            style:
            {
                classes: 'ui-tooltip-dark action_qtip'
            },
           show: 'click',
           hide: 'click unfocus'
        });
    });

    $('.table').each(function(){
        $(this).children('tr').each(function(){
            $(this).click(function(event){
                if($(this).hasClass('action_select')){
                    $(this).removeClass('action_select');
                }else{
                    $(this).addClass('action_select');
                }
            });
        });
    });

    $('a.direct_action').each(function(){
            $(this).click(function(event){
                var id = $(this).attr('href');
                $('input.choose').attr('checked', false);
                $(this).closest('tr').children().children('input.choose').attr('checked', true);
                $('input#function').val(id);
                $('#list_form').submit();

                event.stopPropagation();
                return false;
            });
        });

    $('.table_row_action').each(function(){
        $(this).click(function(event){
            var tr = $(this).closest('tr');
            disselectActionRow();
            tr.children('input.choose').attr('checked', true);
            tr.addClass('action_select');
            event.stopPropagation();
            $('input#selected_row').val(tr.attr('id'));
        });
    });

    $('a.row_action, a.event_action').live('click', function(){
        var func = $(this).attr('href');
        $('input.choose').attr('checked', false);
        $('input#function').val(func);
        $(jqid($('input#selected_row').val())+ " .choose").attr('checked', true);
        
        $('#list_form').submit();
        return false;
    });

    $('a.action').live('click', function(){
        var func = $(this).attr('href');
        $('input#function').val(func);
        $(jqid($('input#selected_row').val())+ " .choose").attr('checked', true);

        $('#list_form').submit();
        return false;
    });
});
