// JavaScript Functions for DBDnet.com - Demand Plan Measure Build (DPMB) //

var currentPanel = 1;

$(document).ready(function(){
	setButtonNavClicks();
	showPanel(1);
	
	//initialize for Flash communications
	initialize();
});


function setButtonNavClicks(){
	$("#nonflashlink").click(function(){
		//non-flash image button
		if (currentPanel != 2){
			currentPanel = 2;
			showPanel(currentPanel);
		}						  
	});
	$(".dpmb-menu a").eq(0).click(function(){
		//home button
		if (isIE){
			reloadPageForIE();
		}else{
			if (currentPanel != 1){
				currentPanel = 1;
				showPanel(currentPanel);
			}
		}
	});
	$(".dpmb-menu a").eq(1).click(function(){
		//previous button
		if (currentPanel != 1){
			currentPanel--;
			if(currentPanel < 1) currentPanel = 1;
			
			if(currentPanel == 1 && isIE){
				reloadPageForIE();
			}else{
				showPanel(currentPanel);
			}
		}
	});
	$(".dpmb-menu a").eq(2).click(function(){
		//first panel button
		if (currentPanel != 2){
			currentPanel = 2;
			showPanel(currentPanel);
		}
	});
	$(".dpmb-menu a").eq(3).click(function(){
		//second panel button
		if (currentPanel != 3){
			currentPanel = 3;
			showPanel(currentPanel);
		}
	});
	$(".dpmb-menu a").eq(4).click(function(){
		//third panel button
		if (currentPanel != 4){
			currentPanel = 4;
			showPanel(currentPanel);
		}
	});
	$(".dpmb-menu a").eq(5).click(function(){
		//fourth panel button
		if (currentPanel != 5){
			currentPanel = 5;
			showPanel(currentPanel);
		}
	});
	$(".dpmb-menu a").eq(6).click(function(){
		//next button
		if (currentPanel != 5){
			currentPanel++;
			if(currentPanel > 5) currentPanel = 5;
			showPanel(currentPanel);
		}
	});
}

function reloadPageForIE(){
	//reloading page for panel 1 to avoid IE7/Flash/SwfObject issue of JS talking to Flash movie
	window.location = "../dpmb/";	
}

function showPanel(pnlNum){
	//keep variables updated (may be calling this function direct from Flash)
	if(pnlNum == 2){
		currentPanel = 2;	
	}
	
	//if showing first panel, hide the menu system
	if(pnlNum == 1) {
		$(".dpmb-menu").hide();
		$(".dpmb-btns").hide();
	}
	
	//hide any active panels
	$("#dpmbflashfix").hide();
	$("#dpmbpanelcontainer").children("div:visible").hide();

	//set new active panel
	if(pnlNum == 1){
		$("#dpmbflashfix").show(); //can't fade flash SWF file...
	}else{
		//subtract two panel number 4 would be -1 for the first panel being outside of this container, and -1 again for zero-based index
		$("#dpmbpanelcontainer").children("div").eq(pnlNum-2).fadeIn("slow");
	}
	
	//hide all active buttons
	$(".dpmb-menu a img[src*=av.]").each(function(index, domElement){
		var $thisDE = $(domElement);
		var imgsrc = $thisDE.attr("src");
		var imgsrcON = imgsrc.replace(/-av/ig,"_nm");
		$thisDE.attr("src", imgsrcON);
	});
	//set this active button
	var $thisActiveImage = $(".dpmb-menu a img").eq(getResolvedImageNum(pnlNum));
	var thisOnSRC = $thisActiveImage.attr("src").replace(/_nm/ig,"-av").replace(/_hv/ig,"-av");
	$thisActiveImage.attr("src", thisOnSRC);
	
	//if showing first panel, then restart the Flash movie
	if(pnlNum == 1) {
		callFlashRestart();	
	}
	
	//if not showing first panel, show the menu system
	if(pnlNum != 1) {
		$(".dpmb-menu").show();
		$(".dpmb-btns").show();
	}
	
	//google track
	pageTracker._trackPageview("/dpmb/"+getResolvedName(pnlNum)+"/");
}

function getResolvedImageNum(pnlNum){
	//this function helps resolve a panel number to the proper image button index
	var thisIndex = 0;
	switch (pnlNum){
		case 2:
			thisIndex = 2;
			break;
		case 3:
			thisIndex = 3;
			break;
		case 4:
			thisIndex = 4;
			break;
		case 5:
			thisIndex = 5;
			break;
		default:
			thisIndex = 0;
			break;
	}
	return thisIndex;
}

function getResolvedName(pnlNum){
	//this function helps resolve a panel number to the proper image button index
	var thisName = "";
	switch (pnlNum){
		case 2:
			thisName = "demand";
			break;
		case 3:
			thisName = "plan";
			break;
		case 4:
			thisName = "measure";
			break;
		case 5:
			thisName = "build";
			break;
		default:
			thisName = "intro";
			break;
	}
	return thisName;
}