/*jslint bitwise:true*/

// Global variable

var basehtml = document.getElementsByTagName('html')[0],
    ri = false,
    ri_prevwidth = 0,
    droptimer,
    showloadertimer,
    contentholders = '#content01, #TB_ajaxContent, .tb_box02',
    require_config = {
        baseUrl: '/ipank/resp/js/',
        paths: {
            'highcharts': 'highcharts/highcharts',
            'highcharts.more': 'highcharts/highcharts-more',
            'highstock': 'highcharts/highstock',
            'history': 'jquery.history',
            'sparkline': 'jquery.sparkline',
            'swfobject': 'swfobject/swfobject',
            'swipe': 'jquery.touchSwipe.min',
            'footable': 'footable',
            'porthole': 'porthole.min',
            'jquery.extensions.datepicker': 'jquery.extensions.datepicker',
            'jquery': 'jquery',
            'jquery.column': 'jquery.column',
            'jquery.fileupload': 'upload/jquery.fileupload',
            'jquery.fileupload-process': 'upload/jquery.fileupload-process',
            'jquery.fileupload-validate': 'upload/jquery.fileupload-validate',
            'jquery.iframe-transport': 'upload/jquery.iframe-transport',
            'jquery.ui': 'jquery.ui',
            'jquery.ui.widget': 'jquery.ui.widget',
            'tablesorter': 'jquery.tablesorter',
            'bootstrap': 'bootstrap',
            'stickykit': 'jquery.sticky-kit.min'
        },
        priority: [
            'jquery',
            'jquery.ui',
            'jquery.ui.widget',
            'porthole',
            'utils',
            'highcharts',
            'highcharts.more',
            'highstock',
            'history',
            'sparkline',
            'swfobject',
            'swipe',
            'bootstrap',
            'bootstrap.multiselect',
            'footable',
            'jquery.column',
            'jquery.cookie',
            'jquery.ibcookie',
            'jquery.fileupload',
            'jquery.fileupload-process',
            'jquery.fileupload-validate',
            'jquery.iframe-transport',
            'jquery.responsiveiframe',
            'jquery.tablesorter'
        ],
        shim: {
            'history': { exports: 'hashchange' },
            'sparkline': { exports: 'sparkline' },
            'bootstrap.multiselect': { deps: ['bootstrap'] },
            'footable': { exports: 'footable' },
            'highcharts': { exports: 'Highcharts' },
            'highcharts.more': { deps: ['highcharts'] },
            'highstock': { exports: 'Highcharts' },
            'jquery': { exports: '$' },
            'jquery.cookie': { deps: ['jquery'] },
            'jquery.ibcookie': { deps: ['jquery', 'jquery.cookie'] },
            'hopscotch': { deps: ['jquery'] },
            'jquery.fileupload': { deps: ['jquery.ui', 'jquery.iframe-transport', 'jquery.fileupload-process', 'jquery.fileupload-validate'] },
            'jquery.tablesorter': { deps: ['jquery'] },
            'jquery.ui': { exports: '$', deps: ['jquery'] },
            'utils': { deps: ['jquery', 'jquery.ibcookie','jquery.ui', 'porthole'] }
        }
    },
    highchartoptions = {
        title: { text: '' },
        subtitle: { text: '' },
        chart: {
            backgroundColor: null,
            borderWidth: 1,
            borderColor: '#e6e6e6',
            plotBorderColor: '#e6e6e6',
            plotBackgroundColor: '#ffffff',
            plotBorderWidth: 1,
            shadow: false,
            plotShadow: false,
            borderRadius: '0',
            style: {
                fontFamily: '"SEB Basic Web", "Arial Narrow", "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif',
                fontSize: '11px'
            },
            events: {
                load: function () {
                    $(window).trigger('resize');
                }
            },
            spacingTop: 10,
            spacingBottom: 10,
            spacingLeft: 10,
            spacingRight: 10
        },
        //colors: ['#60cd18', '#b6e498', '#99dcea', '#0092aa', '#b16694', '#943263', '#6fa4af', '#1c7080'],
        colors: [
            '#005f71', // petrol - eluase
            '#8a1b60', // purple - söök&jook
            '#60cd18', // neoonroheline - säästmine
            '#b16694', // purple 66 - laste kulud
            '#a07ea3', // wisteria - riietus
            '#bfa8c1', // wisteria 66 - isiklikud kulud
            '#54b6c0',  // capri 66 - transport
            '#0092aa',  // capri - side
            '#1c7080', // petrol 66 - majapidamine
            '#808080',  // tumehall	- muud
            '#dfd3e0', // wisteria 33 - vaba aeg
            '#cfeff5',  // capri 33 - finants
            '#378710',  // tumeroheline - haridus
            '#e6f4dc',  // heleroheline - tervis
            '#F3F3F3'  // helehall - unsorted
             ],
        tooltip: {
            enabled: true,
            headerFormat: '<b>{point.key}</b><br/>',
            backgroundColor: '#fcffe6',
            borderColor: "#cccccc",
            borderWidth: 1,
            borderRadius: 3,
            shadow: true,
            style: {
                padding: "7px",
                paddingLeft: '10px',
                paddingRight: '10px',
                paddingTop: '6px',
                paddingBottom: '0',
                width: 'auto',
                fontSize: '13px',
                lineHeight: '16px'
            }
        },
        xAxis: {
            lineWidth: 0,
            gridLineWidth: 1,
            gridLineColor: 'rgba(0,0,0,0.1)',
            minorGridLineWidth: 1,
            minorGridLineColor: 'rgba(0,0,0,0.1)',
            tickLength: 0,
            minorTickLength: 0,
            tickInterval: null,
            minorTickInterval: null,
            showFirstLabel: false,
            showLastLabel: false,
            labels: {
                style: {
                    color: '#999999',
                    fontSize: '11px'
                },
                staggerLines: 1,
                step: 2
            },
            title: {
                text: '',
                style: {
                    fontSize: 12,
                    color: '#000000'
                }
            }
        },
        yAxis: {
            gridLineWidth: 1,
            gridLineColor: 'rgba(0,0,0,0.1)',
            minorGridLineWidth: 1,
            minorGridLineColor: 'rgba(0,0,0,0.1)',
            tickLength: 0,
            minorTickLength: 0,
            tickInterval: null,
            tickPixelInterval: '30',
            minorTickInterval: null,
            showFirstLabel: true,
            showLastLabel: false,
            offset: 0,
            labels: {
                align: 'right',
                style: {
                    color: '#999999',
                    fontSize: '11px'
                },
                staggerLines: 1,
                step: 2,
                x: -10,
                y: -2
            },
            title: {
                text: '',
                style: {
                    fontSize: 12,
                    color: '#000000'
                }
            }
        },

        legend: {
            enabled: false,
            borderWidth: 0,
            borderRadius: 0,
            itemHoverStyle: {
                color: '#333333'
            },
            symbolWidth: 10,
            symbolHeight: 10,
            symbolPadding: 10,
            itemMarginTop: 3,
            useHTML: false,
            margin: 10,
            padding: 0,
            align: 'right',
            verticalAlign: 'top',
            layout: 'vertical',
            labelFormatter: function() {
                var words = this.name.split(/[\s]+/);
                var numWordsPerLine = 4;
                var str = [];

                for (var word in words) {
                    if (word > 0 && word % numWordsPerLine == 0)
                        str.push('<br>');

                     str.push(words[word]);
                }

                return str.join(' ');
            }
        },
        navigator: {
            enabled: false,
            outlineColor: '#aaaaaa',
            outlineWidth: 1,
            maskFill: 'rgba(255,255,255,0.5)',
            series: {
                type: 'areaspline',
                color: '#000000',
                fillOpacity: 0.01,
                lineWidth: 1.5,
                lineColor: 'rgba(0,0,0,0.3)',
                shadow: false
            },
            margin: 10
        },
        plotOptions: {
            series: {
                shadow: false,
                animation: false,
                marker: {
                    enabled: false,
                    symbol: 'circle',
                    radius: 2,
                    states: {
                         hover: {
                            enabled: true,
                            radius: 4
                        }
                    }
                }
            },
            column: {
                borderWidth: 0,
                pointPadding: 0.09,
                groupPadding: 0.25
            },
            area: {
                lineWidth: 1,
                fillOpacity: 0.33
            },
            line: {
                lineWidth: 1.5
            },
            gauge: {
                dataLabels: { enabled: false },
                dial: {
                    baseLength: '10%',
                    baseWidth: '14',
                    rearLength: '0%',
                    radius: '90%',
                    borderWidth: '1',
                    borderColor: '#fff'
                },
                pivot: { radius: '6' }
            },
            pie: {
                borderWidth: 0,
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        credits: { enabled: false },
        scrollbar: {
            barBorderRadius: 7,
            barBorderWidth: 1,
            barBorderColor: '#aaaaaa',
            buttonBorderWidth: 1,
            buttonBorderColor: '#aaaaaa',
            buttonBorderRadius: 7,
            trackBorderWidth: 1,
            trackBorderRadius: 8,
            trackBorderColor: '#dddddd'
        },
        rangeSelector: {
            enabled: false,
            inputDateFormat: '%Y/%m/%d',
            inputEditDateFormat: '%Y/%m/%d',
            buttonSpacing: 3,
            buttonTheme: {
                fill: '#e6e6e6',
                stroke: '#fff',
                'stroke-width': 0,
                'height': 20,
                'padding': 1,
                r: 10,
                /*width: null,*/
                style: { color: '#000000', fontFamily: '"SEB Basic Web", "Arial Narrow", "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', fontSize: '14px' },
                states: {
                    hover: { fill: '#008296', stroke: '#ffffff', style: { color: 'white' }  },
                    select: { fill: '#666666', stroke: '#ffffff', style: { color: 'white' } }
                }
            },
            /* debug
            buttons: [
                { type: 'month', count: 1, text: '\u20021m\u2002' },
                { type: 'month', count: 3, text: '\u20023m\u2002' },
                { type: 'month', count: 6, text: '\u20026m\u2002' },
                { type: 'ytd', text: '\u2002YTD\u2002' },
                { type: 'year', count: 1, text: '\u20021y\u2002' },
                { type: 'all', text: '\u2002All data\u2002' }
            ],
            */
            inputPosition: { x: -100, y: -100 },
            inputBoxBorderColor: 'gray',
            inputBoxWidth: 124,
            inputBoxHeight: "ontouchstart" in window ? 36 : 28,
            inputStyle: { color: '#000000', fontFamily: '"SEB Basic Web", "Arial Narrow", "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', fontSize: '14px', textAlign: 'left' },
            labelStyle: { color: '#000000', fontFamily: '"SEB Basic Web", "Arial Narrow", "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', fontSize: '14px', textAlign: 'left' },
            selected: 1
        }
    },
    jqueryPreload = [],
    jqueryPreloadFn = {},
    isBadIE = '\v'=='v',
    $$$ = function (item) { if (typeof item === 'function') { jqueryPreload.push(item); } return { ready: function (cb) { jqueryPreload.push(cb); } } };

$$$.fn = jqueryPreloadFn;

/*
$$$.fn.DatePicker = function (a) {
    var t = this;
    require(['jquery.extensions.datepicker'], function () {
        $$$(t).DatePicker(a);
    });
}
*/

Date.now = Date.now || function () { return +new Date; };

// Add default config

require.config(require_config);

// Extend default config with custom config

if (typeof require_config_custom !== 'undefined') {
    require.config(require_config_custom);
}

// Basic tagging features before loading to reduce flicker

function hasClass(ele, cls) { return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); }
function addClass(ele, cls) { if (!hasClass(ele, cls)) ele.className += " " + cls; }
function removeClass(ele, cls) { if (hasClass(ele, cls)) { var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'); ele.className = ele.className.replace(reg, ' '); } }

addClass(basehtml, 'hasjs');
removeClass(basehtml, 'nojs');
if ("ontouchstart" in window) {
    addClass(basehtml, 'hastouch');
    removeClass(basehtml, 'notouch');
} else {
    addClass(basehtml, 'notouch');
    removeClass(basehtml, 'hastouch');
}
if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) { addClass(basehtml, 'ios'); }
if (!!navigator.userAgent.match(/(Android (2.0|2.1))|(Nokia)|(Opera (Mini|Mobi))|(w(eb)?OSBrowser)|(UCWEB)|(Windows Phone)|(XBLWP)|(ZuneWP)/)) { addClass(basehtml, 'nofontface'); }
if (!isBadIE) { addClass(basehtml, 'modern'); }

// Start loading


    $.ajaxSetup({ cache: false });

    $(document).ready(function(){

        // Call this when general frame should be ready (once per general frame)
        initFrame();
        // Call this when content blocks should be ready (may be multiple, also with optional DOM chunk to process)
        initContent();

        // Restore preloaded jquery calls from HTML
        $.each(jqueryPreloadFn, function (a, b) { $$$.fn[a] = b; });
        $.each(jqueryPreload, function (a, b) { b(); });
    });