
/**
 * fix height
 */

var pageHeight = false;
var fixHeight = function() {
	var windowHeight = $(window).height()-12;
	if (pageHeight == false)
		pageHeight = $('#bg_bottom').height();
	if (pageHeight == windowHeight) {
		return;
	} if (pageHeight < windowHeight) {
		$('#bg_bottom').css('height', windowHeight+'px');
	} else {
		$('#bg_bottom').css('height', '');
	}
};

$(document).ready(fixHeight);
$(window).resize(fixHeight);


/**
 * image changer
 */

var imageChanger = {
	
	// current group
	group: null,
	
	// fade timeout
	timeout: null,
	
	// start changing images
	start: function(group) {
		group -= 1;
		if (this.group !== null) {
			this.resetGroup(this.group);
		}
		this.group = group;
		var firstLink = $('#group'+group+' a.image0');
		this.startTimeout(firstLink);
	},
	
	// start timeout on active image
	startTimeout: function(el) {
		this.active = el;
		this.timeout = window.setTimeout(function() {
			imageChanger.transition(el);
		}, 5000);
	},
	
	// transition to next image
	transition: function(current) {
		// find next link
		var next = current.next();
		if (next.length == 0) {
			var siblings = current.parent().children();
			if (siblings.length == 1) {
				return false;
			}
			next = siblings.eq(0);
		}
		// show image
		var fade = function() {
			var _next = next.show();
			var _current = current;
			current.fadeOut(1500, function() {
				_current.css('zIndex', 400);
				_next.css('zIndex', 401);
				imageChanger.startTimeout(_next);
			});
		};
		// load image
		var span = next.find('span');
		if (span.length > 0) {
			var newImg = new Image;
			newImg.onload = function() {
				var nextImg = $(document.createElement('img')).attr('src', this.src);
				next.hide().css('zIndex', 400).html(nextImg);
				fade();
			};
			newImg.src = span.html()
					.replace('&amp;', '&')
					.replace(/^[\s]*/m, '')
					.replace(/[\s]*$/m, '');
		} else {
			fade();
		}
	},
	
	// stop fading
	stop: function() {
		if (this.timeout !== null)
			window.clearTimeout(this.timeout);
	},
	
	// reset group to first image
	resetGroup: function(group) {
		$('#group'+group+' a').css('zIndex', 400).stop(true).css('opacity', 1).hide();
		$('#group'+group+' a.image1').show();
		$('#group'+group+' a.image0').show().css('zIndex', 401);
	}
	
};


/**
 * portfolio services
 * akkordeon content
 */

$(document).ready(function() {
	var ie6 = navigator.userAgent.toLowerCase().indexOf("msie 6.") >= 0;
	var show = function(el) {
		if (ie6) el.show();
		else el.show('blind', {}, 400);
	};
	var hide = function(el) {
		if (ie6) el.hide();
		else el.hide('blind', {}, 400);
	};
	$('#port_main .service_head').css({
		'cursor': 'pointer'
	}).click(function() {
		$('#lefttip').hide();
		var nextContent = $(this).next().find('.service_content');
		var openNew = !nextContent.is(':visible');
		hide($('#port_main .akkordeon_on').removeClass('akkordeon_on')
				.next().find('.service_content'));
		if (openNew) {
			hide($('#port_main .akkordeon_text:visible'));
			show(nextContent);
			$(this).addClass('akkordeon_on');
		}
	}).eq(0).trigger('click');
	$('#lefttip').show();
});


/**
 * portfolio navigation
 */

$(document).ready(function() {
	
	/**
	 * change portfolio item
	 */
	$('#port_nav li').each(function(pi) {
		var i = pi+1;
		$(this).click(function() {
			$('#lefttip').hide();
			var el = $(this).blur();
			if (!el.hasClass('active')) {
				el.blur().parent().find('.active').removeClass('active').css('opacity', 0.5);
				el.addClass('active');
				imageChanger.stop();
				var group = i;
				$('#port_slider').stop(true).animate({marginLeft: (-606*i)+'px'}, 500, function() {
					imageChanger.start(group);
				});
			}
			return false;
		}).css('opacity', 0.5).hover(function() {
			if (!$(this).hasClass('active'))
				 $(this).stop(true).animate({opacity: 1}, 'fast');
		}, function() {
			if (!$(this).hasClass('active'))
				 $(this).stop(true).animate({opacity: 0.5}, 'fast');
		});
	});
	
	$('#port_nav').data('index', 0);
	var numItems = $('#port_nav li').length;
	
	// next button
	$('#port_next2').click(function() {
		$(this).blur();
		
		var nav = $('#port_nav');
		var i = parseInt(nav.data('index'), 10);
		var items = numItems;
		
		if (i + 5 >= items) {
			// end reached
			$('#port_next2').hide();
			return false;
		} else if (i + 10 < items) {
			//scroll full interval
			i += 5;
		} else {
			// scroll to end
			$('#port_next2').hide();
			i = items - 5;
		}
		
		nav.data('index', i);
		nav.stop(true).animate({
			marginLeft: (-128*i)+'px'
		}, 'normal');
		
		$('#port_prev2').show();
		
		return false;
	});
	if (numItems <= 5) {
		$('#port_next2').hide();
	}
	
	// prev button
	$('#port_prev2').click(function() {
		$(this).blur();
		
		var nav = $('#port_nav');
		var i = parseInt(nav.data('index'), 10);
		var items = numItems;
		
		if (i <= 0) {
			// end reached
			$('#port_prev2').hide();
			return false;
		} else if (i >= 5) {
			//scroll full interval
			i -= 5;
			if (i == 0) $('#port_prev2').hide();
		} else {
			// scroll to beginning
			$('#port_prev2').hide();
			i = 0;
		}
		
		nav.data('index', i);
		nav.stop(true).animate({
			marginLeft: (-128*i)+'px'
		}, 'normal');
		
		$('#port_next2').show();
		
		return false;
	}).hide();

	
	if (window.location.href.indexOf('Fotografie') > 0) {
		// no service list for photography
		$('#port_nav li:first').trigger('click').css('opacity', 1);
	}
});



/**
 * contact form
 */

function contactform_check() {
	var felder = {
		"name": "Name", "e-mail": "E-Mail"
	};
	
	var error  = new Array();
	var values = new Array();
	for (key in felder) {
		val = document.getElementById("contactform_"+key).value;
		if (val == "" || val == " ") {
			error.push(felder[key]);
		}
	}
	
	var errorHTML = new Array();
	if (error.length > 0) {
		document.getElementById("contactform_error").innerHTML = "<b>Sie haben folgende Pflichtfelder nicht ausgef&uuml;llt:</b> "+error.join(", ")+".";
		document.getElementById("contactform_error").style.display = "block";
	} else {
		document.getElementById("contactform").action = "";
		document.getElementById("contactform").submit();
	}
}
