$(document).ready(function() {
	var index = $('#indexBlock');

	if (index.length > 0) {
		$('#action_lightbox').delay(1000).fadeIn();
	}





	/* news */
	var news = $('#newsBlock');

	if (news.length > 0) {
		$('.news__link').hover(
			function() {
				$(this).closest('li').find('.news__link').addClass('hover');
			},
			function() {
				$(this).closest('li').find('.news__link').removeClass('hover');
			}
		);
	}





	/* gallery */
	var gallery = $('#galleryBlock').not('#schemesBlock, #schemesBlock > *');

	if (gallery.length > 0) {
		gallery.gallery();
	}

	var schemes = $('#schemesBlock');

	if (schemes.length > 0) {
		schemes.gallery();
	}

/*
	var wysiwyg_images = $('.wysiwyg')
	if (wysiwyg_images.length > 0) {
		wysiwyg_images.gallery();
	}
*/
	var news = $('#newsBlock')
	if (news.length > 0) {
		news.viewer();
	}
	var news = $('#mainItem')
	if (news.length > 0) {
		news.viewer();
	}

	$('#the_action').click(function() {
			$('#action_lightbox').delay(1000).fadeIn();
			return false;
	});
	/* contacts */
	var contacts = $('#contactsBlock');

	if (contacts.length > 0) {
		$('.contacts__map').click(function() {
			var lat = $(this).attr('data-latitude'),
				lng = $(this).attr('data-longitude');

			$('#lightbox').trigger('show');
			$('.lightbox__text').html('<b>' + $(this).attr('title') + '</b>');

			var mapLatLng = new google.maps.LatLng(lat, lng);
			var mapOptions = {
				zoom: 15,
				center: mapLatLng,
				mapTypeControl: false,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}

			var map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);

			var marker = new google.maps.Marker({
				position: mapLatLng,
				map: map
			});

			return false;
		});
	}





	/* contacts -> form -> tabs */
	var priceform = $('#priceformBlock');

	if (priceform.length > 0) {
		/* Проверялка форм */
		$('form').each(function() {
			new formChecker($(this));
		});

		/* Переключалка табов */
		var tabs = $('.priceform__tab')
			tabs.click(function() {
				if ($(this).not('.active')) {
					$(this).siblings()
							.removeClass('active')
							.end()
							.next('dd')
							.andSelf()
							.addClass('active');
				}
			});

		/* Переключалка табов по УРЛ'у */
		var hash = location.hash;
			hash = (hash) ? hash : '#plastic';

		$(hash + '-tab').trigger('click');

		/* Чекбокс с услугой "Транспорт" - когда кликаем, появляется поле с адресом. */
		$('#p-service-3').click(function() {
			$('#p-address-row').toggle();
		});
		$('#w-service-3').click(function() {
			$('#w-address-row').toggle();
		});
	}





	/* products */
	var products = $('#productsBlock');

	if (products.length > 0) {
		$('.products__menu__item td').hover(
			function() { $(this).closest('li').find('.products__submenu').show(); },
			function() { $(this).closest('li').find('.products__submenu').hide(); }
		);

		$('.products__submenu').hover(
			function() { $(this).show(); },
			function() { $(this).hide(); }
		);
	}





	/* lightbox */
	var lightbox = $('#lightbox');

	if (lightbox.length > 0) {
		/* show */
		lightbox.bind('show', function() {
			$(this).show();
			$('.lightbox__window').css('top', 105 + $(window).scrollTop());
		});

		/* close */
		$('.lightbox__close').click(function() {
			lightbox.hide();
			return false;
		});
	}
		/* lightbox */
	var action_lightbox = $('#action_lightbox');

	if (action_lightbox.length > 0) {
		/* show */
		action_lightbox.bind('show', function() {
			$(this).show();
			$('.lightbox__window').css('top', 105 + $(window).scrollTop());
		});

		/* close */
		$('.lightbox__close').click(function() {
			action_lightbox.hide();
			return false;
		});
	}
});





/* ext.Gallery */
(function($) {
	$.fn.gallery = function() {
		var lightbox = $('#lightbox'),
			lightboxInner = $('.lightbox__window', lightbox),
			lightboxPhoto = $('.lightbox__content__inner', lightbox),
			lightboxText = $('.lightbox__text', lightbox),
			lightboxArrows = $('.lightbox__arrow', lightbox);

		return this.each(function() {
			var thumbs = $('.thumb', this),
				descriptions = $('.description', this),
				photos = [],
				i = 0;

			thumbs.each(function() {
				var image = new Image();

				image.src = $(this).attr('href');
				if ($(this).attr('prod:num') !== undefined) {
					photos[$(this).attr('prod:num')] = image;
				}else {
					photos.push(image);
				}
			});



			thumbs.click(function() {
				if ($(this).attr('prod:num') !== undefined) {
					i=$(this).attr('prod:num');
				}else{
					i = $(this).parent().index();
				}

				lightbox.show();
				lightboxPhoto.html(photos[i]);
				lightboxText.html(descriptions.eq(i).html());

				var image = lightboxPhoto.find('img');

				lightboxInner.width(image.width() + 20*2 + 4*2);
				if ($(this).attr('kind') !== undefined) {
					lightboxInner.css({
						'margin-left': -lightboxInner.width() / 2
						, 'top':105});
				}else {
					lightboxInner.css({
						'margin-left': -lightboxInner.width() / 2
						, 'top':$(window).scrollTop()+105});
				}
				lightboxInner.width('auto');


				return false;
			});

			lightboxArrows.click(function() {
				if ($(this).is('.lightbox__arrow_right')) {
					i++;
				} else {
					i--;
				}

				if (i > photos.length - 1) {
					i = 0;
				}

				thumbs.eq(i).trigger('click');

				return false;
			});
		});
	}

	$.fn.viewer = function() {
		var lightbox = $('#lightbox'),
			lightboxInner = $('.lightbox__window', lightbox),
			lightboxBody = $('.lightbox__content__inner', lightbox),
			lightboxText = $('.lightbox__text', lightbox),
			lightboxArrows = $('.lightbox__arrow', lightbox);

		return this.each(function() {
			var thumbs = $('.news__link', this),
				descriptions = $('.description', this),
				photos = [],
				i = 0;

			thumbs.click(function() {
				i = $(this).parent().index();

				lightbox.show();
//				alert(photos[i]);
				lightboxBody.html(descriptions.eq(i).html());

//				var image = lightboxPhoto.find('img');

				lightboxInner.width('800px');

				lightboxInner.css({
					'margin-left': -400
				});

				return false;
			});

			lightboxArrows.click(function() {
				if ($(this).is('.lightbox__arrow_right')) {
					i++;
				} else {
					i--;
				}

				if (i > descriptions.length - 1) {
					i = 0;
				}
				if (i < 0) {
					i = descriptions.length - 1;
				}
				thumbs.eq(i).trigger('click');

				return false;
			});
		});

	}
})(jQuery);
