(function() {
	YAHOO.namespace("HLK");
    YAHOO.HLK.FaqQuestion = function(el, attr) {
		attr = attr || {};
		YAHOO.HLK.FaqQuestion.superclass.constructor.call(this, el, attr); 	
	};

    YAHOO.extend(YAHOO.HLK.FaqQuestion, YAHOO.util.Element);
	
	var proto = YAHOO.HLK.FaqQuestion.prototype;
	var Dom = YAHOO.util.Dom;
	
	proto.ACTIVE_CLASSNAME = 'selected';
	proto.LOADING_CLASSNAME = 'loader';
	proto.ERROR_CLASSNAME = 'error';
	
	proto.FORM_RESPONSE_CLASSNAME = 'form-response';
	proto.FEEDBACK_FROM_CLASSNAME = 'feedback';
	
	proto.ERROR_LOADING_TEXT = 'Feil under kommunikasjon med server';
	proto.LOADING_TEXT = '<div class="loader">Laster data...</div>';
	
	proto._loading = false;

	proto._feedbackForm = null;
	
	proto.initAttributes = function(attr) {
		YAHOO.HLK.FaqQuestion.superclass.initAttributes.call(this, attr);
		
		this.addClass('question');
		
		this.setAttributeConfig('answerEl', {
            value: attr.answerEl || 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('answer', {
            value: attr.answer,
            method: function(value) {
                this.get('answerEl').innerHTML = value;
            }
        });
		
		this.setAttributeConfig('activationEvent', {
            value: attr.activationEvent || 'click'
        });
		
		this.setAttributeConfig('dataSrc', {
            value: attr.dataSrc
        });	
		
		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('active', {
            value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
            method: function(value) {
                if (value === true) {
					Dom.addClass(this.get('element').parentNode, this.ACTIVE_CLASSNAME);
					this.get('answerEl').style.display = 'block';
					
					if ( this.get('dataSrc') ) {						
						if ( !this._loading && !(this.get('dataLoaded') && this.get('cacheData')) ) {                 
	                        _dataConnect.call(this);
						}
                    }
					this.get('element').blur();
                } else {
					Dom.removeClass(this.get('element').parentNode, this.ACTIVE_CLASSNAME);
					this.get('answerEl').style.display = 'none';	
                }
            }
        });		
		
		var _dataConnect = function() {	  					
			if (!YAHOO.util.Connect) {
				alert("YAHOO.util.Connect dependency not met")
				return false;
			}		
			this.set('answer', this.LOADING_TEXT);
        	this._loading = true; 
			
						
			this.dataConnection = YAHOO.util.Connect.asyncRequest(
	            this.get('loadMethod'),
	            this.get('dataSrc'), 
	            {
	                success: function(o) {						
						this.set('answer', "<h3>Svar:</h3>" + o.responseText);	                  
						
						_initFeedbackForm.call(this);
						
						this.set('dataLoaded', true);
	                    this.dataConnection = null;
							                   
	                    this._loading = false;
	                },
	                failure: function(o) {
						this.set('answer', o.ERROR_LOADING_TEXT);
	                    this.dataConnection = null;
	                    this._loading = false;
	                },
	                scope: this,
	                timeout: 5000
	            }
        	);
        	
		};
		
		var _sendForm = function(e, form) {			
			YAHOO.util.Connect.setForm(form); 

			// Temporarily disable the form
			for(var i=0; i<form.elements.length; i++) { 
				form.elements[i].disabled = true; 		
			}

			// Hide form
			Dom.setStyle(form, 'display', 'none');
			
			//Show loader
			var el = document.createElement('div');
			el.className = this.LOADING_CLASSNAME;	
			el.innerHTML = "Sender tilbakemelding";
			Dom.insertAfter(el, form);
								
			YAHOO.util.Connect.asyncRequest(
				'POST',
				form.action,
				{
					success: function(o) {						
						el.className = this.FORM_RESPONSE_CLASSNAME;	
						// Quick hack, the text we are looking for is inside div.cmsinputreturn
						el.innerHTML = o.responseText;
						var result = Dom.getElementsByClassName( 'cmsinputreturn', 'div', el );						
						el.innerHTML = result[0].innerHTML;
					},
					failure: function(o) {
						el.className = this.ERROR_CLASSNAME;
						el.innerHTML = this.ERROR_LOADING_TEXT;
					},
					scope: this,
					timeout: 5000
				}
			);
			
		};
		
		var _initFeedbackForm = function() {
			var self = this;
			
			this._feedbackForm = Dom.getElementsByClassName('feedback', 'form', this.get('answerEl') )[0] ||  alert("no feedback form");
			var inputs = this._feedbackForm.getElementsByTagName("input");			
	
			for (i = 0; i < inputs.length; i++) {
				if(inputs[i].type == 'submit')
					Dom.setStyle(inputs[i], 'display', 'none'); 
				if(inputs[i].type == 'radio') {
					YAHOO.util.Event.addListener(inputs[i], 'click', _sendForm, this._feedbackForm, this); 
				}
				
			}
		};	
	};
	
})();
