﻿var ClientServices_Class = new Class({

    _periodical: null,

    run: function() {
        window.addEvent('resize', this._resized.bindWithEvent(this));
        window.addEvent('partialload', this._onPartialLoad.bindWithEvent(this));

        _periodical = this._review.periodical(200, this);
        this._sortAttachedPanels($isIE() && !$isIE8());
        this._sortNewWinAnchors();
    },

    _resized: function() {
        this._sortAttachedPanels();
    },

    _review: function() {
        this._sortAttachedPanels();
    },

    _onPartialLoad: function() {
        this._sortNewWinAnchors();
    },

    _sortAttachedPanels: function(hideAndShow) {

        // for all attached panels
        $$('.right_attached').each(function(p) {
            var mains = p.getParent().getElements('.main_panel .atl_curvedcontainer').erase(p).getElement('.curv_ctnt');
            mains.each(function(m) {
                m.setStyle('min-height', p.getSize().y + 10);
                if (hideAndShow) {
                    m.setStyle('display', 'none');

                    var fn = function() {
                        m.setStyle('display', 'block');
                    };

                    fn.delay(1, this);
                }
            } .bind(this));
        } .bind(this));
    },

    _sortNewWinAnchors: function() {
        $$('a.require_newwin').each(function(a) {

            var title = a.get('title');
            if (!title) title = '';
            a.set('title', title + ' (new window)');

            a.removeClass('require_newwin')
             .addClass('newwin')
             .addEvent('click', this._newWinLinkClicked.bindWithEvent(this, a));

        } .bind(this));
    },

    _newWinLinkClicked: function(evt, a) {
        evt.stop();

        var parts = Atl_Url.parse(a.href);
        var newLocation = Atl_Url.build(parts);
        var winName = "";

        for (var i = 0; i < newLocation.length; ++i) {
            if (newLocation.charAt(i).isAlphanum()) {
                winName += newLocation.charAt(i);
            }
        }

        var width = Math.floor(screen.width * 0.40);
        var height = Math.floor(screen.height * 0.50);

        var params = 'location=0,toolbar=no,width=' + width + ',height=' + height + ',directories=no,status=no,scrollbars=yes,resizable=yes';
        var win = window.open(newLocation, 'docwin_' + winName, params);
        win.focus();
    }

});


var ClientServices = new ClientServices_Class();
window.addEvent('atldomready', function() {
    ClientServices.run();
});
