/*
 * Terminkalender
 *
 * author:  Christoph Schuessler (schreib@herr-schuessler.de)
 *
 */


////////////////////////////////////////////////////////////////////////////////
//
// firebug console output
// @param text String the debug message
// @param type String the message type [error | info | warn] (optional)
//
////////////////////////////////////////////////////////////////////////////////
function debug(text,type) {
    if (window.console && window.console.log) {
        if(type === 'error' && window.console.error) {
            window.console.error(text);
        }
        else if(type === 'info' && window.console.info) {
            window.console.info(text);
        }
        else if(type === 'warn' && window.console.warn) {
            window.console.warn(text);
        }
        else {
            window.console.log(text);
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
//
// domready
//
////////////////////////////////////////////////////////////////////////////////
jQuery.noConflict();
jQuery(document).ready(function($) {
    
    // calendar setup
    //--------------------------------------------------------------------------
    $(".cal-trigger").tooltip({
    
        // tooltip options
        position:   'top center',
        relative:   true,
        offset:     [4, 2],
        effect:     'fade',
        events: { 
            def: 'click, mouseout'
        },
        
        // load events into tooltip 
        onBeforeShow: function() {
        
            // the selected date
            var calDay = this.getTrigger().text();
            
            // use string data format to send a GET-request
            var data = 'd=' + calDay + '&m=' + calMonth +'&y=' + calYear;
            var dateStr = calDay + '. ' + calMonth + '. ' + calYear;
            
            // the current tip
            var tip = this.getTip();
            
            tip.append( '<div class="cal-tooltip-head">' +
                        '   <span class="date">' + dateStr + '</span>' +
                        '   <span class="close">' +
                        '       <a href="#" class="close" title="' + calClose + '">' + calClose + '</a>' +
                        '   </span>' +
                        '</div>' +
                        '<div class="cal-tooltip-body">' +
                        '   <div class="load scroll" />' +
                        '</div>');
            
            // add event listener to close tip
            tip.find('.close').click(function(e){
                e.preventDefault();
                tip.empty();
                tip.hide();
            });
            
            // remotely load events for selected date
            $.getJSON('termine-json-generator.php',data,
                function(responseData,textStatus){
                    
                    //if call was succesfull
                    if(textStatus == 'success' || textStatus == 'notmodified') {
                        
                        //remove loading graphic once call has completed
                        tip.find('.load').removeClass('load');
                        
                        //create and append list with return values to scroll area
                        var list =  $('<ul />').appendTo(tip.find('.scroll'));
                        
                        // iterate through JSON array
                        $.each(responseData.events, function(i,event){
                            
                            //insert list items
                            if (event.url) {
                                list.append('<li><a href="' + event.url + '"><span class="title">' + event.title + '</span></a><br /><span class="mute smaller">' + event.info + '</span></li>');
                            }
                            else {
                                list.append('<li><span class="title">' + event.title + '</span><br /><span class="mute smaller">' + event.info + '</span></li>');
                            }
                        });
                    }
                    // if call was not successfull, hide tip
                    else {
                        debug(textStatus);
                        tip.hide();
                    } 
                    
                }
            );
        },
        onHide: function() {
            this.getTip().empty();
        }
    }).click(function(e){
        //prevent browser from following links
        e.preventDefault();
    });
    
});


