
	//Global Namespaces & vars
	var tempMissionData	 			= {};
	tempMissionData.tempid 			= 0;
	tempMissionData.lastcommand 	= 0;
	
	// Init on page load
	dojo.addOnLoad(function(){
		//makeIFrameProxy();
	});
	
	function changestep(direction){
		var curstephandler  = "step" + stepdata.curstep;
		var tipshandler 	= "tips" + stepdata.curstep;
		hideMe(curstephandler);
		hideMe(tipshandler);
		
		if (direction == 'next'){
			stepdata.nextstep = stepdata.curstep +1;
		} else {
			stepdata.nextstep = stepdata.curstep -1;
		}
		if (stepdata.nextstep > 1){
			showMe('backbutton');
			showMe('nextbutton');
		} else {
			hideMe('backbutton');
		}
		
		if (stepdata.nextstep == stepdata.totalsteps){
			hideMe('nextbutton');
			showMe('setupbutton');
		} else {
			hideMe('setupbutton');
		}
		var nextstephandler = "step" + stepdata.nextstep;
		var nexttipshandler = "tips" + stepdata.nextstep;
		showMe(nextstephandler);
		showMe(nexttipshandler);
		
		stepdata.curstep = stepdata.nextstep;
	}
	function startup(){
		hideMe('introtext');
		hideMe('startbutton');
		showMe('nextback');
		changestep('next');
		gettempmissionid();
	}
	
	//create a temporary missionid
	function gettempmissionid(){
		var xhrArgs = {
				url: '/build/newmissionshell/format/json',
				handleAs: 'json',
				content: {},
				load: function(response) {
					extrasLogger("gettempmissionid response: " + response.missionid);
					tempMissionData.tempid = response.missionid;
					//tellIframeAboutMissionId(tempMissionData.tempid);
					if (configdata.uploader){
						writeThirdPartyFrame(tempMissionData.tempid);
					}
				},
				error: function(error) {
					extrasLogger('gettempmissionid ERROR: ' + error);
				}
		};
		var deferred = dojo.xhrPost(xhrArgs);
	}
	
	
	//WRITE 3rd PARTY FRAME

	
	//CROSSFRAME
	//crossframe tecnique from: 
	//http://www.shouldersofgiants.co.uk/Blog/post/2009/08/17/Another-Cross-Domain-iFrame-Communication-Technique.aspx
	
	//create an iframe for crossframe proxy communication
	//This isn't working for some reason - wont pass msg on unless iframe is embedded in host parent page
	function makeIFrameProxy() {
	   extrasLogger('PARENT: makeIFrameProxy');
	   ifrm = document.createElement("IFRAME"); 
	   ifrm.setAttribute("src", stepdata.innerFrameProxy); //this src is set by the parent page
	   ifrm.setAttribute("id", "innerFrameProxy");
	   ifrm.style.width = 200+"px"; 
	   ifrm.style.height = 200+"px"; 
	   proxyNode 		= document.getElementById('proxyNode');
	   proxyNode.appendChild(ifrm);
	} 
	
	//using the iframe crossframe proxy method, send the missionid to the iframe
	function tellIframeAboutMissionId(mid){
		extrasLogger('Parent: Create.js: tellIframeAboutMissionId:' + mid);
		SendMessageToFrame("mid=" + mid);
	}
	
    function OnMessageFromChild(message) {
        if (message.length > 0) {
            var parameters = parseParameters(message);
            if (parameters["height"] != null) {
                document.getElementById('hostFrame').height = parseInt(parameters["height"]);
            }

            if (parameters["command"] != null) {
            	extrasLogger('PARENT: OnMessageFromChild Command: ' + parameters["command"]);
            	switch (parameters["command"]){
            		case 'getMissionId':
            			getMissionId();
            			break;
            		case 'hideControlButtons':
            			hideControlButtons();
            			break;
            		case 'showControlButtons':
            			showControlButtons();
            			break;
            		case 'changestepNext': //command will still be set in the proxy frame - this bit avoids a repeat call of these functions which will mess up the UI. repeats of the others are ok
            			if (parameters["command"] != tempMissionData.lastcommand){
            				changestep('next');
            			}
            			break;
            		case 'changestepBack':
            			if (parameters["command"] != tempMissionData.lastcommand){
            				changestep('back');
            			}
            			break;
            	}
            	tempMissionData.lastcommand = parameters["command"];
            }
        }
    }

    function parseParameters(message) {
        var dictionary = new Array();
        var pairs = message.split(/&/);
        for (var keyValuePairIndex in pairs) {
            var nameVal = pairs[keyValuePairIndex].split(/=/);
            dictionary[nameVal[0]] = nameVal[1];
        }
        return dictionary;
    }

    // Send a message to the iFrame via the proxy, toggling the iFrame's
    // size to indicate there is a message ready to be read
    function SendMessageToFrame(message) {
    	var proxyWithMessage		= stepdata.innerFrameProxy + '#' + message;
    	extrasLogger('PARENT: SendMessageToFrame:' + proxyWithMessage)
        var elem = document.getElementById('innerFrameProxy');
        elem.contentWindow.location = proxyWithMessage;
        elem.width = elem.width > 50 ? 50 : 100;
    }
   
  //iframe CONTROLLERS - allow iframes to do various things
	function getMissionId(){
		extrasLogger('PARENT: getMissionId:' + tempMissionData.tempid);
		SendMessageToFrame("mid=" + tempMissionData.tempid);
	}
	function hideControlButtons(){
		extrasLogger('PARENT: hideControlButtons');
		hideMe('nextbutton');
		hideMe('backbutton');
	}
	function showControlButtons(){
		showMe('nextbutton');
		showMe('backbutton');
	}
