var Lines = Class.create({
        width: 0,
        height: 0,
        canvas: null,
        context: null,
        color: 'rgba(120,120,120,0.2)',
		colors: ['5891a5','cb5b1b','8db88a','b88aa7','7b77af','ce613b','8db88a'],
        randAng: Math.PI / 8,
        thirdPI: Math.PI / 3,                             
        initialize: function(){
            this.getSizes();
            this.eps = Math.floor( Math.abs( (this.width - 960) / 4 ) );
            if (typeof(G_vmlCanvasManager) != 'undefined') this.canvas = G_vmlCanvasManager.initElement($('lines'));
            else this.canvas = $('lines');
            this.canvas.width = this.width;
            this.canvas.height = this.height;
            this.context = this.canvas.getContext('2d');
            this.draw();
        },
        drawLine: function(x0, y0, x1, y1, color){
	    this.context.strokeStyle = color;
	    this.context.beginPath();
	    this.context.moveTo(x0, y0);
	    this.context.lineTo(x1, y1);
	    this.context.closePath();
	    this.context.stroke();          
        },
		drawCircle: function(x0, y0, color){
		    this.context.strokeStyle = color;
		    this.context.fillStyle = color;
			this.context.beginPath();
			this.context.arc(x0, y0, 10, 0, Math.PI*2, true); 
			this.context.closePath();
			this.context.fill();
		},
        spanLine: function(x0, y0, x1, y1){
            var k = (x1 == x0 ? 0 : (y1 - y0) / (x1 - x0)),
                b = y0 - k*x0,
                x2 = 0,
                y2 = k*x2 + b,
                x3 = this.width,
                y3 = k*x3 + b;
            return [x2, y2, x3, y3];
        },
        pointAttention: function(x, y){
            this.context.fillRect(x-5,y-5,10,10);
        },
        draw: function(){
            var partitions = [], cur_h = $('pagein').cumulativeOffset()[1], vpheight = document.viewport.getHeight();
            while (true) {
                partitions.push([cur_h, cur_h += vpheight]);
                if (cur_h > $('pagein').getHeight()) break;
            }
            for (var i = 0, iL = partitions.length; i < iL; i++) {
                var points = [],
                    phi = this.thirdPI * Math.random(),
                    radius = 250 + (Math.random() > 0.5 ? -1 : 1) * 100 * Math.random(), //Math.ceil( (partitions[i][1] - partitions[i][0]) * 0.1 * Math.random() + (partitions[i][1] - partitions[i][0])/2 ),
                    center = [Math.floor(this.width / 2), Math.floor( (i > 0 ? partitions[i-1][1] : 0) + (partitions[i][1] - partitions[i][0]) / 2 )];

                for (var j = 0; j < 3; j++) {
                    points.push([Math.floor(radius * Math.cos(phi)) + center[0], Math.floor(-radius * Math.sin(phi)) + center[1]]);
                    if (j > 0) for (var k = j-1; k >= 0; k--) {
                        var pts = this.spanLine(points[k][0], points[k][1], points[j][0], points[j][1]);
                        this.drawLine(pts[0], pts[1], pts[2], pts[3], this.color);
                    }
                    phi += this.thirdPI + (Math.random() > 0.5 ? 1 : - 1) * this.randAng * Math.random();
                }
            }
		/* конфетти для дня рождения
			for (var i=0; i<this.height/80; i++) {
				this.drawCircle(Math.random()*920+this.width/2-460, Math.random()*this.height, this.colors[Math.round(Math.random()*6)]);
			}
		*/
        },
        getSizes: function(){
            this.height = window.getDocumentHeight();
            this.width = document.body.clientWidth;
        }
});

/*
var Lines = Class.create({
	lines: [],
	total: 2,
	segmentsMax: 16,
	half: 0,
	drawn: false,
	initialize: function() 
	{
		this.half = this.segmentsMax / 2;
		this.colors = ['rgba(120, 120, 120, 0.2)'];// #3c3c39
		this.element = $('lines');
		if (typeof(G_vmlCanvasManager) != 'undefined') this.element = G_vmlCanvasManager.initElement($('lines'));
		this.lines = [];
		this.resize();
		this.num = Math.round(this.dimensions.height / 10);
		if (this.dimensions.height < 1000) this.total = 4;
        this.draw();
        // Event.observe(window, 'resize', this.resize.bind(this));
        Event.observe(document, 'custom:content_loaded', this.resize.bind(this));
	},
	draw: function() 
	{
	    this.lines = [];
	    this.context = this.element.getContext('2d');
 		var height = this.dimensions.height, segm = [], step = Math.ceil( height / this.segmentsMax), i;
		for (i = 0; i < this.segmentsMax; i++ ) segm.push([i * step, (i + 1) * step]);
	    for (i = 0; i < this.total; ++i)  {
			var l = new line(
				this.random2(segm, this.total),
				this.random2(segm, this.total),
				this.colors[Math.floor(Math.random() * this.colors.length)]
			);
			this.lines.push(l);
		}
		for (var i=0; i<this.lines.length; i++) {
			var l = this.lines[i];
			this.line(l);
		}
	},
	random2: function(segm)
	{
		var half = this.half; //yeah!		
		var i 	 = Math.floor( random(-half, half) ) + half;
		return random(segm[i][0], segm[i][1]);
	},
	line: function(l) {
		this.context.strokeStyle = l.color;
		this.context.beginPath();
		this.context.moveTo(0, l.y1);
		this.context.lineTo(this.dimensions.width, l.y2);
		this.context.closePath();
		this.context.stroke();
	},
	resize: function() {
		this.dimensions = {
			width: document.body.clientWidth,
			height: $('pageout').getDimensions().height + 50
		};
		this.element.width = this.dimensions.width;
		this.element.height = this.dimensions.height;
		
		if (this.lines.length) {
            // this.context.globalCompositeOperation = 'xor';
			this.lines.each(this.line.bind(this));
            // this.context.globalCompositeOperation = 'source-over';
        }
		this.draw();
	}
});
*/

