frommelt = window.frommelt || {
	fotonovella: {
		interval: 5 //seconds
	},
	mapframe: ''
};




/**
 * Custom CSS when JavaScript is enabled.
 */

document.write('<style type="text/css">');
document.write('.frommelt-project-home #subnavigation ul ul, .frommelt-project #subnavigation ul ul { display: none; }');
document.write('<\/style>');


(function($) {
	
	
	/**
	 * Document ready
	 */

	$(function() {

        for (var k in $.browser) {
            if (k == 'version') continue;
            if ($.browser[k]) {
                $(document.documentElement).addClass(k);
            }
        }
		
		$('#flow').flow();
		$('.image-viewer').viewer();
		
		$('.sidebar-banner').teaser();
		
		$('.frommelt-home .banner').fotonovella();
		$('.fotonovella').fotonovella();
		
		$('.frommelt-project-home #subnavigation, .frommelt-project #subnavigation').projectnavigation();
		
		// Initialize the different preloaders seperatly
		// (otherwise the content images will load after the flow image)
		$('#content .wall-preloader').wallpreloader();
		$('#flow .wall-preloader').wallpreloader();
		
		$('#dock').dock();
		
		$('#dock .dock-panel.contact>.content>.form').contact();
		
		
		/**
		 * Livesearch
		 */
		
		$('#dock .search input').oiLivesearch('/home',{
			container: 'livesearch-results',
			willblur: function() {
				return false;
			},
			didshow: function() {
				$('#dock').css({
					height: 'auto'
				});
			}
		});
	});
	
	
})(jQuery);



/**
 * "Wall" image preloader
 */ 

(function($) {
	
	$.fn.wallpreloader = function() {
			
		var t = 100;
			
		return this.each(function() {
					
			var $item = $(this);
			
			setTimeout(function() {
			
				var $src = $item.next('.img-src');
			
				var img = new Image();
				img.onload = function() {
								
					var top = 0;
					var left = 0;
				
					var w = $item.outerWidth();
					var h = $item.outerHeight();
				
					if (img.width > w) {
						left = parseInt((img.width-w)/-2);
					}
					if (img.height > h) {
						top = parseInt((img.height-h)/-2);						
					}
				
					$item.append($(img).css({
						top: top+'px',
						left: left+'px',
						display: 'none'
					}).fadeIn(1000));
				};
				img.src = $src.attr('href');
			
				$item.hover(
					function() {
						$(img).stop().fadeTo(100,0);
					},
					function() {
						$(img).stop().fadeTo(300,1);
					}
				);
				$src.remove();
			
			},t);
			t += 100;
			
		});
	}
	
})(jQuery);


/**
 * Project navigation
 */

(function($) {
		
	$.fn.projectnavigation = function() {
		
		return this.each(function() {
			
			var $nav = $(this);
						
			var $level1 = $nav.find('.level1>li>a.toggle');
			var $level2 = $nav.find('.level2>li>a.toggle');
			
			$level1.click(function() {
				if ($(this).parent().find('ul:first').filter(':visible').length) {
					$(this).parent().find('ul:visible').hide('blind',100);
				}
				else {
					$(this).parent().find('ul:first').show('blind',100);
				}
				return false;
			});

			$level2.click(function() {
				$(this).parent().find('ul').toggle('blind',100);
				return false;
			});
			
			$nav.find('.link').click(function() {
				var index = $nav.find('.link').index($(this));
				window.location.href = this.href+'#'+index;
				return false;
			});
			
			
			var loc = window.location.href.substring(10);
			loc = loc.substring(loc.indexOf('/')).split('?')[0].split('#')[0].replace('.html','');
			$('#subnavigation').find("a.link[href='"+loc+"']").addClass('selected');
			
			
			
			var hash = window.location.hash.replace('#','');
			if (hash) {
				var index = parseInt(hash);
				if (!isNaN(index)) {
					var $a = $nav.find('.link').eq(index);
					$a.parents('ul').show();
				}
			}
		});
		
	}
		
})(jQuery);




/**
 * "Coverflow" plugin.
 */

