
var PLAYER_XEN = 0;
var PLAYER_VMWARE = 2;
var PLAYER_EC2 = 1;

var MENUNAME = 0;
var DIVNAME = 1;
var ENABLED = 2;

var steps_xen = null; // to be defined by custom tour files
var steps_ec2 = null;    // to be defined by custom tour files
var steps_start = null;
var blessedAMIId = null; // to be defined by custom.js in tour
var launcher = null

// get custom information for tour
var tour = getParameter("tour");
if (tour == "") 
	tour = "content";			
			
var appliance = null;
var steps = null;
var stepDiv = null;
var current = null;
var max_visited = null;
var ipaddress = null;

function getParameter ( name )
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function button_hover(e) {
        setNodeAttribute(e.src(), 'src', getNodeAttribute(e.src(), 'src').replace(/_[n]\./, '_h.'));
}

function button_normal(e) {
        setNodeAttribute(e.src(), 'src', getNodeAttribute(e.src(), 'src').replace(/_[h]\./, '_n.'));
}

function behavior_hover(el) {
        connect(el, "onmouseover", this, "button_hover");
        connect(el, "onmouseout", this, "button_normal");
}

function open_win(url, target) {
        var newWin = window.open(url, target);
        newWin.focus();
}

function enable_next_step(step) {
    var nextStepId = $("toStep" + step);
    var nextStepImgId = nextStepId.getElementsByTagName('img')[0];
    setNodeAttribute(nextStepId, 'href', 'javascript:goToNextStep();');
    setNodeAttribute(nextStepId, 'class', 'next-link');
    setNodeAttribute(nextStepImgId, 'src', getNodeAttribute(nextStepImgId, 'src').replace('grey', 'blue'));
}

function initialize(current) {
	if ($(steps[current][DIVNAME]) == null) {
		callLater(0.2, initialize, current);
	}
	else {
        showStep(current);
        if ($('startButton') != null) {
            launcher = new EC2Launcher();
        }
        else {
            logDebug("No EC2 launcher configured");
        }

        forEach(getElementsByTagAndClassName(null, "button_image"), behavior_hover);
        forEach(getElementsByTagAndClassName(null, "button_player"), behavior_hover);
    }
}

function stepInit() {
	steps = steps_start;
	stepDiv = $("step_content");
	current = 0;
	checkSteps();
	initialize(current);
	document.getElementById("tour_title").innerHTML = tour_name;
}

function checkSteps() {
	if (steps != null) {
		for (i=0; i < steps.length; i++) {
			if (steps[i][MENUNAME]) {
				if (steps[i][MENUNAME] == steps[current][MENUNAME]) {
					$(steps[i][MENUNAME]).className = "menu_item_selected";
				} else {
					$(steps[i][MENUNAME]).className = 
						steps[i][ENABLED] ? "menu_item" : "menu_item_disabled";
				}
			}
		}
	}
}

function alignBottom(bottomDiv) {
	bottomDiv.style.marginTop = "";
	
	var totalHeight = Math.max(document.getElementById("menu").offsetHeight,
				document.getElementById("step_content").offsetHeight);
	totalHeight -= 23; // height of title div - can't find calculated height
	var offset = totalHeight - bottomDiv.parentNode.offsetHeight;
	bottomDiv.style.marginTop = offset + 'px';
}

function resetBottomDiv(currentDiv) {
	currentDiv.style.display = "none";
	findAndAlignBottomDiv(currentDiv);
}

function findAndAlignBottomDiv(currentDiv) {
	if (currentDiv.style.display != "") {
		currentDiv.style.display = "";
		for (i=0; i < currentDiv.childNodes.length; i++) {
			if (currentDiv.childNodes[i].className == "step_detail") {
				var stepDetail = currentDiv.childNodes[i];
				for (j=0; j < stepDetail.childNodes.length; j++) {
					if (stepDetail.childNodes[j].className == "bottom") {
						alignBottom(stepDetail.childNodes[j]);
							break;
						}
					}
				}
			}
		}
	}

function showStep(step_id) {
        if (!steps[step_id][ENABLED])
            return;
	if (current != null)
		document.getElementById(steps[current][DIVNAME]).style.display = "none";
	current = step_id;
	var nextDiv = document.getElementById(steps[current][DIVNAME]);
	// if not already being displayed, calculate position of any "bottom" div
	findAndAlignBottomDiv(nextDiv);
	checkSteps();
}

function previousStep() {
    var tempStep = current;
	do {
        tempStep--;
        if (tempStep < 0)
            return 0;
    } while (steps[tempStep][DIVNAME] == null)
	showStep(tempStep);
	checkSteps();
}

function goToNextStep() {
    var tempStep = current;
	do {
        tempStep++;
        if (tempStep >= steps.length)
            return;
    } while (steps[tempStep][DIVNAME] == null)
    
    steps[tempStep][ENABLED] = true;
	showStep(tempStep);
	checkSteps();
}

function setIpAddress(address) {
    if (address != null) {
        var rAA = "http://" + address + "/rAA/";
        var appliance = "http://" + address;
            
        //document.getElementById("config_rAA_url").href = 
        //    "javascript:open_win('"+rAA+"','raa');enable_next_step(" + 5 + ");";
        document.getElementById("appliance_url").href = 
            "javascript:open_win('" + appliance + "','appliance')";
    }
}

function selectPlayer(player_id) {
	if (player_id == PLAYER_XEN) {
		if (steps != steps_xen) {
            steps[4] = steps_xen[0];
			setIpAddress(null);
		}
	}
	else if (player_id == PLAYER_EC2) {
		if (steps != steps_ec2) {
			steps[4] = steps_ec2[0];
			setIpAddress(null);
		}
	}
	else if (player_id == PLAYER_VMWARE) {
		if (steps != steps_vmware) {
			steps[4] = steps_vmware[0];
			setIpAddress(null);
		}
	}
	else {
		steps = steps_start;
	}
}

function playerStarted(address) {
    if (address == null || address == "")
        return false;
	var i = address.indexOf("https://");
	if (i >= 0)
		address = address.substr(i + 8);
	else {
		i = address.indexOf("http://");
		if (i >= 0)
			address = address.substr(i + 7);
	}
	i = address.indexOf(":");
	if (i >= 0) 
		address = address.substr(0, i);
	setIpAddress(address);
}

function checkURL(alink) {	
	if (alink.href == alink.ownerDocument.URL) {
		alert("You must start the appliance before accessing the rPath Appliance Platform.");
		return false;
	}
	return true;
}

function email_registration() {
        
    goToNextStep();
}

