// ********************************
//
// Class: EventsIndex
//
// ********************************

function EventsIndex() {
	this.FO = null;

	this.chapterVideos = [
		"Chapter-1-PreHike-faststart_640x480.flv", 
		"Chapter-2-Montana-faststart_640x480.flv", 
		"Chapter-3-Wyoming-faststart_640x480.flv", 
		"Chapter-4-Colorado-faststart_640x480.flv",
		"Chapter-5-NewMex-faststart_640x480.flv"
	];
};


// --------------------------------
//
// Behaviour
//
// --------------------------------

EventsIndex.prototype.behaviour = function() {
	var eventsIndexEvents = {
		"#div-Chapters a.download": {
			"click": {
				context: this, handler: this.chaptersSaveACopy
			}
		},
		"#div-Chapters a.watch": {
			"click": {
				context: this, handler: this.launchFLV
			}
		},
		"#a-FlvPlayer-Close": {
			"click": {
				context: this, handler: this.closeFLV
			}
		}
	};

	Behaviour.applyRules(eventsIndexEvents);
} // end EventsIndex.behaviour()


// --------------------------------
//
// Event handling methods
//
// --------------------------------

EventsIndex.prototype.init = function(e, el) {
	this.behaviour();
} // end EventsIndex.init()

// Don't allow users to launch individual chapters in QuickTime plug in.  Only 
// allow them to RightClick and save link/target as.
EventsIndex.prototype.chaptersSaveACopy = function(e, el) {
	Events.cancel(e);
	return false;
}

// Close the FLV player
EventsIndex.prototype.closeFLV = function(e, el) {
	if (e) {
		Events.cancel(e);
	}

	if (this.divFlvPlayer) {
		Element.remove(this.divFlvPlayer);
		this.divFlvPlayer = null;
	}

	// Delete our existing FO object
	try {
		delete this.FO;
		this.FO = null;
	}
	catch (exc) {
		this.FO = null;
	}
} // end EventsIndex.closeFLV()
	
// Launch the low resolution FLV player in a new absolutely positioned div
EventsIndex.prototype.launchFLV = function(e, el) {
	Events.cancel(e);

	// 1) Remove any existing div-FlvPlayer 
	this.closeFLV();

	// 2) Create the div-FlvPlayer and child elements
	this.divFlvPlayer = Element.create("div", {id: "div-FlvPlayer"});

	this.divFlvPlayer.appendChild(
		Element.create("a", {id: "a-FlvPlayer-Close", href: "javascript:void(0);"}, "Close window"));

	var pFlvPlayer = Element.create("p", {id: "p-FlvPlayer"});
	pFlvPlayer.appendChild(
		Element.create("a", 
			{href: "http://www.macromedia.com/go/getflashplayer"}, 
			"Get the Flash Player to see this player."));

	this.divFlvPlayer.appendChild(pFlvPlayer);

	// 3) Absolutely position the div-FlvPlayer
	var divContainer = Element.get("div-Container");

 	var topPos = 0;

	if (parent.frames && parent.frames.length) {
		var fr = parent.frames[0];
		topPos = fr.pageYOffset || fr.document.documentElement.scrollTop || fr.document.body.scrollTop;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {
		topPos = document.documentElement.scrollTop;
	}

	topPos += 115;

	var leftPos = 75 + divContainer.offsetLeft;
	Element.setXY(this.divFlvPlayer, leftPos, topPos);

	// 4) Add the FLV Player to the DOM	
	divContainer.appendChild(this.divFlvPlayer);

	// 5) Get chapter number of the requested video
	var chapterNum = String(Element.getParent(el, "div").id).split("-")[2] - 1;

	// 6) Delete our existing FO object
	try {
		delete this.FO;
		this.FO = null;
	}
	catch (exc) {
		this.FO = null;
	}

	// 7) And create the new FO object
	this.FO = {
		movie: "https://media.dreamhost.com/mediaplayer.swf",
		allowfullscreen: "false",
		width: "640",
		height: "480",
		majorversion: "7",
		build: "0",
		bgcolor: "#ffffff",
		flashvars: "file=http://d-low.com/thewalkumentary/movies/" + this.chapterVideos[chapterNum] + "&showdigits=true&autostart=true&allowfullscreen=false" 
	};
	UFO.create(this.FO, "p-FlvPlayer");	
	
	// 8) And finally apply behaviour...	
	this.behaviour();	
} // end EventsIndex.prototype.launchFLV()


// --------------------------------
//
// Global Instance
//
// --------------------------------

var eventsIndex = new EventsIndex();


// --------------------------------
//
// Initialize on page load
//
// --------------------------------

Events.add({element: window, type: "load", context: eventsIndex, handler: eventsIndex.init});



