var elementsOne=new Array();
var counter=0;
elementsOne[counter]=new codehelper_vars(counter++);
elementsOne[counter]=new codehelper_one(counter++,'0'); 
elementsOne[counter]=new codehelper_one(counter++,'1');
elementsOne[counter]=new codehelper_one(counter++,'2');
elementsOne[counter]=new codehelper_one(counter++,'3');
elementsOne[counter]=new codehelper_one(counter++,'4');
elementsOne[counter]=new codehelper_one(counter++,'5');
elementsOne[counter]=new codehelper_one(counter++,'6');
elementsOne[counter]=new codehelper_one(counter++,'7');
elementsOne[counter]=new codehelper_one(counter++,'8');
elementsOne[counter]=new codehelper_one(counter++,'9');
elementsOne[counter]=new codehelper_one(counter++,'(');
elementsOne[counter]=new codehelper_one(counter++,')');
elementsOne[counter]=new codehelper_one(counter++,'+');
elementsOne[counter]=new codehelper_one(counter++,'-');
elementsOne[counter]=new codehelper_one(counter++,'*');
elementsOne[counter]=new codehelper_one(counter++,'/');
elementsOne[counter]=new codehelper_one(counter++,'=');
elementsOne[counter]=new codehelper_two(counter++,'END INSTRUCTION',';\n');
elementsOne[counter]=new codehelper_func(counter++,'SUM','$a=$var1+23;');
elementsOne[counter]=new codehelper_func(counter++,'SUB','$a=$var1-23;');
elementsOne[counter]=new codehelper_func(counter++,'DIV','$a=$var1/23;');
elementsOne[counter]=new codehelper_func(counter++,'MUL','$a=$var1*23;');
elementsOne[counter]=new codehelper_func(counter++,'POW','$a=pow($var1,2);');
elementsOne[counter]=new codehelper_func(counter++,'CEIL','$a=ceil($var1);');
elementsOne[counter]=new codehelper_func(counter++,'FLOOR','$a=floor($var1);');
elementsOne[counter]=new codehelper_func(counter++,'MIN','$a=min($var1,$var2,$var3);');
elementsOne[counter]=new codehelper_func(counter++,'MAX','$a=max($var1,$var2,$var3);');
elementsOne[counter]=new codehelper_func(counter++,'NUMBER FORMAT',"$a=number_format($var1,2,',','.');");

jQuery(document).ready(function($) {
	$('#addvarButton').click(function(){
		var valSelected=$('#addvar').val();
		if (valSelected=='') valSelected='newresult';
		editAreaLoader.insertTags('code_ta', "",'\n$'+valSelected+'= ;\n');
	    var cursor=editAreaLoader.getSelectionRange('code_ta')['start'];
	    editAreaLoader.setSelectionRange('code_ta', cursor+valSelected.length+3, cursor+valSelected.length+3);
	});
	$('#select_existing_fields').change(function(){
		var valSelected=jQuery(this).val();
    	editAreaLoader.insertTags('code_ta', "",'$'+valSelected);
    	var cursor=editAreaLoader.getSelectionRange('code_ta')['start'];
    	editAreaLoader.setSelectionRange('code_ta', cursor+valSelected.length+1, cursor+valSelected.length+1);
    	jQuery(this).find('option:first').attr('selected','selected');
	});
});


	
(function($){
	$.fn.codehelper = function() {
		return this.each( function() {
        	$.fn.codehelper.setHTMLDiv($(this));
        	$.fn.codehelper.setEvents($(this));
        });
    },    
	$.fn.codehelper.setHTMLDiv = function(elem) {
    	var container=$(elem).parent().find('.chcontainer');
    	var container0=$(elem).parent().find('.chcontainer0');
    	var container2=$(elem).parent().find('.chcontainer2');   
    	$(elementsOne).each(function(index,innerelem){
    		if(index==0){
    			$(container0).append(innerelem.toView(elem));
    		} else if(index>=19){
    			$(container2).append(innerelem.toView(elem));
    		} else {
    			$(container).append(innerelem.toView(elem));	
    		}    		
    	});
    },    
	$.fn.codehelper.setEvents = function(elem) {
    	var helpercontainer=$(elem).parent().find('.chcontainer,.chcontainer0,.chcontainer2');
    	$(helpercontainer).find('.chelem').each(function(index,innerelem){
    		var id=$(innerelem).attr('chid');
    		$(innerelem).bind(elementsOne[id].getEvent(), function() {
    			var id=$(this).attr('chid');
    			elementsOne[id].executeEvent(elem);
    		});
    	});
    }     
})(jQuery);

function codehelper_one(id,item) {	
	return new codehelper_two(id,item,item);
}

function codehelper_two(id,item,code) {
	var itself= this;
    this.item= item;
    this.code= code;
    this.id=id;
    this.getEvent= function() {
        return 'click';
    };
    this.toString= function() {
        return itself.item;
    };
    this.toView= function(dest) {    	
        return createCHelem(itself);
    };
    this.executeEvent= function(dest) {    	
    	var id=jQuery(dest).attr('id');    	
    	var cursor=editAreaLoader.getSelectionRange(id)['start'];
    	editAreaLoader.insertTags(id, "",itself.code );    	
    	editAreaLoader.setSelectionRange(id, cursor+1, cursor+1);
    };
}

