$(function()
{
    /*$('.TablePage').delegate('a', 'click', function()
    {
        tablepager();
    });
    
    $('.TableSort').delegate('a', 'click', function()
    {
        tablesort();
    });*/

    tabletooltip();

    $('.TablePage').find('a').click
    (
        tablepager()
    );

    $('.TableSort').find('th').click
    (
        tablesort()
    );
    
    $('.TableTime').find('a').click
    (
        tabletime()
    );
    
    $('.TableIndex').find('a').click
    (
        tableindex()
    );    
});


function tablesort()
{
    /*$('.TableSort').delegate("a", 'click', function(e){
    });*/
    $('.TableSort').find("th").click
    (
        function(e)
        {
            e.preventDefault();
            ET_Event.eventStart('Kursliste','Sortiert nach - '+$(this).text(),'Klick');
            $(this).parent().parent().parent().parent().parent().find('th').removeClass('active');
            $(this).parent().addClass('active');
            var integer = $(this).parent().parent().attr('id').replace(/\D+/,'');
            if($(this).attr('id') != "false")
            {
                $.ajax(
                {
                        type:"POST",
                        url:"/ajax/table.php",
                        data:$(this).attr('id'),
                        beforeSend: function()
                        {
                            if(document.push)
                            {
                                if($('#'+push.PUSH_BUTTON_ID).val() == push.ACTIVATED)
                                {
                                    push.deactivatePush();
                                }
                            }    

                            var pos = $('#TableSort_'+integer).offset();
                            var width = $('#tablediv').width();
                            var height = $('#TableSort_'+integer).height();
 
                            $('#loading').html('<center><img id="loadingimg" src="'+IMAGES_URL+'Symbole/loading.gif"></center>');
                            $('#loading').css('top',pos.top+height+10).css('left',pos.left+10).css('width', width-20).css("z-index", 3);
                            
                        },
                        success: function(msg)
                        {
                            $('#loading').html('').css("z-index", 0);
                            $('#TableBody_'+integer).slideUp(1000,function(){
                                $('#TableBody_'+integer).html(msg).slideDown(1000);
                                tabletooltip();
                                tablesort();
                                tablepager();
				tabletime();
				tableindex();
                                ReloadIVW();
                            })
                        }
                 });
            }
    });
}

function tablepager()
{
    /*
    $('.TablePage').delegate("a", 'click', function(ev){
    }); */

     $('.TablePage').find("a").click
     (
        function(e)
        {
            e.preventDefault();
            ET_Event.eventStart('Kursliste','Seite - '+$(this).text(),'Klick');
            var integer = $(this).parent().parent().parent().parent().parent().attr('id').replace(/\D+/,'');

            $.ajax(
            {
                    type:"POST",
                    url:"/ajax/table.php",
                    data:$(this).attr('id'),
                    beforeSend: function()
                    {
                        if(document.push)
                        {
                            if($('#'+push.PUSH_BUTTON_ID).val() == push.ACTIVATED)
                            {
                                push.deactivatePush();
                            }
                        }

                        var pos = $('#TablePage_'+integer).offset();
                        var width = $('#tablediv').width();

                        $('#loading').html('<center><img id="loadingimg" src="'+IMAGES_URL+'Symbole/loading.gif"></center>');
                        var imgHeight = $("#loadingimg").height();

                        if(imgHeight == 0)
                        {
                            imgHeight = 100;
                        }

                        $('#loading').css('top',pos.top-imgHeight-10).css('left',pos.left+10).css('width', width-20).css("z-index", 3);

                    },
                    success: function(msg)
                    {        
                        $('#loading').html('').css("z-index", 0);
                        $('#TableBody_'+integer).slideUp(1000,function(){
                            $('#TableBody_'+integer).html(msg).slideDown(1000);
                            tabletooltip();
                            tablepager();
                            tablesort();
			    tabletime();
			    tableindex();
                            ReloadIVW();
                        })
                    }
             });
    });
}

