/******************************************************************************************
*
*	META EXPR BUILDER
*
*******************************************************************************************/
   	
   	if (!Atira) var Atira = {}
   
   /**
	* Constructor for the meta expr builder
	*/
    Atira.ExprBuilder = function(service, windowIds){
   		
   		
   		
   		if(service){
   			this.service = service;
   		}else{
   			alert('ArgumentException: you must specify the engine editor service');
   			return false;
   		}
   		
		if(windowIds){
   			this.wins = {
			 		editor: new Atira.FloatingWindow(windowIds['editorId'], {width: 500, height: 350, footertext: '* Build the expression by adding values and functions', oktext: 'BUILD', canceltext:'Cancel'})
			};	
	   	}else{
   			alert('ArgumentException: you must specify the editor window id in order to create an Expression Builder');
	   		return false;
	   	}
	   	
	   	this.expr_id = 'expr_builder_expression_div';
	   	
    }
   	
   	Atira.ExprBuilder.prototype.getWindow = function(identifier){
   		if(identifier){
   			return this.wins[identifier];
   		}
   		return false;
   	} 
   	
   	
   	Atira.ExprBuilder.prototype.showWindow = function(winId, top, rowIdentifier, excelIdentifier){
   	
   			var win = this.wins[winId];
   			var service = this.service;
   			var operators = this.operators;
			var functions = this.functions;
			var expr_id = this.expr_id;
			
			if(win && winId == 'editor'){
			
				win.setDynamicTitle('Create new expression for ' + excelIdentifier);
				win.disableDynamicContentPaneScrolling();
	
				service.newExpr(rowIdentifier, excelIdentifier,
					
					function(expr){	
						
						win.setDynamicContent(Atira.ExprBuilder.renderEditorContent(expr_id, expr, operators, functions));
						win.setTop(top);
						win.show();
					
					});
					
				
			
			}else{
				alert('Internal error: Unable to open the editor');
			}
   			
   			return false;
   	}
   	
   	Atira.ExprBuilder.prototype.hideWindow = function(identifier){
   		if(identifier){
   			var win = this.wins[identifier];
			win.hide();   		
   		}else{
   			alert('Internal error: Unable to close the '+identifier+' window');
   		}
   		return false;
   	}
   		 
	Atira.ExprBuilder.prototype.setFunctions = function(data){
		this.functions = data;
	} 
	
	Atira.ExprBuilder.prototype.setOperators = function(data){
		this.operators = data;
	} 
	
	Atira.ExprBuilder.prototype.getExpressionHTMLElement = function(){
		return document.getElementById(this.expr_id); 
	} 

/******************************************************************************************
*
*	EXPR MANIPULATION
*
*******************************************************************************************/

   /**
	* TODO
	*
	* @return 			void 
	*/
	Atira.ExprBuilder.prototype.addFunction = function(obj){
		var parent = obj.parentNode;
		var select = parent.getElementsByTagName('select').namedItem('selector');
		
		var value = select.value;
		
		this.service.addFunction(value, this.updateExpr); 
		
		return false;
	
	}
   
   /**
	* TODO
	*
	* @return 			void 
	*/
	Atira.ExprBuilder.prototype.addOperator = function(obj){
		var parent = obj.parentNode;
		var select = parent.getElementsByTagName('select').namedItem('selector');
		
		var value = select.value;
		
		this.service.addOperator(value, this.updateExpr); 
		
		return false;
	
	}
	
	
	/**
	* TODO
	*
	* @return 			void 
	*/
	Atira.ExprBuilder.prototype.addReference = function(obj){
		
		var parent = obj.parentNode;
		var input = parent.getElementsByTagName('input').namedItem('ref');
		
		var value = input.value;
		
		this.service.addReference(input.value, this.updateExpr); 
		
		return false;
		
	}
	
	/**
	* TODO
	*
	* @return 			void 
	*/
	Atira.ExprBuilder.prototype.addBoolean = function(obj){
		
		var parent = obj.parentNode;
		var input = parent.getElementsByTagName('input').namedItem('bool');
		
		var value = input.value;
		
		this.service.addBool(input.value, this.updateExpr); 
		
		return false;
		
	}
	
	/**
	* TODO
	*
	* @return 			void 
	*/
	Atira.ExprBuilder.prototype.addString = function(obj){
		
		var parent = obj.parentNode;
		var input = parent.getElementsByTagName('input').namedItem('str');
		
		var value = input.value;
		
		this.service.addString(input.value, this.updateExpr); 
		
		return false;
		
	}
	
	/**
	* TODO
	*
	* @return 			void 
	*/
	Atira.ExprBuilder.prototype.addNumber = function(obj){
		
		var parent = obj.parentNode;
		var input = parent.getElementsByTagName('input').namedItem('num');
		
		var value = input.value;
		
		this.service.addNumber(input.value, this.updateExpr); 
		
		return false;
		
	}
   
   /**
	* TODO
	*
	* @return 			void 
	*/
	Atira.ExprBuilder.prototype.updateExpr = function(expr){
	
		var builder = CountSystem.getInstance().getPageController().exprBuilder;
		var element = builder.getExpressionHTMLElement();
		
		if(element && builder){
			element.innerHTML = Atira.ExprBuilder.renderCurrentExpr(expr);
		}else{
			alert('Internal error: unable to update expression');
		}
	
		return false;
	}


