var atmotabs = new Class({
	Implements: Options,
	options: {
		id: 'tabset'
	},

	initialize: function(options) {
		this.setOptions(options);
		var tab = $(this.options.id);
		if(typeof(tab) != 'object' ) { return; }

		var el		= [];
		var tabs	= tab.getElements('a');

		tabs.each(function(item, i) {
			var id = item.getProperty('href').replace('#', '');
			el[i] = $(id);
			if(el[i]) {
				if(!item.hasClass('active')) {
					el[i].setStyle('display', 'none');
				}
				item.addEvent('click', function(e) {
					e.stop();
					tabs.removeClass('active');
					el.each(function(item){
						item.setStyle('display', 'none');
					});

					el[i].setStyle('display', 'block');
					tabs[i].addClass('active');
				});
			}
		});
	}
});

window.addEvent('domready', function() {
	var DBody = $(document.documentElement).setProperty('id', 'js');


	var m3boxSlide = $('m3box');
	if($type(m3boxSlide)) {
		var aS1 = new atmoSlide({
			box: m3boxSlide,
			items: m3boxSlide.getChildren(),
			buttons: {
				previous: $('m3-prev'),
				next: $('m3-next')
			},
			step: 1,
			itemSize: 669,
			handles: $$('#m3handles span')
		});
	}

	if($type($('tabset'))) {
		var aTabs = new atmotabs();
	}
	if($type($('tabset1'))) {
		var aTabs1 = new atmotabs({id:'tabset1'});
	}

	if($type($('accordion'))) {
		var activeItem = -1;
		var togglers = $('accordion').getElements('li.toggler');

		togglers.each(function(el, i) {
			if(el.getFirst().hasClass('active')) {
				activeItem = i;
			}
		});

		var accordion = new Fx.Accordion($('accordion'), 'a.toggler', 'div.level1', {
			opacity: false,
			display: activeItem,
			onActive: function(toggler, element){
				toggler.addClass('active');
				element.setStyle('overflow', 'hidden');
			},
			onBackground: function(toggler, element){
				toggler.removeClass('active');
				element.setStyle('overflow', 'hidden');
			}
		});
	}

	var showCars = $('showcars');

	if($type(showCars)) {
		var showCarsItems = $('showcars').getElements('a');
		var carType = {
			't0': 'auto',
			't1': 'commercial',
			'all': 'all'
		};

		var vkScroll = $('vk-scroller');
		var vkScrollItems = vkScroll.getElements('a');

		var scrollCars = $('vk-scroller');
		if($type(scrollCars)) {
			var aS1 = new atmoSlide({
				box: scrollCars,
				items: scrollCars.getChildren(),
				buttons: {
					previous: $('vk-left'),
					next: $('vk-right')
				},
				step: 3,
				itemSize: 197
			});
		}

		showCarsItems.each(function(el, i) {
			el.addEvent('click', function(e) {
				e.stop();
				var thisCarType = this.get('href').replace("#", "");
				showCarsItems.removeClass('active');
				this.addClass('active');

				vkScrollItems.each(function(el, i) {
					if(thisCarType == carType.t0) {
						if(el.hasClass(carType.t1)) {
							el.getParent().setStyle('display', 'none');
						}
						if(el.hasClass(carType.t0)) {
							el.getParent().setStyle('display', 'block');
						}
					}

					if(thisCarType == carType.t1) {
						if(el.hasClass(carType.t0)) {
							el.getParent().setStyle('display', 'none');
						}
						if(el.hasClass(carType.t1)) {
							el.getParent().setStyle('display', 'block');
						}
					}

					if(thisCarType == carType.all) {
						el.getParent().setStyle('display', 'block');
					}
				});

				aS1.setNewWidth();
			})
		});

		/*open6
		open6-box*/
		var open6Box = $('open6-box');

		if($type(open6Box)) {
			$('open6').addEvent('click', function(e) {
				e.stop();

				if(open6Box.hasClass('active')) {
					open6Box.removeClass('active');
					open6Box.tween('margin-top', [0, -171]);
					this.removeClass('active');
				} else {
					open6Box.addClass('active');
					open6Box.tween('margin-top', [-171, 0]);
					this.addClass('active');
				}

			});
		}

		var aS = new atmoSlide({
			box: $('box'),
			items: $$('#box li'),
			buttons: {
				previous: $('prev4'),
				next: $('next4')
			},
			step: 1,
			itemSize: 703,
			play: true,
			interval: $vars.interval
		});
	}

	var vkScroll1 = $('vk-scroller1');

	if($type(vkScroll1)) {
		var aS1 = new atmoSlide({
			box: vkScroll1,
			items: vkScroll1.getChildren(),
			buttons: {
				previous: $('vk-left1'),
				next: $('vk-right1')
			},
			step: 4,
			itemSize: 103,
			preview: true,
			previewId: $('carpreview')
		});
	}

	var box4 = $('box4');
	var box4close = $('box4close');
	var ajaxloader1 = $('ajaxloader');

	$vars.forms.f1.each(function(item, i) {
		item.addEvent('click', function(e) {
			e.stop();
			box4.addClass('box4e');

			var url = this.getProperty('href');

			testDriveFormHelper.request.setOptions({url: url});
			// Send Request
			testDriveFormHelper.request.get({'ajax': '1'});
		});
	});

	$vars.forms.f2.each(function(item, i) {
		item.addEvent('click', function(e) {
			e.stop();
			box4.removeClass('box4e');

			var url = this.getProperty('href');

			testDriveFormHelper.request.setOptions({url: url});
			// Send Request
			testDriveFormHelper.request.get({'ajax': '1'});
		});
	});

	$vars.forms.f3.each(function(item, i) {
		item.addEvent('click', function(e) {
			e.stop();
			box4.addClass('box4e');

			var url = this.getProperty('href');

			testDriveFormHelper.request.setOptions({url: url});
			// Send Request
			testDriveFormHelper.request.get({'ajax': '1'});
		});
	});

	var testDriveFormHelper = {
		href: null,
		request: null,
		request1: null,
		ajax: null,
		ajaxLoader: function(val) {
			ajaxloader1.setStyle('display', val);
			testDriveFormHelper.setPosition();
		},

		hide: function() {
			box4.setStyle('top', -9999);
			$('box4html').empty();
			$('box4html').setStyle('display', 'none');
			$('ajaxloader').setStyle('display', 'block');
			$('overlay').setStyle('display', 'none');
			box4close.setStyle('display', 'none');
		},

		initForm: function(responseHTML) {
			$('box4html').setStyle('display', 'block');
			$('box4html').set('html', responseHTML);

			var form5 = $('form5');
			var form5Lbl = form5.getElements('label.fm5l');

			form5Lbl.each(function(item, i){
				var inputText = $(item.getProperty('for'));
				item.addEvent('click', function(){
					inputText.focus();
					item.setStyle('display', 'none');
				});
				inputText.addEvents({
					'blur': function(){
						if(this.value == ''){
							item.setStyle('display', 'block');
						}
					},
					'focus': function(){
						if(this.value == ''){
							item.setStyle('display', 'none');
						}
					},
					'change': function(){

					}
				});
			});

			form5.addEvent('submit', function(e){
				this.set('send', {
					url: 'index.php',
					method: 'post',
					onSuccess: function(responseText, responseXML){
						var data = JSON.decode(responseText);

						if(!data.error) {
							alert(data.msg);
						} else {
							alert(data.msg);
							testDriveFormHelper.hide();
						}
					}
				});
				this.send();

				e.stop();
			});

			var scodetd = $('ctestdrive').getProperty('src');
			$('ctestdrive').addEvent('click', function(){
				this.setProperty('src', scodetd + '&' + Math.random());
			});

			testDriveFormHelper.setCalendar();
			testDriveFormHelper.setPosition1();
		},

		overlay: function() {
			$('overlay').setStyles({
				'display': 'block',
				'height': $(window).getScrollSize().y + 'px',
				'opacity': 0.3
			});
		},

		setCalendar: function() {
			if($type($('f-btn1'))) {
				var jCal = Calendar.setup({
					inputField   : "flbl4",		// id of the input field
					ifFormat     : "%d.%m.%Y",	// format of the input field
					button       : "f-btn1",	// trigger for the calendar (button ID)
					align        : "Tl",		// alignment (defaults to "Bl")
					singleClick  : true,
					onSelect: function(cal) {
						var p = cal.params;
						var update = (cal.dateClicked || p.electric);
						if (update && p.inputField) {
							p.inputField.value = cal.date.print(p.ifFormat);
							if (typeof p.inputField.onchange == "function") p.inputField.onchange();
						}
						if (update && p.displayArea) p.displayArea.innerHTML = cal.date.print(p.daFormat);
						if (update && typeof p.onUpdate == "function") p.onUpdate(cal);
						if (update && p.flat) {
							if (typeof p.flatCallback == "function") p.flatCallback(cal);
						}
						if (update && p.singleClick && cal.dateClicked) {
							cal.callCloseHandler();
							$('flbl4lbl').setStyle('display', 'none');
						}
					}
				});
			}
		},

		setPosition: function() {
			box4.setStyles({'top':  $(window).getScroll().y + $(window).getSize().y.toInt()/2 - box4.getStyle('height').toInt()/2});
		},

		setPosition1: function() {
			var bodySize = $(document.body).getSize();
			var boxSetPos = (bodySize.y < box4.getStyle('height').toInt()) ? $(window).getScroll().y+50 : $(window).getScroll().y + $(window).getSize().y.toInt()/2 - box4.getStyle('height').toInt()/2;

			box4.setStyles({'top': boxSetPos});
		}
	};

	box4close.addEvent('click', testDriveFormHelper.hide);

	testDriveFormHelper.request = new Request.HTML({
		method: 'post',
		url: null,
		onRequest: function() {
			testDriveFormHelper.ajax = true;
			testDriveFormHelper.ajaxLoader('block');
			testDriveFormHelper.overlay();
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			testDriveFormHelper.ajax = false;
			testDriveFormHelper.ajaxLoader('none');
			box4close.setStyle('display', 'block');
			testDriveFormHelper.initForm(responseHTML);
		},
		onCancel: function() {
			testDriveFormHelper.hide();
		},
		onFailure: function(xhr) {
			testDriveFormHelper.hide();
		}
	});

	var ymapsid44 = $('ymapsid-3334');
	if($type(ymapsid44)){
		initYaMap($yaMapParams);

		if($type(ymapsid44)){
			$$('.showyandexmap').each(function(item, i){
				item.addEvent('click', function(e){
					e.stop();
					var elPos = this.getCoordinates($('main5'));

					if(i==0) {
						ymapsid44.removeClass('ymshow1');

						if(ymapsid44.hasClass('ymshow0')){
							ymapsid44.removeClass('ymshow0');
							ymapsid44.setStyle('top', -9999);
						} else {
							ymapsid44.addClass('ymshow0');
							ymapsid44.setStyles({
								'left': elPos.left-400,
								'top': elPos.top+50
							});
						}
					} else {
						ymapsid44.removeClass('ymshow0');

						if(ymapsid44.hasClass('ymshow1')){
							ymapsid44.removeClass('ymshow1');
							ymapsid44.setStyle('top', -9999);
						} else {
							ymapsid44.addClass('ymshow1');
							ymapsid44.setStyles({
								'left': elPos.left-240,
								'top': elPos.top-290
							});
						}
					}

				});
			});
		}
	}
});

