var $slideTimer;

$(document).ready(function() {
	offlineAnimate();
	menuDown();
	doSpotlight();
	itemFilter();
	
	var $screens = $('.screenshots a');
	if ($screens.length > 0) {
		$screens.lightBox();
	}
});

function offlineAnimate() {
	$('#offline').css('margin-top', 180).animate({
		marginTop: '60px'
	}, 750, function() {
		
	});
	$('#offline .content').css('padding-top', 0).css('padding-bottom', 0).hide().animate({
		paddingTop: '50px',
		paddingBottom: '50px',
		height: 'toggle'
	}, 750, function() {
		$(this).children().fadeIn(1000);
	}).children().hide();
}

function menuDown() {
	var $item = $('#header a');
	$item.mousedown(function() {
		$(this).addClass('down');
	});
	$item.mouseleave(function() {
		$(this).removeClass('down');
	});
}
function doSpotlight() {
	doSlideshow();
	$('.spot-list').each(function() {
		var $this = $(this);
		if (!$this.hasClass('enabled')) {
			doMediumBanner($this);
		}
	});
}
function doSlideshow() {
	var $target = $('#slideshow').addClass('enabled');
	var $items = $target.find('li a');
	var $display = $target.find('.display');
	var $count = $target.attr('count');
	var $timeout = 10000;
	
	clearTimeout($slideTimer);
	
	if ($count == undefined) {
		$count = 0;
	}
	if (!$target.hasClass('pause')) {
		if ($display.length == 0) {
			$display = $('<div class="display"></div>');
			$display.insertBefore($target.children().first());
			$items.each(function(index) {
				var $image = $(this).find('img');
				var $src = $image.attr('src');
				$image.attr('src', $src.replace('.png', '_large.png'));
				$(this).click(function(event) {
					$target.attr('count', index);
					$target.removeClass('pause');
					event.preventDefault();
					
					doSlideshow();
				});
			});
		}
		
		if ($count < $items.length) {
			var $slide = $($items.get($count));
			$slide.parent().addClass('slide-on');
			$items.not($slide).parent().removeClass('slide-on');
			var $clone = $slide/*.find('a')*/.clone().empty();
			$clone.css('background-image', 'url('+$slide.find('img').attr('src')+')');
			//$clone.append($('<div class="text"></div>').append($slide.find('h5').clone())
			//										   .append($slide.find('p').clone()));
			$clone.append($('<div class="text-block"></div>').append($slide.find('.head').clone())
													   .append($slide.find('.text').clone()));
			$clone.mouseover(function(event) {
				$target.addClass('pause');
			});
			$clone.mouseout(function(event) {
				$target.removeClass('pause');
			});
			$display.empty().append($clone);
			$count++;
		}
		if ($count == $items.length) {
			$count = 0;
		}
		
		$target.attr('count', $count);
		
		var $text = $display.find('.text-block')
		$text.children().hide();
		$text.hide().animate({
			width: 'toggle'
		}, 500, function() {
			$text.children().fadeIn(1000);
			if ($items.length > 1) {
				$slideTimer = setTimeout('doSlideshow()', $timeout);
			}
		});
	} else {
		setTimeout('doSlideshow()', $timeout);
	}
}
function doMediumBanner($target) {
	$target.find('li').each(function() {
		var $link = $(this).find('a');
		var $image = $link.find('img');
		$(this).css('background-image', 'url('+$image.attr('src')+')');
		$image.hide();
	});
}

function itemFilter() {
	var $list = $('#item-options').find('a');
	$list.click(function(event) {
		var $select = $(this).attr('href').slice(1);
		$list.not($(this).addClass('on')).removeClass('on');
		
		var $items = getItems();
		$items.removeClass('hide');
		if ($select != 'all') {
			$items.not('.'+$select).addClass('hide');
		}
		$items.not('.hide').each(function(index) {
			if ((index % 2) == 0) {
				$(this).removeClass('on').addClass('off');
			} else {
				$(this).removeClass('off').addClass('on');
			}
		});
		itemCount();
		
		event.preventDefault();
	});
	itemCount();
}
function itemCount() {
	var $items = getItems();
	var $totals = $items.not('.hide').length+' Items Found';
	$('#item-list tfoot td').html($totals);
}
function getItems() {
	var $items = $('#item-list').find('li');
	if ($items.length == 0) {
		$items = $('#item-list').find('tbody').find('tr');
	}
	return($items);
}
