/*
 * Copyright(c) 2010
 *
 */

// create application
fds.abgTooltip = function() {

    // do NOT access DOM from here; elements don't exist yet

    // private variables

    // private functions
    function viewport() {
        return {
            x: $(window).scrollLeft(),
            y: $(window).scrollTop(),
            cx: $(window).width(),
            cy: $(window).height()
        };
    }

    function renderAbgTooltip(event){

        var hideDelay = 100; // 500;
        var currentID;
        var hideTimer = null;
        var default_errortext = "Der Hilfetext konnte nicht geladen werden, bitte wenden Sie sich bei Fragen an den Helpdesk.";

        // One instance that's reused to show info for the current helptext
        var container = $('<span id="abgTooltipContainer">'
            + '<dl class="abgeordneten_tooltip">'
            + '   <dt id="abgImage"></dt>'
            + '   <dd id="abgContent"></dd>'
            + '</dl>'
            + '</span>'
        );

        $('body').append(container);
        $('#abgTooltipContainer').css('display', 'none');

        $('.tooltipTrigger').live('mouseover', function(event) {

            currentID = this.id;

            // format of 'rel' tag: project_alias,module_alias,action,field,custom1,custom2
            // need to be semicolon separated
            var settings = $(this).attr('rel');

            // Make sure to pass all the required parameters, custom1 and custom2 are optional
            var url_to_call = "/ltnds/php/abg.php?id="+settings;

            if(hideTimer) clearTimeout(hideTimer);

            $('#abgContent').html('&nbsp;');

            $.getJSON(url_to_call,
                function(data){

                    var success = data.success;
                    var content = data.content;
                    var image = data.image;
                    var guid = data.guid;

                    if(success){
                        $('#abgTooltipContainer').css('display', 'block');

                        var width = $('#abgTooltipContainer').outerWidth();
                        var height = $('#abgTooltipContainer').outerHeight();
                        var left = event.pageX  + 15;
                        var top = event.pageY - 15;

                        var v = viewport();
                        // check horizontal position
                        if (v.x + v.cx < left + width) {
                            left -= width + 20;
                        }
                        // check vertical position
                        if (v.y + v.cy < top + height) {
                            top -= height + 20;
                        }

                        $('#abgTooltipContainer').css({'left': left + 'px', 'top': top + 'px'});

                        $('#abgImage').html('<img src="' + image + '"/>');
                        $('#abgContent').html(content);
                    }
                    else {
                        $('#abgContent').html('<span>'+default_errortext+'</span>');
                    }
                });
        });


        $('.tooltipTrigger').live('mouseout', function() {
            if(hideTimer) clearTimeout(hideTimer);

            hideTimer = setTimeout(function() {
                $('#abgTooltipContainer').css('display', 'none');
            }, hideDelay);
        });

        // Allow mouse over of details without hiding details
        $('#abgTooltipContainer').mouseover(function() {
            if(hideTimer) clearTimeout(hideTimer);
        });

        // Hide after mouseout
        $('#abgTooltipContainer').mouseout(function() {
            if(hideTimer) clearTimeout(hideTimer);

            hideTimer = setTimeout(function() {
                $('#abgTooltipContainer').css('display', 'none');
            }, hideDelay);
        });
    }

    // public space
    return {
        // public properties, e.g. strings to translate

        // public methods
        init : function() {
            this.abgTooltip();
        },
        abgTooltip: function(){
            renderAbgTooltip();
        }
    }
}(); // end of app

// end of file
