var infs = [];
function addMarker(pos, map, t, c, icon) {
	var opts = {position: pos, map: map, title: t};
	if(icon)
		opts.icon = icon;
	var marker = new google.maps.Marker(opts);
	if(c != '') {
		var inf = new google.maps.InfoWindow({'content': c});
		infs.push(inf);
		google.maps.event.addListener(marker, 'click', function() {
			for(var i=0; i<infs.length; i++)
				infs[i].close();
			inf.open(map,marker);
		});
	}
}

function mapInitMany(zoom,coords) {
	var clat = 0, clon = 0, i = 0, slat = 0, slon = 0;
	for(i = 0; i < coords.length; i++) {
		slat += parseFloat(coords[i][0]);
		slon += parseFloat(coords[i][1]);
	}
	clat=slat / coords.length;
	clon=slon / coords.length;
	var latlng = new google.maps.LatLng(clat, clon);
	var myOptions = {
		zoom: zoom, scrollwheel: true,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.HYBRID
	};
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
	for(i=0; i<coords.length; i++) {
		var icon = (coords[i].length == 5) ? coords[i][4] : false;
		var latlng2 = new google.maps.LatLng(coords[i][0], coords[i][1]);
		var contents = (coords[i][3] != '') ? "<div style='color:#000'><b>"+coords[i][2]+"</b><p>"+coords[i][3]+"</p></div>" : '';
		addMarker(latlng2, map, coords[i][2], contents, icon);
	}
}


$(document).ready(function() {
	/* menu icons */
	$('.menu__item__link').hover(
		function() {
			if (!$(this).is('.active')) $(this).find('i').toggleClass('active');
		},
		function() {
			if (!$(this).is('.active')) $(this).find('i').toggleClass('active');
		}
	);





	/* banners */
	var adBlock = $('#advertisement');
	if (adBlock.length) {
		adBlock.adRotate();
	}




	/* catalog */
	$('.catalog__item').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);

	$('.catalog__item').click(function() {
		document.location = $(this).find('a').eq(0).attr('href');
	});




	/* index page -> news */
	var h = 0;
	$('.short-news__item').each(function() {
		if ($(this).height() > h) {
			h = $(this).height();
		}
	});
	$('.short-news__item').height(h);
	$('.short-news__item').click(function() {
		document.location = $(this).find('a').attr('href');
	});



/*contacts*/

	$('#contactPhoto').click(function() {
		photo=$('#contactPhoto img');
		newImg = $('<img></img>');
		newImg.attr('src',photo.attr('src'));
		newImg.css('max-width','1000px');




		$('#gallery .lightbox__photo').css('width','1000');
		$('#gallery .lightbox__photo').css('height','auto');
		$('#gallery .lightbox__photo').css('max-width','none');
		$('#gallery .lightbox__photo').css('max-height','none');
		$('#gallery .lightbox__content__wrapper ul').remove();
		$('#gallery .lightbox__inner_gallery').css('width','auto');
		$('#gallery .lightbox__inner_gallery').css('margin-left','-500px');

		$('#gallery .lightbox__photo img').css('max-width','1000px');
		$('#gallery .lightbox__photo img').css('max-height','none');

		$('#gallery .lightbox__photo').html(newImg);
		$('#gallery').show();
		return false;

	});


	/* product */
	var photos = [],
		thumbs = $('.product__thumbs a'),
		photo = $('.product__image__photo'),
		index = 0;

	thumbs.each(function() {
		var image = new Image();
			image.src = $(this).attr('href');
		photos.push(image);
	});

//	photo.html(photos[0]);
	photo.click(function() {
		$('#gallery').show().gallery({'index': index});
        adjustGalleryOffset();
        $('#fullscreen_overlay').show();
	});

	thumbs.eq(index).addClass('active');

	thumbs.hover(
		function() {
			thumbs.removeClass('active');
			index = $(this).parent().index();
			photo.html(photos[index]);
		},
		function() {
			$(this).addClass('active');
		}
	);

	thumbs.click(function() {
		$('#gallery').show().gallery({'index': index});
        $('#fullscreen_overlay').show();
        
        adjustGalleryOffset();
        
		return false;
	});

	/* Pieprasit informaciju */
	var button = $('.product .button');

	button.click(function() {
		$('#form').show();
        $('#fullscreen_overlay').show();
		return false;
	});





	/* parts */
	$('.parts .link').click(function() {
		var _this = $(this);
		if (_this.is('.active')) {
			_this.removeClass('active');
			_this.closest('tr').removeClass('active');
			_this.closest('tr').next().hide();
		} else {
			_this.addClass('active');
			_this.closest('tr').addClass('active');
			_this.closest('tr').next().show();
		}

		return false;
	});





	/* miniblocks (news, partners) */
	var miniblocks = $('.miniblocks__item_news, .miniblocks__item_partners'),
		miniblocksHeight = 0;

	miniblocks.each(function() {
		if ($(this).height() > miniblocksHeight) {
			miniblocksHeight = $(this).height() + 10;
		}
	});

	miniblocks.height(miniblocksHeight);

	/* miniblocks -> show/hide */
	miniblocks.click(function() {
		if ($(this).is('.miniblocks__item_wide') || ($(this).is('.notoggle'))) return;
		$(this).find('.miniblocks__controls a').eq(1).trigger('click');
	});

	$('.miniblocks__controls a').click(function() {
        if(!$(this).parents('.miniblocks__item').hasClass('notoggle')){
            
            var item = $(this).closest('li');
            
            if (item.is('.miniblocks__item_wide')) {
                miniblocks.removeAttr('style').removeClass('miniblocks__item_wide').height(miniblocksHeight);
            } else {
                item.removeAttr('style').addClass('miniblocks__item_wide');

    /*			if (item.index() % 2 != 0) {
                    item.css('margin-top', -(miniblocksHeight + 30));
                }
    */
            }

            miniblocks.not(item).removeAttr('style').removeClass('miniblocks__item_wide').height(miniblocksHeight);

            return false;
         }
     });

	/* news -> slideshow */
	$('.miniblocks__slideshow').each(function() {
		var thumbs = $('.thumb', this),
			photos = [],
			photo = $('.miniblocks__thumb', this);

		thumbs.each(function() {
			var image = new Image();
				image.src = $(this).attr('href');
			photos.push(image);
		});

		thumbs.hover(
			function() {
				var index = $(this).parent().index();
				thumbs.removeClass('active');
				$(this).addClass('active');
				photo.html(photos[index]);
			},
			function() {}
		);

		thumbs.click(function() {
			return false
		});
	});





	/* lightbox */
	var lightbox = $('.lightbox'),
		lightboxClose = $('.lightbox__close', lightbox),
        overlay = $('#fullscreen_overlay');

	lightboxClose.bind('click', function() {
		lightbox.hide();
        overlay.hide();
		return false;
	});

	$(".lightbox").bind("click", function(e) {
	    if (this == e.target) {
	    	lightbox.hide();
            overlay.hide();
	    }
	});
    
    overlay.live('click', function(e){
        if (this == e.target) {
            lightboxClose.trigger('click');
        }
    });



	/* form */
	$('.form').find('.button').click(function() {
		$(this).closest('form').submit();
		return false;
	});





	/* tabs */
	$('.tabs__tab').click(function() {
		if ($(this).not('.active')) {
			$(this).siblings().removeClass('active').end().next('dd').andSelf().addClass('active');
		}
	});

	if (location.hash) {
		$('.tabs').find('a[href="' + location.hash + '"]').parent().trigger('click');
	}
});


