//Ochrana e/mailu
function safeMails(){
	$('a.em').each(function(){
		e = this.rel.replace('/','@');
		this.href = 'mailto:' + e;
	});
	$('span.em').each(function(){
		e = $(this).text().replace('/','@');
		$(this).text(e);
	});
}


function animateBoat() {
  $('#logo img')
	.animate({
		// marginTop:'5px',
		marginTop:Math.ceil(Math.random()*10)+'px'
	},500,function(){
		$('#logo img').animate({
			// marginTop:'-5px',
			marginTop:Math.ceil(Math.random()*-10)+'px'
		},500,animateBoat())
	})
}
function relation(){
	$('a[rel]').click(function(){
		var elem = $('#'+$(this).attr('rel')).find('a:first');
		if(elem.length > 0){
			elem.attr('href',$(this).attr('href'))
				.trigger('mouseover').trigger('click');
			return false;
		}else{
			return true;
		}

	})

}
function languageSwitch(){
	$('#languages a').click(function(){
		var href = window.location.href;
		window.location.href = href.replace(/(\/+)[a-zA-Z]{2}(\/+)/, $(this).attr('href'));
		return false;
	})
}

function showContent(elem){
	elem.slideDown(function(){
		var head = elem.parents('.article').prev('.show-hide');
		$('body').scrollTo(head, {
			axis: 'y',
			offset: $('#menu-bcg').height() * -1 -7,
			duration: 600,
			margin:false,
			onAfter: function(){
				var winHeight = $(document).height();
				$('body').height(winHeight)
			}
		})
	})
}

function hideContent(){
	
}

function openCurrentArticle(){
	var article = document.location.hash.replace(/#/,'#_')
	if(article.length > 0){ 
		if($(article).length > 0) {
			$(article).trigger('click');
		}else{
			window.location.href =  $('.current-language').attr('href');
			// alert($('li#home').attr('class'))
			// $('#menu li:first').trigger('click');
		}
	}
}

function toggleShow(){
	$('.show-hide').click(function(){
		var elem = $(this).next('.article').find('.toggle-show');
		if(elem.is(':hidden')){
			document.location.hash = $(this).attr('id').replace(/_/,'');
			showContent(elem);
		}else{
			elem.slideUp(function(){
				var winHeight = $('#project').height();
				$('body').height(winHeight);
			});
		}
		return false;
	})
	
}

function cursor(){
	$('.submenu li a')
	.mouseenter(function(){
        $('#mycursor, #star').show();
    })
	.mouseleave(function(){
        $('#mycursor, #star').hide();
    })
    .mousemove(function(e){
        $('#mycursor').css('left', e.clientX + 1).css('top', e.clientY + 1);
		$('#star').css('left', e.clientX - 14).css('top', e.clientY - 14);
    });

}
function mainImage(){
	$('.gallery a').click(function(){
		var article = $(this).parents('.article');
		var container = article.find('.thumbnail');
		var description = article.find('.media_title')
		var src = $(this).attr('href');
		var image = new Image();
		var vimeoId = $(this).attr('rel');
		var id = article.prev('.show-hide').attr('id');
		
		container.empty();
		
		if($(this).hasClass('thumb_vimeo')){
			$('#vimeo').clone().appendTo(container).removeClass('invisible')
			.find('.vimeo_object').attr('value', 'http://vimeo.com/moogaloop.swf?clip_id='+vimeoId+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=f0000c&amp;fullscreen=1')
			.parents('.thumbnail').find('.vimeo_embed').attr('src', 'http://vimeo.com/moogaloop.swf?clip_id='+vimeoId+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=f0000c&amp;fullscreen=1')
		}else{
			$(image).load(function(){
				$(this).appendTo(container);
			}).attr('src',src)
		}

		description.empty().html($(this).attr('title'));
		
		
		if(container.is(':hidden')){
			var elem = $(this).parents('.article').find('.toggle-show');
			showContent(elem);
			document.location.hash = id;
		}
		
		return false
	})
}

function setup(){
	// cursor();
	toggleShow();
	mainImage();
	openCurrentArticle();
	relation();
	languageSwitch();
	safeMails();
}

$(function($) {
	$('a#logo, .fishmonger, .hidden').hide();
	$('.article').map(function(){
		if($(this).hasClass('episode') == false){
			$(this).find('.toggle-show').hide();
		} 
	})
	
	
});


Cufon.DOM.ready(function() {
		
	Cufon.replace('.fishmonger, h1, h2, h3, h4', { 
		fontFamily: 'Fishmonger CR',
		hover:true
	})
	
	setup();
	// animateBoat();
	
	$('a#logo, .fishmonger').show();
	var logoPosition = $('.current').offset().left + $('.current').width() / 2 - $('a#logo').width() / 2 - $('#episodes').offset().left;
	
	$('a#logo').css({'left': logoPosition+'px'}).show();
	
	$('.menu li.interactive a')
		.hover(function(){			
			$('a#logo').stop().animate({
				'position':'absolute',
				left: $(this).offset().left + $(this).width() / 2 - $('a#logo').width() / 2 - $('#episodes').offset().left,
			},1000,	function(){
				if($('.menu').find('a.clicked').length > 0) 
					window.location = $('.menu').find('a.clicked:first-child').attr('href');
			})
		}, function(){
			if($('.menu').find('a.clicked').length < 1){
				$('a#logo').delay(100).stop().animate({
					left: logoPosition
				},1000)
			}
		})
		.click(function(){	
			if($('#logo').is(':animated')){
				$(this).addClass('clicked');
				return false;
			}else{
				return true;
			}
		})
		
});

