$(function() {
	$('.arg-stats-form #arg-stats-gen-list').live('click', gen_list);
	$('form.arg-stats-form').live('submit', prepare_graph);
});

function gen_list()
{
    ShowProgressAnimation();
    this_form = $('.arg-stats-form');
    form_data = this_form.serializeArray();
    
    form_data = $.map(form_data, function(elem)
    {
      if (elem.name != 'arguments' && elem.name != 'arg_types' &&
         elem.name != 'bucket_size')
        return elem;
      else {
        if (elem.name == 'bucket_size')
          bucket_size = elem.value;
      }
    });

	form_data.push({name: 'bucket_size', value: bucket_size});

    $.ajaxJSON({
      method: 'post',
      url: ajax_gen_list,
      data: {
        form_data: form_data
      },

    callback: function(result) {
      HideProgressAnimation();
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'bucket size not selected') {
        HideProgressAnimation();
        error_alert('Wyznacz wielkość kubełka.');
        return false;
      }
      else if(result == 'bucket size not digit' || 
    		  result == 'wrong bucket size range') {
          HideProgressAnimation();
          error_alert('Wielkość kubełka musi być liczbą większą od 0.');
          return false;
      }
      else
      {
        HideProgressAnimation();
        return true;
      }
    },
  });
  return false;	
}

function draw_graph(arguments, selector, xmax, ylabel, dict_label, show_legend)
{
	var series = [];
    var seriesconf = [];
  	for (var i=0; i<arguments.length; i++) {
  		points = [];
  		for (var j=0; j<arguments[i][dict_label].length; j++)
      	{
  			points.push([j+1, arguments[i][dict_label][j]]);
      	}
  		series.push(points);
      	seriesconf.push({markerOptions: { size: 3 },
				label: arguments[i]['text_rep']});
    }
                
    var plot = $.jqplot(selector, series,
    {
      	title:{
      		text: 'Graf wpływu częstości występowania czasowników na posiadane realizacje',
      		fontFamily: 'Georgia, Serif',
            fontSize: '12pt'
      	},
      	series: seriesconf,
      	axes:{
        	xaxis:{
          		label: 'Częstość występowania czasownika (malejąco)',
          		labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
          		pad: 0,
          		labelOptions: {
                	fontFamily: 'Georgia, Serif',
            		fontSize: '12pt'
          		},
          		min: 1,
          		max: xmax,
          		numberTicks: xmax 
        	},
        	yaxis:{
          		label: ylabel,
          		labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
          		pad: 0,
          		labelOptions: {
            		fontFamily: 'Georgia, Serif',
            		fontSize: '12pt'
          		},
        	}
      	},
      	legend: { show     :show_legend, 
      		      location : 'ne',
      		      placement: 'outside',
        },
    });
}

function prepare_graph() {
    ShowProgressAnimation();
    this_form = $(this);
    form_data = this_form.serializeArray();
    var args = new Array();
  
    form_data = $.map(form_data, function(elem)
    {
        if (elem.name != 'arguments' && elem.name != 'arg_types' &&
    	    elem.name != 'bucket_size')
    	    return elem;
    	else {
    	    if (elem.name == 'bucket_size')
    	        bucket_size = elem.value;
    	    else if(elem.name == 'arg_types')
    	    	arg_types = elem.value;
    	    else if(elem.name == 'arguments')
    	    	args.push(elem.value);
    	}
    });

    form_data.push({name: 'bucket_size', value: bucket_size});
    form_data.push({name: 'arg_types', value: arg_types});
    form_data.push({name: 'arguments', value: args});

    $.ajaxJSON({
      method: 'post',
      url: ajax_prepare_graph_data,
      data: {
        form_data: form_data
      },

    callback: function(result) {
      $('#graph-count').empty();
      $('#graph-own').empty();
      draw_graph(result['graph_data'], 'graph-count', result['xmax'], 'Liczba realizacji danego typu', 'freq', true);
      draw_graph(result['graph_data'], 'graph-own', result['xmax'], 'Liczba czasowników posiadająca realizację', 'own', false);
      HideProgressAnimation();
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'select arguments') {
        HideProgressAnimation();
        error_alert('Wybierz realizację i/lub typ realizacji.');
        return false;
      }
      else if (result == 'bucket size not selected') {
          HideProgressAnimation();
          error_alert('Wyznacz wielkość kubełka.');
          return false;
      }
      else if(result == 'bucket size not digit' || 
      		  result == 'wrong bucket size range') {
          HideProgressAnimation();
          error_alert('Wielkość kubełka musi być liczbą od 1 do 100.');
          return false;
      }
      else {
        HideProgressAnimation();
        return true;
      }
    },
  });
  return false;
}

