var base = 'http://bauhaus.com';
var base = 'http://bauhaus.com';
var base = 'http://bauhaus.com';

var scroller = new Scroller();

$(document).ready(function() {
	Slideshow();
	animate();
	
	scroller.set('.slide');
	
	$('.page').click(function() {
		win($(this).attr('rel'));
	});
});

function animate() {
	$('#iron_curtain')
		.show()
		.animate({opacity: 0.5}, 1000);
	$('#main')
		.show()
		.animate({opacity: 0}, 600)
		.animate({opacity: 1}, 1400);
}

function win(rel) {
	$.get(base + '/pages/' + rel +'.html', function(data) {
		var win = $('#page').html(data);
		
		win.show().animate({opacity: 0.98}, 1000);
		
		$('.scroller').css('position', 'absolute');
		scroller.set('#dummy3');
		
		$('.close').click(function () {
			
			$('#page').fadeOut('normal', function() { $(this).hide(); });
				//.animate({opacity: 0}, 2000)
				//.animate({display: 'none'}, 1000);
				
			scroller.set('.slide');
		});
	});
}
function Slideshow() {
	var currentPosition = 0;
	var slideWidth = 800;
	var slides = $('.slide');
	var numberOfSlides = slides.length;
	
	if ($('.slide').eq(currentPosition).attr('rel') == 'scroll') {
		$('.scroller').show();
	} else {
		$('.scroller').hide();
	}
	
	// Remove scrollbar in JS
	$('#slidesContainer').css('overflow', 'hidden');

	// Wrap all .slides with #slideInner div
	
	slides
		.wrapAll('<div id="slideInner"></div>')
		// Float left to display horizontally, readjust .slides width
		.css({ 'float' : 'left', 'width' : slideWidth });

	// Set #slideInner width equal to total width of all slides
	$('#slideInner').css('width', slideWidth * numberOfSlides);

	// Insert left and right arrow controls in the DOM
	$('#slideshow')
		.prepend('<span class="control" id="leftControl">Move left</span>')
		.append('<span class="control" id="rightControl">Move right</span>');

	// Hide left arrow control on first load
	manageControls(currentPosition);

	// Create event listeners for .controls clicks
	$('.control')
		.bind('click', function(){
			// Determine new position
			currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
			
			// Hide / show controls
			manageControls(currentPosition);

			// Move slideInner using margin-left
			$('#slideInner').animate({ 'marginLeft' : slideWidth*(-currentPosition) });
			
			if ($('.slide').eq(currentPosition).attr('rel') == 'scroll') {
				scroller.set('.slide');
				$('.scroller').show();
			} else {
				$('.scroller').hide();
			}
		});
	
		// manageControls: Hides and shows controls depending on currentPosition
	function manageControls(position){
		// Hide left arrow if position is first slide
		if(position==0) {
			$('#leftControl').hide();
		} else {
			$('#leftControl').show();
		}
	
		// Hide right arrow if position is last slide
		if(position==numberOfSlides-1) {
			$('#rightControl').hide();
		} else {
			$('#rightControl').show();
		}
	}
}
function Scroller() {
	var self = this;
	var b;
	
	this.set = function (b) {
		self.b = b;

		var t = $(b).find('.content').height(); // + $('.header').height();
		var s = $(b).find('.scroller').height(); //520;
		var bar = $(b).find('.bar');

		$(b).find('.pane').scrollTop(0);
		$(b).find('.bar').css('top', 0);

		if (t > s) {
			var p = s; //520;
			var b = p / t * s;

			bar.css('height', b);
			bar.show();
			bar.draggable({
				axis: 'y',
				containment: 'parent',
				drag: self.scroll
			});
    		$('.pane').mousewheel(self.wheel);
		} else {
			$(b).find('.scroller').hide();
			bar.hide();
		}
	}
	this.scroll = function (e) {
		//var s = 520;
		var s = $(self.b).find('.scroller').height(); //520;
		var b = $(self.b).find('.bar').height();
		var y = $(self.b).find('.bar').position().top;
		var m = y / (s - b);
		
		var t = $(self.b).find('.content').height();
		var p = s - 60; //480;
		var y2 = m * (t - p);
		
		$(self.b).find('.pane').scrollTop(y2);
	}
	this.wheel = function (e, d) {
		var s = d > 0 ? -10 : 10;
		var t = 0;
		var b = $(self.b).find('.scroller').height(); // - $(self.b + ' .bar').height();
		var y = $(self.b).find('.bar').position().top;
		
		c = y + s;
		//alert(s + ' - ' + t + ' - ' + b + ' - ' + y + ' - '+c);
		
		if (d > 0) { // Up
			if (y + s >= 0) {
				y = y + s;
			} else if (y < t) {
				y = t;
			}
		} else { // Down
			if (y + s <= b) {
				y = y + s;
			} else if (y < b) {
				y = b;
			}
		}
		//alert(y);
		$(self.b).find('.bar').css('top', y);
		self.scroll(e);
//		$('.pane').scrollTop(y);



	}
}