$(document).ready(function() {
	var height = 379;
	var duration = 300;
	var currentSelect = 0;
	var nextSelect = 0;
	var selectMax = $('#featured_properties li').length;
	var interval = setInterval(autoRotator, 15000);
	$('#featured_properties li').live('ON', function(event,current_fp) {
		if($(this).hasClass('selected')) {
			var next_fp =  $(this).attr('id').replace("fp_", "");
			if(!current_fp) {current_fp = 1;}
			if(current_fp != next_fp) {
				$('#featured_images>a').removeClass('selected');
				$('#fp_desc_'+current_fp).fadeOut(200, function() {
					$('#fp_img_'+next_fp).css('top', height+'px');
					$('#fp_img_'+next_fp).animate({'top':0},duration,function() {
						$('#fp_desc_'+next_fp).animate({opacity:0.75}, 1).slideDown(100);				
					});
					$('#fp_img_'+current_fp).animate({'top':-height+'px'},duration,"swing");
				});			
			}else{
				$('#fp_desc_'+next_fp).animate({opacity:0.75}, 1).slideDown(100);
			}
			currentSelect = parseInt(next_fp);
		}
	});
	$('#featured_properties li').live("click",function() {
		prev_fp_sel = $('#featured_properties li.selected').attr('id').replace("fp_", "");
		$('#featured_properties li').removeClass('selected');
		$(this).addClass('selected');
		$(this).trigger('ON', [prev_fp_sel]);
	});
	$('#featured_properties li').trigger('ON', [currentSelect]);
	
	function autoRotator() {
		nextSelect = currentSelect + 1;
		if(nextSelect > selectMax) {nextSelect = 1;}
		$('#fp_'+currentSelect).removeClass('selected');
		$('#fp_'+nextSelect).addClass('selected').trigger('ON', [currentSelect]);	
		nextSelect++;		
		$('#number').val("MAX: "+selectMax);
		$('#current').val("current: "+currentSelect);
		$('#next').val("next: "+nextSelect);
	}
	$('#rotate').click(function() {
		autoRotator();
	});	

});
