var timerAjax = null;

function getDay( day, obj ){
    $('#nologin').hide();
    co = 0;
    //link wiecej
    link = linkpattern.replace('%id%', $(obj).attr('cday'));
    $('#urlday').attr('href', link);

    //obecny dzien
    $('.dayM').children('td').removeClass('today');
    $(obj).parent('td').addClass('today');
    $('.itemRow').remove();
    $('#brakWyd').hide();

    //wydarzenia dla dnia
    for(var c in wydarzenia){
        if( wydarzenia[c][0] == day ){
            eventTitle = eventpattern.replace(/%event_title%/i, wydarzenia[c][1] );
            eventUrl = eventTitle.replace(/%event_url%/i, wydarzenia[c][2] );
            time = eventUrl.replace(/%time%/i, wydarzenia[c][3] );
            rate = time.replace(/%cplayers%/i, wydarzenia[c][4] );
            ico = rate.replace(/%pokertexas%/i, wydarzenia[c][5] );
            
            if( wydarzenia[c][8] != "" && wydarzenia[c][8] != null){
                pkoeromm = roompattern.replace(/%room_url%/i, wydarzenia[c][7]);
                pkoeromm = pkoeromm.replace(/%room_title%/i, wydarzenia[c][8]);
                pkoeromm = pkoeromm.replace(/%room_icon%/i, wydarzenia[c][6]);
                
                out = ico.replace(/%room%/i, pkoeromm);
            }else{
                out = ico.replace(/%room%/i, '');
            }
            
            $('#brakWyd').before(out);
            co = 1;
        }
    }

    if( co == 0 ){
        $('#brakWyd').show();
    }
    $('#WydIndicator').hide();
    return false;
}


function getDayEvents( day, cday ){
    co = 0;
    //link wiecej
    link = linkpattern.replace('%id%', cday );
    $('#urlday').attr('href', link);

    //obecny dzien
    //$('.dayM').children('td').removeClass('today');
    $('.itemRow').remove();

    //wydarzenia dla dnia
    for(var c in wydarzenia){
        if( wydarzenia[c][0] == day ){
            eventTitle = eventpattern.replace(/%event_title%/i, wydarzenia[c][1] );
            eventUrl = eventTitle.replace(/%event_url%/i, wydarzenia[c][2] );
            time = eventUrl.replace(/%time%/i, wydarzenia[c][3] );
            rate = time.replace(/%cplayers%/i, wydarzenia[c][4] );
            ico = rate.replace(/%pokertexas%/i, wydarzenia[c][5] );
            if( wydarzenia[c][8] != "" && wydarzenia[c][8] != null){
                pkoeromm = roompattern.replace(/%room_url%/i, wydarzenia[c][7]);
                pkoeromm = pkoeromm.replace(/%room_title%/i, wydarzenia[c][8]);
                pkoeromm = pkoeromm.replace(/%room_icon%/i, wydarzenia[c][6]);
                
                out = ico.replace(/%room%/i, pkoeromm);
            }else{
                out = ico.replace(/%room%/i, '');
            }
            
            $('#brakWyd').before(out);
            co = 1;
        }
    }

    if( co == 0 ){
        $('#brakWyd').show();
    }
    $('#WydIndicator').hide();

    return false;
}

function prevWeek(){
    var now = new Date();
    currweek--;
    $('.itemRow').remove();
    $('#nologin').hide();
    $('#brakWyd').hide();
    $('#WydIndicator').show();

    if( currweek == 0 ){
        currmonth--;
        year = now.getFullYear();
        month = now.getMonth();
        now.setFullYear(now.getFullYear(), now.getMonth() + currmonth + 1, 1 - 1);
        currweek = tydzienMiesiaca( now );
        now.setFullYear(year, month + currmonth, now.getDate());
        weekRepaint( now );
    }else{
        now.setFullYear(now.getFullYear(), now.getMonth() + currmonth, 1 + (currweek -1) * 7);
        weekRepaint( now );
    }

}

