var slideshow = true;
var currVideo = '';
var intervalStatus = 0;

function videoEnd() {
	slideshow = true;
	if ($.browser.safariOnMac) $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"></div>');
	else $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"><video width="744" height="450" controls="controls"><source src="' + baseUrl + $('div#imageBig' + currVideo).attr('rel') +  '" /></video></div>');
	
	$('div#imageBig div.video').each(function() {			
		$(this).html('<div id="videoBig' + $(this).attr('id').replace('imageBig', '') + '"></div>');
	});
	
	if (intervalStatus == 0)
	{
		project_intervall = setInterval('projects_slide()', sSlideshowInterval);
		intervalStatus = 1;
	}
}

function resize()
{
	// Wenn Auflösung kleiner gleich 1280 dann links zentrieren
	if ($(document).height() < $(window).height())
	{
		$('div#holder').css('height', ($(window).height() - 100) + 'px');
	}
	else
	{
		$('div#holder').css('height', ($(document).height() - 100) + 'px');
	}

	
	if ($(window).width() <= 1280)
	{
		$('div#site').css({
			'margin-left' : '-86px',
			'left' : '0px'
		});
	}
	else
	{
		$('div#site').css({
			'margin-left' : '-737px',
			'left' : '50%'
		});
	}
}

var project_intervall;
function projects_slide() {
	if (slideshow == true)
	{
		var $active = $('div#imageBig div.active');	
		if ($active.length == 0) $active = $('div#imageBig div:last');
		
		// Check if first item is video
		if ($active.hasClass('firstVideo') == true)
		{
			$active.removeClass('firstVideo');
			slideshow = false;
			currVideo = $active.attr('id').replace('imageBig', '');
			
			if ($.browser.safariOnMac) $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"></div>');
			else $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"><video autoplay="autoplay" width="744" height="450" controls="controls"><source src="' + baseUrl + $('div#imageBig' + currVideo).attr('rel') +  '" /></video></div>');
			
			console.log('video embed');
			swfobject.embedSWF(baseUrl + "media/VideoPlayer.swf?server=" + baseUrl + "&skin=media/skin.swf&onComplete=videoEnd&video=" + $active.attr('rel'), $active.attr('id').replace('imageBig', 'videoBig'), "744px", "450px", "9.0.0", "media/expressInstall.swf");
			
			
		}
		else
		{
			var $next =  $active.next().length ? $active.next()
				: $('div#imageBig div:first');
		
			$active.addClass('last-active');
		
			var activeThumbId = $active.attr('id').replace('imageBig', 'imageThumb');
			$('img#' + activeThumbId).removeClass('active').fadeTo(0, 0.5);
			
			var nextThumbId = $next.attr('id').replace('imageBig', 'imageThumb');
			$('img#' + nextThumbId).addClass('active').fadeTo(0, 1);
		
			$next.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, sSlideshowFade, function() { // 1000
					$active.removeClass('active last-active');
				});
				
			if ($next.hasClass('video') == true)
			{
				slideshow = false;
				currVideo = $next.attr('id').replace('imageBig', '');
				
				if ($.browser.safariOnMac) $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"></div>');
				else $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"><video autoplay="autoplay" width="744" height="450" controls="controls"><source src="' + baseUrl + $('div#imageBig' + currVideo).attr('rel') +  '" /></video></div>');
				
				swfobject.embedSWF(baseUrl + "media/VideoPlayer.swf?server=" + baseUrl + "&skin=media/skin.swf&onComplete=videoEnd&video=" + $next.attr('rel'), $next.attr('id').replace('imageBig', 'videoBig'), "744px", "450px", "9.0.0", "media/expressInstall.swf");
					
			}
		}
	}
}

function menuActive()
{
	$(this).find('div.red').css('display', 'block');
	$(this).find('div.red').animate({width: '111px'}, 600);
	$(this).find('a').animate({color: '#FF2900'}, 600);
}

