/*
 * Create namespaces for wyprawy scripts
 */
window.blirt = function() {};
blirt.page = function() {};

$.fn.new_window = function(options) {
    return this.each(function() {
       $(this).click(function() {
            window.open($(this).attr('href'));
            return false;
       });
    });
};

(function(m) {

    m.Base = function() {
        //
    };

    m.Contact = function() {
        $('#p > a.map').new_window();
    };

    m.Quality = function() {
        $('#subcnt a').new_window();
    };

    m.Stock = function() {
        var stock_idx = 'BRE';
        var $stock = $('#stock');
        var chart_data = [];
        
        var chart_opts = {
            grid: {
                color: "#fff",
                tickColor: "#555"
            },
            xaxis: { 
                mode: "time",
                monthNames: ["sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paź", "lis", "gru"]
            },
            points: {
                show: true
            },
            lines: {
                show: true
            },
            colors: ["#51AE07"]
        };

        function update_stock_info() {
            $.getJSON('/static/notowania/aktualne_notowanie.json?v='+(new Date()).getTime(),
                function(data) {
                    $stock.find('tr:eq(0) td:eq(1)').text(data.ostatni_kurs + ' zł');
                    $stock.find('tr:eq(1) td:eq(1)').text(data.kurs_odniesienia);
                    $stock.find('tr:eq(2) td:eq(1)').text(data.kurs_otwarcia);
                    $stock.find('tr:eq(3) td:eq(1)').text(data.max_min);
                    $stock.find('tr:eq(4) td:eq(1)').text(data.wolumen);
                    $stock.find('tr:eq(5) td:eq(1)').text(data.wartosc_obrotu);
                });

            $.getJSON('/static/notowania/aktualne_notowanie2.json?v='+(new Date()).getTime(),
                function(data) {
                    $stock.find('tr:eq(6) td:eq(1)').text(data.kapitalizacja );
                    $stock.find('tr:eq(7) td:eq(1)').text(data.free_float);
                });


        }
        
        function initialize_chart() {
            
            $.get('/static/notowania/notowania.csv', function(data) {
                var lines = data.split('\n');
                $.each(lines, function(idx, val) {
                    if (val.length != 0) {
                        var tab = val.split(',');
                        chart_data.push([(new Date(tab[0])).getTime(), Number(tab[1])]);
                    }
               });
               
               setup_chart_navigation();
               draw_chart(chart_opts);
            });
        }

        function draw_chart(opts) {
            $.plot($('#chart'), [ chart_data ], opts);
        }

        function setup_chart_navigation() {
            var $navi = $('#navi');

            $navi
                .find('.all').click(function() {
                    var opts = $.extend({}, chart_opts);
                    opts.xaxis.min = null;
                    draw_chart(opts);
                    return false;
                })
                .end().find('.year').click(function() {
                    var opts = $.extend({}, chart_opts);
                    opts.xaxis.min = new Date((new Date()).getTime() - (1000 * 3600 * 24 * 365))
                    draw_chart(opts);
                    return false;
                })
                .end().find('.three').click(function() {
                    var opts = $.extend({}, chart_opts);
                    opts.xaxis.min = new Date((new Date()).getTime() - (1000 * 3600 * 24 * 91))
                    draw_chart(opts);
                    return false;
                })
                .end().find('.month').click(function() {
                    var opts = $.extend({}, chart_opts);
                    opts.xaxis.min = new Date((new Date()).getTime() - (1000 * 3600 * 24 * 30))
                    draw_chart(opts);
                    return false;
                });

        }

        function setup() {
            update_stock_info();
            setInterval(update_stock_info, 1000 * 60 * 15);
            initialize_chart();
        }

        setup();
    }

})(blirt.page);


