

$(function() {	
	// $(".article img").lazyload({
	// 	placeholder : "/files/images/loading.gif",
	//         // event : "lazyloadimage"
	//     });
	
	// $('.article img').lazyload({ 
	// 	threshold : 200,
	// 	// placeholder : "/files/images/loading.gif",
	//     effect : "fadeIn",
	// 	event:"sporty"
	// });
	
	setupHTML();
	$('#menu, #container').hide();
	
	// resizeFont();
	clickOnArticle();
	bubble();
	
	program();
	gallery();	
	vote();
	
	$(window).bind('resize', function(){
		resizeFont();
	})
	.trigger('resize')
	.bind('load', function(){
		// var timeout = setTimeout(function() {$("img").trigger("lazyloadimage")}, 2000);
	    
		resizeFont();
		$('#menu, #container').show();
		
		ctyrkaSize();
		
		
		$('#fixer').css('top',$('#menu').css('top'));
		
		zoomOrganizer();
		$('#articles').masonry({
			resize:false,
			animate:true,
			isResizable:false,
			columnWidth:101,
			itemSelector: '.article:visible, #partners',
			gutterWidth:0
		})
		filter();
		
		// var timeout = setTimeout(function() {$("img").trigger("sporty")}, 5000);
	})
	.bind('scroll', function(){
		fixedMenu();
		$('#bubble').fadeOut(100);
	})
});

function vote(){
	$('body').append('<div id="popup"></div>');
	// $('.vote').live('click', function(){
	// 	popup = $('#popup');
	// 	popup.css({
	// 		'left': $(window).width() / 2 - popup.width() / 2,
	// 		'top': $(window).height() / 2 - popup.outerHeight() / 2
	// 	}).fadeIn(10).delay(2000).fadeOut(500);
	// 	return false;
	// })
	
}

function bubble(){
	$('body').append('<div id="bubble"><div class="content"><div class="pad"></div><div id="arrow"></div></div></div>');
	var bubble = $('#bubble');
	var timer = false;
	var hideThis;
	
	bubble.hover(function(){
		timer = true;
		clearTimeout(hideThis);
	}, function(){
		timer = false;
		hideThis = setTimeout(function(){
			if(timer == false){
				bubble.fadeOut(100);
				console.log(timer)
			}
		}, 500)
	})
	
	$('.bubble').parents('.article').map(function(){
		var meta = $(this).find('.bubble')
		meta.hide();
		
		$(this).hover(function(){
			timer = true;
			clearTimeout(hideThis);
			bubble.show().animate({
				// left:$(this).offset().left + $(this).width() - 25,
				top:$(this).offset().top - bubble.height() + 5
			}, 150).find('.pad').html(meta.html());
		}, function(){
			timer = false;
			hideThis = setTimeout(function(){
				if(timer == false){
					bubble.fadeOut(100);
				}
			}, 500)

		}).mousemove(function(e){
			if(e.clientX - 5 + bubble.width() < $(window).width()){
				bubble.css({
					'left': e.clientX - 5
				}).find('#arrow').css({
						left: 10
					})
			}else{
				bubble.css({
					'left': $(window).width() - bubble.width()
				}).find('#arrow').css({
					left: e.clientX - bubble.offset().left + 10
				})
			}
		})
	})
}

function resizeBoxes(){	
	var timeout = setTimeout(function(){
		$('.news').map(function(){
			var pad = $(this).find('.pad');
			$(this).css({
				'height':(Math.ceil( (pad.height() + 30) / 100) * 100) + (Math.floor(pad.height() / 100) - 1)
			})
			if(document.location.hash != '#program'){
				$('#articles').masonry( 'reload' )
			}
			}, 300)
		})
}
function setupHTML(){
	// ie7 inline-block
	if (jQuery.browser.msie && jQuery.browser.version < 8) {
		$('div, span, a, ul, li, p').filter(function(){
			return $(this).css('display') == 'inline-block';
		}).css({display:'inline', zoom:1});
	}
	
	$('body').append('<div id="fixer"></div>');
	resizeBoxes();
}

