$(document).ready(function()
{
	jQuery.fn.fc_slider = function()
	{
		var children = jQuery(this).children().not($('.nojs'));
		children.each(function(index)
		{
			jQuery(this).click(function()
			{
				container = jQuery('#heaven .container_12:nth-child('+(index+1)+')');				
				container_children = container.children();

				container_out = jQuery('.out');
				container_children_out = jQuery('.ch_out');
				
/*			
				rocket.animate(
				{
					top: '-200px',
					left: '200px'
				}, 5000, 'easeInQuad');
*/						
				if (container_out.length > 0)
				{
					if(container.attr('id') == container_out.attr('id'))
					{
						container_children.fadeOut(100).removeClass('ch_out');
						container_out.animate({'height':'toggle'}, 500, 'swing').delay(500).removeClass('out');
					}
					
					else
					{

						if (container_children.css('display') == 'block')
						{
							container_children.css({'display': 'none'});
						}					
					
						container_children_out.fadeOut(100).removeClass('ch_out');
						container_out.animate({'height':'toggle'}, 500, 'swing', function()
						{
							container.animate({'height':'toggle'}, 500, 'swing', function()
							{
								container_children.fadeIn(function()
								{
									$('#searchform input:first').focus();
								}).addClass('ch_out');
							}).addClass('out');						
						}).removeClass('out');
					}
				}
				
				else
				{
					if (container_children.css('display') == 'block')
					{
						container_children.css({'display': 'none'});
					}
					
					container.animate({'height':'toggle'}, 500, 'swing', function()
					{
						container_children.fadeIn(function()
						{
							$('#searchform input:first').focus();
						}).addClass('ch_out');
					}).addClass('out');
				}

				return false;
			})
		})
	}

	jQuery.fn.fc_shaker = function(shakes, distance, duration)
	{
		this.each(function()
		{
			$(this).css({position:'relative'});
	
			var orig_left = parseInt($(this).css('left'), 10)
	
			for (var x=1; x<=shakes; x++)
			{
				$(this).animate({left:orig_left-(distance*-1)}, (((distance/shakes)/4))).animate({left:orig_left+distance}, ((distance/shakes)/2)).animate({left:orig_left}, (((duration/shakes)/4)));
			}
		});
		
		return this;
	};

/* ------------------------------------------------ */

	// hide these elements
	$('div.hideable_target').hide();

	// on day 666 gregory created heaven and a rocket
	var heaven_container = $('<div></div>',
	{
		'id': 'heaven'
	}).prependTo('body');
	
	// create jobs container and hide it
	var jobs_container = $('<div></div>',
	{
		'class': 'container_12',
		'id': 'jobs_container'
	}).prependTo('#heaven');
	
	// create search container and hide it
	var search_container = $('<div></div>',
	{
		'class': 'container_12',
		'id': 'search_container'
	}).prependTo('#heaven');

	// create news container and hide it
	var news_container = $('<div></div>',
	{
		'class': 'container_12',
		'id': 'news_container'
	}).prependTo('#heaven');
/*
	var rocket = $('<img></img>',
	{
		'id': 'rocket',
		'src': '/fileadmin/templates/images/rakete.png'
	}).prependTo('#news_container');
*/
	// load the content
	$('#news_container').load('/ajax/news.html').hide();
	$('#search_container').load('/ajax/search.html').hide();
	$('#jobs_container').load('/ajax/jobs.html').hide();
	
	$('div.meta ul').fc_slider();

/* ------------------------------------------------ */

	// teaser image rollover
	$('.project_feature').each(function(){
		$(this).after('<span><strong>'+$(this).attr('alt')+'</strong>'+$(this).attr('title')+'</span>');
	});

/* ------------------------------------------------ */

	// filter
/*	$('dl.filter_type dd, dl.filter_industry dd').hide();
	$('dl.filter_type dt a, dl.filter_industry dt a').click(function()
	{
		$('dl.'+$(this).attr('class')+' dd').slideToggle();
		return false;
	});
*/

/* ------------------------------------------------ */

	// close heaven
/*	
	$('#close_news, #close_search, #close_jobs').click(function()
	{
		$('#heaven .container_12').children().fadeOut(100).removeClass('ch_out');
		$('.out').animate({'height':'toggle'}, 500, 'swing').delay(500).removeClass('out');
		
		return false;
	});
*/

/* ------------------------------------------------ */

	// scroller
	var scroll_triggers = $('li.csc-section a');
	var scroll_targets = $('div.h35 h2, div.h55 h2, div.h87 h2');
	
	$(scroll_triggers).each(function(index)
	{
		$(this).click(function()
		{
			$.smoothScroll(
			{
				scrollTarget: scroll_targets[index],
				easing: 'easeOutQuad',
				speed: 1000
			});
			return false;
		});
	});
		
/* ------------------------------------------------ */
	
	// hideables	
	var hideable_triggers = $('a.hideable_trigger');
	var hideable_targets = $('div.hideable_target');
	
	$(hideable_triggers).each(function(index)
	{
		$(this).click(function()
		{
			$(hideable_targets[index]).slideToggle();
			
			return false;	
		});
	});

/* ------------------------------------------------ */

	// services
	$('.services_items h4').nextAll('p').hide();
	$('.services_main p').hide();
/*	
	$('.services_items').each(function(index)
	{
	    $(this).mouseenter(function()
	    {
	        $($('.services_main p')[index]).show();
	    }).mouseleave(function()
	    {
	    	$($('.services_main p')[index]).hide();
	    });
	});
*/	
	$('.services_items h4').click(function()
	{
		var parent = $(this);
		var active = $(this).next();
		
	   	$(this).next().slideToggle(200, function()
	   	{
	   		$('.services_items h4').nextAll('p').not($(active)).slideUp(200);
	   	});
	   	return false;	
	});
	
/* ------------------------------------------------ */	

	// image swap
	$('img[src$=bw.jpg]').mouseenter(function()
	{
	    name = $(this).attr('src').replace(/_bw/g, '_col');
	    $(this).attr('src', name);
	}).mouseleave(function()
	{
	    name = $(this).attr('src').replace(/_col/g, '_bw');
	    $(this).attr('src', name);
	});	

/* ------------------------------------------------ */	
	
	
	
	$('.tx-fcprojects, .tx-fcprojects-filter').touchwipe({
		wipeLeft: function() { 
			link = $('ul.pagination li:nth-child(3) a').attr('href');
			if(link){
				window.location = link;
			}
		},
		wipeRight: function() { 
			link = $('ul.pagination li:nth-child(1) a').attr('href');
			if(link){
				window.location = link;
			}
		},
		min_move_x: 120,
		preventDefaultEvents: false
	});
	
	
	
	var count = 0;
	$(document).keydown(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		count = count + code;
		switch(count){
			case 370:
				window.location = '/agentur/#disco';
				count = 0;
			break;
			case 299:
				$('body').css({
					'-webkit-transform' : 'rotate(-180deg)',
					'-moz-transform': 'rotate(-180deg)',
					'transform' : 'rotate(-180deg)'
				});
				count = 0;
			break;
			case 242:
				$('*').css({
					'direction' : 'rtl',
					'unicode-bidi': 'bidi-override'
				});
				count = 0;
			break;
			case 380:
				$('body *').css('opacity', 0.5);
				count = 0;
			break;
			case 364:
				$('body').fc_shaker(3,100,1000);
				count = 0;
			break;
			case 463:
				$('body').animate({
					'width':0,
					'height':0
				}, 1000, 'swing', function(){
					$('body').css('display', 'none');
				});
				count = 0;
			break;
		}
		if(code == 27){
			count = 0;
		}
	});

	stueMe();
	
	$('img[src$="stefan_bw.jpg"], img[src$="stefan_col.jpg"]').click(function(){
		cornify_add();
		return false;
	});
});

function stueMe(){
	if(window.location.hash == "#disco"){
		$('img[src$="stefan_bw.jpg"]').attr('src','fileadmin/teambilder/disco_col.jpg');
		$('img[src$="stefan_col.jpg"]').attr('src','fileadmin/teambilder/disco_col.jpg');
	}
}

