(function( $ ) {
	$.fn.inlineChart = function() {
		var $block = $(this),
			$chart_block = $('.inline_ticker_block_chart', $block),
			$block_h5 = $('div.inline_ticker_block_info h5', $block);

			data = $block.data('data'),
			ticker = $block.data('ticker'),
			exchange = $block.data('exchange'),
			date = '',
			chart_data = [];

		$chart_block.empty();
		var $chart = $('<div>').appendTo($chart_block),
			$xaxis = $('<tr />').appendTo($('<table />').appendTo($chart_block));


		var chartOver = function(pos) {
			var cl = '';
			if (data[pos.row].d > 0) { cl = 'up'; }
			if (data[pos.row].d < 0) { cl = 'down'; }

			$block_h5.removeClass('up').removeClass('down').addClass(cl).text(parseFloat(data[pos.row].c).toFixed(2).replace('.', ','));
		},
		showLast = function() {
			var last = data[data.length - 1];

			var cl = '';
			if (last.d > 0) { cl = 'up'; }
			if (last.d < 0) { cl = 'down'; }

			// установка последних данных в инлайновый элемент в тексте
			var $el = $('.inline_text_ticker[data-type="ticker"][data-ticker="' + ticker + '"][data-exchange="' + exchange + '"]');
			$el
				.addClass(cl)
				.attr('data-delta', cl)
				.data('delta', cl)
				.text($el.text() + ' ' + (last.dp > 0 ? '+' : '') + parseFloat(last.dp).toFixed(2).replace('.', ',') + '%')
				//.text($el.text() + ' ' + parseFloat(last.d).toFixed(2).replace('.', ','))
			;
		}

		for (var n = 0; n < data.length; n++) {
			if (exchange == 'CB') {
				date = data[n].dt.replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, '$3.$2.$1');
			} else {
				date = data[n].dt.replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, '$3.$2.$1 $4:$5');
			}
			chart_data.push([date, parseFloat(data[n].c)]);
		}

		// подстановка крайних и среднего значений
		for (var n = 0; n < chart_data.length; n++) {
			if (
				n == 0
				|| n == chart_data.length - 1
				|| !(n % Math.ceil(chart_data.length / 2))
			) { // последнее
				$xaxis.append('<td>' + chart_data[n][0].replace(/^(\d+).(\d+).(\d+) (\d+):(\d+)$/, '$1.$2 $4:$5') + '</td>');
			}
		}

		var chart = new google.visualization.AreaChart($chart[0]); // Create and draw the visualization.

		var DataTable = new google.visualization.DataTable();
		DataTable.addColumn({type:'string', role:'domain'});
		DataTable.addColumn({type:'number', role:'data', label: 'Close'});
		DataTable.addRows(chart_data);

		var number_format_opts = {
			decimalSymbol:',',
			fractionDigits:2,
			groupingSymbol:'',
			negativeParens:false,
			prefix:'',
			suffix:''
		},
		datetime_format_opts = {pattern: 'dd.MM.yyyy HH:mm'};
		if (exchange == 'CB') {
			number_format_opts.fractionDigits = 4;
			datetime_format_opts.pattern = 'dd.MM.yyyy';
		}
		if (exchange == 'FR') {
			number_format_opts.fractionDigits = 4;
		}
		var format_datetime = new google.visualization.DateFormat(datetime_format_opts),
			format_number = new google.visualization.NumberFormat(number_format_opts);

		// format_datetime.format(DataTable, 0);
		format_number.format(DataTable, 1);

		chart.draw(DataTable, {
			// curveType: 'function',
			width: 247,
			height: 80,
			areaOpacity: .2,
			colors: ['#a03676'],
			interpolateNulls: true,
			/*tooltip: { trigger: 'none' },*/
			legend: { position: 'none' },
			hAxis : {
				gridlines: { color: 'none' },
				textPosition: 'none',
			},
			vAxis: {
				baselineColor: '#c8c8c8',
				textPosition: 'none',
				gridlines: {
					color: '#c1c1c1',
					count: 5,
				},
			},
			chartArea: {
				top: 0,
				left: 0,
				width: 247,
				height: 80,
			}
		});
		google.visualization.events.addListener(chart, 'onmouseover', chartOver);

		chartOver({row:data.length - 1, column:1});
		showLast()

		$block.show();
	};
})( jQuery );
