/*
 * 
 * jQuery Google Charts - Dashboard Plugin 0.9
 * 
 * $Date:2008-06-08 23:11:03 +0200 (dom, 08 giu 2008) $
 * $Rev:162 $
 * 
 * @requires
 * jGCharts Base
 * 
 * Metadata
 * Farbtastic
 * Tablelib + Iterators - Greg Weber
 * Sugar Arrays - Dustin Diaz | http://www.dustindiaz.com
 *
 * Copyright (c) 2008 Massimiliano Balestrieri
 * Examples and docs at: http://maxb.net/blog/
 * Licensed GPL licenses:
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jGDashTpl = function(options){
};
jGDashGet = function(options){
}

jQuery.fn.jgdash = function(options){

	jQuery('head').append('<link type="text/css" href="'+options.url+'jgdash.css" rel="stylesheet" />');
	jQuery(this).load(options.url+'template.html', function(){
		jGDash(jQuery(this), options);			
	});

};

jGDash = function(el, options){
    return el.each(function(){
    	
    	var dash = el;
    	
    	if(!options.target) 
    		throw new Error("Target not set");
    	if(!options.sources) 
    		throw new Error("Sources not set");
    	if(!options.debug) 
            throw new Error("Debug div not set");
        
    	
    	var selector = options.target;
    	var sources = options.sources;
    	var debug = options.debug;
    	
    	//model
	    var type = "lc";//
	    var chart_h = 450;
	    var chart_w = 650;
	    
	    //model - source
	    var source = 0;
	    var axis = [];
	    var axis_step = 1;
	    var legend = [];
	    var tables = [];
	    
	    //model - options
	    var title = false;
	    var title_size = 10;
	    var title_color = "000000";
	    var bar_width = 30;
	    var bar_spacing = 1;
	    var fillarea = true;
        var filltop = false;
        var fillbottom = true;
	    
	    //model - series colors
	    var colors = ["FFFF00","FFCC00","99CC00"];
	    
	    //model - style
	    var bg = false;
	    var bg_type = "solid"; 
	    var bg_offset = false;
	    var bg_angle = 90;
	    var bg_width = 20;
	    var bg_trasparency = false;
	    
	    var chbg = false;
	    var chbg_type = "solid"; 
	    var chbg_offset = false;
	    var chbg_angle = 90;
	    var chbg_width = 20;
	    var chbg_trasparency = false;
	    
	    //model - grid
	    var grid = false;
	    var grid_x = 10;
	    var grid_y = 10;
	    var grid_line = 10;
	    var grid_blank = 10;
	    
	    //model - line style
	    var lines = [[1,1,0], [1,1,0], [1,1,0]];
	    var line_style_thickness = 1;
	    var line_style_line = 1;
	    var line_style_blank = 0;
	    

	    var dochart = function(el){
	    	jQuery(el).jgcharts({
                
                //MANDATORY - source
                data        : tables[source],
                
                //OPTIONAL 

                //source 
                legend      : legend[source],
                axis_labels : axis[source],

                //opzioni 
                size        : chart_w + "x" + chart_h,
                type        : type,

                axis_step   : axis_step,

                title       : title,
                title_color : title_color,
                title_size  : title_size,
                bar_width   : bar_width,
                bar_spacing : bar_spacing,
                fillarea    : fillarea, 
                fillbottom  : fillbottom,
                filltop     : filltop, 
                //series colors
                colors      : colors,
                
                //series line options
                lines       : lines,
                
                //style
                bg          : bg,
                bg_type     : bg_type,
                bg_angle    : bg_angle,
                bg_offset   : bg_offset,
                bg_width    : bg_width,
                bg_trasparency: bg_trasparency,
                
                chbg        : chbg,
                chbg_type   : chbg_type,
                chbg_angle  : chbg_angle,
                chbg_offset : chbg_offset,
                chbg_width  : chbg_width,
                chbg_trasparency: chbg_trasparency,
                
                //grid
                grid        : grid,
                grid_x      : grid_x,
                grid_y      : grid_y,
                grid_line   : grid_line,
                grid_blank  : grid_blank
                
            });
	    };
	    
		
	    jQuery(selector).bind("init", function(){
	        //console.log(legend[source]);
	        //console.log(tables[source]);
	        
	        jQuery(this).empty();
            dochart(this);
            
            jQuery(debug).empty();
            jQuery(debug).html(
                "<h1>Metadata</h1><textarea style=\"width:100%;height:250px;\">" + 
                _replace_var(_metadata) +
                "</textarea>"+
                "<h1>jgchart constructor</h1><textarea style=\"width:100%;height:250px;\">" +  
                _replace_var(_hdoc) +
                "</textarea>"  
            );
            
	    });
	    //SERIES 
	    jQuery(selector).bind("series", function(){
	    	jQuery('#line-select').empty();
	    	jQuery('#colors-select').empty();
	    	if(tables[source][0])
            	for(var x=0; x < tables[source][0].length; x++){
                	jQuery('#line-select').append('<option value="'+(x + 1)+'">serie '+ (x+1) +'</option>');
            		jQuery('#colors-select').append('<option value="'+(x + 1)+'">serie '+ (x+1) +'</option>');
            	}
	        jQuery('#f-series-color', dash).farbtastic('#series-color');        		
        });
	    
        
	    //dash
	    jQuery(".dash h3",dash)
	    .each(function(){
	    	jQuery(this).next().hide();
	    })
	    .click(function(){
	    	jQuery(this).next().toggle();	
	    });
	    //toggle
	    jQuery(".myhead p a",dash).click(function(){
	    	jQuery(".mypanel").toggle();
	        return false;
	    });
	    
	    //source
	    var obj_to_array = function(obj){
	        var _arr = [];
	        var x = 0;
	        for(o in obj){
	            _arr[x] = [];
	            for(z in obj[o])
	                if(typeof obj[o][z] !== "undefined")
	                    _arr[x].push(obj[o][z]);
	                //if(obj[o][z])
	                   
	            x++;
	        }
	        return _arr;
	    };
	    sources.forEach(function(val){
	    	axis.push( jQuery.map( jQuery("#" + val).find("tbody > tr > th.serie"),
		                           function(th) { return jQuery(th).text(); }
		    ));
	    	tables.push( obj_to_array( jQuery.table("#" + val).objs() ) );
	    	legend.push( jQuery.map( jQuery("#" + val).find("thead > tr:last > th.serie"),
	                             function(th) { return jQuery(th).text(); }
	    	));
	    });
	    
	    //sources
	    jQuery("#chart-sources",this).empty();
	    sources.forEach(function(el, index){
	        //console.log(index);
	        var _selected = (el == source) ? ' selected=selected' : '';
	        jQuery("#chart-sources").append('<option value="'+index+'" '+ _selected +'>'+ el +'</option>');
	    });
	    jQuery("#chart-sources",this).change(function(){
	        source = parseInt(jQuery(this).val());
	        jQuery(selector).trigger("init");
	        jQuery(selector).trigger("series");
	    });
	    
	    //chart type
	    jQuery("#chart-type",dash).empty();
	    jGCharts.Api.type.forEach(function(el){
	        var _selected = (el == type) ? ' selected=selected' : '';
	        jQuery("#chart-type").append('<option'+ _selected +'>'+ el +'</option>');
	    });
	    jQuery("#chart-type",dash).change(function(){
	        type = jQuery(this).val(); 
	        jQuery(selector).trigger("init");
	    });    
	    
	    
	    
	    //size
	    jQuery("#chart-h",this).text(chart_h);
	    jQuery("#chart-w",this).text(chart_w);
	
	    //chart-h
	    jQuery("#chart-h-p a",dash)
	    .eq(0)
	    .click(function(){
	        if(chart_h > 100)
	            chart_h -= 50;
	            
	        jQuery("#chart-h",dash).text(chart_h);     
	        jQuery(selector).trigger("init");
	    }).end()
	    .eq(1)
	    .click(function(){
	        if((chart_h * chart_w) < 300000)
	            chart_h += 50;
	        jQuery("#chart-h",dash).text(chart_h);     
	        jQuery(selector).trigger("init");
	    }).end();
	    
	    //chart-w
	    jQuery("#chart-w-p a",dash)
	    .eq(0)
	    .click(function(){
	        if(chart_w > 100)
	            chart_w -= 50;
	            
	        jQuery("#chart-w",dash).text(chart_w);     
	        jQuery(selector).trigger("init");
	    }).end()
	    .eq(1)
	    .click(function(){
	        if((chart_h * chart_w) < 300000)
	            chart_w += 50;
	        jQuery("#chart-w",dash).text(chart_w);     
	        jQuery(selector).trigger("init");
	    }).end();
	    
	    // TITLE
	    jQuery("#chart-title",dash).change(function(){
	    	title = jQuery(this).val(); 
	    	jQuery(selector).trigger("init");	
	    });
	    // TITLE STYLE
	    jQuery("#title-size",dash).text(title_size);
	    jQuery("#p-title-size a",dash)
	    .eq(0)
	    .click(function(){
	        if(title_size > 8)
	            title_size -= 1;
	        
	        title_color = jQuery("#title-color",dash).val().replace("#","");
	        jQuery("#title-size",dash).text(title_size);     
	        jQuery(selector).trigger("init");
	    }).end()
	    .eq(1)
	    .click(function(){
	        if(title_size < 100)
	            title_size += 1;
	        
	        title_color = jQuery("#title-color",dash).val().replace("#","");
	        jQuery("#title-size",dash).text(title_size);     
	        jQuery(selector).trigger("init");
	    }).end();
	    jQuery('#f-title-color', dash).farbtastic('#title-color');
	    jQuery('form', dash).submit(function(){
            title_color = jQuery('#title-color').val().replace("#","");
            jQuery(selector).trigger("init");  
            return false;
        });   
	    
	    // OPTIONS
	    //axis step
        jQuery("#axis-step",dash).text(axis_step);
        jQuery("#p-axis-step a",dash)
        .eq(0)
        .click(function(){
            if(axis_step > 0)
                axis_step -= 1;
                
            jQuery("#axis-step",dash).text(axis_step);     
            jQuery(selector).trigger("init");
        }).end()
        .eq(1)
        .click(function(){
            axis_step += 1;
            
            jQuery("#axis-step",dash).text(axis_step);     
            jQuery(selector).trigger("init");
        }).end();
        
	    // BAR OPTIONS
	    jQuery("#bar-width",dash).text(bar_width);
	    jQuery("#bar-spacing",dash).text(bar_spacing);
	    
	    //bar width
	    jQuery("#p-bar-width a",dash)
	    .eq(0)
	    .click(function(){
	        if(bar_width > 10)
	            bar_width -= 10;
	        else if(bar_width > 0)
	            bar_width -= 1;
	            
	        jQuery("#bar-width",dash).text(bar_width);     
	        jQuery(selector).trigger("init");
	    }).end()
	    .eq(1)
	    .click(function(){
	        if(bar_width < 10)
	            bar_width += 1;
	        else
	            bar_width += 10;
	        jQuery("#bar-width",dash).text(bar_width);     
	        jQuery(selector).trigger("init");
	    }).end();
	    
	    //bar spacing
	    jQuery("#p-bar-spacing a", dash)
	    .eq(0)
	    .click(function(){
	        if(bar_spacing > 0)
	            bar_spacing -= 1;
	            
	        jQuery("#bar-spacing",dash).text(bar_spacing);     
	        jQuery(selector).trigger("init");
	    }).end()
	    .eq(1)
	    .click(function(){
	        if(bar_width < 100)
	            bar_spacing += 1;
	            
	        jQuery("#bar-spacing",dash).text(bar_spacing);     
	        jQuery(selector).trigger("init");
	    }).end();
	    
	    // LINE OPTIONS
	   	//fill area
        if(fillarea)
            jQuery("#fill-area", dash).attr("checked","checked");
        else
            jQuery("#fill-area", dash).attr("checked","");
        
        jQuery("#fill-area", dash).change(function(){
            fillarea = jQuery("#fill-area").attr("checked") ? true : false;
            jQuery(selector).trigger("init");
            return false;  
        });
        
        //fill bottom
        if(fillbottom)
            jQuery("#fill-bottom", dash).attr("checked","checked");
        else
            jQuery("#fill-bottom", dash).attr("checked","");
        
        jQuery("#fill-bottom", dash).change(function(){
            fillbottom = jQuery("#fill-bottom").attr("checked") ? true : false;
            jQuery(selector).trigger("init");
            return false;  
        });
        //fill top
        if(filltop)
            jQuery("#fill-top", dash).attr("checked","checked");
        else
            jQuery("#fill-top", dash).attr("checked","");
        
        jQuery("#fill-top", dash).change(function(){
            filltop = jQuery("#fill-top").attr("checked") ? true : false;
            jQuery(selector).trigger("init");
            return false;  
        });     
	    
	    /////////////////////////////////////////////////////////////////////////////////////
	    ////////////////////////////////BACKGROUND///////////////////////////////////////////
	    /////////////////////////////////////////////////////////////////////////////////////
	    
	    //background color
	    jQuery('#f-bg-color', dash).farbtastic('#bg-color');
	    jQuery('#f-bgo-color', dash).farbtastic('#bgo-color');
	    jQuery('form', dash).submit(function(){
            
            bg = jQuery('#bg-color', dash).val().replace("#","");
            bg_offset = jQuery('#bgo-color', dash).val().replace("#","");

            jQuery(selector).trigger("init");  
            return false;
        });
        //background type
        jQuery("#bg-type", dash).change(function(){
        	bg_type = jQuery(this).val();
			jQuery("#target").trigger("init");
        });
        
        //background angle
        jQuery("#bg-angle", dash).text(bg_angle);
        
        jQuery("#p-bg-angle a", dash)
        .eq(0)
        .click(function(){
            if(bg_angle > 0)
                bg_angle -= 10;
                
            jQuery("#bg-angle", dash).text(bg_angle);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(bg_angle < 90)
                bg_angle += 10;
            jQuery("#bg-angle", dash).text(bg_angle);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        //background width
        jQuery("#bg-width", dash).text(bg_width);
        
        jQuery("#p-bg-width a", dash)
        .eq(0)
        .click(function(){
            if(bg_width > 0)
                bg_width -= 10;
                
            jQuery("#bg-width", dash).text(bg_width);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(bg_width < 100)
                bg_width += 10;
            jQuery("#bg-width", dash).text(bg_width);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        
        //trasparency
        var bt = (!bg_trasparency) ? 0 : parseInt(bg_trasparency);
        jQuery("#bg-trasparency").text(bt);
        
        //background trasparency flag
        jQuery("#bg-trasparency-active", dash).change(function(){
            if(jQuery(this).attr("checked")){
                bg_trasparency = bt = 99;
                jQuery("#bg-trasparency", dash).text(bt);
            }else{
            	bg_trasparency = false;
            	bt = 0;
            	jQuery("#bg-trasparency", dash).text(bt);
            }
            jQuery(selector).trigger("init");
            return false;  
        });
        //background trasparency
        jQuery("#p-bg-trasparency a", dash)
        .eq(0)
        .click(function(){
            if(bg_trasparency > 0)
                bg_trasparency -= 10;
                
            jQuery("#bg-trasparency", dash).text(bg_trasparency);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(bg_trasparency < 100)
                bg_trasparency += 10;
            jQuery("#bg-trasparency", dash).text(bg_trasparency);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        /////////////////////////////////////////////////////////////////////////////////////
	    ////////////////////////////////CH BACKGROUND////////////////////////////////////////
	    /////////////////////////////////////////////////////////////////////////////////////
	    
	    //background color
	    jQuery('#f-chbg-color', dash).farbtastic('#chbg-color');
	    jQuery('#f-chbgo-color', dash).farbtastic('#chbgo-color');
	    jQuery('form', dash).submit(function(){
            
            chbg = jQuery('#chbg-color').val().replace("#","");
            chbg_offset = jQuery('#chbgo-color').val().replace("#","");

            jQuery(selector).trigger("init");  
            return false;
        });
        //background type
        jQuery("#chbg-type", dash).change(function(){
        	chbg_type = jQuery(this).val();
			jQuery("#target").trigger("init");
        });
        
        //background angle
        jQuery("#chbg-angle", dash).text(chbg_angle);
        
        jQuery("#p-chbg-angle a", dash)
        .eq(0)
        .click(function(){
            if(chbg_angle > 0)
                chbg_angle -= 10;
                
            jQuery("#chbg-angle", dash).text(chbg_angle);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(chbg_angle < 90)
                chbg_angle += 10;
            jQuery("#bg-angle", dash).text(chbg_angle);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        //background width
        jQuery("#chbg-width", dash).text(chbg_width);
        
        jQuery("#p-chbg-width a", dash)
        .eq(0)
        .click(function(){
            if(chbg_width > 0)
                chbg_width -= 10;
                
            jQuery("#chbg-width", dash).text(chbg_width);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(chbg_width < 100)
                chbg_width += 10;
            jQuery("#chbg-width", dash).text(chbg_width);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        
        //trasparency
        var chbt = (!chbg_trasparency) ? 0 : parseInt(chbg_trasparency);
        jQuery("#chbg-trasparency").text(chbt);
        
        //background trasparency flag
        jQuery("#chbg-trasparency-active", dash).change(function(){
            if(jQuery(this).attr("checked")){
                chbg_trasparency = chbt = 99;
                jQuery("#chbg-trasparency", dash).text(chbt);
            }else{
            	bg_trasparency = false;
            	chbt = 0;
            	jQuery("#chbg-trasparency", dash).text(chbt);
            }
            jQuery(selector).trigger("init");
            return false;  
        });
        //background trasparency
        jQuery("#p-chbg-trasparency a", dash)
        .eq(0)
        .click(function(){
            if(chbg_trasparency > 0)
                chbg_trasparency -= 10;
                
            jQuery("#chbg-trasparency", dash).text(chbg_trasparency);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(chbg_trasparency < 100)
                chbg_trasparency += 10;
            jQuery("#chbg-trasparency", dash).text(chbg_trasparency);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        ////////////////////////////////////////////////////////////////////
        //COLOR - SERIES
	    jQuery('form', dash).submit(function(){
            
            colors[(parseInt(jQuery('#colors-select').val()) -1 )] =  jQuery('#series-color').val().replace("#","");
            //console.log(colors);
            
            jQuery(selector).trigger("init");  
            return false;
        });
        
        ////////////////////////////////////////////////////////////////////
        //GRID
        
        jQuery("#grid-x-step").text(grid_x);
        jQuery("#grid-y-step").text(grid_y);
        jQuery("#grid-line").text(grid_line);
        jQuery("#grid-blank").text(grid_blank);
        
        //GRID - active
        jQuery("#grid-active", dash).change(function(){
           	grid = jQuery(this).attr("checked");
            jQuery(selector).trigger("init");
            return false;  
        });
        
        //GRID - X
        jQuery("#p-grid-x-step a", dash)
        .eq(0)
        .click(function(){
            if(grid_x > 0)
               grid_x -= 1;
                
            jQuery("#grid-x-step", dash).text(grid_x);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(grid_x < 100)
                grid_x += 1;
            jQuery("#grid-x-step", dash).text(grid_x);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        //GRID - Y
        jQuery("#p-grid-y-step a", dash)
        .eq(0)
        .click(function(){
            if(grid_y > 0)
                grid_y -= 1;
                
            jQuery("#grid-y-step", dash).text(grid_y);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(grid_y < 100)
                grid_y += 1;
            jQuery("#grid-y-step", dash).text(grid_y);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        //GRID - line segment
        jQuery("#p-grid-line a", dash)
        .eq(0)
        .click(function(){
            if(grid_line > 0)
                grid_line -= 10;
                
            jQuery("#grid-line", dash).text(grid_line);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(grid_line < 100)
                grid_line += 10;
            jQuery("#grid-line", dash).text(grid_line);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        //GRID - blank segment
        jQuery("#p-grid-blank a", dash)
        .eq(0)
        .click(function(){
            if(grid_blank > 0)
                grid_blank -= 10;
                
            jQuery("#grid-blank", dash).text(grid_blank);     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(grid_blank < 100)
                grid_blank += 10;
            jQuery("#grid-blank", dash).text(grid_blank);     
            jQuery(selector).trigger("init");
            return false;
        }).end();
        
        /////////////////////////////////////////////////
        //line style
        
        jQuery("#line-style-thickness").text(line_style_thickness);
        jQuery("#line-style-line").text(line_style_line);
        jQuery("#line-style-blank").text(line_style_blank);
		
		var update_lines = function(){
			lines[(parseInt(jQuery("#line-select").val()) - 1)] = [line_style_thickness, line_style_line, line_style_blank];
		};
        //line-style-thickness
        jQuery("#p-line-style-thickness a", dash)
        .eq(0)
        .click(function(){
            if(line_style_thickness > 0)
                line_style_thickness -= 1;
                
            jQuery("#line-style-thickness", dash).text(line_style_thickness);
            update_lines();     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(line_style_thickness < 100)
                line_style_thickness += 1;
            jQuery("#line-style-thickness", dash).text(line_style_thickness);     
            update_lines();
            jQuery(selector).trigger("init");
            return false;
        }).end();
        jQuery("#p-line-style-line a", dash)
        .eq(0)
        .click(function(){
            if(line_style_line > 0)
                line_style_line -= 1;
                
            jQuery("#line-style-line", dash).text(line_style_line);
            update_lines();     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(line_style_line < 100)
                line_style_line += 1;
            jQuery("#line-style-line", dash).text(line_style_line);     
            update_lines();
            jQuery(selector).trigger("init");
            return false;
        }).end();
        jQuery("#p-line-style-blank a", dash)
        .eq(0)
        .click(function(){
            if(line_style_blank > 0)
                line_style_blank -= 1;
                
            jQuery("#line-style-blank", dash).text(line_style_blank);
            update_lines();     
            jQuery(selector).trigger("init");
            return false;
        }).end()
        .eq(1)
        .click(function(){
            if(line_style_blank < 100)
                line_style_blank += 1;
            jQuery("#line-style-blank", dash).text(line_style_blank);     
            update_lines();
            jQuery(selector).trigger("init");
            return false;
        }).end();
	    //init app
	    jQuery(selector).trigger("init");
	    jQuery(selector).trigger("series");


        /////////////////////DEBUG/////////////////////
        function _to_string(str){
        	if(str.constructor == Boolean)
        	   return str;
        	else
        	   return "'" + str + "'";
        }
        function _arr_to_string(arr, inner){
        	var _ret = '';
        	   
        	//console.log(arr);
        	for(el in arr)
        	   if(arr[el].constructor == Array)
        	       _ret += _arr_to_string(arr[el], true);
        	   else{
        	   	   if(arr[el].constructor == String)
        	           _ret += "'" + arr[el]  + "',";
        	       else
        	           _ret += arr[el]  + ',';
        	   }    
        	_ret = _ret.substr(0, _ret.lastIndexOf(","));
        	_ret = "[" + _ret + "]";
        	if(inner)
        	   _ret += ",";
            //console.log(_ret);
        	return _ret;
        }
        function _replace_var(str){
            str = str.replace("this" , _to_string(selector)); 
            str = str.replace("[size]" , _to_string(chart_w + "x" + chart_h)); 
            str = str.replace("[type]" , _to_string(type));
            str = str.replace("[legend]" , _arr_to_string(legend[source]));
            str = str.replace("[tables]" , _arr_to_string(tables[source]));
            str = str.replace("[axis]" , _arr_to_string(axis[source]));
            str = str.replace("[axis_step]" , axis_step);
            str = str.replace("[title]" , _to_string(title));
            str = str.replace("[title_color]" , _to_string(title_color));
            str = str.replace("[title_size]" , title_size);
            str = str.replace("[bar_width]" , bar_width);
            str = str.replace("[bar_spacing]" , bar_spacing);
            str = str.replace("[fillarea]" , fillarea);
            str = str.replace("[fillbottom]" , fillbottom);
            str = str.replace("[filltop]" , filltop);
            str = str.replace("[colors]" , _arr_to_string(colors));
            str = str.replace("[lines]" , _arr_to_string(lines));
            str = str.replace("[bg]" , _to_string(bg));
            str = str.replace("[bg_type]" , _to_string(bg_type));
            str = str.replace("[bg_angle]" , bg_angle);
            str = str.replace("[bg_offset]" , _to_string(bg_offset));
            str = str.replace("[bg_width]" , bg_width);
            str = str.replace("[bg_trasparency]" , bg_trasparency);
            str = str.replace("[chbg]" , _to_string(chbg));
            str = str.replace("[chbg_type]" , _to_string(chbg_type));
            str = str.replace("[chbg_angle]" , chbg_angle);
            str = str.replace("[chbg_offset]" , _to_string(chbg_offset));
            str = str.replace("[chbg_width]" , chbg_width);
            str = str.replace("[chbg_trasparency]" , chbg_trasparency);
            str = str.replace("[grid]" , grid);
            str = str.replace("[grid_x]" , grid_x);
            str = str.replace("[grid_y]" , grid_y);
            str = str.replace("[grid_line]" , grid_line);
            str = str.replace("[grid_blank]" , grid_blank);
            try{
            	//jQuery(selector).empty();
                //eval(str);
            }catch(e){
                console.log(e);	
            };
           return str;
        }

	    
    });
    
};
var _metadata="{\
size:[size],\
type:[type],\
axis_step:[axis_step],\
title:[title],\
title_color:[title_color],\
title_size:[title_size],\
bar_width:[bar_width],\
bar_spacing:[bar_spacing],\
fillarea:[fillarea],\
fillbottom:[fillbottom],\
filltop:[filltop],\
colors:[colors],\
lines:[lines],\
bg:[bg],\
bg_type:[bg_type],\
bg_angle:[bg_angle],\
bg_offset:[bg_offset],\
bg_width:[bg_width],\
bg_trasparency:[bg_trasparency],\
chbg:[chbg],\
chbg_type:[chbg_type],\
chbg_angle:[chbg_angle],\
chbg_offset:[chbg_offset],\
chbg_width:[chbg_width],\
chbg_trasparency:[chbg_trasparency],\
grid:[grid],\
grid_x:[grid_x],\
grid_y:[grid_y],\
grid_line:[grid_line],\
grid_blank:[grid_blank]\
}";

var _hdoc = "jQuery(this).jgcharts({\n\
\n\
//MANDATORY - source\n\
data        : [tables],\n\
\n\
//OPTIONAL\n\
\n\
//source\n\
legend      : [legend],\n\
axis_labels : [axis],\n\
\n\
//opzioni \n\
size        : [size],\n\
type        : [type],\n\
\n\
axis_step   : [axis_step],\n\
\n\
title       : [title],\n\
title_color : [title_color],\n\
title_size  : [title_size],\n\
bar_width   : [bar_width],\n\
bar_spacing : [bar_spacing],\n\
fillarea    : [fillarea],\n\
fillbottom  : [fillbottom],\n\
filltop     : [filltop],\n\
//series colors\n\
colors      : [colors],\n\
\n\
//series line \n\
lines       : [lines],\n\
\n\
//style\n\
bg          : [bg],\n\
bg_type     : [bg_type],\n\
bg_angle    : [bg_angle],\n\
bg_offset   : [bg_offset],\n\
bg_width    : [bg_width],\n\
bg_trasparency: [bg_trasparency],\n\
\n\
chbg        : [chbg],\n\
chbg_type   : [chbg_type],\n\
chbg_angle  : [chbg_angle],\n\
chbg_offset : [chbg_offset],\n\
chbg_width  : [chbg_width],\n\
chbg_trasparency: [chbg_trasparency],\n\
\n\
//grid\n\
grid        : [grid],\n\
grid_x      : [grid_x],\n\
grid_y      : [grid_y],\n\
grid_line   : [grid_line],\n\
grid_blank  : [grid_blank]\n\
\n\
});";   

