// JavaScript Document

window.leifm = {
	init : function(){
		var _menuWidth = $('.navigation ul').width();
		$('.page-content .images-serie a').width(_menuWidth);
		$('.page-template-page-text-php .page-content').width(_menuWidth);
		$('.blog .page-content, .archive .page-content, .error404 .page-content').width(_menuWidth);
		
		$('.page-content .images-serie a').each(function(i, element){
			var _count = $(element).find('.images-thumbnail').length;
			var _rand = Math.round(Math.random()*150);
			var _newWidth = 0; //_menuWidth-_rand
			if(_count == 1){ 
				_newWidth = $(element).find('.images-thumbnail').width() 
			} else {
				for(n=0;n<2;n++){
					_newWidth = _newWidth + $(element).find('.images-thumbnail:eq('+n+')').width()+2.5;
				}
			}
			$(element).width(_newWidth);
		});
		
		$('#ajax-load-content .series-item').live('mouseleave',function(event){
			$(this).find('.series-img').stop(true,false);
			$(this).find('.series-img').fadeTo(leifm.animationTime, 1,leifm.easing);
			leifm.isHighlight = false;
		});
		$('#ajax-load-content .series .series-img').live('click', function(event){
			var _curOpacity = $(this).css('opacity');
			var _centerPosition = ($(window).width() / 2 ) - ($(this).width() / 2);
			var _leftPosition = $(this).offset().left-_centerPosition;
			
			//scrollLeft:
			$('html,body').animate({scrollLeft : _leftPosition},leifm.animationTime,leifm.easing, function(){
				
			});
			
			if(leifm.isHighlight == true && _curOpacity >= 1){
				$('#ajax-load-content .series .series-img').fadeTo(leifm.animationTime, 1,leifm.easing);
				leifm.isHighlight = false;
			} else if(leifm.isHighlight == false && _curOpacity < 1) {
				$('#ajax-load-content .series .series-img').fadeTo(leifm.animationTime, 0.2,leifm.easing);
				$(this).stop(true,false);
				$(this).fadeTo(leifm.animationTime, 1,leifm.easing);
				leifm.isHighlight = true;
			} else {
				$('#ajax-load-content .series .series-img').not(this).stop(true,false);
				$('#ajax-load-content .series .series-img').not(this).fadeTo(leifm.animationTime, 0.2,leifm.easing);
				$(this).stop(true,false);
				$(this).fadeTo(leifm.animationTime, 1,leifm.easing);
				leifm.isHighlight = true;
			}
			
		});
		/*$('#ajax-load-content .series .series-img').live('mouseenter',function(event){
			if($('#ajax-load-content .series .series-img').length > 1){
				$('#ajax-load-content .series .series-img').not(this).stop(true,false);
				$('#ajax-load-content .series .series-img').not(this).fadeTo(leifm.animationTime, 0.2,leifm.easing);
			}
			$(this).stop(true,false);
			$(this).fadeTo(leifm.animationTime, 1,leifm.easing);
		}).live('mouseleave',function(event){
			if($('#ajax-load-content .series .series-img').length > 1){
				$(this).stop(true,false);
				$(this).fadeTo(leifm.animationTime, 0.2,leifm.easing);
			}
		});*/
		
		$('.images-serie a').bind('mouseenter', function(event){
			
			$(this).find('.image-serie-title').fadeTo(leifm.animationTime, 0.8,leifm.easing);
		}).bind('mouseleave', function(event){
			$(this).find('.image-serie-title').fadeOut(leifm.animationTime, leifm.easing);
		}).click(function(event){
			event.stopPropagation();
			event.preventDefault();
			
			var _url = $(this).attr('href');
			var _title = $(this).find('.image-serie-title').text();
			
			//Set Google Tracker:
		    _gat._createTracker('UA-369970-5', _title);
			//Open Overlay:
			leifm.openOverlay(_url);
			
		});
		$('.picture-overlay-close').live('click',function(event){
			leifm.closeOverlay();
		});
		
		//img height:
		var _winHeight = $(window).height();
		var _maxImgHeight = _winHeight / 100 * 75;
		$('.series-item img').height(_maxImgHeight);
		
	},
	isHighlight : false,
	easing : 'easeInOutQuint',
	animationTime : 400,
	curScrollTop : 0,
	openOverlay : function(_url){
		var _overlayElm = $('.picture-overlay');
		var _overlayHeight = $(document).height()-60;
		_overlayElm.height(_overlayHeight);
		_overlayElm.empty();
		$('.ajax-loader').fadeIn(300);
		
		//save scroll Position:
		leifm.curScrollTop = $('html,body').scrollTop();
		//scrolltop:
		$('html,body').animate({scrollTop : 0},leifm.animationTime,leifm.easing, function(){
			$('.picture-overlay-close').fadeIn(leifm.animationTime,leifm.easing);
			$('html,body').css({'overflow-y':'hidden'});
		});
		_overlayElm.load(_url+' #ajax-load-content', function(){
			
			//Set Opacity of Title:
			$('#ajax-load-content h1.ajax-title').css({'opacity':'0.7'});
			
			
			//img height:
			var _winHeight = $(window).height();
			var _maxImgHeight = _winHeight / 100 * 60;
			$(this).find('.series-item img').height(_maxImgHeight);
			
			$('.ajax-loader').fadeOut(leifm.animationTime,leifm.easing);
			$(this).fadeIn(leifm.animationTime,leifm.easing);
			
			
			
			
		});
	},
	closeOverlay : function(){
		$('html,body').css({'overflow-y':'auto'});
		$('html,body').animate({scrollTop : leifm.curScrollTop},leifm.animationTime,leifm.easing);
		var _overlayElm = $('.picture-overlay');
		_overlayElm.fadeOut(leifm.animationTime,leifm.easing, function(){
			$(this).empty();
		});
		$('.picture-overlay-close').fadeOut(leifm.animationTime,leifm.easing);
	}
};
$(window).resize( leifm.initElements );

$(document).ready(function(){
	
	leifm.init();

});


