/* DOJO STUFF */

dojo.require("dojo._base");
dojo.require("dojo._base.fx");
dojo.require("dojo.fx.easing");

function connectivityError(data, ioArgs) {
	alert(dojo.toJson(data));
	alert("There was an error in connecting to the server: " + dojo.toJson(ioArgs));
	}

/* CLASSES */

function page(title, description, front, back, pageNumber) {
	this.pageNumber = pageNumber;
	this.active = false;
	this.frontFacing = true;
	
	this.title = title;
	this.description = description;
	
	this.frontImg = dojo.create("img");
	this.frontImg.src = front;
	this.frontImg.style.width = pageWidth+"px";
	this.frontImg.style.height = pageHeight+"px";
	this.frontImg.owner = this;
	this.frontImg.onclick = function() { this.owner.flip(); };
	
	this.backImg = dojo.create("img");
	this.backImg.src = back;
	
	// If using simple flip method
	this.backImg.style.width = pageWidth+"px";
	this.backImg.style.height = pageHeight+"px";
	this.backImg.style.display = "none";
	
	// If using fancy flip
	/*
	this.backImg.style.width = "0px";
	this.backImg.style.height = pageHeight+"px";
	*/
	
	this.backImg.owner = this;
	this.backImg.onclick = function() { this.owner.flip(); };
	
	this.div = dojo.create("div");
	this.div.style.position = "absolute";
	this.div.style.top = "50px";
	this.div.style.left = (pageWidth+50) * (pageNumber-1);
	
	dojo.place(this.frontImg, this.div, "last");
	dojo.place(this.backImg, this.div, "last");
	
	dojo.place(this.div, dojo.byId("pages"), "last");
}
	
page.prototype.displayText = function() {
	dojo.animateProperty({
		node: dojo.byId("pageText"),
		duration: 400,
		delay: 0,
		properties: {
			opacity: { start: 1.0, end: 0.0, unit: "" }
			}
		}).play();
	
	setTimeout("dojo.byId('pageText').innerHTML = unescape('"+escape(this.description)+"')", 400);
	dojo.animateProperty({
		node: dojo.byId("pageText"),
		duration: 400,
		delay: 0,
		properties: {
			opacity: { start: 0.0, end: 1.0, unit: "" }
			}
		}).play();
	}

page.prototype.flip = function() {
	
	if (this.active) {
		hasFlipped = true;
		// Simple method of flipping...
		if (this.frontFacing) {
			this.frontImg.style.display = "none";
			this.backImg.style.display = "block";
			this.frontFacing = false;
			}
		else {
			this.backImg.style.display = "none";
			this.frontImg.style.display = "block";
			this.frontFacing = true;
			}
		// Fancy flip method
		/*
		if(this.frontFacing) {
			
			this.frontImg.style.marginLeft = "0px";
			this.backImg.style.marginLeft = "0px";
			dojo.animateProperty({
				node: this.frontImg,
				delay: 0,
				duration: 290,
				properties: {
					width: { start: pageWidth, end: 0, unit: "px" },
					marginLeft: {start: 0, end: pageWidth/2, unit: "px" },
					opacity: { start: 1.0, end: 0.6, unit: ""}
					}
				}).play();
			dojo.animateProperty({
				node: this.frontImg,
				delay: 300,
				duration: 1,
				properties: {
					marginLeft: { start: pageWidth/2, end: 0, unit: "px" },
					opacity: { start: 0.6, end: 1.0, unit: "" }
					}
				}).play();
			dojo.animateProperty({
				node: this.backImg,
				delay: 310,
				duration: 300,
				properties: {
					width: { start: 0, end: pageWidth, unit: "px" },
					marginLeft: {start: pageWidth/2, end: 0, unit: "px" },
					opacity: { start: 0.6, end: 1.0, unit: ""}
					}
				}).play();
			
			this.frontFacing = false;
		} else {
			this.backImg.style.marginLeft = "0px";
			this.frontImg.style.marginLeft = "0px";
			dojo.animateProperty({
				node: this.backImg,
				delay: 0,
				duration: 290,
				properties: {
					width: { start: pageWidth, end: 0, unit: "px" },
					marginLeft: {start: 0, end: pageWidth/2, unit: "px" },
					opacity: { start: 1.0, end: 0.6, unit: ""}
					}
				}).play();
			dojo.animateProperty({
				node: this.backImg,
				delay: 300,
				duration: 1,
				properties: {
					marginLeft: { end: 0, start: pageWidth/2, unit: "px" },
					opacity: { start: 0.6, end: 1.0, unit: "" }
					}
				}).play();
			dojo.animateProperty({
				node: this.frontImg,
				delay: 310,
				duration: 300,
				properties: {
					width: { start: 0, end: pageWidth, unit: "px" },
					marginLeft: {start: pageWidth/2, end: 0, unit: "px" },
					opacity: { start: 0.6, end: 1.0, unit: ""}
					}
				}).play();
			
			this.frontFacing = true;
			}*/
		}
	else {
		hasNavigated = true;
		setCurrentPage(this.pageNumber);
		}
	tryRemoveAdvice();
	}

page.prototype.setActive = function(active) {
	if (active) {
		this.div.style.opacity = "1.0";
		this.displayText();
		this.active = true;
		}
	else {
		this.div.style.opacity = "0.4";
		this.active = false;
		}
	}

/* GLOBAL VARIABLES */

var hasFlipped = false;
var hasNavigated = false;

var docWidth;
var pageHeight;
var pageWidth;
var pages = new Array();

/* LOADING FUNCTIONS */

window.onload = function() {
	docWidth = getDocWidth();
	
	dojo.xhrGet({
		url: 'prospectusPageList.json',
		handleAs: 'json',
		load: loadProspectusPageList,
		error: connectivityError
		});
	}

function getDocWidth() {
	if (typeof window.innerWidth != 'undefined') {
		return window.innerWidth;
		}
	else if (typeof document.documentElement != 'undefined'
		&& typeof document.documentElement.clientWidth !=
		'undefined' && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientWidth;
		}
	else {
		return document.getElementsByTagName('body')[0].clientWidth;
		}
	}

function loadProspectusPageList(data, ioArgs) {
	loadPageImages(data);
	}

function loadPageImages(json) {
	pageWidth = json.pageWidth;
	pageHeight = json.pageHeight;
	for( i=0; i < json.pages.length; i++) {
		pages.push(new page(
			json.pages[i].title, 
			json.pages[i].description,
			json.pages[i].front,
			json.pages[i].back,
			i
			));
		}
	setCurrentPage(0);
	}

function setCurrentPage(page) {
	
	for( i = 0; i < pages.length; i++ ) {
		if (i == page) {
			pages[i].setActive(true);
			}
		else {
			pages[i].setActive(false);
			}
		}
	
	newPos = (docWidth/2) + (pageWidth/2) +50 - ((pageWidth+50) * page);
	
	dojo.animateProperty({
		node: dojo.byId("pages"),
		delay: 0,
		duration: 600,
		properties: {
			left: { end: newPos, unit: "px" }
			},
		easing: dojo.fx.easing.backOut
		}).play();
	}

function tryRemoveAdvice() {
	if (hasFlipped && hasNavigated) {
		dojo.animateProperty({
			node: dojo.byId("top"),
			duration: 800,
			delay: 0,
			properties: {
				opacity: { end: 0.0, unit: "" }
				}
			}).play();
		}
	}