(function($) {
	
	$.fn.flow = function() {
		
		
		return this.each(function() {
		    
		    
		    
		    
			var $obj = $(this);
			
			$obj.append('<a href="#prev" class="prev">&lang;</a><a href="#next" class="next">&rang;</a>');
			
			var $items = $obj.find('li>a:first-child');
			
			if ($items.length <= 1) {
				return;
			}
			
			
			var itemWidth = $items.eq(0).outerWidth();
			var flowWidth = itemWidth * $items.length;
			var containerWidth = $obj.width();
			
			
			var $prev = $obj.find('.prev');
			var $next = $obj.find('.next');
			
			var $ul = $obj.find('ul');
			
			
			var flowing = true;
			var flowInterval = setInterval(function() {
			    if (!flowing) return;
		        var left = $ul.position().left;
		        left -= 1;
		        $ul.css({left:left+'px'});
		        
		        validate_buttons();
		    },20);
		    
		    


			var validate_buttons = function(left) {
				if (!left && left!==0) left = $ul.position().left;
				if (left >= 0) {
					($.browser.msie && $prev.hide() || $prev.fadeOut(500));
				}
				else {
					($.browser.msie && $prev.show() || $prev.fadeIn(500));
				}
				
				if (left + flowWidth <= containerWidth) {
				    if (flowing) {
				        flowing = false;
				        clearInterval(flowInterval);
				    }
					($.browser.msie && $next.hide() || $next.fadeOut(500));
				}
				else {
					($.browser.msie && $next.show() || $next.fadeIn(500));
				}
			}
			setTimeout(function() {
				validate_buttons();
			},500);
			
			var sliding = false;
			var slide = function(dir) {
			    			    
				if (sliding) return;
				
				sliding = true;
				
				var left = $ul.position().left;
				
			    if (flowing) {
                    left -= left%itemWidth;
			        flowing = false;
    				clearInterval(flowInterval);
			    }
			    
				var oldLeft = left;
				
				left += -1 * dir * 4*itemWidth;
				
				if (left > 0) left = 0;
				if (left + flowWidth < containerWidth) left = containerWidth-flowWidth;

				validate_buttons(left);
				
				var diff = Math.abs(oldLeft-left);
				
				var dur = diff;
				
				$ul.animate({left: left+'px'},dur, function() {
					sliding = false;
				});
			}
			
			$prev.click(function() {
				slide(-1);
				return false;
			});
			$next.click(function() {
				slide(1);
				return false;
			});
						
									
		});
		
	}
	
})(jQuery);



/**
 * Teaser plugin
 */

(function($) {

	$.fn.teaser = function() {
		
		
		return this.each(function() {

			var $obj = $(this);
			

			var $teasers = $obj.find('.teaser');
		
			if ($teasers.length <= 1) return; // No slide show.

			var current = 0;

			var next = function() {
			
				var $old = $teasers.eq(current);
			
				current++;
				if (current==$teasers.length) current = 0;
			
				var $new = $teasers.eq(current);

				var x = $obj.width();
			
			

				$new.css({top: 0, left:x+'px'}).show().animate({left:0},1000);
			
				$old.animate({left: '-'+x+'px'},1000,function() {
					$old.hide();
				});
			
			}


			setInterval(function() {
				next();
			},frommelt.fotonovella.interval*1000);
			
			$teasers.each(function() {
				var $a = $(this);
				if ($a.attr('href') == '#') {
					$a.css({cursor:'default'}).click(function() {
						return false;
					});
				}
			});
			
			
			/**
			 * Preload the images
			 */
			
			var t = 200;
			var skip = true;
			$teasers.find('.preload').each(function() {
				var $img = $(this);
				var src = $img.text();
				$img.empty().css({backgroundImage:'url('+src+')'}).show();
				$img.parent().show();
				$img.parent().hide();
			});
			
			
			
		});
		
	}
	
})(jQuery);



/**
 * Fotonovella plugin
 */

