var picid;
var theNext = 0;
var timeTestimonial = 5000;
var aktiv;
var t;

$(document).ready(function() {
	if ($('.testimonialpic').length>=2) {
		$('#testimonial').append('<ul class="pagination" id="testimonialpagination"></ul>');
		$('.testimonialpic').each(
		   function (){
		   var uniqueNum = Math.floor( Math.random()*99999 );
		   $(this).attr("picid", uniqueNum);
		   $('#testimonialpagination').append('<li picid="'+ uniqueNum + '"></li>');
		   });
			
			$('.testimonialpic').not('.testimonialpic:first-child').hide();
			$('#testimonialpagination>li:first-child').addClass('active');
			
			$('#testimonialpagination>li').bind('click', function(){
				window.clearInterval(aktiv);

				picid = $(this).attr('picid');

				$('#testimonialpagination>li').removeClass('active');
				$('#testimonialpagination>li[picid=' + picid +']').addClass('active');
				
				$('.testimonialpic').fadeOut('slow', function(){
				});
				$('.testimonialpic[picid=' + picid +']').delay('slow').fadeIn('slow', function(){
				});
		});
		aktiv = window.setInterval("testimonialrandom()", timeTestimonial);
	}
	if ($('.projImages').length) {
		$('.projImages').each(
		   function (){
			if ($(this).children('img').length > 1){
				$(this).parent().append('<ul class="pagination"></ul>');
				$('img', this).each(
						function(){
							var uniqueNum = Math.floor( Math.random()*999999 );
							   $(this).attr("picid", uniqueNum);
							   $(this).parent().parent().children('.pagination').append('<li picid="'+ uniqueNum + '"></li>');
						   });
				
				$(this).children('img').not('img:first-child').hide();
				$(this).parent().children('.pagination').children('li:first-child').addClass('active');
				
				$(this).parent().children('.pagination').children('li').bind('click', function(){
					picid = $(this).attr('picid');
					$(this).parent().find('li').removeClass('active');
					$(this).parent().find('li[picid=' + picid +']').delay('fast').addClass('active');
					
					$(this).parent().parent().children('.projImages').children('img').fadeOut('slow', function(){
	
					});
					$('img[picid=' + picid +']').delay('slow').fadeIn('slow', function(){
					});
				});
			}
		});
	}

	if($('input#name').val() == "Name"){
		if ($('input#name').css('color') != 'rgb(255, 0, 0)'){
			$('input#name').css('color','#C9C9C9');
		}
	}
	if($('input#email').val() == "E-Mail Adresse"){
		if ($('input#email').css('color') != 'rgb(255, 0, 0)'){
			$('input#email').css('color','#C9C9C9');
		}
	}
	
	if($('textarea#message').val() == "Ihre Nachricht"){
		if ($('textarea#message').css('color') != 'rgb(255, 0, 0)'){
			$('textarea#message').css('color','#C9C9C9');
		}
	}
	$('input#name').focus(function(){
		if($(this).val() == "Name"){
			$(this).val("");
			$(this).css('color','#000000');
		}
	});
	$('input#name').focusout(function(){
		if($(this).val() == ""){
			$(this).val("Name");
			$(this).css('color','#C9C9C9');
		}
	});

	$('input#email').focus(function(){
		if($(this).val() == "E-Mail Adresse"){
			$(this).val("");
			$(this).css('color','#000000');
		}
	});
	$('input#email').focusout(function(){
		if($(this).val() == ""){
			$(this).val("E-Mail Adresse");
			$(this).css('color','#C9C9C9');
		}
	});
	
	
	$('textarea#message').focus(function(){
		if($(this).val() == "Ihre Nachricht"){
			$(this).val("");
			$(this).css('color','#000000');
		}
	});
	$('textarea#message').focusout(function(){
		if($(this).val() == ""){
			$(this).val("Ihre Nachricht");
			$(this).css('color','#C9C9C9');
		}
	});

	
	$('#footer').height(27);
	if ($(window).height() > ($('#content').offset().top + $('#content').height() + $('#footer').height() + 50) ){
		$('#footer').css({'position':'fixed'});
	}else{
		$('#footer').css({'position':'relative'});			
	}
	window.onresize = function (){
		if ($(window).height() > ($('#content').offset().top + $('#content').height() + $('#footer').height() + 50) ){
			$('#footer').css({'position':'fixed'});
		}else{
			$('#footer').css({'position':'relative'});			
		}
	}
	
	$('#impressum').bind('click', function () {
		if($('#footer').css('position') == 'fixed'){
			if($('#footer').height() == 27){
				$('#content').height(($('#footer').offset().top - $('#head').height() -40 ));
				$('#footer').css({'position':'relative'});
			}
		}
		if($('#footer').height() == 27){
			theRatio = parseInt((($(document).height() + $('#footer .page').height()+ 75) / $(document).height()) * 1000);
			var iheight = parseFloat($('#impressumContent').height());
			$('#footer').css({'height':parseFloat($('#impressumContent').height())+ 75});
			
			$('html, body').animate({scrollTop: $(document).height()},theRatio);
		}else{
			$('#footer').animate({'height':27},1000, function() {
			if ($(window).height() > ($('#content').offset().top + $('#content').height() + $('#footer').height() + 50) ){
				$('#footer').css({'position':'fixed'});
				$('#content').height('auto');
			}
				
			});
		}
	});
	t = setTimeout ("if ($(window).height() > ($('#content').offset().top + $('#content').height() + $('#footer').height() + 50) ){$('#footer').css({'position':'fixed'});}else{$('#footer').css({'position':'relative'});}",200);	

	$('body').css('backgroundColor','#EEEEEE');
	
});

function testimonialrandom(){
	$('.testimonialpic').fadeOut('slow', function(){
	});
	if((theNext + 1) == $('.testimonialpic').length){
		theNext = -1;
	}
	theNext++;
	$('#testimonialpagination>li').removeClass('active');
	$('#testimonialpagination>li:eq(' + theNext +')').addClass('active');
	$('.testimonialpic:eq(' + theNext +')' ).delay('slow').fadeIn('slow', function(){
	});
}
