var active_album = false;
var animating = false;
var eight_ball_interval = false;
var eight_ball_img = false;
var eight_ball_offset = 7;
var last_image_album = false;
var next_eight_ball_index = 0;
var page_displayed = false;
var email = 'bssvpr@revpjeraa.pbz';


//prevent console.log errors
if(typeof(console) === 'undefined') {
    var console = {}
    console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}

function shuffle(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

String.prototype.rot13 = function(){
    return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
    });
};

if(albums['eight_ball']){
	albums['eight_ball']['photos'] = shuffle(albums['eight_ball']['photos']);
}

function send_email(){
	document.location = 'mailto:' + email.rot13();
}

function eight_ball(){
	if(active_album != 'eight_ball') show_album('eight_ball');
	next();
}

function update_tweet(){
	$('#tweet').load('/latest_tweet.php', function(){
		if(active_album == 'eight_ball'){
			$('a').addClass('white');
		}
		$('#tweet').fadeIn();
	});
}

function prev(){
	var index = albums[active_album]['active'];
	index -=1;
	if(index < 0){
		index = albums[active_album]['photos'].length - 1;
	}
	albums[active_album]['active'] = index;
	change_address_to_active_photo();	
}

function next(){
	if(page_displayed && active_album != 'eight_ball'){
		$.address.value('/office/' + albums['office']['photos'][0].slug);
		return true;
	}

	var index = albums[active_album]['active'];
	if(active_album == 'eight_ball'){
		index = next_eight_ball_index;		
		next_eight_ball_index = parseInt(Math.random() * albums[active_album]['photos'].length);
		preload(albums[active_album]['photos'][next_eight_ball_index]['url']);
	}else{
		index +=1;
		if(index >= albums[active_album]['photos'].length){
			index = 0;
		}		
	}	

	albums[active_album]['active'] = index;
	change_address_to_active_photo();
}

function change_address_to_active_photo(){
	var index = albums[active_album]['active'];
	var old_address = $.address.value();
	var new_address = '/' + active_album + '/' + albums[active_album]['photos'][index].slug;
	$.address.value(new_address);
	
	if(old_address == new_address){
		$.address.update()
	}
}

function show_album_stuff(){
	
	//if(page_displayed){
	//	$('#info_close').fadeOut(250);
	//	$('#info').fadeOut(250);
	//}

	$('#info_close').hide();
	$('#info').hide();
	$('#info_background_image_table').hide();
	
	$('#eight_ball').show();
	$('#title').show();
	
	if(active_album == 'eight_ball'){
		$('#eight_ball_close').show();
		$('#standard_controls').hide();		
		$('a').addClass('white');
	}else{
		$('#eight_ball_close').hide();
		$('#standard_controls').show();
		$('a').removeClass('white');
	}
	
	$('#foreground_image_table').show();
	if($('#foreground_image').attr('src') == ''){
		$('#foreground_image').hide();
	}
}

function show_album(slug){
	active_album = slug;
	if($('body').css('background-color') != albums[active_album]['background_color']){	
		if(page_displayed){
			//going to 8 ball, fade colors
			$('body').animate({'background-color': albums[active_album]['background_color']}, 250);
			$('body').animate({'color': albums[active_album]['text_color']}, 250);
		}else{
			$('body').css('background-color', albums[active_album]['background_color']);
			$('body').css('color', albums[active_album]['text_color']);
		}
	}
	show_album_stuff();
}

function image_currently_displayed(){
	if($('#foreground_image').attr('src') == undefined || !$('#foreground_image').attr('src')){
		return false;
	}
	return true;
}

function show_photo(index){
	albums[active_album]['active'] = index;
	var new_photo = albums[active_album]['photos'][index];
	var new_photo_url = new_photo['url'];
	
	var animate = true;
	
	//don't animate if this is the first thing we're showing
	if(!image_currently_displayed()) animate = false;
	
	//don't animate if we are showing a new image in the same album (And it's not 8-bal)
	//if(animate && (last_image_album == active_album) && (active_album != 'eight_ball') ) animate = false;
	
	//if we are on the info page, only fade if we are going to 8 ball
	if(page_displayed){
		if(active_album == 'eight_ball'){
			animate = true;
		}else{
			animate = false;
		}
	}
	
	if(!animate){
		$('#foreground_image').attr('src', new_photo_url).show();
	}else{	
		animating = true;

		$('#foreground_image').fadeOut(250, 
			function(){
				$('#foreground_image').attr('src', new_photo_url);
				$('#foreground_image').fadeIn(250, function(){ animating = false; });
			}
		);
	}

	if(new_photo['caption']){
		$('#caption').html(new_photo['caption']).show();
	}else{
		$('#caption').hide();
	}
	
	last_image_album = active_album;
	page_displayed = false;
		
	if(active_album == 'eight_ball'){
		next_eight_ball_index = parseInt(Math.random() * albums[active_album]['photos'].length);
		preload(albums[active_album]['photos'][next_eight_ball_index]['url']);
		return true;
	}
	
	//preload prev/next images
	if(albums[active_album]['photos'][index + 1]){
		preload(albums[active_album]['photos'][index + 1]['url']);
	}else{
		preload(albums[active_album]['photos'][0]['url']);
	}
	
	if(albums[active_album]['photos'][index - 1]){
		preload(albums[active_album]['photos'][index - 1]['url']);
	}else{
		var last_index = albums[active_album]['photos'].length - 1;
		preload(albums[active_album]['photos'][last_index]['url']);
	}
}

