/*
	this code links the nav menu (look left, right) with the scrolling image below it in #bilder
	if two images are present, it is assumed that the images will scroll continuously
		the two images are needed for continuity
	if one image is present in #bilder then the image should not be able to scroll past  edges
*/
var nav = {
	"step":10,
	"dy":0,
	"min_dy": $('#bilder img').width() * -1,
	"max_dy": 0,
	"multiplier":0,
	"timer_ms": 50,
	"should_animate": false, // true or false
	"selector":"#bilder",
	"continuous": true,
	
	animate:function( ) {
		dy = nav.multiplier * nav.step; // which way should we go on this move?
		nav.dy += dy; // where are we absolutely
		
		if ( nav.continuous == true ) {
			if ( nav.min_dy > nav.dy ) { nav.dy -= nav.min_dy; }
			if ( nav.dy > nav.max_dy ) { nav.dy += nav.min_dy; }	
		} else {
			if ( nav.dy > 0 ) { nav.dy = 0; }
			if ( nav.dy < ( $(window).width() + nav.min_dy ) ) { nav.dy = ($(window).width() + nav.min_dy); }
		}
		
		$( nav.selector ).css( {left:nav.dy} ); // move the 
		
		if ( nav.should_animate === true ) {
			setTimeout( nav.animate, nav.timer_ms * 1.1 );
		}			
	},
	
	look_far_left:function() {
/*		console.log('look_far_left');*/
		nav.should_animate = true;
		nav.multiplier = 3;
		nav.animate();
		$('#newestpanoramatemplatetop_r2_c04').attr({src:'images/newestpanoramatemplatetop_r2_c04_f7.gif'});
	},
	look_left:function() {
/*		console.log('look_left');*/
		nav.should_animate = true;
		nav.multiplier = 1;
		nav.animate();
		$('#newestpanoramatemplatetop_r2_c04').attr({src:'images/newestpanoramatemplatetop_r2_c04_f3.gif'});
	},
	look_center:function() {
/*		console.log('look_center');*/
		nav.should_animate = false;
		nav.multiplier = 0;
		$('#scroll').stop(); // stop animations
		$('#newestpanoramatemplatetop_r2_c04').attr({src:'images/newestpanoramatemplatetop_r2_c04.gif'});
	},
	look_right:function() {
/*		console.log('look_right');*/
		nav.should_animate = true;
		nav.multiplier = -1;
		nav.animate();
		$('#newestpanoramatemplatetop_r2_c04').attr({src:'images/newestpanoramatemplatetop_r2_c04_f5.gif'});
	},
	look_far_right:function() {
/*		console.log('look_far_right');*/
		nav.should_animate = true;
		nav.multiplier = -3;
		nav.animate();
		$('#newestpanoramatemplatetop_r2_c04').attr({src:'images/newestpanoramatemplatetop_r2_c04_f6.gif'});
	},
	setup:function() {
		//console.log('start');
		$('#newestpanoramatemplatetop_r2_c02').mouseover( nav.look_far_left ).mouseout( nav.look_center );
		$('#newestpanoramatemplatetop_r2_c03').mouseover( nav.look_left ).mouseout( nav.look_center );
		$('#newestpanoramatemplatetop_r2_c04').mouseover( nav.look_center ).mouseout( nav.look_center );
		$('#newestpanoramatemplatetop_r2_c05').mouseover( nav.look_right ).mouseout( nav.look_center );
		$('#newestpanoramatemplatetop_r2_c06').mouseover( nav.look_far_right ).mouseout( nav.look_center );
		if ( $( nav.selector + ' img').length == 2 ) {
			$( nav.selector ).css({left: nav.min_dy});
		} else {
			nav.continuous = false;
		}
	}
};

$(document).ready( nav.setup );