function codehelper_func(id,item,func) {
	var itself= this;
    this.item= item;
    this.func= func;
    this.id=id;
    this.getEvent= function() {
        return 'click';
    };
    this.toString= function() {
        return itself.item;
    };
    this.toView= function(dest) {    	
        return createCHelem(itself);
    };
    this.executeEvent= function(dest) {    	
    	var id=jQuery(dest).attr('id');
    	var cursor=editAreaLoader.getSelectionRange(id)['start'];
    	editAreaLoader.insertTags(id, "",'\n'+itself.func+'\n');
    	editAreaLoader.setSelectionRange(id, cursor+itself.func.length+1, cursor+itself.func.length+1);
    };
}


function codehelper_vars(id) {
	var itself= this;
    this.id=id;
    this.idselect='idsel_12451';
    this.getEvent= function() {
        return 'change';
    };
    this.toString= function() {
        return itself.item;
    };
    this.loadData= function(taid,containerSelect) {
    	jQuery(containerSelect).empty();
    	var textarea=editAreaLoader.getValue(taid);
    	var lines = textarea.replace(/\r\n/g, "\n").split("\n");
    	var arrvars=new Array();
    	jQuery(lines).each(function(index,elem){
    		var elemToInsert=jQuery.trim(elem.match(/^\${1}[a-zA-Z0-9_]*/g));
    		var insert=true;
    		for(var i=0;i<arrvars.length;i++){
    			if (jQuery.trim(arrvars[i])==jQuery.trim(elemToInsert)){
    				insert=false;
    				break;
    			}
    		}
    		if (insert){    		    		
    			arrvars.push(elemToInsert);
    		} 
    	});    	
    	var opt=document.createElement('option');
		jQuery(opt).attr('value','');
		jQuery(opt).html(getLangTrads('CODE_HELPER_VARIABLES'));
		jQuery(containerSelect).append(opt);
    	jQuery(arrvars).each(function(index,elem){
    		var opt=document.createElement('option');
    		var value=elem;
    		if (jQuery.isArray(elem)) value=elem[0];
    		jQuery(opt).attr('value',value);
    		jQuery(opt).html(value.substring(1));
    		jQuery(containerSelect).append(opt);
    	});    	
    };
    this.toView= function(dest) {
    	var id=jQuery(dest).attr('id');
    	var container=document.createElement('div');    	
    	jQuery(container).addClass('input-prepend input-append');
    	jQuery(container).css('clear','both');
    	var containerText=document.createElement('span');
    	jQuery(containerText).addClass('add-on');    	
    	jQuery(containerText).html(getLangTrads('CODE_HELPER_VARIABLES_IN_CODE'));
    	jQuery(container).append(containerText);
    	var containerSelect=document.createElement('select');
    	jQuery(containerSelect).css('width','auto');
    	jQuery(containerSelect).css('margin-right','0px');
    	jQuery(containerSelect).addClass('chselect chelem');
    	jQuery(containerSelect).attr('id',this.idselect); 
    	jQuery(containerSelect).attr('chid',this.id);
    	this.loadData(id,containerSelect);
    	jQuery(container).append(containerSelect);
    	var containerRefresh=document.createElement('span');
    	jQuery(containerRefresh).addClass('add-on');
    	jQuery(containerRefresh).css('cursor','pointer');
    	jQuery(containerRefresh).attr('chid',this.id);
    	jQuery(containerRefresh).attr('taid',id);
    	jQuery(containerRefresh).attr('selectid',this.idselect);
    	var containerRefreshInner=document.createElement('i');
    	jQuery(containerRefreshInner).addClass('icon-refresh');
    	jQuery(containerRefreshInner).css('width','16px');
    	jQuery(containerRefreshInner).css('height','16px');
    	jQuery(containerRefreshInner).css('margin-top','3px');    	
    	jQuery(containerRefresh).append(containerRefreshInner);
    	jQuery(container).append(containerRefresh);
    	jQuery(containerRefresh).click(function(){
    		var chid=jQuery(this).attr('chid');
    		var taid=jQuery(this).attr('taid');
    		var selectid=jQuery(this).attr('selectid');
    		elementsOne[chid].loadData(taid,jQuery('#'+selectid));
    	});
    	return container; 
    };
    this.executeEvent= function(dest) {
    	var id=jQuery(dest).attr('id');
    	var valSelected=jQuery('#'+this.idselect).val();
    	editAreaLoader.insertTags(id, "",valSelected);
    	var cursor=editAreaLoader.getSelectionRange(id)['start'];
    	editAreaLoader.setSelectionRange(id, cursor+valSelected.length, cursor+valSelected.length);
    	jQuery('#'+this.idselect+' option:first').attr('selected','selected');
    };
}

function createCHelem(elem){
	var container=document.createElement('button');
	jQuery(container).attr('class','btn btn-mini chelem');
	jQuery(container).attr('type','button');
	jQuery(container).attr('chid',elem.id);
	jQuery(container).html(elem.item);
	return container; 
}