function initYaMap(params) {
	var map = new YMaps.Map(YMaps.jQuery(params.id)[0]);
	map.setCenter(new YMaps.GeoPoint(params.geoPoint.x,params.geoPoint.y), 11, YMaps.MapType.MAP);

	// ���������� ��������� ����������.
	map.addControl(new YMaps.Zoom());
	map.addControl(new YMaps.ToolBar());
	map.addControl(new YMaps.TypeControl());
	//map.addControl(new YMaps.ScaleLine());

	YMaps.Styles.add("constructor#pmwtlPlacemark", {
		iconStyle : {
			href : $vars.livesite + "images/b-location-balloon.mark.png",
			size : new YMaps.Point(121,96),
			offset: new YMaps.Point(params.icon.offset.x,params.icon.offset.y)
		}
	});

	map.addOverlay(createObject("Placemark", new YMaps.GeoPoint(params.geoPoint.x,params.geoPoint.y), "constructor#pmwtlPlacemark", params.text));

	function createObject (type, point, style, description) {
		var allowObjects = ["Placemark", "Polyline", "Polygon"],
			index = YMaps.jQuery.inArray( type, allowObjects),
			constructor = allowObjects[(index == -1) ? 0 : index];
			description = description || "";

		var object = new YMaps[constructor](point, {style: style, hasBalloon : !!description});
		object.description = description;

		return object;
	}
}



// ie fix
function fixTrans() {
	var transparentImage = "images/transparent.gif";

	if (typeof document.body.style.maxHeight == 'undefined'){
	var imgs = document.getElementsByTagName("img");
	for (i = 0; i < imgs.length; i++){
		if (imgs[i].src.indexOf(transparentImage) != -1){
			return;
		}
		if (imgs[i].src.indexOf(".png") != -1){
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
				imgs[i].style.display = "inline-block";
			}
		}
	}

	var nav = document.getElementById("nav5");
	if (nav){
		var nodes = nav.getElementsByTagName("li");
		for (var i = 0; i < nodes.length; i++){
			nodes[i].onmouseover = function (){
				if (this.className.indexOf("hover") == -1){ this.className += " hover"; }
			};
			nodes[i].onmouseout = function (){ this.className = this.className.replace(" hover", ""); };
		}
	}
}
if (document.all && !window.opera) attachEvent("onload", fixTrans);
