try{document.execCommand('BackgroundImageCache',false,true);}catch(e){}

function changeCarousel(from, to, direction) {
	$('#carousel li.carousel_next').fadeOut(250, function() {
		$(this).css('top', '0');
	});
	$('#carousel li.carousel_previous').fadeOut(250, function() {
		$(this).css('top', '0');
		var anchorImage = $('img', to);
		var title = anchorImage.attr('alt')
		var lastSlash = anchorImage.attr('src').lastIndexOf('/');
		var lastDot = anchorImage.attr('src').lastIndexOf('.');
		var newAnchorImage = anchorImage.attr('src').substring(lastSlash + 1, lastDot);
		var newImage = 'assets/images/carousel/large/' + newAnchorImage + '.jpg';
		var description = anchorImage.attr('title');
		var newLink = to.attr('href');
		var newCarousel = $('<div class="' + direction + '" id="newcarousel"><img alt="' + title + '" height="448" src="' + newImage + '" width="958" /><div><h2>' + title + '</h2><p class="branddetail" style="width: 620px">' + description + '</p><p class="projectinfo"><a href="' + newLink + '">Project info &raquo;</a></p></div></div>');
		$('#carousel').after(newCarousel);

		if (direction == 'left') {
			$('#carousel').animate({left: '-1000px'});
			newCarousel.animate({left: '15px'}, false, false, function() {
				$('#newcarousel').append($('#carousel li').clone(true).fadeIn(250));
				$('#carousel').remove();
				$(this).attr('id', 'carousel').removeAttr('style');
			});
		}
		else {
			$('#carousel').animate({left: '1000px'});
			newCarousel.animate({left: '0'}, false, false, function() {
				$('#newcarousel').append($('#carousel li').clone(true).fadeIn(250));
				$('#carousel').remove();
				$(this).attr('id', 'carousel').removeAttr('style');
			});
		}

		from.removeClass('on');
		var fromImage = $('img', from);
		var currentFromImage = fromImage.attr('src');
		var newFromImage = currentFromImage.substring(0, currentFromImage.length - 8) + '.gif';
		fromImage.attr('src', newFromImage);

		to.addClass('on');
		var currentToImage = anchorImage.attr('src');
		var newToImage = currentToImage.substring(0, currentToImage.length - 4) + '_col.gif';
		anchorImage.attr('src', newToImage);
	});
}

$(
	function() {
		$('#carousel').append('<ul><li class="carousel_previous">Previous</li><li class="carousel_next">Next</li></ul>');
		if ($.browser.msie) {
			$('#carousel li.carousel_previous').css('top', '-438px')
			$('#carousel li.carousel_next').css('top', '-438px')
		}
		$('li.carousel_next').click(function() {
			var from = $('#splash ul a.on');
			var to = $('a', from.parent('li').next('li'));
			if (to.length != 1) {
				to = $('#splash ul li:first-child a');
			}
			changeCarousel(from, to, 'left');
		});
		$('li.carousel_previous').click(function() {
			var from = $('#splash ul a.on');
			var to = $('a', from.parent('li').prev('li'));
			if (to.length != 1) {
				to = $('#splash ul li:last-child a');
			}
			changeCarousel(from, to, 'right');
		});
	}
);