$(document).ready(function() {

	$('.project img').lazyload({ 
		threshold : 200,
		placeholder : "/static/images/loading.gif",
	    effect : "fadeIn" 
	});
	
	//nastaví základní pozici čárky u náhledů
	$('.registration-mark').each(function(e){
		var left = $(this).parent().find('.default-image').position().left;
		var top = $(this).parent().find('.default-image').position().top;
		var margin = $(this).parent().find('.default-image').css('margin-left');
		if($(this).parent().hasClass('horizontal')){
			$(this).css({"left" : left + "px", 'margin-left' : margin});
		}else{
			$(this).css("top", top + "px");
		}
			
	})

	// skrývá obrázek, zobrazuje text
	$(".toggle-show").hover(function(){
		if($(this).parent().hasClass('active')){
			// if($(this).parent().hasClass('default')){
				$(this).find('.face').hide();
				$(this).find('.reverse').show();
			// }else{
			// 	$(this).find('.reverse').hide();
			// 	$(this).find('.face').show();
			// }
		}
	},function(){
		if($(this).parent().hasClass('active')){
			$(this).find('.reverse').hide();
			$(this).find('.face').show();
		}
	});
	
	$('.default-image').click(function(){
		$(this).parent().find('a:first').trigger('mouseover');
	})
	
	// $('.default').click(function(){
	// 	if($(this).hasClass('default') && $(this).hasClass('active')){
	// 		$(this).find('.gallery').find('a:first').trigger('mouseover');
	// 		$(this).find('.face').show();
	// 		$(this).find('.reverse').hide();
	// 		$(this).removeClass('default');
	// 	}
	// })
	
	// náhledy
	$('.gallery a').mouseover(function(){
		
		var parent = $(this).parent().parent();
		
		if(parent.hasClass('active')==false){				
		
			if($(this).hasClass('default-image')){				
				parent.find('.face').hide();
				parent.find('.reverse').show();
				parent.addClass('default');

			}else{
				parent.find('.reverse').hide();
				parent.find('.face').show();
				parent.removeClass('default')
			}
			
		}else{
			
			if($(this).hasClass('default-image')){
				parent.addClass('default');
			}else{
				parent.removeClass('default');
			}
		}

		// zobrazí loader
		$(this).parent().parent().find('.loader').show();
		$(this).parent().parent().show();
		
		var left = $(this).position().left;
		var top = $(this).position().top;
		
		// posun čárky u náhledů
		if($(this).parent().hasClass('horizontal')){
			var el = $(this).parent().find('.registration-mark')
			var margin = $(this).css('margin-left');
			el.css({"left" : left + "px", 'margin-left' : margin, "border-color" : $(this).css('background-color') });
		}else{
			var el = $(this).parent().find('.registration-mark');
			var margin = $(this).css('margin-top');
			el.css({"top" : top + "px", 'margin-top' : margin, "border-color" : $(this).css('background-color') });
		}
		
		// změna popisku náhledu
		$(this).parent().parent().find('.meta').text($(this).attr('title'));
		
		var img = new Image();
		var src = $(this).attr('href');
		var elem = $(this).parent().parent();
				
		$(img)
			.load(function(){
				// výměna obrázku
				$(elem).find('.image-vertical, .image-horizontal').remove();
		    	$(this).appendTo($(elem).find('a.main-image'));
				
				// skryje loader
				setTimeout(function(){
					$(elem).find('.loader').css('display','none');
				}, 250)
			})
			.attr('class',$(elem).find('.image-vertical, .image-horizontal').attr('class'))
			.attr('src', src)
					
	})
	
	// zvětšení / zmenšení náhledu projektu a přescrolování na jeho pozici
	$('.project').toggle(function(){
		var elem = $(this);
		// if($(this).hasClass('default')==false){
		// 	$(this).find('.toggle-show').trigger('mouseout');
		// }
		$(this).attr('myWidth',elem.width());
		$(this).attr('myHeight',elem.height());
		
		if($(this).hasClass('active')){
			if($(this).hasClass('default')){
				$(this).find('.gallery').find('a:first').trigger('mouseover');
				$(this).find('.face').show();
				$(this).find('.reverse').hide();
				$(this).removeClass('default');
			}else{
				$(this).find('.face').show();
				$(this).find('.reverse').hide();
			}
			
		}
		
		elem.removeClass('active');
		
		if($(this).find('.gallery').length > 0){
			var windowHeight = ($(window).height() > 600) ? 650 : $(window).height() - 50;
			$(this).animate({'width':'650px', 'height': windowHeight + 'px'},1300, function(){
				$('body').scrollTo(elem.find('.anchor'), {
					duration: 600,
					easing: 'myEasing',
					axis: 'y',
					onAfter: function(){
						location.hash = elem.find('.anchor').attr('id');
					}
			
				});
		
			});
							
		}else{
			// u projektů bez náhledů pouze scroluje
			$('body').scrollTo(elem.find('.anchor'), {
				duration: 1200,
				easing: 'myEasing',
				axis: 'y',
				onAfter: function(){
					location.hash = elem.find('.anchor').attr('id');
				}
			
			});
			
		}

	},function(){
		// zmenšení projektu na původní velikost
		var elem = $(this);
		var width = $(this).attr('myWidth');
		var height = $(this).attr('myHeight');
		
		$(this).addClass('active');
		
		if($(this).find('.gallery').length > 0){
			$(this).animate({'width':width+'px', 'height':height+'px'},1000, function(){
				$('body').scrollTo(elem.find('.anchor'), {
					duration: 1200,
					easing: 'myEasing',
					axis: 'y',
					onAfter: function(){
						location.hash = elem.find('.anchor').attr('id');
					}
				
				});
			
			});
			
			$(this).addClass('default');
	
		}else{
			
			$('body').scrollTo(elem.find('.anchor'), {
				duration: 1200,
				easing: 'myEasing',
				axis: 'y',
				onAfter: function(){
					location.hash = elem.find('.anchor').attr('id');
				}
			
			});
			
		}
		
	})
	
});