function menuInActive()
{
	$(this).find('div.red').animate({width: '0px'}, 600);
	$(this).find('a').animate({color: '#000'}, 600);
}

/* Kunden functions */
function kundenShowImage()
{
	var image = $(this);
	$(this).find('img.logo').fadeOut(200, function ()
	{
		image.find('img.project').fadeIn(1000);
	});	
}

function kundenHideImage()
{
	var image = $(this);
	$(this).find('img.project').fadeOut(200, function ()
	{
		image.find('img.logo').fadeIn(1000);
	});
}

/*
Projekte Funktionen
*/
function project_show_teaser()
{
	$(this).find('div.projectBar').animate({backgroundColor: '#FF2900'}, 500);
	$(this).find('div.projectBar').css({borderBottom: '#FF2900 1px solid'});
	$(this).find('div.projectBar').find('h2').animate({color: '#FFF'}, 500);
	$(this).find('div.projectBar').find('h2').find('a').animate({color: '#FFF'}, 500);
	$(this).find('div.projectBar').find('h3').animate({color: '#FFF'}, 500);
}

function project_hide_teaser()
{
	$(this).find('div.projectBar').animate({backgroundColor: '#FFF'}, 500);
	$(this).find('div.projectBar').css({borderBottom: '#000000 1px solid'});
	$(this).find('div.projectBar').find('h2').animate({color: '#000'}, 500);
	$(this).find('div.projectBar').find('h2').find('a').animate({color: '#000'}, 500);
	$(this).find('div.projectBar').find('h3').animate({color: '#000'}, 500);
}

/*
Agentur Kontakt Funktionen
*/
function kontakt_info_show()
{
	$(this).find('div.infoBar').animate({top: '-55px'}, 500);
}

function kontakt_info_hide()
{
	$(this).find('div.infoBar').animate({top: '0px'}, 500);
}

function agentur_acc(trig)
{
	
	//$('ul.tabs li').animate({backgroundColor: '#000'}, 500);	
	var src = $('ul.tabs li#tabProfil img').attr('src').replace('_act', '');
	$('ul.tabs li#tabProfil img').attr("src", src);
	
	var src = $('ul.tabs li#tabKontakt img').attr('src').replace('_act', '');
	$('ul.tabs li#tabKontakt img').attr("src", src);
	
	var src = $('ul.tabs li#tabWillkommen img').attr('src').replace('_act', '');
	$('ul.tabs li#tabWillkommen img').attr("src", src);
	
	if ( trig.hasClass('trigger_active') ) {
		trig.next('div.content').slideToggle(2000, 'easeOutQuint');
		trig.removeClass('trigger_active');
		//trig.parent().find('img.arrow').rotateAnimation(90);
	} else {
		$('ul.tabs li').removeClass('active');
				
		$('.trigger_active').next('div.content').slideToggle(2000, 'easeOutQuint');
		$('.trigger_active').removeClass('trigger_active');
		
		if (trig.attr('id') == 'Kontakt') {
			trig.next('div.content').slideToggle(2000, 'easeOutQuint', function() {
				$('div.contactImages div.imgHolder').delay(500).each(function(i) {
					$(this).delay(i*500).fadeIn(1000);
				});
			});
		}
		else if (trig.attr('id') == 'Willkommen')
		{
			trig.next('div.content').slideToggle(2000, 'easeOutQuint');
			willkommen_intervall = setInterval('willkommen_slide()', 5000);	
		}
		else
		{
			clearInterval(willkommen_intervall);
			trig.next('div.content').slideToggle(2000, 'easeOutQuint');
		}
		trig.addClass('trigger_active');
		// Tab inaktiv setzen
		//$('li#tab' + trig.attr('id')).animate({backgroundColor: '#FF2900'}, 500);
		
		var src = $('li#tab' + trig.attr('id')).find('img').attr('src').replace('.gif', '_act.gif');
		$('li#tab' + trig.attr('id')).find('img').attr("src", src);
		
		$('li#tab' + trig.attr('id')).addClass('active');
	}
	return false;
}

