/**
 * @author root
 */
(function() {
	YAHOO.namespace("HLK");
    YAHOO.HLK.FaqView = function(el, attr) {
        attr = attr || {};
        if (arguments.length == 1 && !YAHOO.lang.isString(el) && !el.nodeName) {
            attr = el; // treat first arg as attr object
            el = attr.element || null;
        }
        
        if (!el && !attr.element) { // create if we dont have one
            el = _createFaqViewElement.call(this, attr);
        }
    	YAHOO.HLK.FaqView.superclass.constructor.call(this, el, attr); 			
    };

    YAHOO.extend(YAHOO.HLK.FaqView, YAHOO.util.Element);
	
	var proto = YAHOO.HLK.FaqView.prototype;
	var Dom = YAHOO.util.Dom;
	
	proto.CATEGORIES_PARENT_CLASSNAME = 'categories';
	proto.QUESTIONS_PARENT_CLASSNAME = 'questions';
	
	proto._categoriesParent = null;
	proto._questionsParent = null;	
	
	proto.initAttributes = function(attr) {
		YAHOO.HLK.FaqView.superclass.initAttributes.call(this, attr);
		
		var el = this.get('element');
		
		this.setAttributeConfig('categories', {
            value: [],
            readOnly: true
        });
		
		this.setAttributeConfig('dataSrcAppend', {
			value: '?open&ce=1&qnfl=none'
		});		
		
		this._categoriesParent = 
        	this.getElementsByClassName(this.CATEGORIES_PARENT_CLASSNAME, 'div' )[0] || 
			_createCategoriesParent.call(this);
		
		this._questionsParent = 
        	this.getElementsByClassName(this.QUESTIONS_PARENT_CLASSNAME, 'div' )[0] || 
			_createQuestionsParent.call(this);	 	
			 	
		if ( this._categoriesParent ) {
           _initCategories.call(this);
        }	
			
		this.setAttributeConfig('activeCategory', {
            value: attr.activeCategory,
            method: function(category) {
                var activeCategory = this.get('activeCategory');

                if (category) {  
                    category.set('active', true);
                    category.set('contentVisible', true);					                
					this._questionsParent.style.display = 'block';
                } else {
					this._questionsParent.style.display = 'none';
				}
                
                if (activeCategory && activeCategory != category) {
                    activeCategory.set('active', false);
                    activeCategory.set('contentVisible',false);	
                }                
            }
    	});					
	};
	
	proto.addCategory = function(category, index) {		
		var self = this;
		
		var contentEl = category.get('contentEl');
		var headerEl = category.get('headerEl');
		
		if ( contentEl && !Dom.isAncestor(this._questionsParent, contentEl) ) {
            this._questionsParent.appendChild(contentEl);
        }

		if ( !category.get('active') )
            category.set('contentVisible', false, true); /* hide if not active */				
		
		var activate = function(e) {		
			YAHOO.util.Event.preventDefault(e);
			var silent = false;
			
			if (this == self.get('activeCategory'))
                silent = true; // dont fire activeTabChange if already active
            
			self.set('activeCategory', this, silent);
        };
        
        category.addListener( category.get('activationEvent'), activate);				
	};
	
	var _createFaqViewElement = function(attr) {
	    var el = document.createElement('div');
	
	    if ( this.CLASSNAME )
	        el.className = this.CLASSNAME;
				    
	    return el;
    };
	
	var _createCategoriesParent = function(attr) {				
		var el = document.createElement('div');

        if ( this.CATEGORIES_PARENT_CLASSNAME )
            el.className = this.CATEGORIES_PARENT_CLASSNAME;
		
        this.get('element').appendChild(el);
       
        return el;
	};
		
    var _createQuestionsParent = function(attr) {
		var self = this;
		var el = document.createElement('div');
				
        if ( this.QUESTIONS_PARENT_CLASSNAME )
            el.className = this.QUESTIONS_PARENT_CLASSNAME;
		
        this.get('element').appendChild(el);
		
		// Add close button
		var closeEl = document.createElement('a');
		closeEl.className = "btn-close";
		closeEl.innerHTML="Lukk";
		closeEl.href="#";
        el.appendChild(closeEl);

		var close = function(e) {	
			YAHOO.util.Event.preventDefault(e);
			self.set('activeCategory', null, false);
		}
		YAHOO.util.Event.addListener(closeEl, "click", close ); 

        return el;
	};
	
	var _initCategories = function() {
		var el = this.get('element'); 
		var categories = _getChildNodes(this._categoriesParent);
		//var questionElements = _getChildNodes(this._questionsParent);
		
		for (var i = 0, len = categories.length; i < len; ++i) {
			attr = {};
            
			if (categories[i].href)
				attr.dataSrc = categories[i].href;
		
			if (this.get('dataSrcAppend'))
				attr.dataSrcAppend = this.get('dataSrcAppend');
			
			var category = new YAHOO.HLK.FaqCategory(categories[i], attr);
			this.addCategory(category);
		}
	};
	
	var _getChildNodes = function(el) {
        var nodes = [];
        var childNodes = el.childNodes;
        
        for (var i = 0, len = childNodes.length; i < len; ++i) {
            if (childNodes[i].nodeType == 1) {
                nodes[nodes.length] = childNodes[i];
            }
        }    
        return nodes;
    };

})();