$(document).ready(function(){

	var numPanels = 2;
	var currentPanel = 1;
	var nextPanel = 2;
	var panelInterval = window.setTimeout(changePanel, 7000);
	
	function changePanel() {
		var fadingPanel = currentPanel;
		if (currentPanel >= numPanels) {
			currentPanel = 0;
			nextPanel = 1;
		}
		$("#p_0" + fadingPanel).css("z-index",888);
		$("#p_0" + (nextPanel) ).css("z-index",100);
		$("#p_0" + (nextPanel) ).show();
		$('.panel_link').removeClass('pl_on');
		$("#pl_0" + nextPanel).addClass('pl_on');
		$("#p_0" + fadingPanel).fadeOut("slow", function() {
			currentPanel++;
			nextPanel++;

		});
		panelInterval = setTimeout(changePanel, 8000);
	}
	function stopInterval() {
		clearTimeout(panelInterval);
	}
	
	$('#panelbox').mouseenter(function() {
		stopInterval();
	});
	$('#panelbox').mouseleave(function() {
		panelInterval = window.setTimeout(changePanel, 7000);
	});
	
	$('.panel_link').click(function() {
		var whichPL = "#"+$(this).attr("id");
		var fadeOutPanel = currentPanel;
		if( whichPL == "#pl_01") {
			currentPanel = 1;
			nextPanel = 2;
			fadeInPanel = 1;
		}
		if( whichPL == "#pl_02") {
			currentPanel = 2;
			nextPanel = 3;
			fadeInPanel = 2;
		}
		if( whichPL == "#pl_03") {
			currentPanel = 3;
			nextPanel = 4;
			fadeInPanel = 3;
		}
		if( whichPL == "#pl_next") {
			if (currentPanel >= numPanels) {
				currentPanel = 0;
				nextPanel = 1;
			}
			fadeInPanel = (currentPanel + 1);
			currentPanel = fadeInPanel;
			nextPanel = (fadeInPanel + 1);
		}
		if( whichPL == "#pl_back") {
			if (currentPanel == 1) {
				currentPanel = numPanels+1;
				nextPanel = numPanels+2;
			}
			fadeInPanel = (currentPanel - 1);
			currentPanel = fadeInPanel;
			nextPanel = (fadeInPanel + 1);
		}
		
		
		$("#p_0" + fadeOutPanel).css("z-index",888);
		$('.panel_link').removeClass('pl_on');
		$("#pl_0" + fadeInPanel).addClass('pl_on');
		$("#p_0" + (fadeInPanel) ).css("z-index",100);
		$("#p_0" + (fadeInPanel) ).show();

		/* Testing Alerts
		alert("fadeOut: "+fadeOutPanel);
		alert("fadeIn: "+fadeInPanel);
		alert("current: "+currentPanel);
		alert("next: "+nextPanel);
		*/
		$("#p_0" + fadeOutPanel).fadeOut("fast", function() {
		});
		

		
	});


});




