var GraficoAndamentoPrezzi = Class.create({
	data: {},
	formGraf: null,
	tipoImmobile: null,
	contratto: null,
	container: null,
	imgLoading: "/img/ajax-loader.gif",
	graphWidth: 260,
	graphHeight: 250,
	unitaGrafico: "EUR/m²",

	initialize: function(container, formGraf, params) {
		this.container = $(container);
		this.formGraf = $(formGraf);
		this.formGraf.disable();

		this._parseParams(params);

		this.tipoImmobile = this.formGraf.getElementsBySelector("select[name=tipo_immobile]").first();
		this.contratto = this.formGraf.getElementsBySelector("select[name=contratto]").first();

		// Osserva i cambiamenti sulla tipologia di immobile
		// e il tipo di contratto:
		Element.observe(this.tipoImmobile, "change", this.getStats.bind(this));
		Element.observe(this.contratto, "change", this.getStats.bind(this));
	},

	_parseParams: function(params) {
		this.data 		= (params.data != null)		? params.data 		: this.data;
		this.graphWidth = (params.graphWidth > 0) 	? params.graphWidth : this.graphWidth;
		this.graphHeight	= (params.graphHeight > 0) 	? params.graphHeight: this.graphHeight;
		this.unitaGrafico 	= (params.unita_grafico != null) ? params.unita_grafico : this.unitaGrafico;
	},

	getStats: function() {
		var urlAjaxStats = "/moduli/annuncio/getDatiGraficoPrezzi.php?tipo_immobile=" + this.tipoImmobile.getValue()
						 + "&contratto=" + this.contratto.getValue()
						 + "&" + Object.toQueryString(this.data);

		new Ajax.Request(urlAjaxStats, {
			evalJSON: true,

			onCreate: function() {
				// Disattiva il form:
				this.formGraf.disable();

				// Mostra il loading:
				this.container.update(new Element("img", { src: this.imgLoading }));
			}.bind(this),

			onSuccess: function(r) {
	    		var data = r.responseJSON;

	    		// Disegna il grafico:
	    		var graph = this._drawChart(data);
			}.bind(this),

			onComplete: function() {
				// Riattiva il form:
				this.formGraf.enable();
			}.bind(this)
	    });
	},

	_drawChart: function(r) {
		var data = new google.visualization.DataTable();
	    data.addColumn('string', 'Mese');
	    data.addColumn('number', this.unitaGrafico);
	    data.addColumn('number', this.unitaGrafico);
	    data.addColumn('number', this.unitaGrafico);
	    data.addRows(r.length);

	    var cont = 0;
	    for (var i=0; i<r.length; i++) {
	    	var d = r[i];
	    	data.setCell(cont, 0, d.data);

	    	if (d.nazione != null) {
	    		data.setCell(cont, 1, d.nazione);
	    	}
	    	if (d.zona != null) {
	    		data.setCell(cont, 2, d.zona);
	    	}
	    	if (d.localita != null) {
	    		data.setCell(cont, 3, d.localita);
	    	}
	      	cont++;
	    }

	    var chart = new google.visualization.LineChart($('grafico_andamento_prezzi'));
	  	chart.draw(data, {
		    width: this.graphWidth,
		    height: this.graphHeight,
		    colors: ['#006633', '#996633', '#DD0000'],
		    legend: 'none',
		    chartArea: {
				top: 10,
				left: 40,
				width: this.graphWidth - 40,
				height: this.graphHeight - 50
			},
			interpolateNulls: true,
		    vAxis: { title: "", baselineColor: "#CCCCCC", format: '# ###.###' },
	      	hAxis: { title: "", baselineColor: "#CCCCCC", slantedText: true, slantedTextAngle: 30 },
	      	gridlineColor: "#CCCCCC",
	      	pointSize: 4
		});

	  	google.visualization.events.addListener(chart, 'ready', this._fixGoogleCharts);
	},

	_fixGoogleCharts: function() {
		$A($$('iframe[id*=Drawing_Frame_]')).each( function(item) {
        	var idoc = item.contentDocument || item.contentWindow.document;
        	var collection = idoc.getElementsByTagName("g");

        	for (var i=0; i<collection.length; i++) {
        		var elem = collection[i];
        		var clipPath = elem.getAttribute("clip-path");
        		if (clipPath != null) {
        			if (clipPath.indexOf( "url(#") != -1) {
        				elem.setAttribute("clip-path", document.location + clipPath.substring(4));
        			}
        		}
        	}
        });
    }
});

