var front_loop = null;
var folio_loop_ = null;

$(document).ready(function(){
	
	Cufon.replace('#menu li a, #sidemenu li a, #langs li a, #sortnav a, #bottom-nav a, .oldfolio', {
		// textShadow: '0 1px #CCCCCC',
		// fontFamily: 'menu',
		hover: 'true'
	});
	
	// Cufon.replace('h1, h2, h3, h4, .home #slider .project .description a');
	Cufon.replace('h1, h2, h3');
	if (!($.browser.msie)) {
		Cufon.replace('h4, .home #slider .project .description a');		
	} else {
		$('h4, .home #slider .project .description a').css({'font-family': 'Arial'});
	}
	
	if ($('#content .shifted img').length > 1) {
		$('#content .shifted img:first').show();
		setTimeout('shiftedLoop()', 4000);
	}	

	$('.nav a').tooltip({
		track: true,
		bodyHandler: function() {
			return $(this).text();
		}
	});
	
	$('#menu .page_item.page-item-14').find('a').attr('target', '_blank');
	
	if ($('body.home').length == 1) {
		$('.home #content .dynamic-col-1').click(function(){
			window.location = $('#header #menu a[title=tasty]').attr('href');
		});
		$('.home #content .dynamic-col-2').click(function(){
			window.open('http://blog.tastyagency.com');
		});
		$('.home #content .dynamic-col-3').click(function(){
			window.location = $('#header #menu a[title=portfolio]').attr('href');
			return false;
		});
		
		if ($('.home #content .dynamic-col-3 a').length >= 1) {
			$('.home #content .dynamic-col-3 a').each(function(){
				if ($(this).children().length == 0) {
					$(this).remove();
				}
			});
			$('.home #content .dynamic-col-3 a:first').show();
			setTimeout('logaLoop()', 4000);
		}
		
		
		var slider = $('#slider');
		var inner = slider.find('.inner');
		
		var first = slider.find('.project:first').clone();
		slider.find('.project:last').after(first);
		slider.find('.inner').css({left: -960});
		
		inner.css({width: slider.find('.project').length * 960});
		
		slider.find('.nav a').click(function(){
			var offset = -960;
			if ($(this).hasClass('left')) {
				offset = -1 * offset;
			}
			clearTimeout(front_loop);

			if ((parseInt(inner.css('left')) == -1*(slider.find('.project').length-1) * 960) && ($(this).hasClass('right'))) {
				inner.css({left: 0});
			}
			if ((parseInt(inner.css('left')) == 0) && ($(this).hasClass('left'))) {
				inner.css({left: -1*(slider.find('.project').length-1) * 960});
			}
			
			inner.animate({left: '+='+offset}, 600);
			if ($.browser.msie && $.browser.version.substr(0,1)==7) {
			} else {
				slider.find('.description').slideToggle(220).slideToggle(380);
				Cufon.replace(slider.find('.description h4, .description a'));
			}


			return false;
		});
		
		$('#slider .nav a').hover(function(){
			$(this).stop().animate({opacity: 0.7}, 500).delay(100).animate({opacity: 0}, 700);
		}, function(){});
		
		front_loop = setTimeout('frontLoop()', 4000);
	}
	
	
	if ($('body.tasty').length == 1) {
		// var h = $('#content').css('height');
		// h = h.substring(0, $('#content').css('height').length - 2);
		// var margin_bottom = h-330;
		// if (margin_bottom<0) {
		// 	margin_bottom = 0;
		// }
		// margin_bottom = margin_bottom+'px';
		// // alert(margin_bottom);
		// $('#content img.alignleft').css({'margin-bottom': margin_bottom});
		
	}
	
	
	if ($('body.portfolio').length == 1) {
		$('#portfolio .item').hover(function(){
			$(this).find('img:last').stop(true, true).fadeOut(200);
			$(this).find('.excerpt').stop(true, true).animate({bottom: -94}, 200);
		}, function(){
			$(this).find('img:last').stop(true, true).fadeIn(200);			
			$(this).find('.excerpt').stop(true, true).animate({bottom: 0}, 200);
		});
		
		$('#portfolio .item').click(function(){
			window.location = $(this).find('h3 > a').attr('href');
		});
		
		$('#sortnav a').click(function(){
			if ($(this).attr('rel') != '0') {
				$('.oldfolio').hide();
				var id = $(this).attr('rel');
				$('#portfolio .item:not(.i' + id + ')').fadeOut(200);
				$('#portfolio .item.i' + id).delay(200).fadeIn(250);
			} else {
				$('#portfolio .item[style*=display]').each(function(idx, el){
					el.style.display='';
				});
				$('#oldfolio-old').show();
			}
			$('#sortnav .active').removeClass('active');
			$(this).addClass('active');
			Cufon.replace('#sortnav a');
			return false;
		});
		
		$('#oldfolio-old').click(function(){
			$('#portfolio .item:not(.archive)').addClass('new-hidden');
			$('#portfolio .item.archive').addClass('archive-visible');
			$(this).hide();
			$('#oldfolio-new').show();
			return false;
		});
		
		$('#oldfolio-new').click(function(){
			$('#portfolio .item.new-hidden').removeClass('new-hidden');
			$('#portfolio .item.archive-visible').removeClass('archive-visible');
			$(this).hide();
			$('#oldfolio-old').show();
			return false;
		});
		
	}
	
	
	if ($('body.portfolio-projekt').length == 1) {
		$('.portfolio-projekt #images .image:first').css({opacity:1}).addClass('active');
		$('#images .nav a').hover(function(){
			$(this).stop().animate({opacity: 0.7}, 500)
		}, function(){
			$(this).stop().animate({opacity: 0}, 700);
		});
		$('#images .nav a').click(function(){
			clearTimeout(folio_loop_);
			if ($(this).hasClass('right')) {
				// $('#images .nav a').stop().css({opacity: 0});
				if ($('#images .set').css('left') == (-960 * ($('#images .image').length-1))+'px') {
					$('#images .set').animate({left: 0}, 500);
					$('#images .set .image.active').animate({opacity: 0.1}, 500).removeClass('active');
					$('#images .set .image:first').addClass('active').animate({opacity: 1}, 500);
				} else {
					$('#images .set').animate({left: '-=960'}, 500);
					$('#images .set .active').animate({opacity: 0.1}, 500);
					$('#images .set .active').next().animate({opacity: 1}, 500);
					$('#images .set .active').removeClass('active').next().addClass('active');
				}
			}
			if ($(this).hasClass('left')) {
				if ($('#images .set').css('left') == '0px') {
					$('#images .set').animate({left: (-960 * ($('#images .image').length-1))}, 500);
					$('#images .set .image.active').animate({opacity: 0.1}, 500).removeClass('active');
					$('#images .set .image:last').addClass('active').animate({opacity: 1}, 500);					
				} else {
					$('#images .nav a').stop().css({opacity: 0});
					$('#images .set').animate({left: '+=960'}, 500);
					$('#images .set .active').animate({opacity: 0.1}, 500);
					$('#images .set .active').prev().animate({opacity: 1}, 500);
					$('#images .set .active').removeClass('active').prev().addClass('active');
				}
			}
			return false;
		});
		
		folio_loop_ = setTimeout('folio_loop()', 3000);
		
	}
	
	if ($('body.kontakt').length == 1) {
		$('input').each(function(){
			$(this).attr('rel', $(this).val());
		});
		$('textarea').attr('rel', $('textarea').text());
		
		$('input').focus(function() {
			$(this).val('');
		});
		$('input').blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('rel'));
			}
		});
		
		
		$('textarea').focus(function() {
			$(this).text('');
		});
		$('textarea').blur(function() {
			if ($(this).text() == '') {
				$(this).text($(this).attr('rel'));
			}
		});
		
		$('form').submit(function(){
			if ($(this).val() == $(this).attr('rel')) {
				$(this).val('');
			}
		});
	}
	
});

