OFC = {};
 
OFC.jquery = {
    name: "jQuery",
    version: function(src) { return $('#'+ src)[0].get_version() },
    rasterize: function (src, dst) { $('#'+ dst).replaceWith(OFC.jquery.image(src)) },
    image: function(src) { return "<img src='data:image/png;base64," + $('#'+src)[0].get_img_binary() + "' />"},
    popup: function(src) {
    	var currencyUnit = $('#ChartCurrency option:selected').text();
    	var volumeUnit = $('#ChartVolumeUnit option:selected').text();
    	var currentTitle = $('h1').text();
    	var imgSrc = '';
    	var message = '<p>Erreur</p>';
    	var valid = false;
        var img_win = window.open('', '')
        img_win.document.write('Loading...')
        $.post("/charts/create_image",
	        {
	        	'data[Image]': $('#'+src)[0].get_img_binary()
	        },
	    	function(data,textStatus)
	    	{
	    		if(textStatus == "success" && data.success)
	    		{
	    			imgSrc = data.value;
	    			if(imgSrc != '')
	    			{
					    message = '<img src="'+imgSrc+'"/>';
					    valid = true;
					}   
	    		}
	    		img_win.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>'+currentTitle+'<\/title><\/head><body><h1 style="color:#2a4c54;font-family:Helvetica,Arial;font-size:22px;text-align:center;width:714px;margin-top:20px;">'+currentTitle+'<div style="font-size:16px;margin-top:7px;">('+currencyUnit+' par '+volumeUnit+')</div></h1>'+message+'<\/body><\/html>');
	    		if(valid)
	    		{
	    			img_win.document.close();
	    			img_win.print();
	    		}
	    	},
	    	'json'    
	    )
	    
        
		// stop the 'loading...' message
		img_win.document.close();
     }
}
 
// Using an object as namespaces is JS Best Practice. I like the Control.XXX style.
//if (!Control) {var Control = {}}
//if (typeof(Control == "undefined")) {var Control = {}}
if (typeof(Control == "undefined")) {var Control = {OFC: OFC.jquery}}
 
 
// By default, right-clicking on OFC and choosing "save image locally" calls this function.
// You are free to change the code in OFC and call my wrapper (Control.OFC.your_favorite_save_method)
// function save_image() { alert(1); Control.OFC.popup('my_chart') }
function save_image() { OFC.jquery.popup('ofchart') }
function moo() { return false };
