function slider() {
	var i, l,
	run = null,
	delay = 5000,
	speed = 300,
	current = 0,
	buttons = $('#scroll-buttons a'),
	indent = Math.ceil(buttons.length / 2) - 1,
	boxWidth = $('#scroll-box img').outerWidth(),
	leftValue = boxWidth * (-indent),
	
	enable = function () {
		for (i = 0, l = buttons.length; i < l; i++) {
			if (i === current) {
				buttons[i].className = 'current';
			}
			buttons[i].onclick = goTo(i);
		}
	},
	
	disable = function () {
		buttons.removeClass('current');
		for (i = 0, l = buttons.length; i < l; i++) {
			buttons[i].onclick = null;
		}
	},
	
	rotate = function(num, backwards) {
		var dist = backwards ? parseInt($('#scroll-box').css('left')) - (-boxWidth * num) : parseInt($('#scroll-box').css('left')) - (boxWidth * num);
		$('#scroll-box').animate({'left' : dist}, (speed * num), function () {
			for (i = 0; i < num; i++) {
				if (backwards) {
					$('#scroll-box img:first').before($('#scroll-box img:last'));
				} else {
					$('#scroll-box img:last').after($('#scroll-box img:first'));
				}
				$('#scroll-box').css({'left' : leftValue});
			}
			enable();
		});
	},
	
	goTo = function (which) {
		return function () {
			var left, right;
			if (which === null) {
				which = current + 1;
			}
			if (which !== current) {
				disable();
				if (which === buttons.length) {
					which = 0;
				}
				if (which > current) {
					right = which - current;
					left = current + (buttons.length - which);
				} else {
					right = which + (buttons.length - current);
					left = current - which;
				}
				current = which;
				which = null;
				if (left < right) {
					rotate(left, true);
				} else {
					rotate(right, false);
				}
			}
			return false;
		};
	};
	
	$('#scroll-box').css({'width' : boxWidth * $('#scroll-box img').length});
	for (i = 0; i < indent; i++) {
		$('#scroll-box img:first').before($('#scroll-box img:last'));
	}
	$('#scroll-box').css({'left' : leftValue});
	$('#scroll').hover(
		function() {
			clearInterval(run);
			run = null;
		},
		function() {
			if (run === null) {
				run = setInterval(goTo(null), delay);
			}
		}
	);
	$(document).blur(function(e) {
		clearInterval(run);
		run = null;
    });
	$(document).focus(function(e) {
		if (run === null) {
			run = setInterval(goTo(null), delay);
		}
    });
	enable();
	run = setInterval(goTo(null), delay);
}

function contactSwitch() {
	var change = function () {
		return function () {
			$('#contact')[0].className = $(this).attr('id') + ' shadow';
			$('#contact-links')[0].className = $(this).attr('id');
			this.blur();
			return false;
		};
	};
	
	$('#contact-info').click(change());
	$('#quote-request').click(change());
}

function workTabs() {
	var content = [],
	current = 'web';
	
	$('#link-box a').click(function(e) {
		this.blur();
		var selected = $(this).attr('title');
		if (selected !== current) {
			if ($(this).parent().has('ul').length > 0) {
				if ($('#link-box a[title="' + selected + '"] ~ ul li a[title="' + current + '"]').length < 1) {
					$('#link-box > li > a.active ~ ul:first').hide();
					$('#link-box a[title="' + selected + '"] ~ ul:first').show();
				}
				$('#link-box li a.active').removeClass('active');
			} else {
				$('#link-box > li > ul > li > a.active').removeClass('active');
			}
			$(this).addClass('active');
			content[current] = $('#' + current).detach();
			content[selected].appendTo('#work-body');
			if ($('a[rel]').length > 0) {
				$('a[rel]').each(function(index, element) {
					if ($(this).hasClass('video')) {
						$(this).fancybox({
							'autoScale'			: true,
							'autoDimensions'	: true,
							'transitionIn'		: 'elastic',
							'transitionOut'		: 'elastic',
							'overlayOpacity'	: 0.9,
							'overlayColor'		: '#000',
							'type'				: 'iframe'
						});
					} else {
						$(this).fancybox({
							'transitionIn'		: 'elastic',
							'transitionOut'		: 'elastic',
							'overlayOpacity'	: 0.9,
							'overlayColor'		: '#000',
							'titlePosition' 	: 'outside',
							'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
								return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
							}
						});
					}
                });
			}
			current = selected;
		}
		return false;
    });
	
	
	$('#work-body > div img').hover(
		function() {
			this.src = this.src.replace('a.jpg', 'b.jpg');
		},
		function() {
			this.src = this.src.replace('b.jpg', 'a.jpg');
		}
	);
	$('#work-body > div').not('[id="web"]').each(function() {
        content[$(this).attr('id')] = $(this).detach();
    });
	$('#link-box > li > a[title!="web"] ~ ul').hide();
	$('#link-box a[title="web"]').addClass('active');
}

$('html').removeClass('no-js');

$(document).ready(function(e) {
	slider();
	contactSwitch();
	workTabs();
});

$(window).load(function() {
	$('#load-screen').hide();
});
