var isPaused = false;



function slideSwitch() {
    var $active = $('#picture IMG.active');

    if ( $active.length == 0 ) $active = $('#picture IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#picture IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

var slideshow = {
	active: $('#picture .active'),
	current: 0,
	elements:$('#picture img'),
	interval:null,
	
	init : function(autoplay) {
		this.current = 0;
		this.elements = $('#picture img');
		
		if(autoplay && slideshow.elements.length > 1) {
			slideshow.autoPlay();
		}
	},
	
	next : function() {	
		$('#picture .active').fadeOut('slow', function(el) {
			$('#picture .active').removeClass('active');
			
			if(slideshow.current < slideshow.elements.length-1) {
				slideshow.current++;
			} else {
				slideshow.current = 0;
			}
			
			$('#pictureIndicator').animate({
				'marginLeft' : Math.ceil((slideshow.current*30)+15)+'px'
			});
			
			$(slideshow.elements[slideshow.current]).fadeIn('slow', function() {
				$(slideshow.elements[slideshow.current]).addClass('active');
			});
		});
		
		return slideshow.current;		
	},
	
	goto : function(num) {	
		slideshow.stop();
		$('#picture .active').fadeOut('slow', function(el) {
			$('#picture .active').removeClass('active');
			
			if(num < slideshow.elements.length) {
				slideshow.current = num;
			} else {
				slideshow.current = 0;
			}
			
			$('#pictureIndicator').animate({
				'marginLeft' : ((slideshow.current*29)+15)+'px'
			});
			
			$(slideshow.elements[slideshow.current]).fadeIn('slow', function() {
				$(slideshow.elements[slideshow.current]).addClass('active');
			});
		});
		
		return slideshow.current;		
	},
	
	autoPlay : function() {
		slideshow.interval = setInterval('slideshow.next()', 4000);
	},
	
	stop : function() {
		if(slideshow.interval != null) {
			clearInterval(slideshow.interval);
		}
	}
};

var menuIndicator = {
	marginTop: 84,
	el: null,
	current: 1,
	steps: 20,
	
	calculateTop : function(item) {
		return (this.marginTop + (item*this.steps));
	},
	
	setTo : function(item) {
		this.current = item;
		this.el.css('top',this.calculateTop(item)+'px');
	},
	
	animateTo : function(item) {
		this.el.animate({'top':this.calculateTop(item)+'px'}, 100);
	},
	
	reset : function() {
		this.animateTo(this.current);
	}
};

function removeIntro() {
	if($('#intro').length <= 0) return false;
	$('#intro').fadeOut('slow', function(){
		$('#intro').remove();
	});
}

$(function(){
	
	menuIndicator.el = $('#activeIndicator');
	menuIndicator.setTo(84);
	
	// Slideshow
	slideshow.init(true);
	
	$('#nav a').bind('mouseover',function(el){
		var num = $(el.currentTarget).attr('id').substr(5);
		menuIndicator.animateTo(num);
	});
	
	$('#nav').bind('mouseleave',function(el){
		menuIndicator.reset();
	});
		
	$('#content').jScrollPane();
	
	//slideshow.play();
	
});
