jQuery.fn.planBus = function(options){

    // настройки по умолчанию
    var options = jQuery.extend({
      numPlace: 42,
      numCol: 11,
      numRow: 4,
      topFirst: 160,
      leftFirst: 507,
      myPlace:'',
      valR: 0
    },options);

    return this.each(function() {
       //рисуем схему автобуса
	k=0;    
        for(var i=1;i<=options.numCol;i++){
            for(var j=1;j<=options.numRow;j++){ 
	    	n=options.topFirst-32*j;
	    	if(j==3) n=options.topFirst-35*j;
	    	if(j==4) n=options.topFirst-32*j-9;
            if(options.valR==0){
                $(this).append('<div class="empty" style="top:'+n+'px; left:'+(options.leftFirst-39*i)+'px;"><span>'+(k+1)+'</span></div>');
            }else{
                if(arrayPlace[k+1]==1 && orderPlace[k+1]!=1) $(this).append('<div class="busy" style="top:'+n+'px; left:'+(options.leftFirst-39*i)+'px;" id="'+(k+1)+'"><span>'+(k+1)+'</span></div>');
                else {
                    if(orderPlace[k+1]==1) $(this).append('<div class="myPlace" style="background:url(/js/planBus/place1.png) no-repeat; top:'+n+'px; left:'+(options.leftFirst-39*i)+'px;" id="'+(k+1)+'"><span>'+(k+1)+'</span></div>');
                    else  $(this).append('<div style="top:'+n+'px; left:'+(options.leftFirst-39*i)+'px;" id="'+(k+1)+'"><span>'+(k+1)+'</span></div>');
                }
            }
            k++;
            if(k==options.numPlace) break;
            }
        };

        if(options.myPlace!=''){
            arrayMyPlace = new Array();
            arrayMyPlace = options.myPlace.split(", ");
            for(i=0;i<arrayMyPlace.length;i++){
                jQuery('div#'+arrayMyPlace[i],this).addClass('myPlace')
                                                .css('background','url("/js/planBus/place1.png") no-repeat');
            }
        }
        
        //наведение на места
        jQuery('div',this).hover(
            function(){
                if(!$(this).hasClass('busy') && !$(this).hasClass('empty')) $(this).css('background','url("/js/planBus/place1.png") no-repeat');
            },
            function(){
                if(!$(this).hasClass('busy') && !$(this).hasClass('myPlace') && !$(this).hasClass('empty')) $(this).css('background','url("/js/planBus/place0.png") no-repeat');
            }
        );
        //клик по месту
        jQuery('div',this).click(function(){
		if(!$(this).hasClass('busy') && !$(this).hasClass('empty')){
			if(!$(this).hasClass('myPlace')){
				$(this).css('background','url("/js/planBus/place1.png") no-repeat');
				$(this).toggleClass('myPlace');
			}else{
				$(this).css('background','url("/js/planBus/place0.png") no-repeat');
				$(this).toggleClass('myPlace');
			}
		}
        });
    });
};
