/*
	omq-self-support.js
*/
(function($){
	// help function
	jQuery.fn.outerHTML = function() {
		return $('<div>').append( this.eq(0).clone() ).html();
	};
	
	$.fn.omq_self_support = function(textElements, sendButtons, options){
		// set default options
		options = $.extend({
		    buildURL: function(path){
    				return ("https://127.0.0.1:8080/debug"+path);
    			},
    		solutionListSendHandler: function(list){
    				// empty
    			},
			ajaxErrorHandler: function(XMLHttpRequest, textStatus, errorThrown){
					var errorStr = "";
					// http request
					errorStr += (XMLHttpRequest.status != 0)? 
									"RESPONSE: "+XMLHttpRequest.statusText + " ["+XMLHttpRequest.status+"]\n"
									: "";
					// text status
					errorStr += (textStatus != "")?
									"TEXT: "+textStatus+"\n"
									: "";					
					// error thrown
					errorStr += (errorThrown != null)?
									"EXCEPTION: "+errorThrown+"\n"
									: "";						
				},
			logHandler: function(msg){
					if(window.console){ 
						window.console.log(msg);
					}
				},
			problemLimit:			'3',
			maxLenProblemPreview:	'90',
            maxLenSolutionPreview:  '30',
			analyserVersion:		'2',
			locale:                 de_DE,
			cssPattern:				css_pattern,
            
            size:                   'large',
            cssFile:                '',
            style:                  {}
		}, options);
		
        // load css file
        if(options.cssFile != null && options.cssFile != '') {
            $.get(options.widgetPath + options.cssFile, function(css) {
                $("head").append("<style type='text/css'>"+css+"</style>");
            }).error(function(x,e) {
                alert('Fehler: CSS-Datei konnte nicht geladen werden! \n\n HTTP status codes: '+x.status);
            });
        }
        
        // check lang
        if(options.lang != null){
            // rewrite english
            options.lang = (options.lang == "EN_GB")? "EN": options.lang;
        }
        
        // check if elements, textElements and sendButtons have the same length.
        if( (this.length != textElements.length) || 
            (this.length != sendButtons.length)){
			alert("Error: The widget is implemented in the wrong way: missing text elements or send button.");
		}
    
		// build object
		return this.each(function(i){
            new OMQSelfSupport(
                this,
                textElements[i],
                sendButtons[i],
                options);
		});
	}
})(jQuery);

var OMQSelfSupport = Obj.extend({
	// private
	_element: null,
	_options: null,

	_problems: null,	
	_text: null,
    _sendButton: null,

    _tracker: null,

	// public
	init: function(element, textElement, sendButton, options){
			var self = this;
      
			this._element 		= element;
			this._options 		= options;

			this._options = $.extend({
				ajaxErrorHandler: function(XMLHttpRequest, textStatus, errorThrown){
					var errorStr = self._options.locale.CONNECTION_ERROR;
					MessageBox(self, self._options).error(errorStr);
				}
			}, this._options);
			
			// add ui style
			$(this._element).addClass("omq");
			$(this._element).addClass("ui_widget");
            
            // set options
            this._options = $.extend({
                    ajax: 	new AjaxPhpProxy(this._options)
                }, this._options);            
			
			// init children
            this._tracker       = new Tracker(this, this._options);
            
			this._problems		= new Problems(this, this._options);
            this._text      	= new DynamicText(this, textElement, this._options);
            this._sendButton    = new SendButton(this, sendButton, this._options);
		},
        
    getElement: function(){
			return this._element;
		},
		
    getProblems: function() {
			return this._problems;
		},

    getTextElement: function() {
			return this._text;
		},
      
    getTracker: function() {
            return this._tracker;
        },
      
    textAnalysedHandler: function(problemsXML){
			// update problem list
			this._problems
				.update(problemsXML);
		}
});