function adjustGalleryOffset(){
    var calculatedHeight = ( $(window).height() - $('.lightbox__inner_gallery').height() ) / 2;
    if(calculatedHeight > 0){
        $('#gallery').css({'top' : calculatedHeight});
    }else {
        $('#gallery').css({'top' : 0});
        $(window).scrollTop(0);
    }
}


/* ext.gallery */
(function($) {
	$.fn.gallery = function(options) {
		var options = $.extend({
			'index': 0
		}, options);

		var thumbs = $('.lightbox__thumbs a', this),
			photo = $('.lightbox__photo', this),
			photos = [],
			index = options.index;

		thumbs.each(function() {
			var image = new Image();
				image.src = $(this).attr('href');
			photos.push(image);
		});

		thumbs.hover(
			function(){
				thumbs.removeClass('active');
				index = $(this).parent().index();
				photo.html(photos[index]);
			},
			function(){
				$(this).addClass('active');
			}
		);

		thumbs.click(function() {
			return false;
		});

		photo.html(photos[index]);
		thumbs.removeClass('active');
		thumbs.eq(index).addClass('active');
	}
})(jQuery);



/* ext.slideShow */
(function($) {
	$.fn.adRotate = function() {
		var self = this;

		var banners = $('.ad__banners li', this),
			pages = $('.ad__pages a', this);

		var timeout,
			i = 0;

		pages.click(function() {
			i = $(this).parent().index();
			self._rotate();
			return false;
		});

		banners.hover(
			function() {clearTimeout(timeout);},
			function() {timeout = setTimeout(self._rotate, 5000);}
		);

		this._rotate = function() {
			if (timeout) clearTimeout(timeout);

			pages.removeClass('active');
			pages.eq(i).addClass('active');

			banners.fadeOut(750);
			banners.eq(i).fadeIn(750);

			i++;
			if (i > pages.length - 1) i = 0;

			timeout = setTimeout(self._rotate, 5000);
		};

		this._rotate();
	}
    
    

})(jQuery);

