/**
 * @author root
 */
(function() {
	
	
	YAHOO.namespace("HLK");
	YAHOO.HLK.FaqCategory = 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.FaqCategory.superclass.constructor.call(this, el, attr);
	};

	YAHOO.extend(YAHOO.HLK.FaqCategory, YAHOO.util.Element);
	
	var proto = YAHOO.HLK.FaqCategory.prototype;
	var Dom = YAHOO.util.Dom;
	
	proto.ACTIVE_CLASSNAME = 'selected';
	proto.LOADING_CLASSNAME = 'loading';
	proto.ERROR_CLASSNAME = 'loading';
	
	proto.LOADING_TEXT = '<div class="loader">Laster data...</div>';
	proto.ERROR_LOADING_TEXT = '<div class="error">Feil ved lasting av data</div>';
	
	proto._loading = false;

	proto.addQuestion = function(question) {
		var self = this;		
		var questionEl = question.get('element');
		var answerEl = question.get('answerEl');
		
		questionEl.parentNode.appendChild(answerEl);
			
		if ( !question.get('active') )
            question.set('active', false); /* hide if not active */
		
		var activate = function(e) {		
			YAHOO.util.Event.preventDefault(e);
			self.set('activeQuestion', this);
		}
		
		question.addListener( question.get('activationEvent'), activate);
	};

	proto.initAttributes = function(attr) {
        attr = attr || {};
        YAHOO.HLK.FaqCategory.superclass.initAttributes.call(this, attr);

		var el = this.get('element');
		
		this.setAttributeConfig('activationEvent', {
            value: attr.activationEvent || 'click'
        });     		   
		
		this.setAttributeConfig('contentEl', {
            value: attr.contentEl || document.createElement('div'),
            method: function(value) {
                var current = this.get('contentEl');
				
                if (current) {
                    if (current == value) {
                        return false; // already set
                    }
                    this.replaceChild(value, current);
                }				
            }
        });
		
		this.setAttributeConfig('active', {
            value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
            method: function(value) {
                if (value === true) {
                    this.addClass(this.ACTIVE_CLASSNAME);                    
                } else {
                    this.removeClass(this.ACTIVE_CLASSNAME);
                  	var activeQuestion = this.get('activeQuestion');
					if( activeQuestion )
						activeQuestion.set('active', false);
                }
            },
            validator: function(value) {
                return YAHOO.lang.isBoolean(value) && !this.get('disabled') ;
            }
        });
		
		this.setAttributeConfig('contentVisible', {
            value: attr.contentVisible,
            method: function(value) {
                if (value) {
                    this.get('contentEl').style.display = 'block';					
					
                    if ( this.get('dataSrc') ) {
						if ( !this._loading && !(this.get('dataLoaded') && this.get('cacheData')) ) {                 
	                        _dataConnect.call(this);
						}
                    }
                } else {
                    this.get('contentEl').style.display = 'none';
                }
            },
            validator: YAHOO.lang.isBoolean
        });			
		
		this.setAttributeConfig('activeQuestion', {
            value: attr.activeQuestion,
            method: function(question) {
                var activeQuestion = this.get('activeQuestion');
				
                if (question && activeQuestion != question) {  
                    question.set('active', true);               
                } else if ( question == activeQuestion ) {						
                    question.set('active', !question.get('active'));               
				}
					                
                if (activeQuestion && activeQuestion != question) {
                    activeQuestion.set('active', false);
                }                
            }
    	});	
		
		this.setAttributeConfig('content', {
            value: attr.content,
            method: function(value) {												
                this.get('contentEl').innerHTML = value;
            }
        });
		
		this.setAttributeConfig('dataSrc', {
            value: attr.dataSrc
        });	

		this.setAttributeConfig('dataSrcAppend', {
            value: attr.dataSrcAppend || '',
            validator: YAHOO.lang.isString
        });
		
		this.setAttributeConfig('loadMethod', {
            value: attr.loadMethod || 'GET',
            validator: YAHOO.lang.isString
        });
		
		this.setAttributeConfig('dataLoaded', {
            value: false,
            validator: YAHOO.lang.isBoolean,
            writeOnce: true
        });
		
		this.setAttributeConfig('cacheData', {
            value: attr.cacheData || true,
            validator: YAHOO.lang.isBoolean
        });
		
		this.setAttributeConfig('title', {
            value: attr.cacheData || this.get('element').innerHTML,
            validator: YAHOO.lang.isString
        });
		
        var _initQuestions = function() {
			var el = this.get('contentEl');			
			var childNodes = el.getElementsByTagName("a");
			
			for (var i = 0, len = childNodes.length; i < len; ++i) {
				attr = {};
				if (childNodes[i].href) {
					attr.dataSrc = childNodes[i].href;
				}				
				var question = new YAHOO.HLK.FaqQuestion(childNodes[i], attr);
				this.addQuestion(question);
            }
		};
		
		var _dataConnect = function() {	      			
			if (!YAHOO.util.Connect) {
				alert("YAHOO.util.Connect dependency not met")
				return false;
			}
			this.set('content', this.LOADING_TEXT);
        	this._loading = true; 			
			this.dataConnection = YAHOO.util.Connect.asyncRequest(
	            this.get('loadMethod'),
	            this.get('dataSrc') + this.get('dataSrcAppend'), 
	            {
	                success: function(o) {						
						this.set('content', "<h3>" + this.get('title') + ":</h3>" + o.responseText);
	                    _initQuestions.call(this);
						
						this.set('dataLoaded', true);
	                    this.dataConnection = null;
							                  
	                    this._loading = false;
	                    
	                },
	                failure: function(o) {
						this.set('content', this.ERROR_LOADING_TEXT);
	                    this.dataConnection = null;
	                    this._loading = false;
	                },
	                scope: this,
	                timeout: 5000
	            }
        	);
	 	};

	};		
})();