/******************************************************************************************
*
*	STATELESS RENDERING METHODS  
*
*******************************************************************************************/

	/**
	* Renders the content inside the editor window
	*
	* @return 			a html string
	*/
	Atira.ExprBuilder.renderEditorContent = function(expr_id, expr, operators, functions) {
		
		var html = '';
		
					
			html += '<label>*Expression</label>';
					
					
			html+= '<div id="'+expr_id+'">';
			html+=	Atira.ExprBuilder.renderCurrentExpr(expr);	
			html+= '</div>';
			
			html+=	Atira.ExprBuilder.renderAddOperator(operators);	
			
			
			
			html+=	Atira.ExprBuilder.renderAddReference();	
			
			html+=	Atira.ExprBuilder.renderAddFunction(functions);	
			
			html+=	Atira.ExprBuilder.renderAddBoolean();	
			
			html+=	Atira.ExprBuilder.renderAddNumber();	
			
			html+=	Atira.ExprBuilder.renderAddString();	
					
					
			html+=	'<div style="clear:both;"></div>';
					
	
		
		return html;
	}
  
   /**
	* Renders the current meta expression
	*
	* @return 			a html string
	*/
	Atira.ExprBuilder.renderCurrentExpr = function(expr) {
		
		var html = '';
		
		html += '<b>['+expr+']</b>';
		
		return html;
	}
	
   /**
	* Renders the expression creator component
	*
	* @return			a html string
	*/
	Atira.ExprBuilder.renderAddReference = function() {
	
		var html = '';
		
		html += '<div style="margin-top: 10px; display: block; font-size: 9pt;">' +
		
				'<i style="margin-right: 10px;">Add reference</i>' +
		
				'<input name="ref" type="text" size="10" value="A1"/>' +
								
				'<button onclick="CountSystem.getInstance().getPageController().exprBuilder.addReference(this)">add</button>' + 
		
				'</div>';		 
		
		
		return html;
	}
	
	/**
	* Renders the expression creator component
	*
	* @return			a html string
	*/
	Atira.ExprBuilder.renderAddOperator = function(operators) {
	
		var html = '';
		
		html += '<div style="margin-top: 10px; display: block; font-size: 9pt;">' +
		
				'<i style="margin-right: 10px;">Add operator</i>' +
		
				'<select name="selector">' +
									
					'<option>Select operator</option>';
									
						if(operators){
										
							for(var I in operators){
								html += '<option value="'+operators[I]+'">'+ I +'</option>';
							}
									
						}
						
		html+=	'</select>' +
								
				'<button onclick="CountSystem.getInstance().getPageController().exprBuilder.addOperator(this)">add</button>' + 
		
				'</div>';		 
		
		
		return html;
	}
	
	/**
	* Renders the expression creator component
	*
	* @return			a html string
	*/
	Atira.ExprBuilder.renderAddFunction = function(functions) {
	
		var html = '';
		
		html += '<div style="margin-top: 10px; display: block; font-size: 9pt;">' +
		
				'<i style="margin-right: 10px;">Add function</i>' +
		
				'<select name="selector">' +
									
					'<option>Select function</option>';
									
						if(functions){
										
							for(var i = 0; i<functions.length; i++){
								html += '<option value="'+functions[i]+'">'+ functions[i] +'</option>';
							}
									
						}
						
		html+=	'</select>' +
								
				'<button onclick="CountSystem.getInstance().getPageController().exprBuilder.addFunction(this)">add</button>' + 
		
				'</div>';		 
		
		
		return html;
	}
	
	
   /**
	* Renders the expression creator component
	*
	* @return			a html string
	*/
	Atira.ExprBuilder.renderAddBoolean = function() {
	
		var html = '';
		
		html += '<div style="margin-top: 10px; display: block; font-size: 9pt;">' +
		
				'<i style="margin-right: 10px;">Add boolean</i>' +
		
				'<input name="bool" type="text" size="10" value="true"/>' +
								
				'<button onclick="CountSystem.getInstance().getPageController().exprBuilder.addBoolean(this)">add</button>' + 
		
				'</div>';		 
		
		
		return html;
	}
   /**
	* Renders the expression creator component
	*
	* @return			a html string
	*/
	Atira.ExprBuilder.renderAddNumber = function() {
	
		var html = '';
		
		html += '<div style="margin-top: 10px; display: block; font-size: 9pt;">' +
		
				'<i style="margin-right: 10px;">Add number</i>' +
		
				'<input name="num" type="text" size="10" value="0.0"/>' +
								
				'<button onclick="CountSystem.getInstance().getPageController().exprBuilder.addNumber(this)">add</button>' + 
		
				'</div>';		 
		
		
		return html;
	}
	
   /**
	* Renders the expression creator component
	*
	* @return			a html string
	*/
	Atira.ExprBuilder.renderAddString = function() {
	
		var html = '';
		
		html += '<div style="margin-top: 10px; display: block; font-size: 9pt;">' +
		
				'<i style="margin-right: 10px;">Add string</i>' +
		
				'<input name="str" type="text" size="30" value="undefined"/>' +
								
				'<button onclick="CountSystem.getInstance().getPageController().exprBuilder.addString(this)">add</button>' + 
		
				'</div>';		 
		
		
		return html;
	}