
var OTOS = window.OTOS || {};
OTOS.Statistics = function () {

    return {

        /**
         * The height of the chart-element compared to its width
         */
        CHART_SIZE_FACTOR: 0.8,


        /**
         * Displays only the chosen statistics in the content area. Removes all
         * the other elements from the content area before creating the statistics.
         *
         * @param   {string}    serverAddress   The server address of the image area
         * @param   {string}    camId           The camera id the area belongs to
         * @param   {int}       areaId          The id of the image area
         */
        showStatisticsOnly: function (serverAddress, camId, areaId) {
            var newStatistics = new Statistics(serverAddress, camId, areaId, $(CONTENT_ELEMENT_ID));
            newStatistics.show();
        },


        /**
         * Creates or removes the statistics (image area) element.
         *
         * @param   {string}    serverAddress   The address of the server
         * @param   {string}    camId           The id of the camera
         * @param   {int}       areaId          The id of the image area
         * @param   {boolean}   update          Whether to update the window.location.hash or not
         */
        statisticsInfo: function (serverAddress, camId, areaId, update) {

            var newStatistics = new Statistics(serverAddress, camId, areaId, $(CONTENT_ELEMENT_ID));

            newStatistics.showInfo();

            if (update) {
                OTOS.Main.updateHash();
            } // if

        },


        /**
         * Updates the iframes in the content area. If the serverAddress, camId and
         * the areaId variables are set, only the selected iframe (the one with the
         * serverAddress, camId and areaId) will be updated. Otherwise every iframe
         * will be updated.
         *
         * @param   {string}    serverAddress   The ip-address of the server
         * @param   {string}    camId           The id of the camera
         * @param   {int}       areaId          The id of the area
         */
        refreshStatistics: function (serverAddress, camId, areaId) {

            var statType        = OTOS.ElementConfig.STAT_TYPE,
                chartType       = OTOS.ElementConfig.CHART_TYPE,
                parameters      = OTOS.Navigation.getStatParameters(),
                statisticsList  = [];

            // statistics type 3 means realtime statistics
            var filename    = (statType == 3)
                                ? 'realtimemeasurements.php'
                                : ((parameters['period'].indexOf('PRED') != -1) ? 'prediction.php'
                                                                                : 'measurements.php'),
                width       = OTOS.ElementManager.getStatWidth();

            // if the function parameters are set, only the selected iframe will be updated
            if ((serverAddress != null) && (camId != null) && (areaId != null)) {
                statisticsList.push($(AREA_ELEMENT_ID_PREFIX + '_' + serverAddress + '_' + camId + '_' + areaId));
            } else {
                statisticsList = document.getElementsByClassName(AREA_ELEMENT_CLASS);
            } // else

            // go through all the selected iframes and update their contents
            for (var i = 0; i < statisticsList.length; i++) {

                var idString        = statisticsList[i].id,
                    serverAddress   = idString.split('_')[1],
                    cameraId        = idString.split('_')[2],
                    areaId          = idString.split('_')[3],
                    myStatistics    = new Statistics(serverAddress, cameraId, areaId, null),
                    iframe          = $('iframe_' + serverAddress + '_' + cameraId + '_' + areaId);

                myStatistics.updateChart(iframe, width, filename, chartType, statType, parameters);

            } // for

        },


        /**
         * Changes the chart type in the iframes. Function does not refresh the whole
         * document in the frame, but only updates the flashmovie.
         */
        changeChartType: function () {

            var divContent          = $(CONTENT_ELEMENT_ID),
                chartType           = $F('lstChartTypes'),
                iframeCollection    = divContent.getElementsByTagName('iframe'),
                movieWrapper,
                id,
                tempMovie,
                movie,
                param;

            // go through all the iframes in the content area and update the chart type

            for (var i = 0; i < iframeCollection.length; i++) {

                id              = iframeCollection[i].getAttribute('id');
                iframeDoc       = $(id).contentWindow;
                movieWrapper    = iframeDoc.document.getElementById('fcwrapper');
                movie           = iframeDoc.document.getElementById('FusionChart');
                param           = iframeDoc.document.getElementById('fcmovie');

                param.setAttribute('value', 'Charts/' + chartType + '.swf');
                movie.setAttribute('data', 'Charts/' + chartType + '.swf');

                tempMovie = movie.cloneNode(true);

                OTOS.Dom.removeChildNodes(movieWrapper);
                movieWrapper.appendChild(tempMovie);

                // remove the references to another document

                iframeDoc = null;
                movieWrapper = null;
                movie = null;
                param = null;
                tempMovie = null;

            } // for

        },


        /**
         * Changes the stat type in the iframes.
         */
        changeStatType: function () {

            OTOS.Statistics.refreshStatistics(null, null, null);

        //     var divContent          = $(CONTENT_ELEMENT_ID),
        //         iframeCollection    = divContent.getElementsByTagName('iframe'),
        //         iframeDoc;
        //
        //     // go through all the iframes in the content area and update the stat type
        //
        //     for (var i = 0; i < iframeCollection.length; i++) {
        //
        //         id          = iframeCollection[i].getAttribute('id');
        //         iframeDoc   = $(id).contentWindow;
        //
        //         iframeDoc.setFCNewData(iframeDoc.dataArray[statType]);
        //
        //     } // for

        },


        /**
         *
         */
        toggleStatistics: function (server, camId, areaId) {
            OTOS.Statistics.statisticsInfo(server, camId, areaId, true);
            OTOS.ElementManager.setCameraBarHeights($(CONTENT_ELEMENT_ID));
        }

    };

}();
