var G = {};
G.plotOptions = {
    colors: ["#92c7e7", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"],
    points: {show: true},
    lines: {show: true},
    legend: {position: 'nw', margin: 2},
    grid: {hoverable: true, borderColor: '#daf0fd', tickColor: '#daf0fd'},
    xaxis: {
        mode:'time',
        timeformat: '%d.%m'
    },
    yaxis: {
        invert:true,
        transform: function(x) { return -x;},
        inverseTransform: function(x) { return -x;},
        autoscaleMargin: 0.05,
        tickDecimals: 0
    }
};

(function($) {
    
    var options = {
        _tooltip: {
            content: function(plot, x, y) {
                return x + ': ' + y;
            },
            x: 10,
            y: 10
        }
    };

    function showTooltip(x, y, contents, opt) {
        $('<div id="tooltip">' + contents + '</div>').css( {
            position: 'absolute',
            display: 'none',
            top: y,
            left: x,
            border: '1px solid #fdd',
            padding: '2px',
            'background-color': '#fee',
            opacity: 0.80
        }).appendTo("body").show();
    }

    var previousPoint;

    function init(plot) {
        function bindTooltip() {
            var o = plot.getOptions();
            $.extend(true, o._tooltip, o.tooltip);
            if (!o.grid.hoverable) {
                alert('Tooltip plugin: Grid not hoverable!');
                return;
            }
            $(plot.getPlaceholder()).bind("plothover", function (event, pos, item) {
                if (item) {
                    if (previousPoint != item.datapoint) {
                        previousPoint = item.datapoint;
                        $("#tooltip").remove();
                        var x = item.datapoint[0],
                            y = item.datapoint[1];
                        showTooltip(item.pageX + o._tooltip.x, item.pageY + o._tooltip.y, o._tooltip.content(plot, x, y), o);
                    }
                } else {
                    $("#tooltip").remove();
                    previousPoint = null;
                }
            });
        }
        plot.hooks.bindEvents.push(bindTooltip);
    }

    $.plot.plugins.push({
        init: init,
        options: options,
        name: 'tooltip',
        version: '0.1'
    });

    G.phraseTableSorter = function() {
        try {
            var opt = {
                sortList: [($.cookie('phraseSort') || '0,0').split(',')],
                headers: {4: {sorter: false}},
                textExtraction: function(node) {
                    return $(node).metadata().sortValue || node.innerHTML;
                },
                widgets: ['zebra'],
                widgetZebra: {css: ['', 'parzysty']}
            };
            $('#phrase-list').tablesorter(opt).bind('sortEnd', function() {
                $.cookie('phraseSort', this.config.sortList.join(','));
            });
        } catch(e) {}
    }
    window.fbAsyncInit = function() {
        FB.init({
            appId: G.facebookId,
            status: true,
            cookie: true,
            xfbml: true
        });
    };
    function fbInit() {
        $('<script>', {
            async: true, 
            src: document.location.protocol + '//connect.facebook.net/pl_PL/all.js'
        }).appendTo('#fb-root');
        fbInit = function(){};
    }
    G.fbFanBox = function(el, opt) {
        opt = opt || {};
        opt.profile_id = G.facebookId;
        var c = '';
        for (o in opt) {
            c += o + '="' + opt[o] + '" '
        }
        var xbml = "<fb:fan " + c + ' scrolling="no"></fb:fan>';
        $(el).append(xbml);
        fbInit();
    }
})(jQuery);