function nextWeek(){
    var now = new Date();
    var cnow = new Date();
    currweek++;
    $('.itemRow').remove();
    $('#nologin').hide();
    $('#brakWyd').hide();
    $('#WydIndicator').show();
    
    //obecny miesiac
    cnow.setMonth(cnow.getMonth() + currmonth +1, 0);
    
    if( currweek <= tydzienMiesiaca(cnow) ){
        now.setFullYear(now.getFullYear(), now.getMonth() + currmonth, 1 + (currweek -1) * 7);
        now.setDate(now.getDate() - dayofweek(now.getDay()));
        //pobieranie danych tygodnia
        weekRepaint( now );
    }else{
        //nowy miesiac pobieranie informacji ( polecane )
        currmonth++;
        currweek = 1;
        cnow.setMonth(cnow.getMonth() + 1, 1);
        weekRepaint( cnow );
    }

}
//generowanie widoku
function weekRepaint( today ){
    getCalendar(currmonth, currweek);

    var now = new Date();
    now.setFullYear(now.getFullYear(), now.getMonth(), now.getDate());
    
    $('#weeknum').html( "0" + currweek );
    $('#monthnum').html( miesiace[today.getMonth()].toUpperCase() + " " + today.getFullYear() );
    month = today.getMonth();

    //today ustawiony na poniedzialek
    today.setDate(today.getDate() - dayofweek(today.getDay()) );
    $('.dayM').children('td').removeClass('cur');
    //różnica
    var dni;
    var czas = today.getTime() - now.getTime();
    dni = Math.round(czas/(1000*60*60*24));
    

    for(var i = 1; i < 8; ++i ){
        $('.dayM').children('td').eq(i).html( '<a onclick="return getDay(\'' + today.getDate() +'\', this);" cday="' + dni + '" href="#">' + today.getDate() + '</a>');
        if( today.getMonth() == month )
            $('.dayM').children('td').eq(i).addClass('cur');
        today.setDate( today.getDate() + 1 );
        dni++;
    }
}

var miesiace = [
        'Styczeń',
        'Luty',
        'Marzec',
        'Kwiecień',
        'Maj',
        'Czerwiec',
        'Lipiec',
        'Sierpień',
        'Wrzesień',
        'Październik',
        'Listopad',
        'Grudzień'
];

function dayofweek(dow){
    dow--;
    if( dow == -1 )
        return 6;
    return dow;
}

function tydzienMiesiaca( testdate ){
    var now = new Date();
    var tydzien;
    now.setFullYear(testdate.getFullYear(), testdate.getMonth(), 1);
    roznica_dni = 7 -  dayofweek( now.getDay() );

    if (testdate.getDate() <= roznica_dni)
          tydzien = 1;


    if ((testdate.getDate() > roznica_dni) && (testdate.getDate() <= roznica_dni + 7 ))
          tydzien = 2;


    if ((testdate.getDate() > (roznica_dni + 7)) && (testdate.getDate() <= roznica_dni + 7 * 2 ))
          tydzien = 3;


    if ((testdate.getDate() > (roznica_dni + 7 * 2)) && (testdate.getDate() <= roznica_dni + 7 * 3 ))
          tydzien = 4;


    if ((testdate.getDate() > (roznica_dni + 7 * 3)) && (testdate.getDate() <= roznica_dni + 7 * 4 ))
          tydzien = 5;


    if ((testdate.getDate() > (roznica_dni + 7 * 4)) && (testdate.getDate() <= roznica_dni + 7 * 5 ))
          tydzien = 6;

    return tydzien;
}

function getCalendar( mon, week ){
    //zatrzymanie czasu
    if(timerAjax != null){
        clearTimeout(timerAjax);
    }

    timerAjax = setTimeout("getCalendarData(" + mon + ", " + week + ")", 300);
}

function getCalendarData(mon, week){
    $.ajax({
            type		: "GET",
            cache       : true,
            dataType    : 'json',
            url		: '?type=2316',
            data        : {
                'month' : mon,
                'week'  : week,
                'my'    : $('#myEvent').val()
            },
            success: function(data) {
                if( mon == currmonth && week == currweek ){
                    wydarzenia = data;
                    getDayEvents( $('.dayM').children('td.today').children('a').html(),  $('.dayM').children('td.today').children('a').attr('cday'));
                }
            }
        });
}


function eventChange( type, obj ){

    if( isLogin == 0 ){
        if( type == 1){
            $('#nologin').show();
            $('.itemRow').remove();
            $('#brakWyd').hide();
            return;
        }else{
            $('#nologin').hide();
        }
    }


    $('#WydIndicator').show();
    ulobj = $(obj).parents('li').parents('ul');
    ulobj.children('li').removeClass('act');
    ulobj.children('li').removeClass('actFirst');
    if( type == 1 ){
        $('#myEvent').val(1);
        ulobj.children('li:eq(1)').addClass('act');
    }else{
        $('#myEvent').val(0);
        ulobj.children('li:eq(0)').addClass('actFirst');
    }

    var now = new Date();
    $('.itemRow').remove();
    $('#brakWyd').hide();

    if( currweek == 0 ){
        currmonth--;
        year = now.getFullYear();
        month = now.getMonth();
        now.setFullYear(now.getFullYear(), now.getMonth() + currmonth + 1, 1 - 1);
        currweek = tydzienMiesiaca( now );
        now.setFullYear(year, month + currmonth, 1 + (currweek -1) * 7);
        weekRepaint( now );
    }else{
        now.setFullYear(now.getFullYear(), now.getMonth() + currmonth, 1 + (currweek -1) * 7);
        weekRepaint( now );
    }

}