function frontLoop(){
	var offset = -960;
	var slider = $('#slider');
	var inner = slider.find('.inner');

	if (parseInt(inner.css('left')) == -1*(slider.find('.project').length-1) * 960) {
		inner.css({left: 0});
	}
	
	inner.animate({left: '+='+offset}, 600);
	if ($.browser.msie && $.browser.version.substr(0,1)==7) {
	} else {
		slider.find('.description').slideToggle(220).slideToggle(380);
		Cufon.replace(slider.find('.description h4, .description a'));
	}
	
	front_loop = setTimeout('frontLoop()', 7000);
}

function logaLoop(){
	var current = $('.home #content .dynamic-col-3 a:visible');
	current.fadeOut(450)
	if (current.next().length != 0) {
		current.next().fadeIn(500);
	} else {
		$('.home #content .dynamic-col-3 a:first').fadeIn(500);
	}
	
	setTimeout("logaLoop()", 3000);
}

function shiftedLoop(){
	var current = $('#content .shifted img:visible');
	current.fadeOut(900)
	if (current.next().length != 0) {
		current.next().fadeIn(1500);
	} else {
		$('#content .shifted img:first').fadeIn(500);
	}
	
	setTimeout("shiftedLoop()", 7000);
}

function folio_loop(){
	if ($('#images .set').css('left') == (-960 * ($('#images .image').length-1))+'px') {
		$('#images .set').animate({left: 0}, 500);
		$('#images .set .image.active').animate({opacity: 0.1}, 500).removeClass('active');
		$('#images .set .image:first').addClass('active').animate({opacity: 1}, 500);
	} else {
		$('#images .set').animate({left: '-=960'}, 500);
		$('#images .set .active').animate({opacity: 0.1}, 500);
		$('#images .set .active').next().animate({opacity: 1}, 500);
		$('#images .set .active').removeClass('active').next().addClass('active');
	}
	folio_loop_ = setTimeout('folio_loop()', 3000);
}
