var slides = {};
var current = {};
var rotationSlides = [];
var paused = false;
var delay = 2500;
var duration = 1000;
var auto;
var currentLevel = 0;
var moving = false;
var width = 0;
var height = 0;
var currentClicked = 0;
var nav = {};
var navitems = {};

function debug(str){
	if (typeof console != "undefined")
		console.log('[portal] : ' + str);
}

function play(id,direction,clicked){
	debug('processing play for ' + id);
	if(!slides[rotationSlides[id-1]]){
		debug('false 1');
		return false;
	}
	if(current.id){
		debug('current.id - ' + current.id);
		debug('moving - ' + moving);
		if(current.id == id || moving){
			debug('false 2');
			return false;
		}
	}
	
	$(navitems).each(function(index) {
		$(this).removeClass('active');
	});
	$(navitems[id-1]).addClass('active');
	
	move(rotationSlides[id-1],direction);
	if(!clicked)
		current.id = id;
	currentClicked = rotationSlides[id-1];
}

function transition(id,direction,clicked){
	debug('processing trans for ' + id);
	if(!slides[id])
		return false;
	if(moving || currentClicked == id)
			return false;
	move(id,direction);
	currentClicked = id;
}

function move(id,direction){
	var startx = 0, starty = 0, endy = 0, endx = 0;

	currentLevel = currentLevel + 1;

	if(direction == 'up'){
		starty = height;
	}else if(direction == 'down'){
		starty = -height;
	} else if(direction == 'left'){
		startx = width;
	} else if(direction == 'right'){
		startx = -width;
	}
	moving = true;
	$(slides[id])
		.css('top',starty)
		.css('left',startx)
		.css('z-index',currentLevel)
		.animate({
				top: endy,
				left: endx
			},duration, 'easeOutExpo', function(){
				moving = false;
			});	
}

function stop(){
	debug('clearing int');
	clearInterval(auto);
}

function start(){
	debug('starting int');
	auto = setInterval(function(){next();},delay);
}

function clearnav(){
	$('#awardsButton').removeClass('active');
	$('#contactButton').removeClass('active');	
	$(navitems).each(function(index) {
		$(this).removeClass('active');
	});
}

function next(id){
	if(id)
		play(id,'left',false);
	else
		play(current.id%rotationSlides.length+1,'left',false);
}

function prev(id){
	if(id)
		play(id,'right',false);
	else
		play((current.id+(rotationSlides.length-2))%rotationSlides.length+1,'right',false);
}

function splash(){
	var logo = $('<img />')
		.attr('src','images/logo_fwa_whitebg.gif')
		.addClass('splash')
		.css({'top':height,'opacity':0})
		.appendTo('#portal')
		.load(function(){
			logo.animate({
					top: (height/2)-42,
					opacity:1
				},1500, 'easeOutExpo', function(){
					logo
						.animate({
							top: 0-84,
							opacity:0
						},1500, 'easeInExpo',function(){
							$('#content')
								.animate({
									opacity:1
								},1000);
							play(1,'up');
							start();
						})
				})
		});
}

$(document).ready(function(){
	slides = $('div.page');
	slides.each(function(index){
		if(!$(this).hasClass('ignore')){
			rotationSlides.push(index);
			$(this).click(function(){
				stop();
				transition(index+1,'up',true);
			});
		}else{
			$(this).click(function(){
				clearnav();
				next();
				start();
			});
		}
	});
	width = $('#portal').width();
	height = $('#portal').height();
	$('#awardsButton').click(function(){
		clearnav();
		if(!moving){
			if(currentClicked == 24){
				next();
				start();
			}else{
				$(this).addClass('active');
				stop();
				transition(24,'up',true);
			}
		}
	});
	$('#contactButton').click(function(){
		if(!moving){
			clearnav();
			if(currentClicked == 25){
				next();
				start();
			}else{
				stop();
				$(this).addClass('active');
				transition(25,'up',true);
			}
		}
	});

	$('#continueLink').click(function(){
		if(!moving){
			stop();
			clearnav();
			next();
			start();
		}
	});

	nav = $('<ul />').appendTo($('#portalnav'));
	$.each(rotationSlides,function(index){
		$('<li><a href="#" rel="'+(index+1)+'"></a></li>').appendTo(nav);
	});
	navitems = $('#portalnav ul li a');
	navitems.each(function(index) {
		$(this).click(function(){
			stop();
			clearnav();
			var nextslide = $(this).attr('rel');
			if(next < current.id)
				next(nextslide);
			else
				prev(nextslide);
			start();
		});
	});

	$('.morebutton a').each(function(){
		var $this = $(this);
		var $parent = $this.parent().parent();
		$this.click(function(e){
			moving = true;
			e.stopPropagation();
			stop();
			if($this.html() == 'LESS'){
				$this.html('MORE');
				$parent
					.animate({top:-15},700,'easeOutExpo',function(){moving = false;});
			}else{
				$this.html('LESS');
				$parent
					.animate({top:-$parent.height()},700,'easeOutExpo',function(){moving = false;});
			}
		});
	});

	splash();
});
