(function(){

    Portlet_News_Topics = function( id, classname ){

        this.options = {};

        this.events = ['selectTopic', 'addNode'];

        this.listen = [
            [this, 'addNode', this.addNode]
        ];

        IApp_Portlet.apply(this, arguments);
    };

    Portlet_News_Topics.prototype = $.extend( new IApp_Portlet, {

        init : function(){
            IApp_Portlet.prototype.init.apply(this, arguments);

            /** Place for on init actions **/
            var self = this;

            this.$tree = this.$div.find('ul');

            this.$tree.bind("add", function(event, branches) {
                self.dispatchEvent('addNode', branches);
            });

            //this.initAccordion(this.$tree);
        },

        initAccordion: function($branch){
        	var self = this;
        	if( (typeof this.options.instance.target == 'string') && (this.options.instance.treestyle == 'accordion') ) {
        		$branch.find('li.open, li.collapsable, li.expandable, li.hasChildren').each(function(){
            		$(this).css('cursor', 'pointer').unbind().click(function(){
            			$(this).children('div.hitarea').click();
            			$(this).children('ul').css('cursor', 'default').click(function(){return false;});
            			
            			return true;
            			//return t(typeof self.options.instance.target != 'string') || (! self.options.instance.target.length);
            		});
            	});
            }
        },
        
        addNode: function( branch ){
            var self = this;

            this.initAccordion($(branch));
            
            if( (typeof this.options.instance.target == 'string') && this.options.instance.target.length ){
            	$(branch).find('a.news_topic').each(function(){
                    if( $(this).attr('rel') > 0 ){
                        var rel = $(this).attr('rel');
                        $(this).click( function(){ self.select( rel ); return false; });
                        $(this).attr('rel', 0);
                    }
                });
            }
        },

        select : function( id ){
            if( (typeof this.options.instance.target == 'string') && this.options.instance.target.length ){
                this.dispatchEvent( 'selectTopic', id, this.options.instance.target );
            }
        }

    });

})(jQuery);
