/*
	problem.js
*/
var Problems = Obj.extend({
	// private
	_element: null,
	_parent:  null,
	_options: null,
   
	PROBLEMS_TEMPL_EMPTY: function(locale){
			return ("<div class='empty_list ui_popup_font ui_overlay ui_overflow ui_border ui_border_bot ui_pad'>"+
					"	<h2 class='ui_popup_hl_font'>"+locale.EMPTY_PROBLEMS_LIST_TITLE+"</h2>"+
					"</div>");
		},
		
	PROBLEMS_TEMPL: function(locale){
			return ("<div class='ui_overflow ui_border ui_border_bot' >"+
					"	<form class='problems'></form>"+
                    "</div>"+
                    "<div class='omq_link_div ui_min_width'>"+
			        "	<p>"+locale.OMQ_LINK_TEXT+"<a class='omq_link ui_link' href='http://www.omq.de' target='blank'>OMQ</a></p>"+
					"</div>");
		},
	
	PROBLEM_TEMPL: function(problem){
			return ("<input type='hidden' name='id' value='"+problem.id+"' />"+
					"<div class='problem ui_problem ui_link'>"+
					"	<h2 class='ui_popup_hl_font ui_marg_none'>"+problem.description+"</h2>"+
					"</div>");
		},
	
	G1: '1',
	G2: '2',
	
	// public
	init: function(parent, options){
			var self = this;
	
			$(this.PROBLEMS_TEMPL_EMPTY(options.locale)).appendTo(parent.getElement());
			
			this._element 		= $(this.PROBLEMS_TEMPL(options.locale)).appendTo(parent.getElement());
			this._options 		= options;
			this._parent 		= parent;

			this._problemList	= new Array();
		},
		
	getElement: function(){
			return this._element;
		},
		
	update: function(problemsXML){
			// remove all old problems
			$(".problems", this._element)
                .empty();
			
			// if the tag list is empty then return
    		if(problemsXML.length < 1){
        		return;
    		}
    		
    		// load defualt page 0
    		this.setAsXML(problemsXML);
    	},
    
    getCurrentProblemList: function() {
    		return this._problemList;
    	},
    	
	setAsXML: function(xml){
			var self = this;
            
            var i = -1;
            
			// build object
			var problems = $("problem", xml)
				.map(function(){
                    i++;
                
					return {
						//id:     		$("id:first", this).text(),
                        id:     		$($(this).parent().find("problem > id").get(i)).text(),
                        //description:	$("description:not(solutions > solution > description)", this).text(),
                        description:	$($(this).parent().find("problem > description").get(i)).text(),
						tags:           $("tags > tag", this).map(function(){
                        	return {
                            	id:     	$("id", this ).text(),
                            	value:  	$("representative", this).text()}
                        	}),
                    	solutions:      $("solutions > solution", this).map(function(){
                        	return {
                            	id:             $("id", this ).text(),
                            	title:          $("title", this).text(),
                            	description:    $("description", this).text()}
                        })
					}
				});
			
			this._problemList = problems;
			//show or hide user manual
			if(problems.length < 1){
				$(".empty_list", self._parent.getElement()).removeClass("ui_hidden");
        		return;
    		}else {
    			$(".empty_list", self._parent.getElement()).addClass("ui_hidden");
    		}
			
			// build dom for all problems
			var problemsListDOM = $(problems)
				.each(function(){
					var problem = this;
						
                    // create problem with normal description
                    var problemDOM = $(self.PROBLEM_TEMPL(this));
                    
                    // click handler for description drop down
                    $(problemDOM)
                        .click(function(){
                                // open popup 
                                var problemPopup = new ProblemPopup(problem, self._options);
                                
                                // fire tracker problem clicked                                
                                self._parent.getTracker()
                                    .problemClickHandler(
                                        self._parent.getTextElement().getText(), 
                                        problem
                                    );
                            });
                                
					// add problem
    				$(".problems", self._element).append(problemDOM);
    				
				});
		}
});
