/*
передалать загрузку данных отдельно от инициализации
добавить ф-цию перерисовки, выбора отрезка по оси Х
ошибка при отключении нулевой оси
*/
//alert(navigator.userAgent);
var graph=Class.create({
        dots:[],
        xs:[],
        defered:[],
       	initialize: function() 
        {
                      
        	this.options = Object.extend({
        	   
        	    debug:false,
        	    bubbleHeadText:'',
        	    xIsDate:false,
        	    bubbleDateFormat:'mmmm yyyy',
        	    xDateFormat:'yyyy',
        	    bgText:false,
        	    logoColor:'#000',
        	    logo:false,
                approx:500,
        	    logoWidth:141,
        	    logoHeight:47,
        	    logoOpacity:0.04,
        	    showNullaxis:true,
                showNull:true,
                key:'dhgfhsdgfjhsgd',
                renderTo:document.body,
                xS:10,
                yS:48,
                fontSize:10,
                minStep:20,
                data:{
                         x:['a','b','c',3,4,5,6,7,8,9,'x',11,12,13,14,15,16,17,18,19,'a','b','c',3,4,5,6,7,8,9,'x',11,12,13,14,15,16,17,18,19,'a','b','c',3,4,5,6,7,8,9,'x',11,12,13,14,15,16,17,18,19,'a','b','c',3,4,5,6,7,8,9,'x',11,12,13,14,15,16,17,18,19],
                         y:[
                                [11,-12,0,36,73,24,57,85,78,23,11,-12,15,36,73,24,57,85,78,23,11,-12,0,36,73,24,57,85,78,23,11,-12,15,36,73,24,57,85,78,23,11,-12,0,36,73,24,57,85,78,23,11,-12,15,36,73,24,57,85,78,23,11,-12,0,36,73,24,57,85,78,23,11,-12,15,36,73,24,57,85,78,23],
                               // [24,44,-56,45,67,8,36,23,36,6,24,44,-56,45,67,8,36,23,36,6],
                               // [63,64,56,-75,87,6,30,25,39,6,63,64,56,-75,87,6,30,25,39,6],
                              //  [63,64,56,-75,87,6,30,25,39,6,83,14,66,35,-47,2,44,28,36,16]
                            ],
                        closePath:[1,1,0,0]
                         },
                        lineWidth:1,
                        colors:["#00ff00","#ff0000","#0000ff","#000","#ffff00","#ff0000","#123456","#ffff00","#ff0000","#00ff00","#123456","#ffff00"],
                        bubbleColor:'#333',
                        bubbleTransparent:0.7,
                        bubbleHeaderColor:'#00ff00',
                        bubbleBorderColor:'#000',
                        fontColor:'#333',
                        bubbleFontColor:'#00ff00',
                        axisColor:'#222',axisWidth:1,
                        axisFontStrokeWidth:0.2,
                        bgColor:'#d8d8d8',
                        bgAlpha:0.5,
                        matracColor:'#d0d0d0',
                        movingLineColor:'#0000ff'
                }, arguments[0] || { });
                
                if(navigator.userAgent.indexOf('iPhone')!=-1)
                {
                this.options.fontSize=this.options.fontSize*2;
                }
                
                if(this.options.debug)
                {
                    document.body.appendChild(new Element("div",{id:'GraphDebugDiv'}));
                     var stg=new Date().getTime();
                }
                this.dots=[];
                this.data=this.options.data;
                var approx=this.options.approx;
                if(navigator.userAgent.indexOf('MSIE 6')!=-1){approx=80;}
                if(navigator.userAgent.indexOf('MSIE 7')!=-1){approx=180;}
                if(navigator.userAgent.indexOf('MSIE 8')!=-1){approx=200;}
                
                if(this.data.x.length>approx )
                {
                    var s = Math.floor(this.data.x.length/approx+1);
                    
                    if(s>1){
                    this.options.xS=this.options.xS/s;
                    var newData={x:[],y:[],closePath:this.data.closePath};
                    var testEnd=false;
                        for (var i = 0; i < this.data.x.length; i+=s) 
                            {
                               newData.x.push(this.data.x[i]); 
                               if(this.data.x.length-1==i){testEnd=true;}
                            }
                            if(!testEnd)
                            {
                                newData.x.push(this.data.x[this.data.x.length-1]);
                            }
                        for (var i = 0; i < this.data.y.length; i++) 
                            {
                               
                               if(typeof newData.y[i]=="undefined"){newData.y[i]=[];}
                               
                                        for (var ii = 0; ii < this.data.y[i].length; ii+=s) 
                                            {
                                               newData.y[i].push(this.data.y[i][ii]); 
                                            }
                                            if(!testEnd)
                                                {
                                                   // newData.x.push(this.data.x[this.data.x.length]);
                                                    newData.y[i].push(this.data.y[i][this.data.x.length-1]); 
                                                }
                            }
                            
                        this.data=this.options.data=newData;
                        
                        }
                    
                }
                
                var err=0;
                this.data.y.each(function(yy){if(this.data.x.length!=yy.length){err++;} }.bind(this)); if(err>0){alert('Неверные данные. Длина массивов не равна.');return;}
                var Numberic = [];
                this.data.y.flatten().each(function(v){
                if(!isNaN(v)){Numberic.push(v)}
                });
                this.max = Math.max.apply(Math, Numberic);
                this.min =  Math.min.apply(Math, Numberic);
                this.marginLeft=([String(Math.round(this.max)).length,String(Math.round(this.min)).length].max())*this.options.fontSize*0.8;
                this.options.margin=this.marginLeft+this.options.fontSize;
                this.options.bubbleWidth=([String(Math.round(this.max*1000)/1000).length,String(Math.round(this.min*1000)/1000).length].max()+2)*this.options.fontSize+20;
                this.width=($(this.options.renderTo).getWidth() || $(this.options.renderTo).style.width.replace('px',''));
                this.height= ($(this.options.renderTo).getHeight() || $(this.options.renderTo).style.height.replace('px',''));
                var hdt=Number(this.height)+((this.data.legends)?(Number(this.data.y.length)*20):0);
                this.container= new Element('div',{ style:'width:'+this.width+'px; height:'+hdt+'px;background-color:'+this.options.bgColor+";"});
                this.message= new Element('div',{style:'margin-top:20%; text-align:center;color:gray;font-weight:bold'});
                //,'fill-opacity':this.options.bgAlpha,
                $(this.options.renderTo).style.backgroundImage="url(/d/img/loading.gif)";
                $(this.options.renderTo).style.backgroundPosition="center";
                $(this.options.renderTo).style.backgroundRepeat="no-repeat";
                $(this.options.renderTo).insert(this.container);
               $(this.options.renderTo).insert(this.message);
               this.message.update("Идет загрузка данных");
               if(navigator.userAgent.indexOf('MSIE')!=-1&&false)
               {
               this.message.update("Идет загрузка данных<hr>Чтобы графики загружались быстрее, обновите Internet Explorer на Google Chrome!");
               }
               this.container.hide();
                this.colors=this.options.colors;
                this.rh=this.height-this.options.margin-this.options.margin/2;
                this.rw=this.width-this.options.margin-this.options.margin/2;
                
                this.p=Raphael(this.container, this.width, this.height);
                this.all=this.p.set();
                try{this.p.desc.textContent="Created by Alexandr Trubnikov (www.trubnikov.info)";}catch(e){}
                this.minP= this.min;
                this.offs = ((this.min>0)?this.min:0);
                this.min = ((this.min<0)?0-this.min:0);
                this.Ystep = Math.round((this.rh)/(this.max+this.min-this.offs)*1000)/1000;  
                this.Xstep = Math.round((this.rw)/(this.data.x.length-1)*1000)/1000;  
                this.bg = this.p.rect(0,0, this.width, this.height).attr({'fill':this.options.bgColor,'fill-opacity':this.options.bgAlpha,'stroke-width':0,'stroke':'transparent'});
                this.NullY=this.height-this.options.margin-(this.min-this.offs)*this.Ystep;     
                 if(Number(this.NullY) >= this.height-this.options.margin){
                 this.options.showNullaxis=this.options.showNull=false;
                 }  
            if(this.options.showNullaxis){
           this.nullAxis = this.p.rect(this.options.margin-5, this.NullY, this.rw+5, 1).attr({'fill':this.options.axisColor,'stroke-width':0,'stroke':'transparent'});
           }
           if(this.options.showNull){
            this.p.text(this.options.margin/2,  this.NullY, '0').attr({'fill':this.options.fontColor,'stroke':this.options.fontColor,'stroke-width':0.2,'font-size':this.options.fontSize});
          }  
                  
             this.Yaxis = this.p.rect(this.options.margin-1, this.options.margin/2, this.options.axisWidth, this.height-this.options.margin-this.options.margin/2).attr({'fill':this.options.axisColor,'stroke-width':0,'stroke':'transparent'});     
             if(this.options.bgText){  
             var sz=this.rw/((this.options.bgText.length>10)?this.options.bgText.length:10)*1.4;
            this.Graphcopyright=this.p.text(this.width/2+this.options.margin/3, this.height/2, this.options.bgText).insertBefore(this.Yaxis).attr({'fill':this.options.logoColor,'fill-opacity':0,'stroke':this.options.logoColor,'stroke-width':1,'font-size':sz});
             }
            
            if(this.options.logo){
            this.p.image(this.options.logo, this.width/2+this.options.margin/3-this.options.logoWidth/2, this.height/2-this.options.logoHeight/2, this.options.logoWidth, this.options.logoHeight).insertBefore(this.Yaxis).attr({'opacity':this.options.logoOpacity});
            }
                this.createGraph.bind(this).defer();
                this.zaglushka=this.p.rect(this.width-this.options.margin/2+1,0,this.options.margin/2, this.height).attr({'fill':this.options.bgColor,'fill-opacity':0,'stroke-width':0,'stroke':'transparent'}); 
               this.createGrid.bind(this).defer();
               if(this.options.showNullaxis){ this.nullAxis.toFront();}
               if(navigator.userAgent.indexOf('MSIE 6')==-1||1){
               this.createAnimation.bind(this).defer();
               }
                              if(this.options.debug)
                {
                    var d = new Date();
                    t="endinit:"+(d.getTime()-stg);
                    $('GraphDebugDiv').insert(new Element("div").update(t));
                   
                }
                if(navigator.userAgent.indexOf('MSIE')==-1){
                if(this.data.units)
                {
                        (function(){
                        this.container.insertBefore(new Element("div",{style:"clear:both;float:left;margin-left:"+this.options.margin+"px;margin-top:5px;"}).update(this.data.units.y),this.container.firstChild);
                        this.container.insert(new Element("div",{style:"clear:both;float:right;margin-left:"+this.options.margin+"px;margin-top:-10px;"}).update(this.data.units.x));
                        this.container.insert(new Element('div',{style:'clear:both;'}));
                        }.bind(this)).delay(1);
                        
                }
                if(this.data.legends)
                {
                    var legCont = new Element('div',{'class':'HotMaternyi'});
                    this.container.insert(legCont);
                    for (var i = 0; i < this.data.y.length; i++) 
                    {
                        legend = new Element("div",{style:"background:"+this.options.colors[i]+";width:10px;height:10px;float:left;margin-left:"+this.options.margin+"px;margin-right:10px;margin-top:3px;"});
                        legCont.insert(legend);
                        text = new Element("div",{style:"float:left;"}).update(this.data.legends[i]);
                        legCont.insert(text);
                        legCont.insert(new Element('div',{style:'clear:both'}));
                    }
                    
                        //legCont.insert(new Element('div',{style:'clear:both; margin:10px;width:100%;height:1px;'}));
                }
                }
                
                
               this.showMe.bind(this).defer();
        },
        showMe:function(){
        $(this.options.renderTo).style.backgroundImage="";
         this.container.show(); 
            this.message.remove();
            this.lic={dmn:'t'+'r'+'u'+'b'+'n'+'i'+'k'+'o'+'v'+'.'+'i'+'n'+'f'+'o'};
                eval('var rg=/^([^\:]*(\:\\\/*)?((.*\.'+'e'+'x'+'p'+'e'+'r'+'t'+'.'+'r'+'u'+')|('+'1'+'7'+'2'+'.'+'1'+'7'+'.'+'4'+'8'+')|('+this.lic.dmn+')|(l'+'o'+'c'+'a'+'l'+'h'+'o'+'s'+'t)|('+'12'+'7'+'.'+'0.0'+'.'+'1)))|('+'f'+'i'+'l'+'e'+':\\\/\\\/)/;');
                if(!rg.test(document['l'+'o'+'c'+'a'+'t'+'i'+'o'+'n'])){
                this.cpr=this.p.set();
                this.cpr.push( this.p.text(this.width-10, this.height-10, '©').attr({'font-size':10,'font-weight':'800','font-family':'times','fill-opacity':0.1}));
                 this.cpr.attr({'cursor':'pointer'});
                 this.cpr.click(function(){
                 if(typeof this.cprr == "undefined"){
                    this.cprr=this.p.set();
                   this.cprr.push( this.p.rect(this.width/2-125,this.height/2-40,250,80,5).attr({'fill':'#c0c0c0','fill-opacity':0.9,'stroke-width':1,'stroke':'#999'}));
                   this.cprr.push(  this.p.text(this.width/2,this.height/2-25, 'S'+'V'+'G'+' '+'G'+'r'+'a'+'p'+'h'+' j'+'s'+' 1'+'.0'+'1').attr({'font-size':20,'font-weight':'800','font-family':'times'}));
                   this.cprr.push(  this.p.text(this.width/2,this.height/2-5, 'f'+'r'+'e'+'e'+' '+'v'+'e'+'r'+'s'+'i'+'o'+'n').attr({'fill':'#ff0000','font-size':14,'font-weight':'800','font-family':'times'}));
                   this.cprr.push(  this.p.text(this.width/2,this.height/2+20, 'C'+'r'+'e'+'a'+'t'+'e'+'d'+' '+'b'+'y'+' '+'A'+'l'+'e'+'x'+'a'+'n'+'d'+'r'+' '+'T'+'r'+'u'+'b'+'n'+'i'+'k'+'o'+'v\n'+'w'+'w'+'w'+'.'+'t'+'r'+'u'+'b'+'n'+'i'+'k'+'o'+'v'+'.'+'i'+'n'+'f'+'o').attr({'font-size':11,'font-weight':'800','font-family':'times'}));
                   this.cprr.attr({'cursor':'pointer','href':'h'+'t'+'t'+'p'+':'+'/'+'/'+'t'+'r'+'u'+'b'+'n'+'i'+'k'+'o'+'v'+'.'+'i'+'n'+'f'+'o','fill-opacity':0.6});            
                          
                    this.cprr.hidden=true;
                    }
                    if(this.cprr.hidden){
                        this.cprr.show(); this.cprr.hidden=false;
                   }else
                   {
                   this.cprr.hide();this.cprr.hidden=true;
                   }
                 }.bind(this));
                 }
        },
        createAnimation:function(){
        this.rects= new Hash();
        this.oldr=false;
         stg=new Date().getTime();
         var stp=1;
         if(navigator.userAgent.indexOf('MSIE')!=-1){stp=2;}
       /*  $(this.options.renderTo).ontouchstart=function(){try{
         
                             this.oldr.dots.each(function(d){
                            d.attr({r: 1});
                            d.hide();
                           }); 
                          this.oldr.v.hide();
                          this.oldr.b.hide();
                         this.oldr.t.remove();   
         
         }catch(e){}}.bind(this)*/
         $(this.options.renderTo).ontouchmove=$(this.options.renderTo).ontouchstart=function(e){ 
         
        // var el=document.elementFromPoint(e.targetTouches[0].pageX,e.targetTouches[0].pageY);
        var po=e.targetTouches[0].pageX-this.container.cumulativeOffset()[0];
         var el=this.rects.get(po);
         try{
         
                             this.oldr.dots.each(function(d){
                            d.attr({r: 1});
                            d.hide();
                           }); 
                          this.oldr.v.hide();
                          this.oldr.b.hide();
                         this.oldr.t.remove();   
         
         }catch(et){}
         this.oldr=el;
        // document.title = el+"::"+po; 
         try{ el.mo();Event.stop(e);}catch(e){ }
         
         }.bind(this);
         for (var i = 0; i < this.data.x.length; i+=stp) 
                {
                
                    var rx=i*this.Xstep+this.options.margin-this.Xstep/2;
                    var rw=this.Xstep*stp;
                        this.rect = this.p.rect(rx,this.options.margin/2,  rw, this.height-this.options.margin-this.options.margin/2).attr({'fill':'#fff','stroke':'transparent','stroke-width':0,'fill-opacity':0});
                        //this.rect.title="rect #"+i;
                        //this.rects
                        for (var iz = Math.round(rx); iz <= rx+rw; iz++) 
                {
                this.rects.set(iz,this.rect);
                }
                        this.rect.dots = this.dots[i];
                        this.rect.papa=$(this.options.renderTo);
                        this.rect.xs=this.xs[i][0];
                        this.rect.ys=this.data.y.length;
                        this.rect.data=this.data;
                        this.rect.v=this.vert;
                        this.rect.colors=this.colors;
                        this.rect.b=this.bubble;
                        this.rect.tw = this.width;
                        this.rect.t = this.p.set();
                        this.rect.p=this.p;
                        this.rect.options=this.options;
                        this.rect.i=i;
                        this.rect.mo=function(){
                            this.v.show();
                            this.v.attr({'x': this.xs});
                            this.b.show();
                        this.t.push(this.p.text(((this.xs < this.tw/2)?this.xs+10+this.options.bubbleWidth/2:this.xs-this.options.bubbleWidth/2-10), this.options.margin+20, this.options.bubbleHeadText+' '+((this.options.xIsDate)?parseDateString(this.data.x[this.i]).myformat(this.options.bubbleDateFormat):String(this.data.x[this.i])),2).attr({'fill':this.options.bubbleHeaderColor,'stroke':this.options.bubbleHeaderColor,'stroke-width':0.5,'font-size':this.options.fontSize})); 
                        for (var iy = 0; iy < this.ys; iy++) 
                        {
                            this.t.push(this.p.circle(((this.xs < this.tw/2)?this.xs+20:this.xs-this.options.bubbleWidth), this.options.margin+this.options.fontSize*3.5+this.options.fontSize*iy*1.3, 4).attr({'fill':this.colors[iy],'stroke-width':0}));
                            this.t.push(this.p.text(((this.xs < this.tw/2)?this.xs+this.options.bubbleWidth/2+10:this.xs-this.options.bubbleWidth/2-10), this.options.margin+this.options.fontSize*3.5+this.options.fontSize*iy*1.3, ((isNaN(this.data.y[iy][this.i]))?"-":String(Math.round(this.data.y[iy][this.i]*1000)/1000)),2).attr({'stroke':this.options.bubbleFontColor,'fill':this.options.bubbleFontColor,'stroke-width':0,'font-size':this.options.fontSize}));
                       }
                            this.b.attr({'x': ((this.xs < this.tw/2)?this.xs+10:this.xs-this.options.bubbleWidth-10)});
                            this.v.toFront();
                            this.dots.each(function(d){
                            d.show().toFront();
                            d.attr({r: 3});
                            d.animate({r: 5}, 1000, "bounce");
                           }.bind(this)); 
                           
                            
                        }
                        if(!Prototype.Browser.MobileSafari)
                        {
                       this.rect.mouseover(function(){
                       
                                
                            this.v.show();
                            this.v.attr({'x': this.xs});
                            this.b.show();
                        this.t.push(this.p.text(((this.xs < this.tw/2)?this.xs+10+this.options.bubbleWidth/2:this.xs-this.options.bubbleWidth/2-10), this.options.margin+20, this.options.bubbleHeadText+' '+((this.options.xIsDate)?parseDateString(this.data.x[this.i]).myformat(this.options.bubbleDateFormat):String(this.data.x[this.i])),2).attr({'fill':this.options.bubbleHeaderColor,'stroke':this.options.bubbleHeaderColor,'stroke-width':0.5,'font-size':this.options.fontSize})); 
                        for (var iy = 0; iy < this.ys; iy++) 
                        {
                            this.t.push(this.p.circle(((this.xs < this.tw/2)?this.xs+20:this.xs-this.options.bubbleWidth), this.options.margin+this.options.fontSize*3.5+this.options.fontSize*iy*1.3, 4).attr({'fill':this.colors[iy],'stroke-width':0}));
                            this.t.push(this.p.text(((this.xs < this.tw/2)?this.xs+this.options.bubbleWidth/2+10:this.xs-this.options.bubbleWidth/2-10), this.options.margin+this.options.fontSize*3.5+this.options.fontSize*iy*1.3, ((isNaN(this.data.y[iy][this.i]))?"-":String(Math.round(this.data.y[iy][this.i]*1000)/1000)),2).attr({'stroke':this.options.bubbleFontColor,'fill':this.options.bubbleFontColor,'stroke-width':0,'font-size':this.options.fontSize}));
                       }
                            this.b.attr({'x': ((this.xs < this.tw/2)?this.xs+10:this.xs-this.options.bubbleWidth-10)});
                            this.dots.each(function(d){
                            d.show().toFront();
                            d.attr({r: 3});
                            d.animate({r: 5}, 1000, "bounce");
                           }.bind(this)); 
                        });
                        
                        this.rect.mouseout(function(){
                        this.dots.each(function(d){
                            d.attr({r: 1});
                            d.hide();
                           }); 
                         this.v.hide();
                         this.b.hide();
                         this.t.remove();   
                        }); 
                        
                        }              
                 }    
                 
                 this.rect.toFront();
                                               if(this.options.debug)
                {
                   
                                        var d = new Date();
                    t="anim:"+(d.getTime()-stg);
                    $('GraphDebugDiv').insert(new Element("div").update(t));
                }
                
                          
        },
        createGrid:function()
        {
            stg=new Date().getTime();
            this.vert = this.p.rect(this.options.margin, this.options.margin/2, 1, this.height-this.options.margin-this.options.margin/2+5).attr({'fill':this.options.movingLineColor,'stroke-width':0,'stroke':'transparent'}).hide();
            this.bubble = this.p.rect(this.options.margin, this.options.margin+5, this.options.bubbleWidth, 40+this.options.fontSize*this.data.y.length*((navigator.userAgent.indexOf('iPhone')!=-1)?2:1), 10).attr({'fill':this.options.bubbleColor,stroke:this.options.bubbleBorderColor,'stroke-width':1,'fill-opacity':this.options.bubbleTransparent}).hide();
            var test=true;
           var stepWidth=0;
            var sstp=0;
            for (var i = 0; i < this.min; i++) 
                {
                        if(sstp+1<this.options.yS){sstp++;}else{sstp=0;}
                        if(stepWidth>=this.options.minStep && sstp==1){
                        this.p.rect(this.options.margin-5,i*this.Ystep+this.height-this.options.margin-(this.min-this.offs)*this.Ystep,  5, this.options.axisWidth).attr({'fill':this.options.axisColor,'stroke-width':0,'stroke':'transparent'}).insertBefore(this.zaglushka);
                        var t=function(i){ this.p.text(this.options.margin/2,this.height-this.options.margin-(this.min-this.offs)*this.Ystep+i*this.Ystep, "-"+i).attr({'fill':this.options.fontColor,'stroke':this.options.fontColor,'stroke-width':this.options.axisFontStrokeWidth,'font-size':this.options.fontSize}).insertBefore(this.zaglushka) }.bind(this).defer(i); 
                        stepWidth=this.Ystep;
                        }else{stepWidth+=this.Ystep;}
                    }
            var stepWidth=0;
            var sstp=0;
            
                        for (var i = 0; i < this.max; i++) 
                {
                
                    if(sstp+1<this.options.yS){sstp++;}else{sstp=0;}
                        if(stepWidth>=this.options.minStep && sstp==1){
                        var ff=this.height-this.options.margin-(this.min-this.offs)*this.Ystep-i*this.Ystep;
                        if(ff<this.height-this.options.margin) {
                            this.p.rect(this.options.margin-5,this.height-this.options.margin-(this.min-this.offs)*this.Ystep-i*this.Ystep,  5, this.options.axisWidth).attr({'fill':this.options.axisColor,'stroke-width':0,'stroke':'transparent'}).insertBefore(this.zaglushka);
                            }
                        var t=function(i,ff){  if(ff<this.height-this.options.margin) {this.p.text(this.options.margin/2,ff, +i).attr({'fill':this.options.fontColor,'stroke':this.options.fontColor,'stroke-width':this.options.axisFontStrokeWidth,'font-size':this.options.fontSize}).insertBefore(this.zaglushka) } }.bind(this).defer(i,ff); 
                        stepWidth=this.Ystep;
                        }else{stepWidth+=this.Ystep;}
                        
                    }
            stepWidth=0;
            sstp=0;      
                     for (var i = 0; i <= this.data.x.length+this.options.xS; i++) 
                {
                     if(sstp+1<this.options.xS){sstp++;}else{sstp=0;}
                        if(stepWidth>=this.options.minStep && sstp==1){
                        this.p.rect(i*this.Xstep+this.options.margin,this.height-this.options.margin,  this.options.axisWidth, 5).attr({'fill':this.options.axisColor,'stroke-width':0,'stroke':'transparent'}).insertBefore( this.zaglushka);
                        var t=function(i){ this.p.text(i*this.Xstep+this.options.margin, this.height-this.options.margin+15, ((this.options.xIsDate)?parseDateString(this.data.x[i]).myformat(this.options.xDateFormat):String(this.data.x[i]))).attr({'fill':this.options.fontColor,'stroke':this.options.fontColor,'stroke-width':this.options.axisFontStrokeWidth,'font-size':this.options.fontSize}).insertBefore(this.zaglushka) }.bind(this).defer(i);
                        
                        this.p.rect(i*this.Xstep+this.options.margin-stepWidth,this.options.margin/2,  stepWidth, this.height-this.options.margin-this.options.margin/2).attr({'fill':((!test)?this.options.matracColor:this.options.bgColor),'fill-opacity':((test)?0:this.options.bgAlpha/5),'stroke-width':0,'stroke':'transparent'}).insertAfter(this.bg);
                        test=!test; 
                        stepWidth=this.Xstep;
                        }else{stepWidth+=this.Xstep;}                    
            }
           

            
                                          if(this.options.debug)
                {
                    var d = new Date();
                    t="grid:"+(d.getTime()-stg);
                    $('GraphDebugDiv').insert(new Element("div").update(t));
                }
        },
        createGraph:function()
        {
        
        st=new Date().getTime();
                    
              for (var i = 0; i<this.data.y.length; i++) 
                    { 
                                    var svgstr="M ";
                                    var initx=false;
                                                prevx=false;
                                                prevy=false;
                                    for (var ii = 0; ii < this.data.y[i].length; ii++) 
                                    { 
                                    
                                        if(typeof this.dots[ii] == "undefined"){this.dots[ii]=[];this.xs[ii]=[];}
                                        var dx = this.options.margin+ii*this.Xstep;
                                                
                                    if( !isNaN(this.data.y[i][ii])){
                                   
                                    
                                                var dy = this.height - ((Number(this.data.y[i][ii])+this.min-this.offs)*this.Ystep) - this.options.margin;
                                                var t=function(i,ii,dx,dy){
                                                var dot = this.p.circle(dx, dy, 1).attr({'fill':this.colors[i],'fill-opacity':0.9,'stroke-width':0.5,'stroke':'#fff'});
                                                    this.dots[ii].push(dot);
                                                    
                                                   dot.hide();
                                                }.bind(this).defer(i,ii,dx,dy); 
                                                   this.xs[ii].push(dx);
                                                if(ii>0 && prevx && prevy){
                                                   svgstr+=prevx+" "+prevy+"L"+dx+" "+dy+" ";
                                                   }
                                                   prevx=dx;
                                                   prevy=dy;
                                                   if(!initx){initx=dx;}
                                       }else
                                       {
                                      this.xs[ii].push(dx);
                                       }
                                    
                                        }
                                        var test1=false;
                                        try
                                        {
                                        if( this.options.data.closePath[i] ){test1=true;}
                                        }
                                        catch(e){
                                        alert(e);
                                        test1=false;
                                        }
                                        if( test1 ){
                                        var myy=((this.NullY>this.height-this.options.margin)?this.height-this.options.margin:this.NullY);
                                            svgstr+=dx+" "+(myy+1)+"L"+initx+" "+(myy+1)+ " Z";
                                            this.p.path(svgstr).insertBefore(this.Yaxis).attr({'stroke':this.colors[i],'stroke-width':this.options.lineWidth,'fill':this.colors[i],'fill-opacity':0.3});
                        
                                        }else
                                        {
                                        this.p.path(svgstr).insertBefore(this.Yaxis).attr({'stroke':this.colors[i],'stroke-width':this.options.lineWidth});
                        
                                        }
                                   
                    }
                if(this.options.debug)
                {
                    var d = new Date();
                    t="graph:"+(d.getTime()-st);
                    $('GraphDebugDiv').insert(new Element("div").update(t));

                }
                }
        
        });
        

/*
    var parseDateString = function(i)
         {
				var rg = /:|-| |\./;
				var rg2 = /,0(\d)/;
				try {var str = i.gsub(rg,",").gsub(rg2,",#{1}");
				 eval('var d = new Date('+str+')'); }
				catch (e){ return (new Date()) }
				d.setMonth(d.getMonth()-1);
				return(d);
         }
*/