$(function(){
	
	$('img[data-hover],input[data-hover]').imghover();

	$('input[data-placeholder]').inputPlaceholder();

	// Popups
    $('a.popup').each(function () {
       var href = $(this).attr('href');
       $(this).attr('href', 'watch/' + href);
    });

	$('a.popup').click(function(e){
		var href = $(this).data('href') ? $(this).data('href') : $(this).attr('href');
		var width = $(this).hasClass('video') ? 468 : $(this).hasClass('slideshow') ? 706 : 450;
		var height = $(this).hasClass('video') ? 347 : $(this).hasClass('slideshow') ? 475 : 114;
		openPopup(href, this, width, height);
		e.preventDefault();
	});

	$('form.popup').each(function(){
		if ($(this).data('action')) $(this).attr('action', $(this).data('action'));
		$(this).submit(function(e){
				e.preventDefault();
				var form = this;
				var width = $(this).hasClass('wide') ? 630 : 450;
				openPopup('about:blank', this, width, 114, function(){
					$(form).attr('target',$('#fancybox-frame').attr('name'));
					form.submit();
				});
			});
	});

	// Language dropdown
	$('#currentlang').hover(function(e){
		$(this).toggleClass('hover',e.type == 'mouseenter');
	}).click(function(e){
		toggleLang(!$(this).hasClass('active'));
		e.stopPropagation();
	});

	function toggleLang(state) {
		$('#currentlang').toggleClass('active',state);
		$('#lang').toggle(state);
	}

	$(document).click(function(){
		toggleLang(false);
	});

	// Header video
	$('#button-play').click(function(e){
		e.preventDefault();
		var url = $(this).attr('href');
		var h = $('#header-visual');
		var p = h.find('>div.visual');
		var v = h.find('>div.video');
		$(this).hide();

		p.fadeOut(500, function(){
			h.animate({height:347},500,function(){
				v.prepend('<iframe src="'+url+'" frameborder="0" width="468" height="347" scrolling="no"></iframe>')
					.show();

				// Recalculate menu-top
				if ($('#menu').length)
					navTop = $('#menu>ul').offset().top;
			});
		});
	});

	$('#header-visual div.video div.sublink a.close').click(function(e){
		e.preventDefault();
		var h = $('#header-visual');
		h.find('iframe').remove();
		h.find('>div.video').hide();
		h.find('>div.visual').fadeIn(500,function(){
			$('#button-play').show();
		});
		h.animate({
			height: 370
		},500,function(){
			if ($('#menu').length)
				navTop = $('#menu>ul').offset().top;
		});
	});

	// Inline video's
	$('#content a.openvideo').click(function(e){
		var p = $(this).parents('div.contentblock');
		var c = p.find('>div');
		var v = c.find('div.content_video');
		var d = c.find('div.content_default');
		var h = v.height();
		var s = 400;
		var id = $(this).attr('data-videoid');

		c.animate({height: h+'px'},s);
		d.fadeOut(s,function(){
			v.fadeIn(s,function(){
				// Force redraw for some Firefox versions
				//p.hide(); p[0].offsetHeight; p.show();

				loadMovie($(this).find('div.video_placeholder'),id);
			});
		});

		var y = p.offset().top;
		$('html,body').animate({scrollTop: y-navHeight},500);

		e.preventDefault();
	});

	$('#content div.content_video div.close a').click(function(e){
		var p = $(this).parents('div.contentblock');
		var c = p.find('>div');
		var v = $(this).parents('div.content_video');
		var d = c.find('>div.content_default');
		var h = d.height();
		var s = 400;

		v.find('div.video_placeholder').html('');
		v.hide();

		d.fadeIn(s);
		c.animate({height: h+'px'}, s, function(){
			// Force redraw for some Firefox versions
			//p.hide(); p[0].offsetHeight; p.show();
		});

		e.preventDefault();
	});

	function loadMovie (placeholderDiv, id) {
		placeholderDiv.html('<iframe src="/popup/video/' + id + '" class="video-player" frameborder="0" marginheight="0" marginwidth="0" scrolling="no"></iframe>');
	}
});
