$(document).ready( function()
	{
		deobfuscate_emails();
		setInterval( "slideSwitch()", 5000 );
		init_scrolling_images();
		init_about_background();
		init_facebook_wall();
	}
);

function deobfuscate_emails()
{
	$(".replaceAt").replaceWith("@");
	$(".obfuscate").each(function(i){
		this.href = "mailto:" + this.innerHTML;
	});
}


function slideSwitch() {
	var $active = $('ul#slideshow li.active');
	
	if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
	
	var $next =  $active.next().length ? $active.next()
		: $('ul#slideshow li:first');
	
	$active.addClass('last-active');
	
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});

	popupSwitch();
}


function popupSwitch() {
	var $active = $('ul#popups li.active');
	
	if ( $active.length == 0 ) $active = $('#popups li:first');
	
	var $next =  $active.next().length ? $active.next()
		: $('ul#popups li:first');
	
	$active.addClass('last-active');

	$active.animate({top: '107px'}, 500, function() {
		$active.removeClass('active last-active');
	});
	
	setTimeout(function(){
		$next.addClass('active').animate({top: '0px'}, 500, function() {
			$active.removeClass('active last-active');
		});
	}, 500);
}


function init_scrolling_images()
{
	if ($("#photo_block").length) {
		var photo_block = $("#photo_block");
		var total_width = photo_block.innerWidth();
		
		var left = $('<img src="/images/project.scroll_left.png" alt="Scroll Left" class="arrow scroll_left">');
		var right = $('<img src="/images/project.scroll_right.png" alt="Scroll Right" class="arrow scroll_right">');
		
		var intervalID;
		var speed = 1;
		
		left.hover(function(){
			var button = this;
			intervalID = setInterval(function(){
				if ($(button).mousedown(function(){speed = 4;}))
				if ($(button).mouseup(function(){speed = 1;}))
				scroll_images(-5 * speed)
			}, 15);
		}, function(){
			clearInterval(intervalID);
		});
		
		right.hover(function(){
			var button = this;
			intervalID = setInterval(function(){
				if ($(button).mousedown(function(){speed = 4;}))
				if ($(button).mouseup(function(){speed = 1;}))
				scroll_images(5 * speed)
			}, 15);
		}, function(){
			clearInterval(intervalID);
		});
		
		$('#top').append(left);
		$('#top').append(right);
		
		
		
		$("p.scroll img").hover(function(){
			var button = this;
			intervalID = setInterval(function(){
				if ($(button).mousedown(function(){speed = 4;}))
				if ($(button).mouseup(function(){speed = 1;}))
				scroll_images(5 * speed)
			}, 15);
		}, function(){
			clearInterval(intervalID);
		});
	}
}


function scroll_images(x)
{
	var photo_block = $("#photo_block");
	var photos = $("#photos");
	var photos_width = photos.innerWidth();
	var offset = photo_block.scrollLeft();

	offset = offset + x;
	
	//if (offset < photos_width) offset = photos_width;
	//if (offset > 0) offset = 0;
	
	photo_block.scrollLeft(offset);
}


function init_about_background()
{
	$("div#page.about").parent().addClass('about_background');
}


function init_facebook_wall()
{
	if ($('#fb_wall').length) {
		$(function(){
			$('#fb_wall').fbWall({
				id:'117563218267505',
				accessToken:'AAAC7gAoWDQ0BADBEPo0JElQCD2vTXfpWPYZAFoCAUwAbatZAhgPwLvNv6mft9XAGRiPe7A5Ho0ZBjLKi3rBgylTp7fH9pQZD',
				showGuestEntries:false,
				showComments:false,
				max:5,
				timeConversion:24
			});
		});
	}
}





