

function Linedraw(node, options) {		
		
	this.options = {
		canvasWidth : 600,
		canvasHeight : 300,
		volLineAttr : {stroke: '#aaa', "stroke-width": 1},
		micLineAttr : {stroke: 'red', "stroke-width": 1},
		dx : 20,					//шаг между точками по x
		h : 10,						//коэффициент сглаживания кривой, от 0 до ...
		shift : 0,					//величина сдвига кривых по фазе
		pointCount : 21,			// должно быть нечетным.
		middle : 50,
		staticPoints : 0
  	};

	this.options = $.extend(this.options, options || {});

	this.node = $(node);
	this.node[0].self = this;
	

	//this.mi = -1;
	//this.vi = -1;
	this.intencivity_scale = (1. / 0xFFFF) * 2.5 * this.options.middle; 
	this.te = 0;

	this.len2 = Math.floor(this.options.pointCount / 2);
	this.end = this.options.pointCount - 1;
	this.q = 1 - 5. / this.options.pointCount;

	this.arrMic = [];
	this.arrVol = [];



	this.init();

};
	
Linedraw.prototype = {

	init: function(){
		
			this.canvas = Raphael(this.node[0], this.options.canvasWidth, this.options.canvasHeight);
			
			if (($.browser.safari)||($.browser.webkit)) this.canvas.safari();
			
			this.lineMic = this.canvas.path().attr( this.options.micLineAttr );
			this.lineVol = this.canvas.path().attr( this.options.volLineAttr );
			
			
			this.resetArrs();
			
		},


	resetArrs: function(){
		
			for (var i = 0; i < this.options.pointCount; i++) {
				this.arrMic[i] = this.options.middle;
				this.arrVol[i] = this.options.middle;
			}		
		
		},


	prepareVal: function(val, shift){
			//debugger;
			var dx = this.options.dx;
			var h = this.options.h;
			shift = shift || 0;
			
			//var p = 'M0'+','+val[0];
			var p = 'M'+shift+','+val[0];
			
			for (var i=1; i<val.length; i++){
				p += 'C' + ((i-1)*dx+shift+h) + ',' + val[i-1] + ',' + (i*dx+shift-h) + ',' + val[i] + ',' + (i*dx+shift) + ',' + val[i];
				//p += 'C' + ((i-1)*dx+h) + ',' + val[i-1] + ',' + (i*dx-h) + ',' + val[i] + ',' + i*dx + ',' + val[i];	
			}	
			return p;
		},
	
	
	redrawVol: function(vol) {

	    	this.redraw_intencivity( -vol * this.intencivity_scale, 'vol');

		},


	redrawMic: function(vol) {

	    	this.redraw_intencivity( -vol * this.intencivity_scale, 'mic');

		},

	
	resetVol: function() {
		
			for (var i = 0; i < this.options.pointCount; i++) {
				this.arrVol[i] = this.options.middle;
			}
	    	this.redrawVol(0);
			
		},
		
		
	resetMic: function() {
		
			for (var i = 0; i < this.options.pointCount; i++) {
				this.arrMic[i] = this.options.middle;
			}
			this.redrawMic(0);
			
		},
				

	resetAll: function() {
		
			this.resetArrs();
			this.redrawVol(0);
			this.redrawMic(0);
			
		},
		

	redraw_intencivity: function(val, lineName){

		var middle = this.options.middle;

		if (lineName == 'vol') {
			
			var line = this.lineVol;
			var arr = this.arrVol;
			var shift = 0;	
			
		} else {
			
			var line = this.lineMic;
			var arr = this.arrMic;
			var shift = this.options.shift;					
			
		}
		
		for (var i = 0; i < this.options.staticPoints; i++) {
	        arr[i] = middle; arr[this.end - i] = middle;
	    }
		
	    for (var i = this.options.staticPoints; i < this.len2; i++) {
	        var x = -(arr[i + 1] - middle) * this.q + middle;
	        arr[i] = x; arr[this.end - i] = x;

	    }
		
		x = val + middle;
		
		if (x < 0) x = 0
		else if (x > middle * 2) x = middle * 2; 
		
	    arr[this.len2] = x;
		
		line.attr({path: this.prepareVal(arr, shift)});
	
	}



}
