function fixedMenu(){
	if($('#fixer').position().top <= $(window).scrollTop()){
		$('#menu').addClass('fixed');
	}else{
		$('#menu').removeClass('fixed');
	}
}

function resizeFont(){
		var preferredWidth = 1280, fontsize = 18, fontSizeLimit = 12; 
		var displayWidth = $(window).width();
		var percentage = displayWidth / preferredWidth;
		var newFontSize = Math.floor(fontsize * percentage) - 1;
		var oldFontSize;
		
		var space = newFontSize * 1.2;
		var top;
		
		
		// top = $('img#logo').height() + space;
		// $('#claim').css({
		// 	'top':top//,
		// 	// 'font-size':newFontSize * 3.5
		// })
		// .find('p').css({
		// 	'font-size':newFontSize
		// })
		
		$('#claim p').css('width',$('#logo').width())
		
		top = $('#head').outerHeight();
		$("#menu, #fixer").css({
			'top': top
		})
				
		if(newFontSize >= fontSizeLimit){
			$("body").css({
				"font-size": newFontSize
			});
			
			ctyrkaSize();


			resizeBoxes();
			
		}
			
		top += $('#menu').outerHeight() - 10// + space;
		$('#container').css({
			'top' :  top
		})
		
		// $('#head').css('height', $('#fixer').position().top);
		
		
		if(newFontSize != oldFontSize){
			$('#articles li, *').stop();
			if(document.location.hash != '#program'){
				$('#articles').masonry( 'reload' );
			}else{
				$('.article').stop();
				$('#articles').masonry( 'destroy' );
				showTimeline();
			}
			oldFontSize = newFontSize;
		}
}

function ctyrkaSize(){
	$('#black').css({
		'left': $('.menu-category:first').offset().left - 20,
		'width': $('#filter_press').offset().left - $('.menu-category:first').offset().left
	})
	$('#ctyrka').css({
		'left': $('.menu-category:first').offset().left - 130,
		'width': $('#filter_press').offset().left - $('.menu-category:first').offset().left + 210
	})
}

function filter(){
	$('#menu a.filter').click(function(){
		$('#ajax-content').hide();
		if($(this).is('#filter_all')){
			$(this).addClass('active').parent().siblings('li').children().removeClass('active');
			hideTimeline();
			$('ul#articles li').show();
			document.location.hash = '';
			resizeBoxes();
			
			$('#articles').masonry( 'reload' );
			if(page == 'home') {
				return false;
			}else{
				return true;
			}
		}
		
		if($(this).hasClass('active')){
			$(this).removeClass('active');
			hideTimeline();
			$('ul#articles li').show();
			document.location.hash = '';
			resizeBoxes();
			
			$('#articles').masonry( 'reload' );
			return false;
		}else{
			$(this).addClass('active').parent().siblings('li').children().removeClass('active');
			
			if($(this).parent().is('#filter_program')){
				$('ul#articles li').hide();
				$('#articles').css('height',$('#timeline').height() + $('#timeline').offset().top);
				
				showTimeline();
				
				

			}else{
				hideTimeline()
				$('ul#articles li').hide();
				$('.filter_'+$(this).attr('href').replace('/#', '')).show();
				$('#articles').masonry( 'reload', function(){
					$(window).trigger('scroll')
				});
			}
		}
	})
	
	var hash = document.location.hash.replace('#', '');
	if(document.location.hash){
		if($('li#filter_' + hash + ' a').length > 0){
			$('li#filter_' + hash + ' a').trigger('click');
		}else{
			$('li#' + hash).trigger('click')
		}
	}
}

