function Carousel(){
	this.items = {};
	this.previews = {};
	this.previews_wrapper = {};
	this.preview_width = 0;
	this.preview_height = 0;
	this.number = 0;
	this.visible = 5;
	this.active = 0;
	this.offset = 0;
	this.horizontal = true;
	
	this.init = function init(){
		this.previews_wrapper = $('<div class="carousel-previews-wraper" />');
		$('.carousel-previews').html(this.previews_wrapper.html($('.carousel-previews').html()));
		
		this.items = $('.carousel-items').children();
		this.previews = this.previews_wrapper.children();
		this.preview_width = 169;
		this.preview_height = 40;
		this.number = this.items.length;
		
		this.items.eq(0).show();
		
		this.previews_wrapper.css({
			position: 'relative',
			left: 0,
			top: 0
		}).parent().css({
			position: 'relative',
			overflow: 'hidden'
		});
		
		if(this.horizontal) {
			this.previews_wrapper.css({ 
				width: this.preview_width * this.number,
				height: this.preview_height
			});
		} else {
			this.previews_wrapper.css({ 
				width: this.preview_width,
				height: this.preview_height * this.number
			});
		}
	}
	
	this.change = function(index) {
		if(index != this.active) {
			newItem = this.items.eq(index);
			newButton = this.previews.eq(index);

			prevItem = this.items.eq(this.active);
			prevButton = this.previews.eq(this.active);
			
			if(this.horizontal) {
				offset = this.preview_width * (index - this.visible + 1);
				
				if(offset >= 0) {
					this.offset = offset;
					this.previews_wrapper.animate({
						left: - offset
					}, 300);
				} else if (index == 0 && this.offset > 0) {
					this.previews_wrapper.animate({
						left: 0
					}, 300);
				}
			} else {
				offset = this.preview_height * (index - this.visible + 1);
				
				if(offset >= 0) {
					this.offset = offset;
					this.previews_wrapper.animate({
						top: - offset
					}, 300);
				} else if (index == 0 && this.offset > 0) {
					this.previews_wrapper.animate({
						top: 0
					}, 300);
				}
			}

			this.previews.removeClass('act');
			newButton.addClass('act');

			this.items.removeClass('act');
			newItem.addClass('act');
			prevItem.fadeOut(200, function(){
				newItem.fadeIn(200);
			});

			this.active = index;
		}
	};
	
	this.next = function(){
		if(this.active+1 <= this.number-1) {
			index = this.active+1;
		} else {
			index = 0;
		}
		this.change(index);
	};
	
	this.prev = function(){
		if(this.active-1 >= 0) {
			index = this.active-1;
		} else {
			index = this.number-1;
		}
		this.change(index);
	};
}

$(document).ready(function() {
	
	var carousel = new Carousel();
	carousel.horizontal = false;
	carousel.init();
	
	var autochange;
	
	function autochange_start() {
		autochange = setInterval(function(){
			carousel.next();
		}, 5000);
	}
	
	function autochange_stop() {
		clearInterval(autochange);
	}
	
	autochange_start();
	
	$('.bottom-arrow').click(function(e){
		e.preventDefault();
		
		carousel.next();
		autochange_stop();
	});
	
	$('.top-arrow').click(function(e){
		e.preventDefault();
		
		carousel.prev();
		autochange_stop();
	});
	
	carousel.previews.click(function(e){
		e.preventDefault();
		
		index = carousel.previews.index($(this));
		carousel.change(index);
		autochange_stop();
	});
	
	$('.item-area').mouseenter(function(){
		autochange_stop();
	}).mouseleave(function(){
		autochange_start();
	});
	
	$(".search-area .searh-icon a").click(function(event){
		event.preventDefault();
		var term = $("form[name='search-form'] input").val();
		if(term != '' && term != 'поиск') {
			$("form[name='search-form']").submit();
		}
	});
	
	if($('.product-page').length > 0 && window.location.hash != '') {
		$('html, body').animate({ scrollTop: $("a[name='price']").offset().top }, 500);
	}
	
	function initialize(){
		var ll_map = new google.maps.LatLng(55.62206,37.62135);
		var ll_marker = new google.maps.LatLng(55.62206,37.62135);
		var options = {
			zoom: 15,
			center: ll_map,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			disableDefaultUI: true
		};
		var styles = [
		    {
				featureType: "all",
				elementType: "all",
				stylers: [
					{ saturation: -100 }
		      	]
		    }
		];

		map = new google.maps.Map(document.getElementById("google-map"), options);

		// Черно-белая карта
		var mapType = new google.maps.StyledMapType(styles, { name:"Grayscale" });    
		map.mapTypes.set('tehgrayz', mapType);
		map.setMapTypeId('tehgrayz');

		// Кастомный логотип
		var logo = new google.maps.MarkerImage('/bitrix/images/front/google-map-logo.png',
			new google.maps.Size(78,47),
			new google.maps.Point(0,0),
			new google.maps.Point(39,47)
		);

		var companyMarker = new google.maps.Marker({
			position: ll_marker,
			map: map,
			icon: logo,
			title:"Lightec"
		});
	}
	
	if($('#google-map').length) {
		initialize();
	}
	
	$(".fancybox").fancybox();
	
	
	var order_call_wrapper = $('#order-call-block');
	var order_call_windows = $('.order-call', order_call_wrapper);
	
	$('.order-block a', order_call_wrapper).click(function(event) {
		event.preventDefault();
		order_call_windows.removeClass('step-3').addClass('step-1');
	});
	
	$('.i2 a', order_call_windows).click(function(event) {
		event.preventDefault();
		order_call_windows.removeClass('step-1');
	});
	
	$('.i2 form', order_call_windows).submit(function(event) {
		event.preventDefault();
		
		var $this = $(this),
			has_errors = false,
			name = $('input[name="name"]', $this),
			phone = $('input[name="phone"]', $this);
		
		if(name.val() == '') {
			has_errors = true;
		}
		
		if(phone.val() == '') {
			has_errors = true;
		}
		
		if( ! has_errors) {
			order_call_windows.removeClass('step-1').addClass('step-2');
			
			$.ajax({
				type: 'POST',
				url: '/ajax/order_call.php',
				data: $this.serialize(),
				success: function(){
					order_call_windows.removeClass('step-2').addClass('step-3');
					
					setTimeout(function(){
						order_call_windows.removeClass('step-3');
					}, 5000);
					
					name.val('');
					phone.val('');
				}
			});
		}
	});
});
