var NAAMA = window.NAAMA || {};
NAAMA.Util = function () {

    return {

        POPUP_WIDTH:    600,
        POPUP_HEIGHT:   700,

        init: function () {

            var allCorners = { topLeft: true, topRight: true, bottomLeft: true, bottomRight: true },
                topCorners = { topLeft: true, topRight: true, bottomLeft: false, bottomRight: false },
                bottomCorners = { topLeft: false, topRight: false, bottomLeft: true, bottomRight: true },
                rad = 10;

            // bottom corners
            $A(document.getElementsByClassName('roundBottom')).each(function (item) {
                new Rounded(item, {
                    radius: rad,
                    corners: bottomCorners
                });
            });

            // top corners
            $A(document.getElementsByClassName('roundTop')).each(function (item) {
                new Rounded(item, {
                    radius: rad,
                    corners: topCorners
                });
            });

            // all corners
            $A(document.getElementsByClassName('roundAll')).each(function (item) {
                new Rounded(item, {
                    radius: rad,
                    corners: allCorners
                });
            });

        },


        /**
         * Adds a function to window's onload-event.
         *
         * @param   {function}  myfunc  The function to add to window's onload -event
         */
        addOnload: function (myfunc) {
            if (window.addEventListener) { // w3c
                window.addEventListener('load', myfunc, false);
            } else if(window.attachEvent) { // ie
                window.attachEvent('onload', myfunc);
            } // else if
        },


        /**
         *
         */
        editSample: function (sampleId) {

            var url     = 'samplePopup.php?action=edit&id=' + sampleId,
                wName   = 'editSample_' + sampleId,
                wWidth  = NAAMA.Util.POPUP_WIDTH,
                wHeight = NAAMA.Util.POPUP_HEIGHT;

            NAAMA.Util.openWindow(url, wName, wWidth, wHeight);

        },


        /**
         *
         */
        addSample: function () {

            var url     = 'samplePopup.php?action=new',
                wName   = 'newSample',
                wWidth  = NAAMA.Util.POPUP_WIDTH,
                wHeight = NAAMA.Util.POPUP_HEIGHT;

            NAAMA.Util.openWindow(url, wName, wWidth, wHeight);

        },


        /**
         * Opens a new browser window
         *
         * @param   {string}    url     The url of the page that is loaded to the new window
         * @param   {string}    name    The name for the new window
         * @param   {int}       width   The width of the new window
         * @param   {int}       height  The height of the new window
         */
        openWindow: function (url, name, width, height) {
            var settings = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=" + width + ", height=" + height;
            return window.open(url, name, settings);
        },


        /**
         *
         */
        filter: function () {

            var list            = $('samplelist'),
                nameStr         = $F('txtName'),
                numberStr       = $F('txtNumber'),
                lstDistricts    = $('lstDistrict'),
                lstParties      = $('lstParty'),
                samples         = list.getElementsByClassName('sample'),
                idArr           = [],
                dArr            = [],
                pArr            = [];

            nameStr     = nameStr == undefined ? '' : nameStr.toLowerCase();
            numberStr   = numberStr == undefined ? '' : numberStr.toLowerCase();

            // districts
            $A($('districts').getElementsByTagName('input')).each(function (item) {
                if (item.checked) {
                    dArr.push(item.value);
                }
            });

            // parties
            $A($('parties').getElementsByTagName('input')).each(function (item) {
                if (item.checked) {
                    pArr.push(item.value);
                }
            });

            for (var i in s) {
                if ((nameStr == '' || s[i].name.toLowerCase().indexOf(nameStr) != -1) &&
                    (numberStr == '' || s[i].number.indexOf(numberStr) != -1) &&
                    (NAAMA.Util.in_array(s[i].district, dArr)) &&
                    (NAAMA.Util.in_array(s[i].party, pArr))) {
                        idArr.push(i);
                }
            } // for

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

                var sampleId = samples[i].id.split('_')[1];

                if (NAAMA.Util.in_array(sampleId, idArr)) {
                    Element.show(samples[i]);
                } else {
                    Element.hide(samples[i]);
                }

            }

        },


        /**
         * Checks if a value exists in an array
         *
         * @param   {mixed}     needle      The value that we're searching
         * @param   {array}     haystack    The array to be searched
         * @return  True, if the value exists and false otherwise
         * @type    {boolean}
         */
        in_array: function (needle, haystack) {

            for (var i = 0; i < haystack.length; i++) {
                if (haystack[i] == needle) {
                    return true;
                } // if
            } // for

            return false;

        },


        /**
         *
         */
        showRoi: function (roiId) {

            $A(document.getElementsByClassName('roiContent')).each(function (item) {

                var roiNum = item.id.split('_')[1],
                    bestMatch = $('bestMatch_' + roiNum),
                    roiRect = $('roiRect_' + roiNum);

                if (item.id == roiId) {
                    if (item && bestMatch) {
                        Element.show(item);
                        Element.show(bestMatch);
                    } // if
                    Element.addClassName(roiRect, 'chosenRoi');
                } else {
                    if (item && bestMatch) {
                        Element.hide(item);
                        Element.hide(bestMatch);
                    } // if
                    Element.removeClassName(roiRect, 'chosenRoi');
                }

            });

        },

        /**
         *
         */
        alterFeedbackForm: function (val) {

            var fields = $('extraFeedbackFields');

            if (val == 4) {
                Element.show(fields);
            } else {
                Element.hide(fields);
            }

        }

    };

}();