function showTimeline(){
	//$('#timeline, #locations').css('opacity',100)
	
	// $('#timeline').animate({
	// 	'width':'100%',
	// 	'left':'0'
	// })
	// 
	// $('#locations').animate({
	// 	'height':'100%',
	// 	'top':'10px'
	// })
	$('#timeline, #locations').show()
	
	$('.filter_events').map(function(){
		var offset = ($(this).data('top')) ? $(this).data('top') : 0;
		$(this).show()
		.css({
			'position':'absolute'
		})
		.animate({
			'top': $('#'+ $(this).data('time')).position().top + offset + $('#timeline').position().top + parseInt($('#timeline').css('margin-top')) * 2 - 2,
			'left':$('#'+ $(this).data('location')).position().left + $('#locations').position().left
		})
	})
	// $('#'+ $('#vystava-vytvarniku').data('time')).css('border','1px solid red')
	
	if($('#vystava-vytvarniku').length > 0){
		$('#vystava-vytvarniku').animate({
			'height' : $('#'+ $('#vystava-vytvarniku').data('end-time')).position().top - $('#'+ $('#vystava-vytvarniku').data('time')).position().top
		})
	}
}
function hideTimeline(){
	// $('#timeline').animate({
	// 	'width':'1px',
	// 	'left':'-150px'
	// }, function(){
	// 	//$('#timeline').css('opacity',0)
	// })
	// 
	// $('#locations').animate({
	// 	'height':'1px',
	// 	'top':'-100px'
	// }, function(){
	// 	//$('#timeline').css('opacity',0)
	// })
	$('#vystava-vytvarniku').animate({
		'height' : 100
	})
	$('#timeline, #locations').hide()
}

function clickOnArticle(){
	$('li.article').each(function(){
		var image = $(this).find('img'),
			badImage = image.data('src'),
			original = (image.data('orig')) ? image.data('orig') : badImage;
		$(this).click(function(){
			var link = $(this).find('a.link_to_article')
			if(link.length > 0){
				var head = $('#container');
				
				$('#loader').show();
				
				$('body').scrollTo(head, {
					axis: 'y',
					offset: -$('#menu').height(),
					duration: 600,
					margin:false,
					onAfter: function(){


					}
				})
				
				// document.location = link.attr('href');
				$('.article').animate({
					'left': '-1000px',
					'top':0
				})
				
				$('#ajax-content').load('/ajax'+link.attr('href'), function(response, status, xhr) {
					  if (status == "error") {
					    var msg = "Je nám líto ale nastala následující chyba: ";
					    $("#ajax-content").html(msg + xhr.status + " " + xhr.statusText);
					  }
					hideTimeline();
					$('#loader').hide();
					$('#menu .active').removeClass('active');
					$(this).show();
				})
			 	document.location.hash = '#'+link.parents('.article').attr('id')
				return false;
			}else{
				return true
			}
		}).hover(function(){
			$(image).attr('src', original);
		}, function(){
			$(image).attr('src', badImage);
		})
	})

}


function gallery(){
	$('#thumbnails a').live('mouseover',function(){
		$('#main-thumbnail').attr('src', $(this).attr('href'))
	})
}



// EFFECTS
function zoomOrganizer(){
	$('.filter_organizer').hover(function(){
		$(this).find('img').stop().animate({
			'width':600,
			'height':600,
			'left': -200,
			'top': -200
		}, 200)
	}, function(){
		$(this).find('img').stop().animate({
			'width':200,
			'height':200,
			'left': 0,
			'top': 0
		}, 200)
	})
}



function program(){
	$('.filter_events').map(function(){
		var date = $('#'+ $(this).data('time'));
		date.addClass('occupied');
		
		if($('[data-cat-d-h=' + $(this).data('cat-d-h') +']').length > 1){
			$('[data-cat-d-h=' + $(this).data('cat-d-h') +']').map(function(e){
				$(this).data('top',e*100);
				date.css('height', 100+e*100);
				// console.log($(this).data('cat-d-h') + ' | ' + $(this).data('height'))
			})
		}
		
		// $('[data-cat-h]').map(function(){
		// 	$(this).css('border','1px solid red')
		// })
		
	});
}




















