// Rotator
$(document).ready(function() {
	
	if ($('#rotator ul li').size() > 1) {
		// remove to make movable
		t = setTimeout ( "rotateNormal()", 6000 );
		//t = setTimeout("",6000);
		$rotate = false;
	}
	
	//Activate PNG Fix
	if (jQuery.fn.pngFix) $(document).pngFix();
});

$('#slides a').hide();
$('#slides a:first').show();
$disable = false;
$disable2 = true;
$disable_scroll = false;

sb_height = 270;

function rotator($next) {
	$disable = true;	
	$('#slides a:visible').fadeOut(1000,
		function() {
			t = setTimeout ( "rotateNormal()", 6000 );
			$disable = false;
		});
	
	$next.fadeIn(1000);
	
	$('.sidebar li.current').removeClass('current');
	
	$count = $next.prevAll('a').size();
	$sidebar = $('.sidebar li:eq('+$count+')');
	
	//automatically move the sidebar so it is showing the current highlighted item.
	
	position = Math.floor($count / 5) * sb_height;
	marginTop = find_top();
	if(position != marginTop && $disable_scroll == false) {
		scroll_sidebar('', 0-position);
	}
	
	//move pointer
	$pos = $count * 54;
	
	$('#pointer').stop().animate({ 
		top: $pos+"px"
	  }, 1000, function(){
		$sidebar.addClass('current');  
		  
	  });
}

function rotateNormal() {
	
	if($('#slides a:visible').next().length > 0) {
		$next = $('#slides a:visible').next();
	
	}else{
		$next = $('#slides a:first');
	}
	
	rotator($next);
}

$('.sidebar .slide a').click(function(){
		if($disable == false) {
			clearTimeout (t);
			
			$count = $(this).parent().prevAll('li').size();
			$next = $('#slides a:eq('+$count+')');
			
			rotator($next);
		}
		return false;
	});
	
	//double click 04.25.2011
$('.sidebar .slide a').dblclick(function(){
		$count = $(this).parent().prevAll('li').size();
		url = $('#slides a:eq('+$count+')').attr("href");
		location.href = $next.attr("href");
	
});

$('#slides a').hover(
	function(){
		if($disable == false) {
			clearTimeout(t);
			$disable2 = false;
		}
	},
	function(){
		if($disable2 == false) {
			if($rotate != false) {
				t = setTimeout ( "rotateNormal()", 1000 );
			}
			$disable2 = true;
		}
	});

$('.sidebar').hover(
	function(){
		$disable_scroll = true;
	},
	function(){
		$disable_scroll = false;
	});


function find_top() {
	marginTop = parseInt($('.slide').css('marginTop'));
	
	if(isNaN(marginTop)) {
		marginTop = 0;
	}
	
	return marginTop;
}

function scroll_sidebar(d, pos) {
	elem = $('.wrap');
	slide = elem.children('.slide');
	marginTop = find_top();
	slideMax = slide.height() - sb_height;

	if(!isNaN(pos)) {
		marginTop = pos;
	} else if (d == 'up'){
		marginTop = marginTop+sb_height;
	} else if (d == 'down'){
		marginTop = marginTop-sb_height;
	}
			
	if(marginTop > 0) {
		marginTop = 0;
	} else if(marginTop < 0-slideMax) {
		marginTop = 0-slideMax;
	}
	

	slide.animate({'margin-top': marginTop}, 300);
	
	//activate or deactivate the arrows
	$('#rotatorNav .buttons').removeClass('disabled');
	
	if (marginTop == 0) {
		$('#prevBtn').addClass('disabled');
	}
	if (marginTop <= 0-slideMax) {
		$('#nextBtn').addClass('disabled');
	
	}
	return false;				
}

$('#prevBtn').click(function(){
		scroll_sidebar('up');
		return false;
	});

$('#nextBtn').click(function(){
		scroll_sidebar('down');
		return false;
	});
