
var Server = Class.create();

Server.prototype = {

    /**
     * Class constructor
     *
     * @param   {String}    serverAddress   The ip address of the server
     * @param   {element}   container       The container for the server element
     */
    initialize: function(serverAddress, container) {
        this.address    = serverAddress;
        this.container  = container;
    },



    /**
     * Creates the server element with all the child elements (cameras, statistics) and then
     * updates the statistics and the images of the server.
     */
    show: function() {

        OTOS.Dom.removeChildNodes(this.container);
        OTOS.Tree.updateTree();    // update the browser in the navigation
        OTOS.Main.TREEMODE = true;    // set the OTOS.Main.TREEMODE on

        this.create();

        OTOS.Statistics.refreshStatistics(null, null, null);
        clearTimeout(OTOS.Camera.TIMER);
        OTOS.Camera.updateCameraImages();

        window.location.hash = '#';

    },



    /**
     * Creates the server element wit all the child elements (cameras, statistics)
     */
    create: function() {

        var el = Builder.node('div', { className: 'serverContainer' }, [
                    OTOS.Dom.createHeading('h2', document.createTextNode(OTOS.ElementConfig.SERVERDATA[this.address]['name']))]);

        this.container.appendChild(el);
        this.createCameraList(el);

    },



    /**
     * Creates all the camera elements of the server
     */
    createCameraList: function(parent) {

        var cameraList  = document.createElement('div'),
            cameras     = OTOS.ElementConfig.SERVERDATA[this.address]['cameras'];

        parent.appendChild(cameraList);

        for (var i in cameras) {
            var newCamera = new Camera(this.address, i, true, cameraList);
            newCamera.create();
        } // for

    },



    /**
     * Creates the server info -element.
     */
    showInfo: function() {

        OTOS.Tree.changeToggleNodeValue($(SERVER_LINK_ID_PREFIX + '_' + this.address));

        var serverElement = $(SERVER_ELEMENT_ID_PREFIX + '_' + this.address);

        // if the server element exists, let's remove it and change the column count

        if (serverElement != null && !OTOS.Main.TREEMODE) {
            OTOS.ElementManager.removeColumn(serverElement, this.container);
        } else {
            OTOS.ElementManager.handleElementAdding(this.container);
            this.createServer();
        } // else

    },


    /**
     *
     */
    createServer: function () {

        var column = OTOS.ElementManager.createElementColumn();

        var serverElement   = Builder.node('div', {
                id: SERVER_ELEMENT_ID_PREFIX + '_' + this.address,
                className: SERVER_ELEMENT_CLASS + ' element'
            }, [
                Builder.node('h2', { className: 'clearfix' }, [
                    Builder.node('span', { className: 'left' }, [
                        document.createTextNode(OTOS.ElementConfig.SERVERDATA[this.address]['name'])
                    ]),
                    Builder.node('span', { className: 'right' }, [
                        Builder.node('a', { href: 'editor.php?s=' + this.address }, [
                            'Hallinta'
                        ]),
                    ])
                ]),
                Builder.node('div', { className: 'elementContent' }, [
                    document.createTextNode('Tietoa palvelimesta')
                ])
            ]);

        column.appendChild(serverElement);
        this.container.appendChild(column);

        new Rounded(column, OTOS.ElementConfig.ELEMENT_ROUNDING_SETTINGS);

    }

};