function tabletime()
{
    /*$('.TableSort').delegate("a", 'click', function(e){
    });*/
    var tmp = 0;
    var integer;
    $('.TableTime').find("a").click
    (
        function(e)
        {
            if(tmp == 0)
            {   
                e.preventDefault();
                ET_Event.eventStart('Kursliste','Zeitraum - '+$(this).text(),'Klick');
                $(this).parent().parent().parent().parent().parent().find('td').removeClass('active');
                $(this).parent().addClass('active');
                integer = $(this).parent().parent().parent().parent().attr('id').replace(/\D+/,'');                                            
                
                if($(this).attr('id') != "false" && tmp == 0)
                {
                    $.ajax(
                    {
                            type:"POST",
                            url:"/ajax/table.php",
                            data:$(this).attr('id'),
                            beforeSend: function()
                            {
                                if(document.push)
                                {
                                    if($('#'+push.PUSH_BUTTON_ID).val() == push.ACTIVATED)
                                    {
                                        push.deactivatePush();
                                    }
                                }    

                                var pos = $('#TableTime_'+integer).offset();
                                var width = $('#tablediv').width();
                                var height = $('#TableTime_'+integer).height();

                                $('#loading').html('<center><img id="loadingimg" src="'+IMAGES_URL+'Symbole/loading.gif"></center>');
                                $('#loading').css('top',pos.top+height+10).css('left',pos.left+10).css('width', width-20).css("z-index", 3);

                            },
                            success: function(msg)
                            {          
                                    msg = msg.replace("TableBody_0", "TableBody_"+integer);
                                    msg = msg.replace("TableIndex_0", "TableIndex_"+integer);
                                    msg = msg.replace("TableTime_0", "TableTime_"+integer);
                                             
                                    $('#loading').html('').css("z-index", 0);
                                    $('#TableBody_'+integer).slideUp(1000,function(){
                                        $('#TableBody_'+integer).html(msg).slideDown(1000);
                                        tabletooltip();
                                        tablesort();
                                        tablepager();
                                        tabletime();
                                        tableindex();
                                        tmp++;
                                         })
                                    ReloadIVW();
                                    //tablesorter(1,6,0);
                                    //$('#pushList').trigger("appendCache");
                                    //$('#pushList').trigger("update");


                               
                            }
                     });
                }
            }
    });
}

function tableindex()
{
    /*$('.TableSort').delegate("a", 'click', function(e){
    });*/

    $('.TableIndex').find("a").click
    (
        function(e)
        {
            e.preventDefault();
            ET_Event.eventStart('Kursliste','Index - '+$(this).text(),'Klick');
            $(this).parent().parent().parent().parent().parent().find('td').removeClass('active');
            $(this).parent().addClass('active');
            var integer = $(this).parent().parent().parent().parent().attr('id').replace(/\D+/,'');
            
            if($(this).attr('id') != "false")
            {
                $.ajax(
                {
                        type:"POST",
                        url:"/ajax/table.php",
                        data:$(this).attr('id'),
                        beforeSend: function()
                        {
                            if(document.push)
                            {
                                if($('#'+push.PUSH_BUTTON_ID).val() == push.ACTIVATED)
                                {
                                    push.deactivatePush();
                                }
                            }    

                            var pos = $('#TableIndex_'+integer).offset();
                            var width = $('#tablediv').width();
                            var height = $('#TableIndex_'+integer).height();
 
                            $('#loading').html('<center><img id="loadingimg" src="'+IMAGES_URL+'Symbole/loading.gif"></center>');
                            $('#loading').css('top',pos.top+height+10).css('left',pos.left+10).css('width', width-20).css("z-index", 3);
                            
                        },
                        success: function(msg)
                        {                         
                            $('#loading').html('').css("z-index", 0);
                            $('#TableBody_'+integer).slideUp(1000,function(){
                                $('#TableBody_'+integer).html(msg).slideDown(1000);
                                tabletooltip();
                                tablesort();
                                tablepager();
				tabletime();
				tableindex();
				ReloadIVW();
				//tablesorter(1,6,0);
				//$('#'+"pushList").trigger("update");
				//$('#'+"pushList").trigger("appendCache");
                            })
                        }
                 });
            }
    });
}

function tabletooltip()
{
    $(".tooltipk div").tooltip({
                      track:true,
                      delay:0,
                      showURL:false,
                      fade:250
                     });

    $(".tablesorter th").tooltip({
                      track:true,
                      delay:0,
                      showURL:false,
                      fade:250
                     });

}


//Zr for logging
/*(function($) {
    $.log = function() {
       if( 'console' in window )
            console.log.apply(null, arguments);
    };
    }(jQuery));*/
