var fade_slide_interval = null;
var fade_slide_duration = 10000;
var fade_animation = 600;

function startFadeSlideshow(parent) {
	var slideups = $(parent).find('.slide-ups');
	if (slideups) {
		$(slideups).find('p:first').show();
		$(slideups).css('bottom', '0px');
	}
	clearInterval(fade_slide_interval);
	fade_slide_interval = setInterval(function () {
		changeFadeSlide(parent, 'next');
	}, fade_slide_duration);
}

function changeFadeSlide(parent, new_index) {
	if ($(parent).find('.slide:animated').length > 0) {
		return false;
	}
	var current = $(parent).find('.slide:visible:eq(0)');
	var is_numeric = (parseInt(new_index) === new_index);
	
	if (is_numeric) {
		var target = $(parent).find('.slide:eq(' + new_index + ')');
	} else if (new_index == 'prev') {
		var target = ($(current).prev().length == 0 || !$(current).prev().hasClass('slide')) ? $(parent).find('.slide:last') : $(current).prev();
	} else { // next
		var target = ($(current).next().length == 0 || !$(current).next().hasClass('slide')) ? $(parent).find('.slide:first') : $(current).next();
	}
	
	$(current).fadeTo(fade_animation, 0, function () {
		$(this).hide();
	});
	$(target).fadeTo(fade_animation, 1);
	var slideups = $(parent).find('.slide-ups');
	if (slideups) {
		$(slideups).animate({
			bottom: (-$(slideups).height()).toString() + 'px'
		}, Math.floor(fade_animation / 1.5), function () {
			$(slideups).find('p').hide();
			$(slideups).find('p:eq(' + $(parent).find('.slide').index(target).toString() + ')').show();
			$(slideups).animate({
				bottom: '0px'
			}, Math.floor(fade_animation / 1.5));
		});
	}
}

var slide_animation = 600;
var slide_width = 149;
$('.slideshow-wrap .rarr').live('click', function() {
	var platform = $(this).parent().find('ul:eq(0)');
	//alert($(platform).parents('.slideshow-wrap').find('.expand-me').width());
	//alert(parseInt($(platform).css('left')));
	//alert($(platform).parents('.slideshow-wrap').find('.expand-me').width() + parseInt($(platform).css('left')) < 0);
	if ($(platform).is(':animated') || $(platform).parents('.slideshow-wrap').find('.expand-me').width() + parseInt($(platform).css('left')) - slide_width * 4.5 < 0) { return false; }
	
	$(platform).animate({
		left: (parseInt($(platform).css('left')) - slide_width).toString() + 'px'
	}, slide_animation);
	return false;
});
$('.slideshow-wrap .larr').live('click', function() {
	var platform = $(this).parent().find('ul:eq(0)');
	if ($(platform).is(':animated') || parseInt($(platform).css('left')) + slide_width > 0) { return false; }
	
	$(platform).animate({
		left: (parseInt($(platform).css('left')) + slide_width).toString() + 'px'
	}, slide_animation);
	return false;
});

$(document).ready(function() {
	$('#nav ul li').hover(function() {
		$(this).find('ul:eq(0)').show();
	}, function() {
		$(this).find('ul:eq(0)').hide();
	});
	
	$('.highlight-link').hover(function() {
		$('#nav > ul > li:contains("Products")').find('ul:eq(0)').show();
	}, function() {
		$('#nav > ul > li:contains("Products")').find('ul:eq(0)').hide();
	});
})

$(window).load(function() {
	var height = Math.max($('#content').height() - 37, $('#sidebar').height()) + $('#footer').height();
	$('#sidebar').height(height).css('margin-bottom', '-' + $('#footer').height().toString() + 'px');
})
