window.addEvent('domready', function(){

/* new functions */

var RolMedLeft = $$('.RolMediaLeft');
RolMedLeft.set('morph', {duration: 'long', transition: Fx.Transitions.Bounce.easeOut});
var LoadRolMedia = function(){
	RolMedLeft.morph({ 'left':'-120px'});
}.delay(10000);

var Tips01 = new Tips(('.otzivRow, .LST, .LFT, .LFB, .tips'), { className: 'zindex' });

var otzivRating = $$('.item .ball B, .showStars');
otzivRating.each(function(item) {
	var rVal = item.get('text');
	var rShow = (rVal*1).round(2);
	var rPers = (rVal/5.0*100).round();
	item.set('html',
	'<SPAN class="ratingStars"><DIV style="width:'+ rPers +'%;"></DIV></SPAN>'+rShow
	);
});

$$('.linesTab .row').addEvents({
	mouseenter:function(){
		this.setProperty('class','row_over');
	},
	mouseleave:function(){
		this.setProperty('class','row');
	}
});

$$('.RolMediaLeft').addEvents({
	mouseenter:function(){
		this.morph({'left':'-10px'});
		
	},
	mouseleave:function(){
		this.morph({'left':'-120px'});
	}
});


var navigators = $$('.navPas .rowDownNav, .navAct .rowDownNav, .navPas .rowTopNav, .navAct .rowTopNav');
navigators.addEvents({
	mouseenter:function(){
		this.setStyle('background-color','#f3e8c3');
		this.getElement('.right').setStyle('background-position','-25px 0px');
	},
	mouseleave:function(){
		this.setStyle('background-color','#f3f1e4');
		this.getElement('.right').setStyle('background-position','-35px 0px');
	},
	click:function(){
		var objName = this.getProperty('name');
		var objClass = this.getProperty('class');
		if(objClass == 'rowDownNav') {
			this.setProperty('class','rowTopNav');
			if(objName) {
				$$('.'+objName).setStyle('display','');
			}
			else {
				navMenu(this.getParent(), 'show');
			}
		}
		else {
			this.setProperty('class','rowDownNav');
			if(objName) {
				$$('.'+objName).setStyle('display','none');
				if(objName=='markaBlock') $$('.markaBlockTop').setStyle('display','none');
			}
			else {
				navMenu(this.getParent(), 'hide');
			}
		}
	}
});

$$('.navPas, .navAct').addEvents({
	mouseenter:function(){
		this.setStyle('z-index', 100);
	},
	mouseleave:function(){
		this.setStyle('z-index', 10);
	}
});

$$('.navMenu').addEvents({
	mouseleave:function(){
		var rowTop = this.getParent().getElement('.rowTopNav');
		if (rowTop) rowTop.setProperty('class','rowDownNav'), this.setStyle('display','none'), $$('#cars_tab SELECT').setStyle('visibility','visible');
	}
});
function navMenu(obj, what) {
	if(what=='show'){
		obj.getElement('.navMenu').setStyle('display','block');
		$$('#cars_tab SELECT').setStyle('visibility','hidden');
	}
	else{
		obj.getElement('.navMenu').setStyle('display','none');
		$$('#cars_tab SELECT').setStyle('visibility','visible');
	}
};


// Процедура сворачивания марок
	var ShowMarkaBlock = $$('.markaBlockTop TABLE TD A.dash, .markaBlock TABLE TD A.dash, .markaBlockSelect A.dash');
	ShowMarkaBlock.addEvents({
		'click':function(){
			var thisName = this.getProperty('name');
			var thisID = this.getProperty('id');
			if (thisName == 'modelShow' || thisID == 'modelShow') {
				$$('.modelRower').setStyle('display','');
				this.erase('name').set({'name':'modelHide','id':'modelHide','text':'скрыть модели'});
				this.getParent().getElement('.RowDown').set('class','RowTop');
			}
			else if (thisName == 'modelHide' || thisID == 'modelHide') {
				$$('.modelRower').setStyle('display','none');
				this.erase('name').set({'name':'modelShow','id':'modelShow','text':'все модели'});
    			this.getParent().getElement('.RowTop').set('class','RowDown');
			}
			else {
				this.getParent('div').setStyle('display','none');
				$$('.'+thisName).setStyle('display','block');
	//			$$('#navigator').getElement('span[name='+this.getParent('div').getProperty('class')+']').set('class','rowDownNav');
			}
		}
	});
// сворачивание закончено
	
	var MarkaLogo = $$('.markaBlockTop IMG, .markaBlock IMG');

	var CarsTabView = Cookie.read('CarsTabView');
	if (CarsTabView) { setCarsTabView(CarsTabView); }
	else { $$('#LikeTable').setStyle('display','block'); }

	var ViewSelect = $$('#ViewSelect span');
		ViewSelect.addEvents({
		click: function(){
			ViewSelect.setStyle('cursor','pointer'),
			setCarsTabView( this.getProperty('class') );
			}
		});


		$$('tr.row3 .small').each(function(item) { if (item.getProperty('name')) { item.set('src', item.getProperty('name')).erase('name'); } });

	
	function setCarsTabView(howView) {
		if (howView == 'LFT') {
			$$('.zoomMe').setStyles({'width':'100px','display':'none'});
			$$('#ViewSelect .LST').setStyle('background-position','0px -19px');
			$$('#ViewSelect .LFT').setStyles({'background-position' : '-19px 0px','cursor' : 'default'});
			$$('#ViewSelect .LFB').setStyle('background-position','-38px -19px');
			$$('#LikeTable').setStyle('display','block');
			$$('#LikeBlocks').setStyle('display','none');
			$$('.carsTabImgA .ico').setStyle('display','none');
			$$('.carsTabImgA .small').setStyle('display','block');
			$$('#LikeTable .zoomMe').setStyle('opacity','0');
			$$('#LikeTable .small').each(function(item) {
				if (item.getProperty('name')) { item.set('src', item.getProperty('name')).erase('name'); }
			});

			Cookie.write('CarsTabView','LFT', {path: '/'});
			viewSort('hide');
			showMarkLogo('hide');
		}
		else if (howView == 'LFB') {
			$$('#ViewSelect .LST').setStyle('background-position','0px -19px');
			$$('#ViewSelect .LFT').setStyle('background-position','-19px -19px');
			$$('#ViewSelect .LFB').setStyle('background-position','-38px 0px');
			$$('#LikeTable').setStyle('display','none');
			$$('#LikeBlocks').setStyle('display','block');
			$$('#LikeBlocks .zoomMe').setStyle('opacity','0');
			$$('#LikeBlocks .hiddenFoto').each(function(item) {
				if (item.getProperty('name')) { item.set('src', item.getProperty('name')).erase('name'); }
				});

			Cookie.write('CarsTabView','LFB', {path: '/'});
			viewSort('show');
			showMarkLogo('show');
		}
		else {
			$$('.zoomMe').setStyles({'width':'20px','display':'none'});
			$$('.carsTabImgAP .zoomMe').setStyle('width','100px');
			$$('#ViewSelect .LST').setStyle('background-position','0px 0px');
			$$('#ViewSelect .LFT').setStyle('background-position','-19px -19px');
			$$('#ViewSelect .LFB').setStyle('background-position','-38px -19px');
			$$('#LikeTable').setStyle('display','block');
			$$('#LikeBlocks').setStyle('display','none');
			$$('.carsTabImgA .ico').setStyle('display','block');
			$$('.carsTabImgA .small').setStyle('display','none');
			$$('#LikeTable .zoomMe').setStyle('opacity','0');
			Cookie.dispose('CarsTabView', {path: '/'});
			viewSort('hide');
			showMarkLogo('hide');
		}
	};

	function viewSort(how) {
		var sortPan = $$('#carsTabPanel .sort');
		if ($('carsTabPanel')) {
			var sW = sortPan.getProperty('rel');
			if (sW =='S') var sortWidth = '300px';
			else if (sW =='SS') var sortWidth = '200px';
			else { var sortWidth = '670px';  }; 
			if (how == 'show') sortPan.morph ({ 'width': sortWidth });
			else if (how == 'hide') { sortPan.morph ({ 'width': '1px' }); } 		
		}
	};


// MarkLogo Display Function

	function showMarkLogo(what) {
		if(what == 'show') {
			var imgURL = 'http://img2.autonavigator.ru/images/logos/ico/';
			$$('.m_List img, .m_ListTop img, .m_List_act img, .m_ListTop_act img').setStyle('display','inline-block').each(function(item) {
				if (item.getProperty('name')) { item.set({'src':imgURL+item.getProperty('name')+'.gif','height':'20','width':'36'}).erase('name'); }
			});
		} else { 
			$$('.m_List img, .m_ListTop img').setStyle('display','none');
		}
	}

// setPriceType
	var PriceType = Cookie.read('PriceType');
	if (PriceType) {
		setCur(PriceType);
	}

	var PriceTypeSelect = $$('#PriceTypeSelect, #PanelPriceTypeSelect');
	PriceTypeSelect.addEvents({
		'change': function(){
			setCur( this.getSelected().getProperty('value') );
		}
	});

	function setCur(cur) {
		if (cur == 'RUR') {
			$$('.DEF, .USD, .EUR').setStyle('display','none');
			$$('.RUR').setStyle('display','inline-block');
			Cookie.write('PriceType','RUR', {path: '/'});
			$$('#PriceTypeSelect, #PanelPriceTypeSelect').getElement('option[value=RUR]').setProperty('selected','selected');
		}
		else if (cur == 'USD') {
			$$('.RUR, .DEF, .EUR').setStyle('display','none');
			$$('.USD').setStyle('display','inline-block');
			Cookie.write('PriceType','USD', {path: '/'});
			$$('#PriceTypeSelect, #PanelPriceTypeSelect').getElement('option[value=USD]').setProperty('selected','selected');
		}
		else if (cur == 'EUR') {
			$$('.RUR, .USD, .DEF').setStyle('display','none');
			$$('.EUR').setStyle('display','inline-block');
			Cookie.write('PriceType','EUR', {path: '/'});
			$$('#PriceTypeSelect, #PanelPriceTypeSelect').getElement('option[value=EUR]').setProperty('selected','selected');
		}
		else {
			$$('.RUR, .USD, .EUR').setStyle('display','none');
			$$('.DEF').setStyle('display','inline-block');
			Cookie.dispose('PriceType', {path: '/'});
			$$('#PriceTypeSelect').getElement('option[value=DEF]').setProperty('selected','selected');
		}
	};
	


	var imgZoomerText = $$('.carsTabImg, .carsTabImgP');
	imgZoomerText.addEvents({
		mouseenter: function(){
			$$('#'+this.id+'a .zoomMe').morph ({ width: 200, opacity: 1 }),
			$$('#'+this.id+'a .zoomMe').setStyle('display','block'),
			$$('#up'+this.id+'a').morph({'z-index': '30'});
			if ($$('#'+this.id+'a .zoomMe').getProperty('name')) { $$('#'+this.id+'a .zoomMe').set('src', $$('#'+this.id+'a .zoomMe').getProperty('name')).erase('name'); }
			
		},
		mouseleave: function(){
			// what ON and what OFF
			if ($$('#'+this.id+'a .small').getStyle('display') != 'none') {
				var oldZoomWidth = 100; /*$$('#'+this.id+'a .small').get('width')*/ var oldOpacity = 0; }
			else if ($$('#'+this.id+'a .ico').getStyle('display') != 'none') {
				var oldZoomWidth = 20; /*$$('#'+this.id+'a .ico').get('width')*/ var oldOpacity = 0; }
			$$('#'+this.id+'a .zoomMe').morph ({ 'width': oldZoomWidth, 'opacity': oldOpacity }),
			$$('#'+this.id+'a .zoomMe').setStyle('display','block');
			$$('#up'+this.id+'a').morph({'z-index': '10'});
		 }
	});

	var imgZoom = $$('.carsTabImgA, .carsTabImgAP');
	imgZoom.addEvents({
		mouseenter: function(){ 
			$$('#'+this.id+' .zoomMe').morph ({ width: 200, opacity: 1 }),
			$$('#'+this.id+' .zoomMe').setStyle('display','block'),
			$$('#up'+this.id).morph({'z-index': '30'});
			if ($$('#'+this.id+' .zoomMe').getProperty('name')) {
			/*$$('#testing').appendText($$('#'+this.id+' .zoomMe').getProperty('name')+','+$$('#'+this.id+' .zoomMe'));*/
			$$('#'+this.id+' .zoomMe').set('src', $$('#'+this.id+' .zoomMe').getProperty('name')).erase('name');
			}
		},
		mouseleave: function(){
			// what ON and what OFF
			if ($$('#'+this.id+' .small').getStyle('display') != 'none') {
				var oldZoomWidth = 100; var oldOpacity = 0; }
			else if ($$('#'+this.id+' .ico').getStyle('display') != 'none') {
				var oldZoomWidth = 20; /*$$('#'+this.id+'a .ico').get('width')*/ var oldOpacity = 0; }
			$$('#'+this.id+' .zoomMe').morph ({ 'width': oldZoomWidth, 'opacity': oldOpacity }),
			$$('#'+this.id+' .zoomMe').setStyle('display','block'),
			$$('#up'+this.id).morph({'z-index': '10'});
		}
	});

	var iZoom = $$('a.carsFotoZoom');
	iZoom.addEvents({
		mouseenter: function(){ 
			$$('#'+this.id+' .zoomMe').morph ({ width: 200, opacity: 1 }),
			$$('#'+this.id+' .zoomMe').setStyles({'display':'inline-block'})
			$$('#'+this.id).setStyle('z-index','300');
		},
		mouseleave: function(){
			$$('#'+this.id+' .zoomMe').morph ({ width: 100, opacity: 0 }),
			$$('#'+this.id).setStyle('z-index','10');
		}
	});


	$$('.row1, .row2, .row3').addEvents({
		mouseenter: function(){ this.addClass('over'); },
		mouseleave: function(){ this.removeClass('over'); }
	});

	var markRow = $$('.m_List, .m_ListTop');
	markRow.addEvents ({
		mouseenter:function(){ this.setStyles({'background-color': '#e1edf9'}); },
		mouseleave:function(){ this.setStyles({'background-color':'transparent'}); }
	});

	var MediaBox = $$('#MediaHBox SPAN, #MediaVBox SPAN');
	MediaBox.addEvents({
		mouseenter:function(){
			var toHeight = this.getElement('.cont').getStyle('height');
			this.setProperty('class','bl_lightgreen');
			this.getElement('.inner').morph({'height': toHeight });
		},
		mouseleave:function(){
			this.setProperty('class','bl_lightgray');
			this.getElement('.inner').morph({'height':'78px'});
		}

	});


if ($('CardsInfoSlider')) {
	var CardsInfoSlider = $('CardsInfoSlider');
	var CardsInfoSlideBut = $$('#CardsInfoSlideBut');
	var CardsInfoSlide = new Fx.Slide (CardsInfoSlider, { mode:'vertical', transition:'sine:in', duration: 300 }).hide();
	CardsInfoSlideBut.addEvent('click', function(){
		CardsInfoSlider.setStyle('display','block');
		CardsInfoSlide.toggle();
		if(CardsInfoSlide.open) $$('#salon_row_ico').setStyle('margin-left','-12px')
		else $$('#salon_row_ico').setStyle('margin-left','0px')
	});
	};

	
/* работаем с меню */
var doCloseMenu = function(){ this.removeClass('over') };
$$('#topMenu DIV[id^=tm_]').addEvents({
	mouseenter:function(){
		if(this.getProperty('class') != 'act') {
			$$('#topMenu DIV[id^=tm_]').removeClass('over');
			this.addClass('over');
			clearTimeout(this.timer);
		} else {
			$$('#topMenu DIV[id^=tm_]').removeClass('over');
		}
	},
	mouseleave:function(){
		if(this.getProperty('class') != 'act') {
			this.timer = doCloseMenu.delay(1500, this);
		}
	}

});	
	
$$('#topheader .sell').addEvent('click',function(){
	var yaSellParams={carSellButton:'верхнее меню'};
	yaCounter5781676.reachGoal('SELL', yaSellParams);
	return true;
});
$$('.rightCCol .button_sale').addEvent('click',function(){
	var yaSellParams={carSellButton:'зеленая кнопка'};
	yaCounter5781676.reachGoal('SELL', yaSellParams);
	return true;
});


});

function alertLayer(what) {
	var doClearAlert = function(){ $('alertLayer').setStyle('display','none'); };
	var doCloseAlert = function(){ $('alertLayer').fade('out'); doClearAlert.delay(1000); };
	if($('alertLayer')) {
		if($('alertLayer').getStyle('display') == 'block') {
			$('alertLayer').set('html', $('alertLayer').get('html')+'<br/>'+what);
			$('alertLayer').setStyle('display','block').fade('in');
		} else {
			$('alertLayer').set('html',what);
			$('alertLayer').setStyle('display','block').fade('in');
		}
	} else {
		var elAlert= new Element('div#alertLayer', {'style':'display:none; position:fixed; top:0px; right:50px;','html':what});
		$$('body').adopt(elAlert);
		$('alertLayer').setStyle('display','block').fade('in');
	}
	doCloseAlert.delay(3000);
};
