function changeImg(){
	$('#graphmenu li').hover(function() {
		if (!$(this).hasClass('active')){
			$('a', this).css("text-decoration", "none");
			$('a.icon img.unhover', this).css("display", "none");
			$('a.icon img.hover', this).css("display", "");
			//для корректной работы png-fix'а
			if ($.browser.version == 6) $('a.icon img', this).css("background-image", '');
		}
	}, function() {
		if (!$(this).hasClass('active')){
			$('a', this).css("text-decoration", "");
			$('a.icon img.hover', this).css("display", "none");
			$('a.icon img.unhover', this).css("display", "");
			//для корректной работы png-fix'а
			if ($.browser.version == 6) $('a.icon img', this).css("background-image", '');
		}
	});
}

function ieHover(){ 
	$('#graphmenu li').hover(function() {
	  $(this).addClass('hover');
	}, function() {
	  $(this).removeClass('hover');
	});
}

function ieWidth(){
	var minWidth = 500;
	if ($('ul#topmenu').width() < minWidth ) $('ul#topmenu').width(minWidth);
}


function colorRowTable(){
	$('div.left-area-content div.parameters table tr:odd').css("background-color", "#dededd");
	$('div.left-area-content div.parameters table tr td:last-child').css('border-right', 'none');
}

function hrLine(){
    $('<hr class="paramenters-line" />').insertAfter('div.left-area-content div.parameters table');
}

function textHeigth(){
	$('span.left').each(function(){
		var logo = $('div.brand-logo', this);
		text = $('div.text', this);

		nextSpan = $(this).next();
		nextLogo = $('div.brand-logo img', nextSpan);
		nextText =$('div.text', nextSpan);
		if (logo.height() >= nextLogo.height()){
			difference = parseInt((logo.height()-nextLogo.height())/2);
			nextLogo.css('margin-top', difference+'px');
			nextText.css('padding-top', difference+'px');
		}
		else if(logo.height() < nextLogo.height()){
			difference = parseInt((nextLogo.height()-logo.height())/2);
			logo.css('margin-top', difference+'px');
			text.css('padding-top', difference+'px');
		}
	})
}

function listFix(){
    $("ul.type-list li:nth-child(3n+1)").css("margin-left", "0");
}

function ieCorners(){
    $('div.round-border, div#content div#brand-rubricator, span.bestseller a.img').each(function() {
        PIE.attach(this);
    });

}

function specialOffersFix(){
    var startIndex = 0; //индекс элемента, откуда ничинаем обход
    var stringCount = 4; // уоличество элементов в строке
    var elemCount = $('span.bestseller').size(); // количество элементов
    var tempHeight = 0;
    var maxiHeight = 0;
    for (i=0; i<elemCount; i++){
        //текущий элемент и его высота
        tempElem = $('span.bestseller').eq(i);
        tempHeight = $('h2', tempElem).height();
        if (i==startIndex) maxiHeight = tempHeight;
        //следующий элемент и его высота
        tempElemNext = $('span.bestseller').eq(i+1);
        tempHeightNext = $('h2', tempElemNext).height();

        if (tempHeightNext>tempHeight && i<(startIndex+stringCount-1)) maxiHeight=tempHeightNext;
        if (i==(startIndex+stringCount-1)){
            $('span.bestseller').each(function(i){
                if($(this).index()>=(startIndex) && $(this).index()<=(startIndex+stringCount-1)){
                    $('h2', this).css('height', maxiHeight);
                }
            })
            startIndex += stringCount;
            tempHeight = 0;
            maxiHeight = 0;
        }
    }
}


$(document).ready(function(){
  /*  // бегущая строка с филиалами
    $('#scroller-container').crawlLine({
        speed:2,
        crawElement:'ul#scroller',
        textElement:'li'
    });
*/
    if ($.browser.msie && $.browser.version == 6){
		ieHover(); // смена класса элемента по наведению (эмуляция :hover для не-ссылок)
		ieWidth(); // эмуляция min-width для верхней менюхи
	}

    if ($.browser.msie && $.browser.version < 9 && $.browser.version > 6) ieCorners(); //скругленные углы элементов в осле (в 6 ие косяки, поэтому их там не будет)
	textHeigth(); //подгоняем размеры блоков там, где 2 колонки
    specialOffersFix(); //выравнивание блоков спецпредложений
	colorRowTable(); //чередование цвета строк в таблице
    hrLine(); //вставка полосы после таблицы с параметрами
	changeImg(); //смена иконок меню
    listFix(); //убираем margin для 1-го li в списке рубрикатора
    
	
    // галерея
    $(function(){
		$('a.lightbox').lightBox({
			imageLoading: '/assets/templates/rusagro/lightbox/images/lightbox-ico-loading.gif',
			imageBtnClose: '/assets/templates/rusagro/lightbox/images/lightbox-btn-close.gif',
			imageBtnPrev: '/assets/templates/rusagro/lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext: '/assets/templates/rusagro/lightbox/images/lightbox-btn-next.gif',
			imageBlank: '/assets/templates/rusagro/lightbox/images/lightbox-blank.gif',
			showCount: false
		});
	});
});