function show_block(id)
{
	var trig = $('div#agentur div.block h2#' + id);
	return agentur_acc(trig);	
}

var willkommen_intervall;
function willkommen_slide() {
    var $active = $('div#agentur div#slideshow div.active');

    if ( $active.length == 0 ) $active = $('div#agentur div#slideshow div:last');

    var $next =  $active.next().length ? $active.next()
        : $('div#agentur div#slideshow div:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(window).load(function(){
	
	$.browser.ipad = (navigator.userAgent.match(/iPad/i) != null);	
	$.browser.safariOnMac = ($.client.browser == 'Safari' && $.client.os == 'Mac');
	
	// Holder auf 100%	
	resize();
	
	var shadowHeight = $('div#shadow').height() + 100;
	$('div#shadowBottom ').css('top', shadowHeight + 'px');
	
	/* Main Menu */
	if ($.browser.ipad == false) $('ul#menuMain li.menu').hoverIntent(menuActive, menuInActive);
	
	/* Lang Menu */
	$('ul#menuLang li a img').hover(function() {
		var src = $(this).attr('src');
		if (src.indexOf('_act') == -1)
		{
			src = $(this).attr('src').replace('.gif', '_act.gif');
			$(this).attr("src", src);
		}
	}, function() {
		if ($(this).hasClass('active') === false)
		{
			var src = $(this).attr('src').replace('_act', '');
			$(this).attr("src", src);
		}
	});
	
	$('ul.tabs li').hover(function() {	
		if ($(this).hasClass('active') === false)
		{
			var src = $(this).find('img').attr('src');
			if (src.indexOf('_act') == -1)
			{
				src = $(this).find('img').attr('src').replace('.gif', '_act.gif');
				$(this).find('img').attr('src', src);
			}
		}
	}, function() {
		if ($(this).hasClass('active') === false)
		{
			var src = $(this).find('img').attr('src').replace('_act', '');
			$(this).find('img').attr("src", src);
		}
	});
	
	
	
	$('h3.link a').hover(function() {	
		
		$(this).css({color: '#FF2900'});
		$(this).parent().css('background-image', 'url(img/layout/arrow_right_hov.png)');  
		
	}, function() {
		
		$(this).css({color: '#000000'});
		$(this).parent().css('background-image', 'url(img/layout/arrow_right.png)');
		
	});
	
	
	
	/*
	Agentur - Kontakt
	*/
	
	$('div#agentur div.block h2.trigger').click( function() {
		var trig = $(this);
		return agentur_acc(trig);		
	});
	
	$('div.imgHolder').hoverIntent(kontakt_info_show, kontakt_info_hide);
	
	$('div.projectBox div.image-bar-holder div.image-bar').hover(function() {
		$(this).animate({
			width: '100%'
		}, 500, function() {
			// Animation complete.
			$(this).find('img').fadeIn(1000);
		});		
	}, function() {
		$(this).find('img').fadeOut(1000, function () {
			$(this).parent().animate({
				width: '224px'
			}, 500);
		});
	});
	
	$('div#colRight div.image-bar-holder div.image-bar').hover(function() {
		if ($(this).hasClass('no-pointer') === false)
		{
			$(this).animate({
				width: '100%'
			}, 500, function() {
				// Animation complete.
				$(this).find('img').fadeIn(1000);
			});		
		}
	}, function() {
		if ($(this).hasClass('no-pointer') === false)
		{
			$(this).find('img').fadeOut(1000, function () {
				$(this).parent().animate({
					width: '544px'
				}, 500);
			});
		}
	});
	
	/*
	Projektauflistung
	*/
	
	// Projekatuflistung Teaserboxen
	$('div.box').hoverIntent(project_show_teaser, project_hide_teaser);
	
	// Teaserboxen einfaden
	$('div.projects div.row div.box').delay(200).each(function(i) {		
		$(this).delay(i*300).fadeIn(1000);
	});
	
	/*
	Projektdetailseite
	*/
	$('div.projects img.thumb').hover(function() {
		if ($(this).hasClass('active') === false)
		{
			$(this).fadeTo(500, 1);
		}
	}, function() {
		if ($(this).hasClass('active') === false)
		{
			$(this).fadeTo(500, 0.5);
		}
	});	

	$('div.projects img.thumb').delay(200).each(function(i) {
		$(this).delay(i*200).fadeIn(500);
	});
	
	$('div.projects img.thumb').queue(function() {		
		$('div.similarProjects img').delay(2000).each(function(i) {
			$(this).delay(i*500).fadeIn(1000);
		});
		$(this).dequeue();
	});
	
	$('div#imageBig img').click( function() {
		projects_slide();
	});
	
	$('div.projects img.thumb').click( function() {
		
		clearInterval(project_intervall);
		intervalStatus = 0;
		/*		
		if (currVideo.length > 0)
		{
			$('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"></div>');
		}
		*/
				
		$('div#imageBig div.video').each(function() {			
			$(this).html('<div id="videoBig' + $(this).attr('id').replace('imageBig', '') + '"></div>');
		});

		$('div.projects img.thumb').removeClass('active').fadeTo(0, 0.5);
		
		var $active = $('div#imageBig div.active');
		if ( $active.length == 0 ) $active = $('div#imageBig div:last');
				
		var $next = $('div#' + $(this).attr('id').replace('imageThumb', 'imageBig'));
		
		$(this).addClass('active').fadeTo(500, 1);
		
		$('div.image-holder').css({opacity: 0.0});
		
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
			
		if ($next.hasClass('video') == true)
		{
			slideshow = false;
			currVideo = $next.attr('id').replace('imageBig', '');
			
			if ($.browser.safariOnMac) $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"></div>');
			else $('div#imageBig' + currVideo).html('<div id="videoBig' + currVideo + '"><video autoplay="autoplay" width="744" height="450" controls="controls"><source src="' + baseUrl + $('div#imageBig' + currVideo).attr('rel') +  '" /></video></div>');

			swfobject.embedSWF(baseUrl + "media/VideoPlayer.swf?server=" + baseUrl + "&skin=media/skin.swf&onComplete=videoEnd&video=" + $next.attr('rel'), $next.attr('id').replace('imageBig', 'videoBig'), "744px", "450px", "9.0.0", "media/expressInstall.swf");					
		}	
	});
	
	if ($('div#imageBig div.active').length != 0)
	{
		project_intervall = setInterval('projects_slide()', sSlideshowInterval);
		intervalStatus = 1;
	}
	
	/* Kunden */
	$('div#kunden div.box-holder').delay(200).each(function(i) {
		$(this).delay(i*250).fadeIn(1000);
	});
	
	$('div#kunden div.image').hoverIntent(kundenShowImage, kundenHideImage);
	
	/* Startseite */
	$('div#colRight div.servicesList div.item').hoverIntent(show_service_item, hide_service_item);
	
	/* Startseite Teaser Blätterfunktion */
	
	$('div.teaserPagination img.pointer').click( function() {
		
		if ($(this).hasClass('back') === true)
		{
			// back
			$('div.image-bar-holder-active').fadeOut(0, function() {
				$(this).removeClass('image-bar-holder-active');
				
				// Video Tag entfernen
				if ($(this).hasClass('video') === true)
				{
					$(this).find('div#videoBigHome').html('');
				}
				
				var $prev = $(this).prev('div#colRight div.image-bar-holder');
				if ($prev.length == 0) $prev = $('div#colRight div.image-bar-holder:last');
				
				// Video einblenden
				if ($prev.hasClass('video') === true)
				{
					$prev.find('div#videoBigHome').html('<div id="videoHolder"><video width="744" height="374" controls="controls" autoplay="autoplay"><source src="' + baseUrl + $prev.attr('rel') +  '" /></video></div>');

					swfobject.embedSWF(baseUrl + "media/VideoPlayer.swf?server=" + baseUrl + "&skin=media/skin.swf&video=" + $prev.attr('rel'), "videoHolder", "744px", "374px", "9.0.0", "media/expressInstall.swf");
				}
				
				$prev.addClass('image-bar-holder-active').fadeIn(0);
			});
		}
		else
		{
			// forward
			$('div.image-bar-holder-active').fadeOut(0, function() {
				$(this).removeClass('image-bar-holder-active');
				
				// Video Tag entfernen
				if ($(this).hasClass('video') === true)
				{
					$(this).find('div#videoBigHome').html('');
				}
				
				var $next = $(this).next('div#colRight div.image-bar-holder');
				if ($next.length == 0) $next = $('div#colRight div.image-bar-holder:first');
				
				// Video einblenden
				if ($next.hasClass('video') === true)
				{
					$next.find('div#videoBigHome').html('<div id="videoHolder"><video width="744" height="374" controls="controls" autoplay="autoplay"><source src="' + baseUrl + $next.attr('rel') +  '" /></video></div>');

					swfobject.embedSWF(baseUrl + "media/VideoPlayer.swf?server=" + baseUrl + "&skin=media/skin.swf&video=" + $next.attr('rel'), "videoHolder", "744px", "374px", "9.0.0", "media/expressInstall.swf");
				}
				
				$next.addClass('image-bar-holder-active').fadeIn(0);				
			});
		}
			
	});
	
});

