var PCLLincs = undefined;
var PCLCtx = undefined;
var PCLCtxBg = undefined;
var PCLProducersUl = undefined;
var PCLCategoriesUl = undefined;
var PCLWrapCanvas = undefined;

var catalogLLM = undefined;

var PCLProducersArray = new Array;
var PCLCategoriesArray = new Array;
var PCLLineTopOffset = 12;
var PCLLineHeight = 1;

TreeDrawing = function(TDdirection, TDElement){
    switch ( TDdirection ){
    case 'leftToRight':
		PCLCtx.clearRect(0,0,PCLLincs.width,PCLLincs.height);
        PCLCtx.fillStyle = '#ffcc33';
        //PCLCtx.fillStyle = '#cccc99';
        PCLCtx.fillRect( PCLCategoriesArray[TDElement.rev].left, PCLCategoriesArray[TDElement.rev].top + PCLLineTopOffset, 83+PCLLineHeight, PCLLineHeight );
        for ( i in  PCLCategoriesArray ){
            if( PCLCategoriesArray[i].arrayElement ){
                jQuery(PCLCategoriesArray[i].a).removeClass('hover');
            }
        }
        for ( i in  PCLProducersArray ){
            if( PCLProducersArray[i].arrayElement ){
                jQuery(PCLProducersArray[i].a).removeClass('hover');
            }
        }
        jQuery(PCLCategoriesArray[TDElement.rev].a).addClass('hover');
        
        var min = PCLCategoriesArray[TDElement.rev].top;
        var max = PCLCategoriesArray[TDElement.rev].top;
		
		jQuery(PCLWrapCanvas).find('.PCLLeftNum').remove();
		jQuery(PCLWrapCanvas).find('.PCLRightNum').remove();
		
		if( PCLCategoriesArray[TDElement.rev].rel.length ){
			for ( i in  PCLCategoriesArray[TDElement.rev].rel ){
				var relI0 = PCLCategoriesArray[TDElement.rev].rel[i][0];
				if( parseInt(relI0) ){
					if ( PCLProducersArray[ relI0 ].top < min ){
						if ( PCLProducersArray[ relI0 ].column == 1 ){
							min = PCLProducersArray[ relI0 ].top + 20;
						}else{
							min = PCLProducersArray[ relI0 ].top;
						}
					}
					if ( PCLProducersArray[ relI0 ].top >= max ){
						if ( PCLProducersArray[ relI0 ].column == 1 ){
							max = PCLProducersArray[ relI0 ].top + 20;
						}else{
							max = PCLProducersArray[ relI0 ].top;
						}
					}
					PCLCtx.fillRect( PCLProducersArray[ relI0 ].left - 50, PCLProducersArray[ relI0 ].top + PCLLineTopOffset, 50, PCLLineHeight );
					
					var tmpCountText = PCLCategoriesArray[TDElement.rev].rel[i][1] + 'шт.';
					var tmpEl = jQuery('<div class="PCLLeftNum"> </div>');
					tmpEl.css({top: (PCLProducersArray[ relI0 ].top + 0)+'px', left: (PCLProducersArray[ relI0 ].left - 40)+'px'}).text(tmpCountText);
					jQuery(PCLWrapCanvas).append(tmpEl);
					if (PCLProducersArray[ relI0 ].column == 1){
						// Линия вниз (в правом столбце)
						PCLCtx.fillRect( PCLProducersArray[ relI0 ].left-50-PCLLineHeight, PCLProducersArray[ relI0 ].top + PCLLineTopOffset, PCLLineHeight, 20+PCLLineHeight );
						// Линия влево (из правого столбца)
						PCLCtx.fillRect( PCLProducersArray[ relI0 ].left-50-225, PCLProducersArray[ relI0 ].top + PCLLineTopOffset + 20, 225, PCLLineHeight );
					}
				}
			}
		}else{
			max = PCLProducersUl.height()+25;
			PCLCtx.fillRect( PCLCategoriesArray[TDElement.rev].left+84, max+13, 389, PCLLineHeight );
		}
		// Вертикальная основная линия
        PCLCtx.fillRect( 434, min + PCLLineTopOffset, PCLLineHeight, max - min + PCLLineHeight );
    break;
    case 'rightToLeft':
        PCLCtx.clearRect(0,0,PCLLincs.width,PCLLincs.height);
        PCLCtx.fillStyle = '#ffcc33';
        //PCLCtx.fillStyle = '#cccc99';
		PCLCtx.fillRect( PCLProducersArray[TDElement.rev].left-50, PCLProducersArray[TDElement.rev].top + PCLLineTopOffset, 50, PCLLineHeight );
		
		if (PCLProducersArray[TDElement.rev].column == 1){
			// Линия вниз (в правом столбце)
			PCLCtx.fillRect( PCLProducersArray[TDElement.rev].left-50-PCLLineHeight, PCLProducersArray[TDElement.rev].top + PCLLineTopOffset, PCLLineHeight, 20+PCLLineHeight );
			// Линия влево (из правого столбца)
			PCLCtx.fillRect( PCLProducersArray[TDElement.rev].left-50-225, PCLProducersArray[TDElement.rev].top + PCLLineTopOffset + 20, 225, PCLLineHeight );
		}
		
        for ( i in  PCLProducersArray ){
            if( PCLProducersArray[i].arrayElement ){
                jQuery(PCLProducersArray[i].a).removeClass('hover');
            }
        }
        for ( i in  PCLCategoriesArray ){
            if( PCLCategoriesArray[i].arrayElement ){
                jQuery(PCLCategoriesArray[i].a).removeClass('hover');
            }
        }
        jQuery(PCLProducersArray[TDElement.rev].a).addClass('hover');
        
        var min = PCLProducersArray[TDElement.rev].top;
		if ( PCLProducersArray[TDElement.rev].column == 1 ){
			var max = PCLProducersArray[TDElement.rev].top + 20;
		}else{
			var max = PCLProducersArray[TDElement.rev].top;
		}
		
		jQuery(PCLWrapCanvas).find('.PCLLeftNum').remove();
		jQuery(PCLWrapCanvas).find('.PCLRightNum').remove();
		
        for ( i in  PCLProducersArray[TDElement.rev].rel ){
			var relI0 = PCLProducersArray[TDElement.rev].rel[i][0];
            if( parseInt(relI0) ){
                if ( PCLCategoriesArray[ relI0 ].top < min ){
                    min = PCLCategoriesArray[ relI0 ].top;
                }
                if ( PCLCategoriesArray[ relI0 ].top > max ){
                    max = PCLCategoriesArray[ relI0 ].top;
                }
                PCLCtx.fillRect( 350, PCLCategoriesArray[ relI0 ].top + PCLLineTopOffset, 85, PCLLineHeight );
				
                var tmpCountText = PCLProducersArray[TDElement.rev].rel[i][1] + 'шт.';
				var tmpEl = jQuery('<div class="PCLRightNum"> </div>');
				tmpEl.css({top: (PCLCategoriesArray[ relI0 ].top + 0)+'px', left: (PCLCategoriesArray[ relI0 ].left - 10)+'px'}).text(tmpCountText);
				jQuery(PCLWrapCanvas).append(tmpEl);
            }
        }
		
		PCLCtx.fillRect( 435, min + PCLLineTopOffset, PCLLineHeight, max - min + PCLLineHeight );
    break;
    default:
        alert('Ошибка :-(');
    }
}
delayGetContext = function(){
	if (PCLLincs.getContext){
		PCLCtx = PCLLincs.getContext('2d');
		PCLCtx.clearRect(0,0,PCLLincs.width,PCLLincs.height);
		
		PCLCategoriesUl.find('a').mouseover( function(){ TreeDrawing('leftToRight', this) } );
		PCLProducersUl.find('a').mouseover( function(){ TreeDrawing('rightToLeft', this) } );
		
		PCLCategoriesUl.find('a:eq(0)').each( function(){ TreeDrawing('leftToRight', this) } );
	}else{
		setTimeout(function(){delayGetContext()}, 10);
	}
}
catalogLLMInit = function(){
	jQuery(catalogLLM).find('li').each(function(){
		if ( jQuery(this).find('>ul').length ){
			jQuery(this).addClass('plus');
			jQuery(this).find('>a').click(function(){
				jQuery(this).parent().find('>ul').slideToggle(300);
				jQuery(this).parent().toggleClass("plus").toggleClass("minus");
				return false;
			});
			if (!jQuery(this).hasClass('active')){
				jQuery(this).find('>ul').slideUp(0);
			}else{
				jQuery(this).removeClass('plus').addClass('minus');
			}
		}
	});
}
jQuery(document).ready(function(){
    producerCategoryLink = document.getElementById("producerCategory");
    PCLProducersUl = jQuery(producerCategoryLink).find('.PCLProducers').find('ul');
    PCLCategoriesUl = jQuery(producerCategoryLink).find('.PCLCategories').find('ul');
    PCLWrapCanvas = jQuery('#PCLWrapCanvas');
    var prevRev = -1;
	var left = 0;
    PCLProducersUl.find('li').each(function(i, val){
		if ( i%2 ){
			left = 485 + 225;
			column = 1;
		}else{
			left = 485 + 0;
			column = 0;
		}
        var tmpA = jQuery(this).find('a');
        var top = 0;
        if ( i > 1 ){
			if ( i%2 ){
				top = PCLProducersArray[prevRev].top;
			}else{
				top = PCLProducersArray[prevRev].top + PCLProducersArray[prevRev].height;
			}
        }
        var prev = -1;
        if ( i > 1 ){
            prev = prevRev;
        }
        PCLProducersArray[tmpA.attr('rev')] = {
            arrayElement: true,
            num: i, 
            rel: eval( '[' + tmpA.attr('rel').split(",") + ']' ), 
            rev: tmpA.attr('rev'), 
            height: jQuery(this).height(), 
            a: tmpA, 
            li: jQuery(this), 
            prev: prev, 
            top: top, 
            left: left,
			column: column
        }
        prevRev = tmpA.attr('rev');
    });
    var prevRev = -1;
    PCLCategoriesUl.find('li').each(function(i, val){
        var top = 0;
        if ( i != 0 ){
            top = PCLCategoriesArray[prevRev].top + PCLCategoriesArray[prevRev].height;
        }
        var prev = -1;
        if ( i != 0 ){
            prev = prevRev;
        }
        var tmpA = jQuery(this).find('a');
        PCLCategoriesArray[tmpA.attr('rev')] = {
            arrayElement: true,
            num: i, 
            rel: new Array(),
            rev: tmpA.attr('rev'), 
            height: jQuery(this).height(), 
            a: tmpA, 
            li: jQuery(this),
            prev: prev, 
            top: top, 
            left: 350,
			column: 0
        }
        var n = 0;
        for ( j in  PCLProducersArray ){
            if( PCLProducersArray[j].arrayElement == true ){
                for ( k in PCLProducersArray[j].rel ){
                    if( PCLCategoriesArray[tmpA.attr('rev')].rev == PCLProducersArray[j].rel[k][0] ){
                        PCLCategoriesArray[tmpA.attr('rev')].rel[n] = [ PCLProducersArray[j].rev, PCLProducersArray[j].rel[k][1] ];
                        n++;
                    }
                }
            }
        }
        prevRev = tmpA.attr('rev');
		
    });
    
    PCLLincs = document.getElementById('PCLLincs');
	if(PCLLincs){
		if ( ( PCLProducersUl.height() + 44 ) > PCLCategoriesUl.height() ){
			PCLLincs.height = PCLProducersUl.height() + 44;
		}else{
			PCLLincs.height = PCLCategoriesUl.height();
		}
		PCLLincs.style.height = PCLLincs.height + 'px';
		delayGetContext();
	}
	
	var catalogCompaniesMenu = jQuery('div.catalogCompaniesMenu>ul>li>a');
	if (catalogCompaniesMenu.length){
		jQuery('div.catalogCompaniesMenuWrap').each(function(){
			jQuery(this).find('>h2>span').click(function(){
				var menu = jQuery(this).parent().parent().find('>div.catalogCompaniesMenu');
				if ( menu.css('display') == 'none' ){
					menu.css({'display':'block'});
				}else{
					menu.css({'display':'none'});
				}
			});
		})
		jQuery(catalogCompaniesMenu).each(function(){
			jQuery(this).click(function(){
				jQuery('div.catalogCompaniesMenu').css({'display':'none'});
				var idP = jQuery(this).attr('href');
				idP = idP.substr(idP.indexOf("#"));
				jQuery.scrollTo(idP, 1000);
				return false;
			});
		});
	}
	catalogLLM = document.getElementById('catalogLeftLevelMenu');
	if (catalogLLM){
		catalogLLMInit();
	}
});