(function($) {
	
	$.fn.fotonovella = function() {
		
		
				
		return this.each(function() {
			var $obj = $(this);
					
			$obj.append('<a href="#prev" class="prev">&lang;</a><a href="#next" class="next">&rang;</a><a href="#play" class="play"></a>');

			var $prev = $obj.find('.prev');
			var $next = $obj.find('.next');
			
			// Avoid the outline in IE
			$prev.focus(function() { $(this).blur(); });
			$next.focus(function() { $(this).blur(); });
		
			var intervalID = 0;
		
		
			var $medias = $obj.find('.media');
		
			if ($medias.length <= 1) return;
			
			if ($medias.eq(0).find('.video').length) {
				$obj.find('.play').show().get(0).onclick = function() {
					$medias.eq(0).find('.image').click();
					return false;
				};
			}
		
			setTimeout(function() {
				if ($.browser.msie) {
					$next.show();
				}
				else {
					$next.fadeIn(500);
				}
			},500);
		
			var current = 0;
		
			var animating = false;
			var slide = function(dir) {
						
				if (animating) return;
				animating = true;
			
				var $old = $medias.eq(current);
				current += dir;
			
				if (current == $medias.length) {
					current = 0;
				}
			
				var $new = $medias.eq(current);
				
				$obj.find('.play').hide();
			
			
				if ($.browser.msie) {
					if (current == 0)					$prev.hide();
					else 								$prev.show();			
					if (current+1 == $medias.length)	$next.hide();
					else								$next.show();
				}
				else {
					if (current == 0)					$prev.fadeOut(500);
					else 								$prev.fadeIn(500);			
					if (current+1 == $medias.length)	$next.fadeOut(500);
					else								$next.fadeIn(500);
				}

				var x = $obj.width();
			
			
				var oldX = (dir<0?'':'-')+x+'px';
				var newX = (dir<0?'-':'')+x+'px';

				$new.find('.image').show();
				$new.css({top: 0, left: newX}).show().animate({left:0},1000);
		
				$old.animate({left:oldX},1000,function() {
					$old.hide();
					$old.find('object,embed').hide();
					if ($new.find('.video').length) {
						$obj.find('.play').show().get(0).onclick = function() {
							$new.find('.image').click();
							return false;
						};
					}
					animating = false;
				});
				
			}
		
			$prev.click(function() {
				clearInterval(intervalID);
				slide(-1);
				return false;
			});
		
			$next.click(function() {
				clearInterval(intervalID);
				slide(1);
				return false;
			});
		
			intervalID = setInterval(function() {
				slide(1);
			},frommelt.fotonovella.interval*1000);
			
			
			/**
			 * Preload the images
			 */
			
			var t = 200;
			var skip = true;
			$medias.find('.preload').each(function() {
				var $img = $(this);
				var src = $img.text();
				$img.empty().css({backgroundImage:'url('+src+')'}).show();
				$img.parent().show();
				$img.parent().hide();
			});
			
			
			/**
			 * Videos.
			 */
			
			$obj.find('.flowplayer').click(function() {
				var $video = $(this);
				clearInterval(intervalID);
				$obj.find('.play').hide();
				if ($video.hasClass('flowplayer')) {
					$video.removeClass('flowplayer');
					$video.flowplayer('/docroot/frommelt/js/flowplayer/flowplayer-3.1.3.swf');
				}
				else {
					$video.find('object,embed').show();
				}
				return false;
			});
					
		});
	}
	
})(jQuery);


/**
 * Image viewer plugin
 */

