

/**
 * produced by BTD Newmedia GmbH - All rights reserved -
 * http://www.btd.de - info@btd.de
 */

var CALENDAR_EVENTS = {};
var CALENDAR_CONFIG = {
    'img': {
        'birthday': '/images/calendar/cal_birthday.png',
        'match': '/images/calendar/cal_match.png',
        'several': '/images/calendar/cal_birthday_match.png'
    }
};

$(function() {


    var showTooltip = function(ctext, offset, size){

        var breiteFenster = $(window).width() - 10;//document.width;
        var weggescrollt = $(window).scrollLeft();

        var breiteToolTip;
        var vonLinks = offset['left'];
        var blase;
        vonLinks -= weggescrollt;

        //Entscheiden welche Sprechblase angezeigt wird
        if(size == "small"){
            breiteToolTip = 166;
            if(breiteFenster < vonLinks + breiteToolTip){
                blase = "small-links";
            }else{
                blase = "small-rechts";
            }
        }else{
            breiteToolTip = 205;
            if(breiteFenster < vonLinks + breiteToolTip){
                blase = "big-links";
            }else{
                blase = "big-rechts";
            }
        }
        
        switch(blase){
            case "small-rechts":
                                $('#datepickerTooltip').css('width', '170px');
                                $('#datepickerTooltip').css('height', '83px');
                                $('#datepickerTooltip').css('left', (offset['left'] - 23) + 'px');
                                $('#datepickerTooltip').css('top', (offset['top'] - 90) + 'px');
                                $('#datepickerTooltip').css('background-image','url(/images/calendar/cal_tooltip.png)');
                                $('#datepickerTooltip').removeClass('datepickerTooltip_text_big');
                                break;
            case "small-links":
                                $('#datepickerTooltip').css('width', '170px');
                                $('#datepickerTooltip').css('height', '83px');
                                $('#datepickerTooltip').css('left', (offset['left'] - 165 ) + 'px');
                                $('#datepickerTooltip').css('top', (offset['top'] - 94) + 'px');
                                $('#datepickerTooltip').css('background-image','url(/images/calendar/cal_tooltip_links.png)');
                                $('#datepickerTooltip').removeClass('datepickerTooltip_text_big');
                                break;
            case "big-rechts":
                                $('#datepickerTooltip').css('width', '208px');
                                $('#datepickerTooltip').css('height', '108px');
                                $('#datepickerTooltip').css('left', (offset['left'] - 23) + 'px');
                                $('#datepickerTooltip').css('top', (offset['top'] - 116) + 'px');
                                $('#datepickerTooltip').css('background-image','url(/images/calendar/cal_tooltip_big.png)');
                                $('#datepickerTooltip').addClass('datepickerTooltip_text_big');
                                break;
            case "big-links":
                                $('#datepickerTooltip').css('width', '208px');
                                $('#datepickerTooltip').css('height', '108px');
                                $('#datepickerTooltip').css('left', (offset['left'] - 200 ) + 'px');
                                $('#datepickerTooltip').css('top', (offset['top'] - 120) + 'px');
                                $('#datepickerTooltip').css('background-image','url(/images/calendar/cal_tooltip_links_big.png)');
                                $('#datepickerTooltip').addClass('datepickerTooltip_text_big');
                                break;
        }


        $('#datepickerTooltip').html(ctext);
        $('#datepickerTooltip').show();
    }

    var hideTooltip = function() {$('#datepickerTooltip').hide();}

    // Renders a certain day
    var eventCallback = function($td, thisDate, month, year)
    {
        m0 = (thisDate.getMonth() + 1 < 10 ? "0" : "") + (thisDate.getMonth() + 1);
        d0 = (thisDate.getDate() < 10 ? "0" : "") + thisDate.getDate();
        var daykey = year + '' + m0 + '' + d0;
        if (typeof(CALENDAR_EVENTS[daykey]) != 'undefined') {

            var ctype = '';
            var ctext = '';
            var conclick = '';
            var ctarget = '_self';
            var countlines = 0;
            var text = '';
            var size = 'big';
            var j = 0;
            for(i = 0; i< CALENDAR_EVENTS[daykey].length; i++){ //count all rows of outputtext
                countlines = countlines+ parseInt(CALENDAR_EVENTS[daykey][i]['lines']);
            }

            ctext += "<div class='datepickerTooltip_text'>";

            for (i = 0; i < CALENDAR_EVENTS[daykey].length; i++) {

                if(CALENDAR_EVENTS[daykey].length == 1 && countlines == 5){countlines = 4;}

                // Wenn mehr als ein Event am Tag dann wird eine horizontale Linie eingefügt.
                if (i > 0) {
                        ctext += '<div class="horiRule'+size + '"></div>';
                }

                //If there are more than 4 lines of text the shorttext will be shown
                if(countlines >  4 && CALENDAR_EVENTS[daykey][i]['type'] == 'birthday'  ){
                    text += '<div class="textTitle">Geburtstag</div>';
                    text += '<table class ="gebTable">';
                    text += '<tr><td class="tdlinks">Name:</td><td class="tdrechts">' + CALENDAR_EVENTS[daykey][i]['name'] + '</td></tr>';
                    text += '<tr><td class="tdlinks">Geb.datum:</td><td class="tdrechts">' + CALENDAR_EVENTS[daykey][i]['gebdate'] + '</td></tr>';
                    text += '</table>';
                    size = 'big';
                }else if(countlines > 4 && CALENDAR_EVENTS[daykey][i]['type'] != 'birthday'){
                    // If there is a headline add it
                    if(CALENDAR_EVENTS[daykey][i]['headline'] != ''){
                        text += '<div class="textTitle">' + CALENDAR_EVENTS[daykey][i]['headline'] + '</div>';
                    }
                    //If there is a location add it
                    text += '<div class="calTextCont">' + CALENDAR_EVENTS[daykey][i]['text'];
                    if(CALENDAR_EVENTS[daykey][i]['location'] != ''){
                        text += '<br />' + CALENDAR_EVENTS[daykey][i]['location'];
                    }
                    text += '</div>';
                    size = 'big';                }

                //If there are less than 4 lines of text the long text will be shown
                if(countlines <= 4 && CALENDAR_EVENTS[daykey][i]['type'] == 'birthday' ){                        
                        text += '<div class="textTitle">Geburtstag</div>';
                        text += '<div class="gebbild"><img src="'+CALENDAR_EVENTS[daykey][i]['image']+'" alt="" height="65px;"/></div>';
                        text += '<table class="gebTable">';
                        text += '<tr><td class="tdlinks">Name:</td><td class="tdrechts">' + CALENDAR_EVENTS[daykey][i]['name'] + '</td></tr>';
                        text += '<tr><td class="tdlinks">Geb.datum:</td><td class="tdrechts">' + CALENDAR_EVENTS[daykey][i]['gebdate'] + '</td></tr>';
                        text += '<tr><td class="tdlinks">Position:</td><td class="tdrechts">' + CALENDAR_EVENTS[daykey][i]['pos'] + '</td></tr>';
                        // If there is a shirt number, add it
                        if(CALENDAR_EVENTS[daykey][i]['rnum'] != ''){ 
                            text += '<tr><td class="tdlinks">R&uuml;ckennr.:</td><td class="tdrechts">' + CALENDAR_EVENTS[daykey][i]['rnum'] + '</td></tr>';
                        }
                        text += '</table>';
                        size = 'big';
                }else if(countlines <= 4 && CALENDAR_EVENTS[daykey][i]['type'] != 'birthday'){
                    if(CALENDAR_EVENTS[daykey][i]['headline'] != ''){
                        text += '<div class="textTitle">' + CALENDAR_EVENTS[daykey][i]['headline'] + '</div>';
                    }
                    text += '<div class="calTextCont">' + CALENDAR_EVENTS[daykey][i]['text'];
                    if(CALENDAR_EVENTS[daykey][i][location] != ''){
                        text += '<br />' + CALENDAR_EVENTS[daykey][i]['location'];
                    }
                    text += '</div>';
                    size = 'small';
                }
                
                ctext += text;
                text = '';

                if (ctype == '') {
                    ctype = CALENDAR_EVENTS[daykey][i]['type'];
                } else if (ctype != CALENDAR_EVENTS[daykey][i]['type']) {
                    ctype = 'several';
                }


                if(CALENDAR_EVENTS[daykey][i]['url'] != null){
                    
                    if(CALENDAR_EVENTS[daykey][i]['url'] != ''){

                        //event mit url
                        conclick += "window.open('" + CALENDAR_EVENTS[daykey][i]['url'] + "', '' , '');";
                        ctarget = '_blank';//TODO: funktioniert noch nicht richtig / hat keine wirkung
                        //TODO: herausfinden, ob url mit http:// anfängt.
                        // wenn url mit http:// anfängt dann neues tab/fenster ansonten im selben fenster


                    }else{
                    //event ohne url
                    conclick += CALENDAR_EVENTS[daykey][i]['js'];
                }
                }else{
                    //hier muss eigentlich popup
                conclick += CALENDAR_EVENTS[daykey][i]['js'];

                }

            }

            ctext += "</div>";
            $td.html('<img src="' + CALENDAR_CONFIG['img'][ctype] + '" onclick="' + conclick + '" target="' + ctarget + '" />');
            // $td.attr('onclick', conclick);
            $td.bind('mouseenter', function() {showTooltip(ctext, $td.offset(), size);});
            $td.bind('mouseleave', function() {hideTooltip();});
        }
    };
    // Called when the user changes the month/year
    var eventMonthChanged = function(e, month, year)
    {
        u = '/misc/events/events.php?&lang=' + LANG +'&month=' + (month + 1) + '&year=' + year;
        $.getJSON(u , function(data) {
            CALENDAR_EVENTS = data;
            $.event._dpCache[$('.turn-me-into-datepicker')[0]._dpId]._rerenderCalendar()
        });
    };

    $('.turn-me-into-datepicker')
    .datePicker({
        inline: true,
        renderCallback: eventCallback,
        startDate: '01/01/2006'
    })
//     .bind(
//         'dateSelected',
//         function(e, selectedDate, $td) { console.log('You selected ' + selectedDate); }
//     )
    .bind(
        'dpMonthChanged',
        eventMonthChanged
    );
    d = new Date();
    eventMonthChanged(null, d.getMonth(), d.getFullYear());
});

