CreatoorSN.namespace('wall1');
CreatoorSN.wall1 = {
    /**
     * object containing wall instances
     */
    walls: {},

    maxMediaAttachments: 3,

    /**
     * gets wall instance by id
     * @param {string} id
     * @return {object}
     */
    getWall: function (id) {
        return CreatoorSN.wall1.walls[id];
    },
    /**
     * gets wall instance config object by id
     * @param {string} id
     * @return {object}
     */
    getConfig: function(id) {
        if (CreatoorSN.wall1.walls[id]) {
            return CreatoorSN.wall1.walls[id].config;
        }
        return null;
    },
    /**
     * initializes new wall, and returns id of initialized wall
     * config object structure
     * id: required - id of the wall
     * @param {object} config
     * @return {string}
     */
    initialize: function(config) {
        if (config && config.id) {
            var id = config.id;
            var form = _$('form-' + id);
            var elements = form ? form.elements : null;
            config.form = form;
            config.formElements = elements;
            config.wrapper = _$('wall-wrapper-' + id);
            config.formAttachments = _$('form-attachments-' + id);
            config.mediaAttachments = _$('wall-media-attachments-' + id);
            config.errors = _$('wall-errors-' + id);
            config.info = _$('wall-info-' + id);
            config.postsList = _$('wall-posts-list-' + id);
            config.content = _$('content-' + id);
            config.startString = config.content ? config.content.value : '';
            config.attachments = {
                data: {
                    links: null,
                    media: {}
                },
                links: _$('attached-links-' + id),
                media: _$('attached-media-' + id),
                buttons: [_$('attach-link-' + id), _$('attach-media-' + id)]
            };
            if (config.attachments.links && config.attachments.media) {
                config.attachments.links.value = config.attachments.media.value = '';
            }
            config.submit = _$('wall-submit-' + id);
            CreatoorSN.wall1.walls[id] = {
                config: config,
                attachments: {}
            };
            CreatoorSN.wall1.initNavigation(id);
            if (config.form) {
                CreatoorSN.wall1.initContentArea(id);
                if (config.attachments.links && config.attachments.media) {
                    CreatoorSN.wall1.initAttachButtons(id);
                }
                CreatoorSN.wall1.initSubmit(id);
                CreatoorSN.wall1.initCommentsForms(id);
            }
            
            return id;
        }
        return false;
    },

    initNavigation: function(id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config) {
            if (config.pagination) {
                //TODO: handle pagination
            } else {
                var sol = _$('wall-show-older-' + id);
                if (sol && sol.firstChild) {
                    sol.firstChild.onclick = function() {
                        CreatoorSN.wall1.showOlderPosts(id, sol);
                        this.onclick = function(){return false;}
                        return false;
                    };
                }
            }
        }
    },

    /**
     * initializes evnets for wall form content textarea
     * @param {string} id
     */
    initContentArea: function (id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config && config.content) {
            config.content.onfocus = function() {
                if (config.content.value == config.startString) {
                    config.content.value = '';
                    DOM.setStyle(config.content, 'height', '3em');
                }
            };
            config.content.onblur = function() {
                if (trim(config.content.value) == '') {
                    DOM.setStyle(config.content, 'height', '1em');
                    if (!config.attachments.links.value && !config.attachments.media.value) {
                        config.content.value = config.startString;
                    }
                }
            }
            config.content.onkeyup = function() {
                CreatoorSN.wall1.resizeContentArea(this);
            }
        }
    },
    /**
     * dynamically resizes wall form content textarea
     * @param {object} contentArea
     */
    resizeContentArea: function (contentArea) {
        if (contentArea.clientHeight < contentArea.scrollHeight) {
            DOM.setStyle(contentArea, 'height', parseInt(contentArea.scrollHeight + 5) + 'px');
        }
    },
    /**
     * initializes wall form attachments buttons events
     * @param {string} id
     */
    initAttachButtons: function (id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config && config.attachments && config.attachments.buttons) {
            DOM.batch(config.attachments.buttons, CreatoorSN.wall1.setAttachButtons, id);
        }
    },
    /**
     * sets wall form attachment buttons events
     * @param {object} button
     * @param {string} id
     */
    setAttachButtons: function(button, id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (button) {
            var el = button.parentNode;
            if (el) {
                if (!DOM.hasClass(el, 'wab-active')) {
                    button.onmouseover = function() {
                        DOM.addClass(el, 'wab-active');                        
                    };
                    button.onmouseout = function() {
                        DOM.removeClass(el, 'wab-active');
                    };
                    
                }
                button.onclick = function () {
                    DOM.batch(config.attachments.buttons, CreatoorSN.wall1.resetAttachButtons);
                    if (!DOM.hasClass(el, 'wab-active')) {
                        DOM.addClass(el, 'wab-active');
                        
                        this.onmouseout = function(){return false;};
                    }
                    
                    if (config.attachments.current && config.attachments.current != button.id) {                        
                        if (config.attachments.forms) {
                            if (config.attachments.current.search('-link-') >= 0) {
                                config.attachments.forms.links = config.formAttachments.innerHTML;
                            } else {
                                if (!(CreatoorSN.common.browser.ie && parseInt(CreatoorSN.common.browser.version) == 8)) {
                                    config.attachments.forms.media.form = config.formAttachments.innerHTML;
                                }
                               //config.attachments.forms.media.form = config.formAttachments.innerHTML;
                            }
                        }
                    }
                    CreatoorSN.wall1.initAttachButtons(id);
                    CreatoorSN.wall1.showAttachmentsFormContainer(this, id);
                    config.attachments.current = button.id;
                    
                };

            }
        }
    },
    /**
     * resets wall form attachment buttons css classes
     * @param {object} button
     */
    resetAttachButtons: function(button) {
        DOM.removeClass(button.parentNode, 'wab-active');
    },

    showAttachmentsFormContainer: function (button, id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (!DOM.hasClass(config.formAttachments, 'wfa-active')) {
            DOM.addClass(config.formAttachments, 'wfa-active');
            if (!config.animation) {
                config.animation = new YAHOO.util.Anim();                
            }
            config.animation.init(config.formAttachments, {height:{from: 0, to: 280}}, 1, YAHOO.util.Easing.easeOut);
            DOM.setStyle(config.formAttachments, 'height', 'auto');
            config.animation.animate();
            CreatoorSN.wall1.showAttachmentsForm(button, id);
        } else {
            if (config.attachments.current == button.id) {
                config.animation.init(config.formAttachments, {height:{to: 0}}, 1, YAHOO.util.Easing.easeOut);
                DOM.removeClass(button.parentNode, 'wab-active');
                CreatoorSN.wall1.setAttachButtons(button, id);
                config.animation.onComplete.subscribe(function(){
                    if (config.animation.getAttribute('height') == 0) {
                        DOM.removeClass(config.formAttachments, 'wfa-active');
                        config.attachments.current = null;
                    }
                });
                config.animation.animate();
            } else {
                CreatoorSN.wall1.showAttachmentsForm(button, id);
            }
        }
        
    },

    showAttachmentsForm: function(button, id)
    {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config.attachments.forms) {
            if (button.id.search('-link-') >= 0) {                
                config.formAttachments.innerHTML = config.attachments.forms.links;
                CreatoorSN.links.setCallback({
                    add: CreatoorSN.wall1.addLink,
                    clear: CreatoorSN.wall1.clearLink
                }, id);

                if (config.attachments.data.links) {
                    var url = config.attachments.data.links.data.url;
                    CreatoorSN.links.showPreview(config.attachments.data.links, null, id);
                }
                CreatoorSN.links.setForm(id, url);
                config.attachments.forms.links = config.formAttachments.innerHTML;
                
                
            } else {
                config.formAttachments.innerHTML = config.attachments.forms.media.form;
                var ulList = DOM.getElementBy(function(el){return DOM.hasClass(el, 'combo-upload-list');}, 'ul',  'combo-upload-section-' + id);
                if (ulList) {
                    if (ulList.firstChild) {
                        ulList.firstChild.innerHTML = '<div id="combo-upload-btn-' + id + '"></div>';
                    }
                    if (ulList.lastChild) {
                        ulList.lastChild.innerHTML = '';
                    }
                    
                    
                    if (!CreatoorSN.mediaLibrary.combo.initialize(id)) {
                        var mlcConfig = config.attachments.forms.media.data;
                        mlcConfig.callback = CreatoorSN.wall1.addMedia;
                        CreatoorSN.mediaLibrary.combo.initialize(id, mlcConfig);
                    }
                    if (!(CreatoorSN.common.browser.ie && parseInt(CreatoorSN.common.browser.version) == 8)) {
                        config.attachments.forms.media.form = config.formAttachments.innerHTML;
                    }
                    
                }
            }
            return;
        }
        config.formAttachments.innerHTML = '<img style="margin:96px 0 0 234px;" src="/images/ajax-loader-medium.gif" alt="' + CreatoorSN.common.translate('loadnig...') + '" />';
        var callback = {
            success: function (o) {
                if (o.responseText && JSON.isSafe(o.responseText)) {
                    var r = JSON.parse(o.responseText);
                    if (r.success) {
                        config.attachments.forms= {links: r.links, media:r.media};
                        if (button.id.search('-link-') >= 0) {
                            config.formAttachments.innerHTML = config.attachments.forms.links;
                            CreatoorSN.links.setCallback({
                                add: CreatoorSN.wall1.addLink,
                                clear: CreatoorSN.wall1.clearLink
                            }, id);
                            CreatoorSN.links.setForm(id);
                            config.attachments.forms.links = config.formAttachments.innerHTML;
                        } else {
                            config.formAttachments.innerHTML = config.attachments.forms.media.form;
                            var mlcConfig = config.attachments.forms.media.data;
                            mlcConfig.callback = CreatoorSN.wall1.addMedia;
                            CreatoorSN.mediaLibrary.combo.initialize(id, mlcConfig);
                            config.attachments.forms.media.form = config.formAttachments.innerHTML;
                        }
                    } else if (r.noAuth) {
                        var login = new noAuth(function(noAuthObj) {
                            CreatoorSN.wall1.showAttachments(button, id);
                        }, r.message);
                    } else {
                        config.formAttachments.innerHTML = r.message;
                    }
                } else {
                    config.formAttachments.innerHTML = CreatoorSN.common.translate('Oops an error occured while executing a script. Please try again later.');
                }
            },
            failure: function (o) {
                config.formAttachments.innerHTML = CreatoorSN.common.translate('Communication failure.');
            }
        };
        YAHOO.util.Connect.asyncRequest(
            "GET",
            '/members/wall/get-attachment-forms?id=' + id,
            callback
        );
    },

    addLink: function(data, id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config) {
            config.attachments.data.links = data;
        }
    },

    clearLink: function(id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config) {
            config.attachments.data.links = null;
            config.attachments.forms.links = config.formAttachments.innerHTML;
        }
    },

    addMedia: function(data, progress, swfu, id) {
        if (progress) {
           if (swfu.processed) {
               swfu.processed--;
           }
           var status = data.fileName + ': ' + CreatoorSN.common.translate('processing completed');
           if (typeof(swfu.processed) != 'undefined' && !swfu.processed) {
               status = CreatoorSN.common.translate('Processing of all files completed');
               CreatoorSN.mediaLibrary.swfu.removeOverlays(swfu);
               swfu.progressObj.setStatus(status);
               setTimeout("CreatoorSN.mediaLibrary.swfu.clearStatus('" + swfu.customSettings.progress.text + "')", 2000);
           } else {
               progress.setStatus(status);
           }           
       }
       var config = CreatoorSN.wall1.getConfig(id);
       if (config) {
           if (config.mediaAttachments.childNodes.length == CreatoorSN.wall1.maxMediaAttachments) {
                var msg = CreatoorSN.common.translate('You reached maximim number of allowed media attachments.');
                CreatoorSN.wall1.showError(id, msg);
           } else {
               if (!config.attachments.data.media[data.mediaType + '-' + data.identifier]) {
                   config.attachments.data.media[data.mediaType + '-' + data.identifier] = data;
                   var li = new creaDomObject({
                       tag: 'li',
                       html: data.thumbHtml + '<a href="#" onclick="CreatoorSN.wall1.removeMedia(this, \'' + id + '\', \'' + data.mediaType + '\', \'' + data.identifier + '\');return false;" />' + CreatoorSN.common.translate('remove') + '</a>'
                   });
                   li.addTo(config.mediaAttachments);
                   if (config.mediaAttachments.childNodes.length == 1) {
                       DOM.setStyle(config.mediaAttachments, 'display', '');
                   }
               }
           }
       }
       
    },

    removeMedia: function(a, id, mediaType, mediaId)
    {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config.attachments.data.media[mediaType + '-' + mediaId]) {
            delete config.attachments.data.media[mediaType + '-' + mediaId];
            removeElement(a.parentNode);
            if (!config.mediaAttachments.childNodes.length) {
               DOM.setStyle(config.mediaAttachments, 'display', 'none');
           }
        }
    },

    showError: function (id, message) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config.errors) {
            config.errors.innerHTML = message;
            DOM.removeClass(config.errors, 'display-none');
            setTimeout('CreatoorSN.wall1.clearError("' + id + '")', 3000);
        }
    },

    clearError: function (id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config.errors) {
            config.errors.innerHTML = '';
            DOM.addClass(config.errors, 'display-none');
        }
    },

    showInfo: function(id, message) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config.info) {
            config.info.innerHTML = message;
            DOM.removeClass(config.info, 'display-none');
            setTimeout('CreatoorSN.wall1.clearInfo("' + id + '")', 3000);
        }
    },

    clearInfo: function(id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config.info) {
            config.info.innerHTML = '';
            DOM.addClass(config.info, 'display-none');
        }
    },

    resetForm: function(id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config) {            
            config.attachments.data = {
                links: null,
                media: {}
            };
            
            if (config.attachments.forms) {
                config.formAttachments.innerHTML = config.attachments.forms.links;
                CreatoorSN.links.clearForm(id);
                config.attachments.forms.media.initialized = false;

            }
            if (config.attachments.links && config.attachments.media) {
                config.attachments.links.value = config.attachments.media.value = '';
                config.mediaAttachments.innerHTML = '';
                DOM.setStyle(config.mediaAttachments, 'display', 'none');
                config.attachments.current = null;
                DOM.setStyle(config.formAttachments, 'height', '0px');
                DOM.removeClass(config.formAttachments, 'wfa-active');
                DOM.batch(config.attachments.buttons, function(el){DOM.removeClass(el.parentNode, 'wab-active')});
            }
            config.content.value = config.startString;
            DOM.setStyle(config.content, 'height', '1em');
            
        }
    },

    initCommentsForms: function(id)
    {
        DOM.getElementsByClassName('wall-post-comment-form', 'form', 'wall-wrapper-' + id, CreatoorSN.wall1.initCommentForm, id);
    },

    initCommentForm: function(form, id)
    {
        var content = DOM.getElementBy(function(el){return true;}, 'textarea', form);
        var submit = DOM.getElementBy(function(el){return el.type.toLowerCase() == 'submit'}, 'input', form);
        if (content && submit) {
            var startString = content.value;
            content.onfocus = function() {
                if (content.value == startString) {
                    content.value = '';
                    DOM.setStyle(content, 'height', '2em');
                    DOM.removeClass(submit.parentNode, 'display-none');
                    if (CreatoorSN.common.browser.ie) {
                        DOM.addClass(form.parentNode.parentNode.parentNode, 'block');
                    }
                }
            }
            content.onblur = function() {
                content.value = trim(strip_tags(content.value));
                if (content.value == '') {
                    content.value = startString;
                    DOM.setStyle(content, 'height', '1em');
                    DOM.addClass(submit.parentNode, 'display-none');
                    if (CreatoorSN.common.browser.ie) {
                        DOM.removeClass(form.parentNode.parentNode.parentNode, 'block');
                    }
                }
            }
            content.onkeyup = function() {
                CreatoorSN.wall1.resizeContentArea(this);
            }
            CreatoorSN.wall1.setCommentFormSubmit(form, startString, id, content);
        }
    },

    addComment: function(response, form, params) {
        if (params.overlay) {
            removeElement(params.overlay);
        }
        var list = form.parentNode.parentNode.parentNode;
        var li = new creaDomObject({
            tag: 'li',
            id: params.id + '-' + response.identifier,
            html: response.html
        });
        DOM.insertBefore(li.domElement, list.lastChild);
        CreatoorSN.wall1.resetCommentForm(form, params.startString, params.id);
    },

    handleCommentError: function(response, form, params) {
        if (params.overlay) {
            removeElement(params.overlay);
        }
        CreatoorSN.wall1.setCommentFormSubmit(form, params.startString, params.id);
        var errorDiv = form.parentNode.nextSibling;
        if (errorDiv && DOM.hasClass(errorDiv, 'widget-errors')) {
            errorDiv.innerHTML = response.message;
            DOM.removeClass(errorDiv, 'display-none');
        }
    },

    clearCommentError: function(elId) {
        var el = _$(elId);
        if (el) {
            el.innerHTML = '';
            DOM.addClass(el, 'display-none');
        }
    },

    resetCommentForm: function (form, startString, id) {
        var content = DOM.getElementBy(function(el){return true;}, 'textarea', form);
        var submit = DOM.getElementBy(function(el){return el.type.toLowerCase() == 'submit'}, 'input', form);
        content.value = startString;
        DOM.setStyle(content, 'height', '1em');
        DOM.addClass(submit.parentNode, 'display-none');
        CreatoorSN.wall1.setCommentFormSubmit(form, startString, id, content);
        if (CreatoorSN.common.browser.ie) {
            DOM.removeClass(form.parentNode.parentNode.parentNode, 'block');
        }
    },

    setCommentFormSubmit: function(form, startString, id, content) {
        content = content ? content : DOM.getElementBy(function(el){return true;}, 'textarea', form);
        form.onsubmit = function() {
            content.value = trim(strip_tags(content.value));
            if (content.value && content.value != startString) {
                var overlay = disableElement(form.parentNode);
                CreatoorSN.common.saveFormData(this, CreatoorSN.wall1.addComment, {startString: startString, overlay: overlay, id: id}, null, CreatoorSN.wall1.handleCommentError);
                this.onsubmit = function(){return false;};
            }
            return false;
        }
    },

    initSubmit: function(id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config) {
            config.form.onsubmit = function() {
                config.content.value = trim(strip_tags(config.content.value));
                if ((config.content.value && config.content.value != config.startString) ||
                    config.attachments.data.links || !isEmpty(config.attachments.data.media)) {
                    if (config.attachments.data.links) {
                        config.attachments.data.links.data.noThumb = config.attachments.data.links.noThumb;
                        config.attachments.links.value = JSON.stringify(config.attachments.data.links.data);
                    }
                    if (!isEmpty(config.attachments.data.media)) {
                        config.attachments.media.value = JSON.stringify(config.attachments.data.media);
                    }
                    config.content.value = config.content.value == config.startString ? '' : config.content.value;
                    if (config.wrapper) {
                        config.overlay = disableElement(config.wrapper.firstChild);
                    }
                    CreatoorSN.common.saveFormData(config.form, CreatoorSN.wall1.handleSubmit, id, null, CreatoorSN.wall1.handleError);
                    this.onsubmit = function(){return false;};
                }
                
                
                return false;
            };
        }
    },

    handleSubmit: function(response, form, id) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config) {
            CreatoorSN.wall1.resetForm(id);
            CreatoorSN.wall1.initSubmit(id);
            if (config.postOrder.toLowerCase() == 'desc') {
                if (config.postsList) {
                    config.postsList.innerHTML = response.html + config.postsList.innerHTML;
                    
                }
                if (config.pagination) {
                    //TODO: handle pagination
                }
            } else {
                CreatoorSN.wall1.showInfo(id, response.message);
                if (config.pagination) {
                    //TODO: handle pagination
                } else {
                    var sol = _$('wall-show-older-' + id);
                    if (!sol) {
                       config.postsList.innerHTML += response.html;
                    }
                }
            }
            if (config.overlay) {
                removeElement(config.overlay);
                delete config.overlay;
            }
            CreatoorSN.wall1.initNavigation(id);
            CreatoorSN.wall1.initCommentsForms(id);
            
        }
    },

    handleError: function (r, form, id) {
        CreatoorSN.wall1.initSubmit(id);
        CreatoorSN.wall1.showError(id, r.message);
        var config = CreatoorSN.wall1.getConfig(id);
        if (config && config.overlay) {
            removeElement(config.overlay);
            delete config.overlay;
        }
    },

    showMediaPlayer: function(el, containerId, width, height) {
        DOM.addClass(el.parentNode.childNodes, 'display-none');
        DOM.removeClass(el.parentNode.firstChild, 'display-none');
        if (CreatoorSN.common.browser.ie) {
            DOM.addClass(el.parentNode.parentNode, 'block');
        }
        var thumb = el.previousSibling.firstChild;
        var directory = DOM.getAttribute(thumb, 'dir');
        var size = Math.max(width, height);
        var image = '/members_data/' + directory + '/videos/' + size + 'bbd/' + DOM.getAttribute(thumb, 'alt');
        var video = '/members_data/' + directory + '/videos/' + DOM.getAttribute(thumb, 'title');
        var flashvars = {file: video, image: image,autostart:'false',skin: '/jwplayer/skins/regular.swf'};
        var params = {allowfullscreen:'true', allowscriptaccess:'always',wmode: 'transparent'};
        var attributes = {id: 'player-' + containerId, name: 'player-' + containerId};
        swfobject.embedSWF('/jwplayer/player.swf', containerId, width, height, '9.0.115', 'false', flashvars, params, attributes);

    },

    hideMediaPlayer: function(el) {
        DOM.removeClass(el.parentNode.parentNode.childNodes, 'display-none');
        if (CreatoorSN.common.browser.ie) {
            DOM.removeClass(el.parentNode.parentNode.parentNode, 'block');
        }
        DOM.addClass(el.parentNode, 'display-none');
        var id = el.nextSibling.firstChild.id.replace('player-', '');
        removeElement(el.nextSibling.firstChild);
        el.nextSibling.innerHTML = '<div id="' + id + '"></div>';
        
    },

    removePost: function(wallId, postId) {
        var config = CreatoorSN.wall1.getConfig(wallId);
        if (config) {
            var overlay = disableElement(wallId + '-' + postId);
            var error = false;
            var msg = '';
            var callback = {
                success: function(o) {
                    removeElement(overlay);
                    if (o.responseText && JSON.isSafe(o.responseText)) {
                        var r = JSON.parse(o.responseText);
                        if (r.success) {
                            removeElement(wallId + '-' + postId);
                        } else if(r.noAuth) {
                            var login = new noAuth(function(noAuthObj) {
                                CreatoorSN.wall1.removePost(wallId, postId);
                            }, r.message);
                        } else {
                            error = true;
                            msg = r.message;
                        }
                    } else {
                        error = true;
                        msg = CreatoorSN.common.translate('Oops an error occured while executing a script. Please try again later.');
                    }
                    if (error) {
                        CreatoorSN.common.showErrorAlert(msg);
                    }
                },
                failure: function(o) {
                    removeElement(overlay);
                    msg = CreatoorSN.common.translate('Communication failure.');
                    CreatoorSN.common.showErrorAlert(msg);
                }
            };
            var postData = 'wallType=' + config.type + '&rid=' + config.resourceId + '&pid=' + postId;
            YAHOO.util.Connect.asyncRequest("POST", "/members/wall/remove-post/", callback, postData);
        }
        
    },

    castOpinion: function (el, wallId, postId, opinion) {
        var config = CreatoorSN.wall1.getConfig(wallId);
        if (config) {
            var overlay = disableElement(wallId + '-' + postId);
            var error = false;
            var msg = '';
            var callback = {
                success: function(o) {
                    removeElement(overlay);
                    if (o.responseText && JSON.isSafe(o.responseText)) {
                        var r = JSON.parse(o.responseText);
                        if (r.success) {
                            DOM.replaceClass(el, 'like', 'unlike');
                            el.innerHTML = CreatoorSN.common.translate('Unlike');
                            var likeInfo = _$('wall-like-count-' + wallId + '-' + postId);
                            if (likeInfo) {
                                likeInfo.innerHTML = r.likeHtml;
                                DOM.removeClass(likeInfo, 'display-none');
                                DOM.removeClass(likeInfo.parentNode, 'display-none');
                            }
                            el.onclick = function() {
                                CreatoorSN.wall1.removeOpinion(this, wallId, postId);
                                return false;
                            }
                        } else if(r.noAuth) {
                            var login = new noAuth(function(noAuthObj) {
                                CreatoorSN.wall1.removePost(wallId, postId);
                            }, r.message);
                        } else {
                            error = true;
                            msg = r.message;
                        }
                    } else {
                        error = true;
                        msg = CreatoorSN.common.translate('Oops an error occured while executing a script. Please try again later.');
                    }
                    if (error) {
                        CreatoorSN.common.showErrorAlert(msg);
                    }
                },
                failure: function(o) {
                    removeElement(overlay);
                    msg = CreatoorSN.common.translate('Communication failure.');
                    CreatoorSN.common.showErrorAlert(msg);
                }
            };
            var postData = 'wallType=' + config.type + '&wid=' + wallId + '&pid=' + postId + '&o=' + opinion;
            YAHOO.util.Connect.asyncRequest("POST", "/members/wall/cast-opinion/", callback, postData);
        }
    },

    removeOpinion: function(el, wallId, postId)
    {
        var config = CreatoorSN.wall1.getConfig(wallId);
        if (config) {
            var overlay = disableElement(wallId + '-' + postId);
            var error = false;
            var msg = '';
            var callback = {
                success: function(o) {
                    removeElement(overlay);
                    if (o.responseText && JSON.isSafe(o.responseText)) {
                        var r = JSON.parse(o.responseText);
                        if (r.success) {
                            DOM.replaceClass(el, 'unlike', 'like');
                            el.innerHTML = CreatoorSN.common.translate('Like');
                            var likeInfo = _$('wall-like-count-' + wallId + '-' + postId);
                            if (likeInfo) {
                                likeInfo.innerHTML = r.likeHtml;
                                if (!r.likeHtml) {
                                    DOM.addClass(likeInfo, 'display-none');
                                    var empty = true;
                                    for (var i = 0; i < likeInfo.parentNode.childNodes.length; i++) {
                                        if (likeInfo.parentNode.childNodes[i].innerHTML) {
                                            empty = false;
                                            break;
                                        }
                                    }
                                    if (empty) {
                                        DOM.addClass(likeInfo.parentNode, 'display-none');
                                    }
                                }
                            }
                            el.onclick = function() {
                                CreatoorSN.wall1.castOpinion(this, wallId, postId, 1);
                                return false;
                            }
                        } else if(r.noAuth) {
                            var login = new noAuth(function(noAuthObj) {
                                CreatoorSN.wall1.removePost(wallId, postId);
                            }, r.message);
                        } else {
                            error = true;
                            msg = r.message;
                        }
                    } else {
                        error = true;
                        msg = CreatoorSN.common.translate('Oops an error occured while executing a script. Please try again later.');
                    }
                    if (error) {
                        CreatoorSN.common.showErrorAlert(msg);
                    }
                },
                failure: function(o) {
                    removeElement(overlay);
                    msg = CreatoorSN.common.translate('Communication failure.');
                    CreatoorSN.common.showErrorAlert(msg);
                }
            };
            var postData = 'wallType=' + config.type + '&wid=' + wallId + '&pid=' + postId;
            YAHOO.util.Connect.asyncRequest("POST", "/members/wall/remove-opinion/", callback, postData);
        }
    },

    showOpinionsUsers: function(el, wallId, postId, opinion) {
        var config = CreatoorSN.wall1.getConfig(wallId);
        if (config) {
            var url = '/members/wall/get-opinion-users/?wallType=' + config.type
                    + '&resource_id=' + config.resourceId + '&identifier=' + postId + '&opinion=' + opinion;
            var name = wallId + '-' + postId;
            CreatoorSN.common.getPopupList(url, name, CreatoorSN.common.translate('Members list'));
        }
    },

    showOlderPosts: function(id, el) {
        var config = CreatoorSN.wall1.getConfig(id);
        if (config) {            
            DOM.addClass(el, 'working');
            var get = 'wid=' + id + '&lid=' + config.lastId
                    + '&rid=' + config.resourceId + '&rpp=' + config.rowsPerPage + '&wallType=' + config.type;
            var callback = {
                success: function(o) {
                    DOM.removeClass(el, 'working');
                    var msg = '';
                    if (o.responseText && JSON.isSafe(o.responseText)) {
                        var r = JSON.parse(o.responseText);
                        if (r.success) {
                            removeElement(el);
                            config.postsList.innerHTML += r.html;
                            config.lastId = r.lastId;
                            CreatoorSN.wall1.initNavigation(id);
                            CreatoorSN.wall1.initCommentsForms(id);
                        } else {
                            msg = r.message;
                        }

                    } else {
                        msg = CreatoorSN.common.translate('Oops an error occured while executing a script. Please try again later.');
                    }
                    if (msg) {
                        CreatoorSN.common.showErrorAlert(msg);
                        CreatoorSN.wall1.initNavigation(id);
                    }

                },
                failure: function(o) {
                    DOM.removeClass(el, 'working');
                    CreatoorSN.common.showErrorAlert(CreatoorSN.common.translate('Comunication failure.'));
                    CreatoorSN.wall1.initNavigation(id);
                }
            };
            YAHOO.util.Connect.asyncRequest("GET", '/members/wall/get-older-posts?' + get, callback);
        }
    },

    getPage: function(state) {
        
    }

}



