var wpc = 'http://www.stiftungeierstockkrebs.de/wordpress/wp-content/';
var path = wpc + 'themes/cms/';

head.js(
	'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js',
	function()
	{
		$(document).ready(
			function()
			{
				initTeaser();
				initFancybox();
				initForms();
			}
		);
	}
);


/**
* init teasers
*/
var initTeaser = function()
{
	$('.teaser', '#main').each(
		function()
		{
			if($('a', this).length > 0)
			{
				$(this).css('cursor', 'pointer');
				$(this).click(
					function()
					{
						window.location.href = $('a', this).first().attr('href');
					}
				);
			}
		}
	);
}


/**
* init fancybox
*/
var initFancybox = function()
{
	if($('.fancylink', '#main').length > 0)
	{
		head.js(
			path + 'js/fancybox/jquery.fancybox-1.3.4.pack.js',
			function()
			{
				$('head').append('<link rel="stylesheet" type="text/css" media="all" href="' + path + 'js/fancybox/jquery.fancybox-1.3.4.css" />');

				$('.videolink').click(
					function()
					{
						$.fancybox({
							'padding'		: 0,
							'autoScale'		: false,
							'transitionIn'	: 'none',
							'transitionOut'	: 'none',
							'title'			: this.title,
							'width'			: 680,
							'height'		: 495,
							'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
							'type'			: 'swf',
							'swf'			: {
								 'wmode'		: 'transparent',
								'allowfullscreen'	: 'true'
							}
						});

						return false;
					}
				);
				
				$('a[href$=".jpg"] img, a[href$=".png"] img')
					.attr('alt', '')
					.parent()
						.addClass('group')
						.attr('rel', 'group')
						.fancybox({
							'zoomOpacity': true,
							'overlayShow': true,
							'overlayOpacity': 0.8,
							'zoomSpeedIn': 400,
							'zoomSpeedOut': 250
						})
						.addClass('zoomLink')
				;
			}
		);
	}
}


var initForms = function()
{
	if($('form', '#main').length > 0)
	{
		head.js(
			wpc + 'plugins/contact-form-7/jquery.form.js',
			wpc + 'plugins/contact-form-7/scripts.js',
			function()
			{
				$('.ti input, textarea', '#main').focus(
					function()
					{
						$(this).parent().addClass('focus');
					}
				);
				
				$('.ti input, textarea', '#main').blur(
					function()
					{
						$(this).parent().removeClass('focus');
					}
				);
				
				$('.wpcf7-validates-as-required', '#main').each(
					function()
					{
						var li = $(this).parent().parent();
						$('label', li).append('*');
					}
				);
				
				$.fn.wpcf7NotValidTip = function(message)
				{
					return this.each(function() {
						var into = $(this).parent();
						into.append('<span class="wpcf7-not-valid-tip">' + message + '</span>');
						into.find(':input').change(function() {
							into.find('.wpcf7-not-valid-tip').not(':hidden').fadeOut('fast');
							into.removeClass('not-valid');
						});
						into.addClass('not-valid');
						if($('body').data('firstError') == 0)
						{
							$('body').data('firstError', 1);
							$('html,body').animate({scrollTop: (into.offset().top - 20)}, 1000);
						}
					});
				};
			}
		);
		
		$('.submit', '#page').each(
			function()
			{
				$(this).parent().append('<input type="submit" class="hidealways" />');
			}
		);
		
		$('.submit', '#page').click(
			function()
			{
				var check = $(this).parent();
				while(!check.is('form') && !check.is('html'))
				{
					check = check.parent();
				}
				
				if(check.is('form'))
				{
					check.submit();
				}
				
				return false;
			}
		);
	}
}



/**
* misc helper functions
*/
var scrollTo = function(id, duration)
{
	if($(id).length < 1) return;
	var o = $(id).offset().top;
	if($('.lt-1024').length > 0) o -= ($('#header').height() + 47);
	$('html,body').delay(200).animate({scrollTop: o}, duration);
}
