function getMovie(name) {
    var isIe = navigator.appName.indexOf("Microsoft") != -1;
    return (isIe ? window : document)[name];
}

var audioList = {
    
    _timer: null,
    _page: 0,

    show: function() {
        if (this._isPopupExists()) {
            return false;
        }
        this._setLoadingContent();
        $('formAddAudioComment').style.display = 'block';
        this._loadList(this._page);
        return false;
    },
    
    showMessage: function() {
        if (this._isPopupExists()) {
            return false;
        }
        this._setLoadingContent();
        $('formAddAudioComment').style.display = 'block';
        this._loadListMessage(this._page);
        return false;
    },

    hide: function() {
        $('formAddAudioComment').style.display = 'none';
        return false;
    },

    loadPage: function(page) {
        this._page = page;
        this._setLoadingContent();
        var searchParam = this._getSearchText() ? this._getSearchText() + '/' : '';
        this._loadList(page);
        return false;
    },

    search: function() {
        var _this = this;
        clearTimeout(this._timer);
        this._timer = setTimeout(function() { _this._loadList()}, 500);
    },

    _loadList: function(page) {
        if (!page) {
            page = 0;
        }
        var text = this._getSearchText();
        if (text.indexOf('/') > -1 || text.indexOf('\\') > -1) {
            $('audioListContainer').innerHTML = '<div style="text-align: center">Аудиозаписей не найдено</div>';
            return;
        }
        jQuery.invoke(CFG_BASE_URL + '/audio/comment/form/' + page + '/' + text + '/');
    },
    
    _loadListMessage: function(page) {
        if (!page) {
            page = 0;
        }
        var text = this._getSearchText();
        if (text.indexOf('/') > -1 || text.indexOf('\\') > -1) {
            $('audioListContainer').innerHTML = '<div style="text-align: center">Аудиозаписей не найдено</div>';
            return;
        }
        jQuery.invoke(CFG_BASE_URL + '/audio/comment/form/' + page + '/' + text + '/message/');
    },

    _setLoadingContent: function() {
        $('audioListContainer').innerHTML = '<div style="text-align: center">Загрузка...</div>';
    },

    _getSearchText: function() {
        if (!this._searchAudioObj) {
            this._searchAudioObj = document.getElementById('searchAudio');
        }
        var text = this._searchAudioObj.value;
        if ('Поиск' == text) {
            return '';
        }
        return text;
    },

    _isPopupExists: function() {
        return ('block' == $('formAddAudioComment').style.display);
    }
};

var audioPlayer = {

    _playButton: null,
    _playerContainer: null,
    _audioInfo: {},
    _isPlayerExists: false,
    _isPlaying: false,

    action: function(id, url, duration) {
        if (this._audioInfo.id != id) {
            this._changeAudio(id);
            this._audioInfo.url = url;
            this._audioInfo.duration = duration;
        }
        this._setPlay(this._isPlaying);
        if (!this._isPlayerExists) {
            this._insertPlayer();
        } else {
            var send = (this._isPlaying) ? 'pause' : 'play';
            this._getMovie().sendFromJs(send);
        }
        return false;
    },

    playNextAudio: function() {
        var id = this._audioInfo.id + 1;
        var button = document.getElementById('playButton[' + id + ']');
        if (button) {
            button.onclick();
        } else {
            this._setPlay(false);
        }
    },

    _getMovie: function() {
        return getMovie('music_player');
    },

    _changeAudio: function(id) {
        this._removePlayer();
        this._audioInfo.id = id;
        if (this._playerContainer) {
            this._playerContainer.className = 'player border';
        }
        this._playerContainer = document.getElementById('audioPlayerContainer[' + id + ']');
        this._setPlay(false);
        this._playButton = document.getElementById('playButton[' + id + ']');
    },

    _setPlay: function(param) {
        if (param) {
            if (this._playButton) {
                this._playButton.className = 'pause';
            }
            this._isPlaying = false;
        } else {
            if (this._playButton) {
                this._playButton.className = 'play';
            }
            this._isPlaying = true;
        }
    },

    _removePlayer: function() {
        if (this._playerContainer) {
            this._playerContainer.innerHTML = '';
            this._isPlayerExists = false;
        }
    },

    _insertPlayer: function() {
        this._playerContainer.className = 'player';
        this._playerContainer.innerHTML =
              '<embed '
            + 'src="/swf/audio_player.swf" '
            + 'loop="false" '
            + 'menu="false" '
            + 'quality="high" '
            + 'bgcolor="#ffffff" '
            + 'width="100%" height="20" '
            + 'name="music_player" '
            + 'align="middle" '
            + 'allowScriptAccess="always" '
            + 'type="application/x-shockwave-flash" '
            + 'pluginspage="http://www.macromedia.com/go/getflashplayer" '
            + 'swliveconnect="true" '
            + 'wmode="transparent"'
            + 'flashvars="url=' + this._audioInfo.url + '&duration=' + this._audioInfo.duration + '" '
            + '/>'
        ;
        this._isPlayerExists = true;
    }
}

var audio = {

    removeFromPlaylist: function(id, title) {
        var result = confirm('Вы действительно хотите удалить "' + title + '"?');
        if (result) {
            document.location.href = '/ru/audio/remove/' + id + '/';
        }
    },

    addToPlayList: function(id) {
        document.location.href = '/ru/audio/add/' + id + '/';
    },

    addComment: function(id) {
        $('data_text').value += ' [audio]' + id + '[/audio] ';
        $('data_text').focus();
        audioList.hide();
    },
    
    addMessage: function(id) {
        $('posting.message.form.text').value += ' [audio]' + id + '[/audio] ';
        $('posting.message.form.text').focus();
        audioList.hide();
    }
}
