
dojo.require("dojo.parser");
dojo.require("dojo.fx");
dojo.require("dojo.io.iframe");

var currentContent = FIRSTCONTENT;
var currentPage = FIRSTPAGE;
var contentDiv;
var processor;

function loadURI(uri,async) {
    var d = Sarissa.getDomDocument();
    d.async = false;
    d.load(uri);
    if (d.parseError && d.parseError.errorCode != 0) {
	throw("Error loading " + uri + ": " + Sarissa.getParseErrorText(d));
    }
    return d;
}

function endsWith(testString, endingString){
      if(endingString.length > testString.length) return false;
      return testString.indexOf(endingString)==(testString.length-endingString.length);
}

dojo.addOnLoad(function() {
    dojo.parser.parse();
    contentDiv = document.getElementById("content");
    dojo.style("content", "height", "0px");
    loadContent(FIRSTCONTENT, FIRSTPAGE);
});

function loadContent(contentLink, firstPage) {
/*    if(contentLink != currentContent) {
	dojo.fadeOut({node: currentPage, duration: 250,
	    onEnd: function() {
		dojo.style(currentPage, "display", "none");
		dojo.style("menu" + currentPage, "color", "black");
	    }
	}).play();
	
    } // Must be coded synchronously or combined with effect in loadedContent!
*/  
    console.log("loadContent started [" + contentLink + "][" + firstPage + "]");
    contentDiv.innerHTML = "<h2>Loading...</h2>";
    FIRSTPAGE = firstPage;

    var xhrArgs = {
	url: contentLink,
	handleAs: "text",
	load: function(data) {
	    contentDiv.innerHTML = data;
	    loadedContent();
	},
	error: function(error) {
	    console.log("An unexpected error occurred: " + error);
	    contentDiv.innerHTML = "An unexpected error occurred: " + error;
	}
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrGet(xhrArgs);
    console.log("loadContent finished");
}

function loadedContent() {
    currentPage = FIRSTPAGE;
    dojo.fadeIn({node: currentPage, duration: 250,
	beforeBegin: function() {
	    dojo.style(currentPage, "opacity", 0);
	    dojo.style(currentPage, "display", "block");
	    dojo.style("menu" + currentPage, "color", "white");
	    dojo.style("content", "height", dojo.style(currentPage, "height") + "px");
	},
	onEnd: function() {
	    dojo.style("content", "height", dojo.style(currentPage, "height") + "px");
	}
    }).play();
    console.log("loadedContent finished");
}

function showPage(page) {
    if(page != currentPage) {
	dojo.fx.combine([
	    dojo.fadeIn({node: page, duration: 250,
		beforeBegin: function() {
		    dojo.style(page, "opacity", 0);
		    dojo.style(page, "display", "block");
		    dojo.style("menu" + page, "color", "white");
		    dojo.style("content", "height", dojo.style(page, "height") + "px");
		},
		onEnd: function() {
		    dojo.style("content", "height", dojo.style(page, "height") + "px");
		}
	    }),
	    dojo.fadeOut({node: currentPage, duration: 250,
		onEnd: function() {
		    dojo.style(currentPage, "display", "none");
		    dojo.style("menu" + currentPage, "color", "black");
		    currentPage = page;
		}
	    })
	]).play();
    }
};


