/* Site Scripts */
$(document).ready(function(){
	
	SiteInit();
	
});

function SiteInit() {

	var pointMenu = $('ul#menu li.point');
	var portfolio = $('table#portfolio');
	var portfolioWorks = $('table#portfolio td');
	var peoples = $('div#our .people .image');
	var pretty = $("a[rel^='prettyPhoto']");
	
	$(".gal a[rel^='prettyGalery']").prettyPhoto({
		opacity: 0.3,
		show_title: false,
		theme: 'dark_rounded'
	});
	$(pretty).prettyPhoto({
		opacity: 0.3,
		show_title: false,
		theme: 'dark_rounded'
	});
	Hover(peoples);
	Hover(pointMenu);
	Hover(portfolioWorks);
	LinkBlock(pointMenu);
	PortfolioSlider(portfolio);
	
}

function PortfolioSlider(wrapper) {
	
	var works = $(wrapper).find('td');
	var stopper;
	var width = 70;     
	var hover = false;
	         
	setTimeout(function() { autoSelect(); }, 3000);
	
	$(works).each(function(index) {
		
		if (($(works).length/2).toFixed(0) - 1 == index) {
			$(this)
				.addClass('active')
				.css('width', 'auto')
				.find('.text').css('display', 'block');
		};
		
		$(this)
			.mouseover(function(){    
				hover = true;
				if ($(this).hasClass('active')) {
					
				} else {
					var openedWork = $(this);
					stopper = setTimeout(function() {
						$(wrapper).find('.active .text').css('display', 'none');
						$(openedWork)
							.addClass('active')
							.css('width', 'auto')
							.siblings('.active')
							.css('width', 'auto')
							.animate({'width': width}, 'fast', function() {
								$(this).siblings('.active').find('.text').css('display', 'block');
							})
							.removeClass('active');
					}, 100);	
				}		
			})
			.mouseout(function(){       
				hover = false;
				clearTimeout(stopper);
			});
		
	});  
	
	function autoSelect () {
		if (!hover) {
			var active = $(wrapper).find('td.active');
			if ($(active).nextAll().length >= 1) {
				var nextelem = $(active).next();
			} else {
				var nextelem = $(wrapper).find('td').first();
			}            
			$(active).find('.text').css('display', 'none');     
			$(nextelem)
				.addClass('active')
				.css('width', 'auto')
				.siblings('.active')
				.css('width', 'auto')
				.animate({'width': width}, 'fast', function() {
					$(this).siblings('.active').find('.text').css('display', 'block');
				})
				.removeClass('active');
		}
		setTimeout(function() { autoSelect(); }, 3000);
	}
	
}

function Hover(tags) {
	/*  
	 *	@author juicy.igor@gmail.com
	 *	Добавление ховер класса на элементы
	 *  над которыми находится указатель пользователя
	 */
	$(tags).each(function(){
		$(this)
			.mouseenter(function(){		$(this).addClass('hover');		})
			.mouseleave(function(){		$(this).removeClass('hover');	});
	});
}

function LinkBlock(tags) {
	/*
	 *	@author juicy.igor@gmail.com
	 *	Добавление возможности использовать href
	 *	на любых элементах
	 */
	$(tags).each(function(){
		$(this).click(function(){
			if ($(this).attr('href')) {
				window.location = $(this).attr('href');
			} else {
				window.location = $(this).find('a').attr('href');
			}
		});
	});
}