(function($) {
	
	$.fn.viewer = function() {
		
		var $thumbnails = this;
		var $current = null;
				
		var $_viewer = null;
		var $viewer = function() {
			if (!$_viewer) {
				$_viewer = $("<div/>").attr('id', 'viewer').append(
					$("<div/>").addClass('background').click(function() {
						_close();
					}),
					$("<div/>").addClass('content').append(
						$("<div/>").addClass('image'),
						$("<div/>").addClass('legend'),
						$("<a/>").attr('href','#prev').addClass('prev').addClass('button').html('&lang;'),
						$("<a/>").attr('href','#next').addClass('next').addClass('button').html('&rang;'),
						$("<a/>").attr('href','#close').addClass('close').addClass('button').html('&times;')
					)
				).appendTo('body');
			}
			return $_viewer;
		}

		var $content = function() {
			return $viewer().children('.content');
		}
		var $image = function() {
			return $content().children('.image');
		}
		var $legend = function() {
			return $content().children('.legend');
		}
		
		
		var $prev = function() {
			return $content().children('.prev');
		}
		var $next = function() {
			return $content().children('.next');
		}
		var $close = function() {
			return $content().children('.close');
		}
		
		
		
		var _close = function() {
			$current = null;
			$close().hide();
			$prev().hide();
			$next().hide();
			$viewer().fadeOut();
		}
		
		var _validateButtons = function() {
			if ($current) {
				var index = $thumbnails.index($current);

				if (index > 0) {
					$prev().show();
				}
				else {
					$prev().hide();
				}
				
				if (index+1 < $thumbnails.length) {
					$next().show();
				}
				else {
					$next().hide();
				}
			}
		}
		
		
		$content().hover(
			function() {
				$close().css({visibility:'visible'});
				$prev().css({visibility:'visible'});
				$next().css({visibility:'visible'});
			},
			function() {
				$close().css({visibility:'hidden'});
				$prev().css({visibility:'hidden'});
				$next().css({visibility:'hidden'});
			}
		);
		
		
		$close().click(function() {
			_close();
			return false;
		});
		
		
		$prev().click(function() {
			$current = $thumbnails.eq($thumbnails.index($current)-1);
			_validateButtons();
			$current.click();
			return false;
		});
		
		$next().click(function() {
			$current = $thumbnails.eq($thumbnails.index($current)+1);
			_validateButtons();
			$current.click();
			return false;
		});
		
		var layout = function(do_layout) {
			
			var outerWidth = $viewer().width();
			var outerHeight = $viewer().height();
			
			var width = $content().outerWidth();
			var height = $content().outerHeight();
			
			var top = parseInt((outerHeight - height) / 3);
			var left = parseInt((outerWidth - width) / 2);
			
			if (do_layout) {
				$content().css({
					top: top+'px',
					left: left+'px'
				});
			}
			
			return {
				top: top+'px',
				left: left+'px'
			};
			
		}
		
		return this.each(function() {
			
			var $obj = $(this);
			var $img = null;		
			
			$obj.click(function() {

				var loaded = !!$current;

				$current = $obj;

				var $thumb = $obj.children('img');
				
				var _setup = function() {
					var pos = $thumb.offset();
					var scrollTop = $(document).scrollTop();
					var scrollLeft = $(document).scrollLeft();
					
					var w = $thumb.get(0).width;
					var h = $thumb.get(0).height;
					
					$content().css({
						left: (pos.left-scrollLeft-5)+'px',
						top: (pos.top-scrollTop-5)+'px'
					});
										
					$legend().hide();
					$viewer().fadeIn();
				}
				
				
				/**
				 * Load from thumbnail.
				 */
				
				var _grow = function() {
						
					var w = $img.get(0).width;
					var h = $img.get(0).height;
					
										
					$img.css({
						width: $image().width()+'px',
						height: $image().height()+'px'
					});
					$image().html($img);
					
					
					
					var outerWidth = $viewer().width();
					var outerHeight = $viewer().height();

					var width = w + 10;
					var height = h + 70;

					var top = parseInt((outerHeight - height) / 3);
					var left = parseInt((outerWidth - width) / 2);
					
					$content().animate({
						top: top+'px',
						left: left+'px'
					},function() {
						$legend().fadeIn();

						$close().show();
						_validateButtons();
					})
					
					$img.animate({
						width: w+'px',
						height: h+'px'
					});
					
					$legend().html($obj.find('.legend').html());
					
				}
				
				
				/**
				 * Prev/Next navigation.
				 */
				
				var _nav = function() {

					
					var w = $img.get(0).width;
					var h = $img.get(0).height;
					
					var $oldImg = $image().find('img');
					
					var oldW = $image().width();
					var oldH = $image().height();
										
					$img.css({
						width: oldW+'px',
						height: oldH+'px',
						opacity: 0
					});
					$image().append($img);
					$oldImg.addClass('old');
					
					
					
					var outerWidth = $viewer().width();
					var outerHeight = $viewer().height();

					var width = w + 10;
					var height = h + 70;

					var top = parseInt((outerHeight - height) / 3);
					var left = parseInt((outerWidth - width) / 2);
					
					$content().animate({
						top: top+'px',
						left: left+'px'
					},function() {
						$legend().fadeIn();

						$close().show();
						_validateButtons();
					})
					
					$oldImg.animate({
						width:w+'px',
						height:h+'px'
					}, function() {
						$oldImg.removeClass('old').remove();
					});
					
					$img.animate({
						width: w+'px',
						height: h+'px',
						opacity: 1
					});
					
					$legend().html($obj.find('.legend').html());
					

					
				}
							

				if (!loaded) {
					$image().html($("<img/>").attr('src', $thumb.attr('src')));
					_setup();
				}
				
				if ($img == null) {
					var src = $obj.attr('href');
					var img = new Image();
					
					img.onload = function() {
						$img = $(img);
						loaded ? _nav(): _grow();
					}
					img.src = src;
				}
				else {
					setTimeout(function() {
						loaded ? _nav(): _grow();
					},200);
				}
				
				return false;
			});
		});
	}
	
})(jQuery);



