var scrolling = false;

function findCurrentIndex() {
	//parse through links, find currently selected
	var $links = $("#home_menu").children();
	for(var i=0;i<$links.size();i++) {
		if($links.eq(i).hasClass("selected")) {
			return i;
		}
	}
	return -1;
}
function scrollCarousel($from, $to, _fromIndex, _toIndex) {
	
	if(!scrolling) {
		scrolling = true;
		
		var duration = 700;
		
		$from.removeClass("selected");
		$to.addClass("selected");
		
		var downDirection = (_fromIndex < _toIndex);
		var padding = $("#home_carousel_content .panel").eq(_fromIndex).height();
		$("#home_carousel_content .panel").hide();
		$("#home_carousel_content .panel").eq(_fromIndex).show();
		$("#home_carousel_content .panel").eq(_toIndex).show();
		var newContentY, newPanelY;
		if(downDirection) {
			newPanelY = ($("#home_carousel_content .panel").eq(_fromIndex).position().top+$("#home_carousel_content .panel").eq(_toIndex).height()+padding)+"px";
			newContentY = $("#home_carousel_content").position().top-($("#home_carousel_content .panel").eq(_fromIndex).height()+padding)+"px";
		} else {
			newPanelY = ($("#home_carousel_content .panel").eq(_fromIndex).position().top-$("#home_carousel_content .panel").eq(_toIndex).height()-padding)+"px";
			newContentY = $("#home_carousel_content").position().top+($("#home_carousel_content .panel").eq(_fromIndex).height()+padding)+"px";
		}
		
		$("#home_carousel_content .panel").eq(_toIndex).css({ top: newPanelY });
		$("#home_carousel_content").animate({ top: newContentY }, duration);
		$("#home_slider img").animate({ top: ($to.position().top+$to.height()/2-$("#home_slider img").height()/2)+"px" }, duration);
		
		//opacity
		$("#home_carousel_content .panel").eq(_toIndex).css({ opacity: 0.0 });
		$("#home_carousel_content .panel").eq(_fromIndex).css({ opacity: 1.0 });
		$("#home_carousel_content .panel").eq(_toIndex).animate({ opacity: 1.0 }, duration, function() {
			$(this).css("opacity", ""); // fixes issue in ie7 when applying more than one filter (css and png transparency)
		});
		$("#home_carousel_content .panel").eq(_fromIndex).animate({ opacity: 0 }, duration, function() {
			scrolling = false;
		});
	}
}

$(document).ready(function() {
	
	//hide device drawer
	//$("#drawer").hide();
	//$("#drawer_mask").hide();
	
	//buttons
	$("#home_wrapper .red_button").roundedButton({ textPadding: "6px 14px 0 14px" });
	$("#home_wrapper .red_button .flexible_button_center").css({ "font-size": "12px", "font-weight": "bold" });

	$("#home_wrapper .dark_gray_button").roundedButton({ textPadding: "6px 14px 0 14px" });

	//carousel functionality
	$("#home_carousel_content .panel").hide();
	$("#home_carousel_content .panel").eq(0).show();
	$("#home_menu a").click(function() {
		//parse through links, find currently selected
		var fromIndex = findCurrentIndex();
		var $from = $("#home_menu").children().eq(fromIndex);
		var toIndex = $(this).index();
		
		if($from && fromIndex > -1 && fromIndex != toIndex) {
			scrollCarousel($from, $(this), fromIndex, toIndex);
		}
	});
	$(".next").click(function() {
		//parse through links, find currently selected
		var fromIndex = findCurrentIndex();
		var $from = $("#home_menu").children().eq(fromIndex);
		
		if($from && fromIndex > -1) {
			var $links = $("#home_menu").children();
			var toIndex = (fromIndex >= ($links.size()-1)) ? 0 : (fromIndex+1);
			$to = $links.eq(toIndex);
			scrollCarousel($from, $to, fromIndex, toIndex);
		}
	});
	
});