function show_service_item()
{
	$(this).animate({
		paddingLeft: ['28px', 'swing']
	});	
}

function hide_service_item()
{
	$(this).animate({
		paddingLeft: ['21px', 'swing']
	});
}


var presseAnimated = false;

/* Startseite - Presse */
function presse_prev(holderID)
{
	if (presseAnimated) return;
	$('div#' + holderID).removeClass('newsAct').fadeOut();
	var $prev = $('div#' + holderID).prev('div.newsHolder');
	
	if ($prev.length === 0)
	{
		$('div#' + holderID).parent().children(':last').addClass('newsAct').fadeIn(1000, 'linear');
	}
	else
	{
		$prev.addClass('newsAct').fadeIn(1000, 'linear');
	}
	
	presseAnimated = true;
	window.setTimeout(function(){
		presseAnimated = false;
	}, 1000);
}

function presse_next(holderID)
{
	if (presseAnimated) return;
	$('div#' + holderID).removeClass('newsAct').fadeOut();	
	var $next = $('div#' + holderID).next('div.newsHolder');
	
	if ($next.length === 0)
	{
		$('div#' + holderID).parent().find(':nth-child(2)').addClass('newsAct').fadeIn(1000, 'linear');
	}
	else
	{
		$next.addClass('newsAct').fadeIn(1000, 'linear');
	}
	
	presseAnimated = true;
	window.setTimeout(function(){
		presseAnimated = false;
	}, 1000);
}

$(window).resize(function() {
	// Holder auf 100%
	resize();
});

setInterval(resize, 500);






$(document).ready(function(){
	if ($('#site').hasClass('startseite'))
	{
		$('#site').fadeIn(0);
		
		$('div.projectBox h3.link').css('background-position', ($('div.projectBox h3.link a').width() + 10) + 'px 3.2px');
		$('div#colRight h3.link').css('background-position', ($('div#colRight h3.link a').width() + 12) + 'px 3.2px');
		
		$('#site > *').fadeOut(0);
		
		$(window).load(function(){
			$('#site > *').fadeOut(0);
			$('#site > *').delay(350).fadeIn(350);
			
			$('#main > *').stop(true, true).fadeOut(0);
			$('#main > #logo').stop(true, true).fadeIn(0);
			$('#main > #menu').stop(true, true).fadeIn(0);
			
			var $rest = $('#main > *').not('#logo').not('#menu').stop(true, true).fadeOut(0).css('visibility', 'hidden');
			window.setTimeout(function(){
				
				$rest.css('visibility', '').fadeOut(0).fadeIn(1100);
			
			}, 1000);
			
		});
		
	}
});