/**
 * Dock
 */

(function($) {
	
	$.fn.dock = function() {
		
		
		
		var kRight = -45;
		
		return this.each(function() {
			
			var $dock = $(this);
			
			// Lazy loading of the embedded google map.
			var mapLoaded = false;
			$dock.find('.map a').click(function() {
				if (mapLoaded) {
					return true;
				}
				mapLoaded = true;
				$dock.find('.lazy-map').html(frommelt.mapframe);
			});
			
			
			var $items = $(this).find('a');
			
			var $panel = null;
			
			$items.each(function() {
				
				var $obj = $(this);
				
				$obj.click(function() {

					var hideOnly = $(this).hasClass('selected');
					$dock.find('.selected').removeClass('selected');
					
					
					var timer = 10;
					if ($panel) {
						$panel.animate({right:'-450px'}, 200, function() {
							$(this).hide();
						});
						timer = 200;
					}
					$panel = null;

					$dock.css({height:'auto'});
					
					if (hideOnly) {
						setTimeout(function() {
							$dock.css({width:'60px'});
						},timer);
						return false;
					}
					
					$dock.css({width: '370px'});

					$(this).addClass('selected');
					$panel = $dock.children('.'+$obj.parent().attr('class'));
					
					
					setTimeout(function() {
						$panel
							.css({
								right: '-400px',
								display: 'block'
							})
							
						$dock.css({height:($panel.outerHeight()+20)+'px'});
							
						$panel
							.animate({
								right: kRight+'px'
							}, 200, function() {
								$(this).find('input').focus();
							});
					},timer);
					return false;
				});
				
			});
			
		});
		
	}
	
})(jQuery);


/**
 * Contact form.
 */

(function($) {
	
	$.fn.contact = function() {
		return this.each(function() {
						
			var $container 	= $(this).parent();
						
			var $lastname	= $('<label for="cName">Name<\/label><input id="cName" class="required text" name="name" type="text"\/>');
			var $firstname	= $('<label for="cFirstname">Vorname<\/label><input id="cFirstname" class="required text" name="firstname" type="text" \/>');
			var $company	= $('<label for="cCompany">Firma<\/label><input id="cCompany" class="text" name="company" type="text"\/>');
			var $email		= $('<label for="cEmail">Email<\/label><input id="cEmail" class="required text" name="email" type="text"\/>');
			var $phone		= $('<label for="cPhone">Telefon<\/label><input id="cPhone" class="text" name="phone" type="text"\/>');
			var $msg		= $('<label for="cMsg">Nachricht<\/label><textarea id="cMsg" class="required textarea" rows="5" cols="10" name="msg"><\/textarea>');
			
			var $send		= $('<a class="send" href="#send">Senden<\a>');
			
			var $form = $("<form/>").attr('action','').attr('method','post').attr('id','dock-contact').append(
					$("<fieldset/>").append(
						$lastname,
						$firstname,
						$company,
						$email,
						$phone,
						$msg,
						$send
					)
				);
			
			$(this).html($form);
			
			$send.click(function() {
				$form.submit();
				return false;
			});
			
			$form.submit(function() {
				
				$form.find('.error').removeClass('error');
				
				var valid = true;
				$form.find('.required').each(function() {
					var val = $.trim($(this).val());
					if (val == '') {
						$(this).prev('label').addClass('error');
						valid = false;
					}
				});
				
				if (valid) {
					$.post('/home',$form.serialize(),function(ret) {
						if ($.trim(ret) == 'OK') {
							$container.find('.form').remove();
							$container.find('.success').fadeIn(200);
						}
					});
				}
				return false;
			});
			
		});
	}
	
})(jQuery);