function show_photo_by_slug(slug){
	if(albums[active_album]['slug_lookup'][slug] != undefined){
		show_photo(albums[active_album]['slug_lookup'][slug]);
	}else{
		$.address.value('/' + active_album + '/' + albums[active_album]['photos'][0].slug);
	}
}

function preload(img_url){
	$('<img src="' + img_url + '" />');
}

function go_to_active_office_photo(){
	var active_office_index = albums['office']['active'];
	$.address.value('/office/' + albums['office']['photos'][active_office_index].slug);
}

$(function(){
	//load the first image of each album
	for (album_slug in albums) {
		if(album_slug == 'eight_ball'){
			albums[album_slug]['background_color'] = '#3300cc';
			albums[album_slug]['text_color'] = '#ffffff';
		}else{
			albums[album_slug]['background_color'] = '#ffffff';
			albums[album_slug]['text_color'] = '#000000';
		}
		
		albums[album_slug]['active'] = 0;
		preload(albums[album_slug]['photos'][0]['url']);
		
		var slug_lookup = {};
		for (var i=0; i < albums[album_slug]['photos'].length; i++) {
			var slug = albums[album_slug]['photos'][i]['slug'];
			slug_lookup[slug] = i;
		}
		albums[album_slug]['slug_lookup'] = slug_lookup;
	}

	//routing for address changes
	$.address.change(function(event) {
		
		//info page
		if(event.value == '/info'){
			$('body').css({'background-color': albums['office']['background_color']});
			$('body').css({'color': albums['office']['text_color']});
			
			active_album = false;
			
			$('a').removeClass('white');
			$('#standard_controls').hide();
			$('#eight_ball_close').hide();
			$('#foreground_image_table').hide();
			$('#foreground_image').attr('src', '');
			$('#caption').hide();
			$('#title').hide();
			$('#info_close').show();
			$('#info').show();
			$('#info_background_image_table').show();

			page_displayed = true;

			return true;
		}

		//home page
		if(event.value == '/'){
			show_album('office');
			show_photo(0);
			return true;
		}

		//show specific project/image		
		var matches = event.value.match(/\/([^\/]+)\/*(.*)/);
		if(matches && albums[matches[1]] != undefined){
			show_album(matches[1]);
			if(matches[2]) show_photo_by_slug(matches[2]);
			return true;
		}
	});

	//click events
	$('#eight_ball').click(function(){ eight_ball(); });
	$('#prev_link').click(function(){ prev(); });
	$('#next_link').click(function(){ next(); });
	$('#foreground_image_wrapper').click(function(){ next(); });
	$('#eight_ball_close_link').click(function(){ go_to_active_office_photo(); });
	$('#info_close_link').click(function(){ go_to_active_office_photo(); });
	$('#info_title').click(function(){ go_to_active_office_photo(); });
	$('#title').click(function(){ $.address.value('/info'); });
	$('#email_link').click(function(){ send_email(); });
	
	$('#info_title a').hover(
		function(){
			$('#info_title a').addClass('hovered');
		},
		function(){
			$('#info_title a').removeClass('hovered');	
		}
	);
	
	$('#tweet').hover(
		function(){
			if(active_album == 'eight_ball'){
				$('#tweet a').addClass('white_hovered');
			}else{
				$('#tweet a').addClass('hovered');
			}	
		},
		function(){
			$('#tweet a').removeClass('white_hovered');
			$('#tweet a').removeClass('hovered');	
		}
	);

	
	$('#eight_ball img').jrumble({
		rumbleEvent: 'hover',
		rangeX: 5,
		rangeY: 5,
		rangeRot: 3, 
		rumbleSpeed: 50
	});
	
	update_tweet();
	setInterval(function(){ update_tweet(); }, 300000);
});
