<!--
	var handlerArray = new Array(
		"Mecklenburg, Linda",
		"Deacon, Ashley",
		"Carbrey, Craig",
		"Bruera, Silvina",
		"Bloopers",
		"Bowers, Lisa",
		"Mantell, Marcy"
	);
	var breedArray = new Array(
		"B.C.",
		"Eng.Cocker Sp.",
		"Sheltie",
		"Pyrenean S."
	);
	var heightArray = new Array(
		'12"',
		'16"',
		'20"',
		'24"'
	);
	var typeArray = new Array(
		"Std",
		"JWW",
		"N.A.",
		"ISC"
	);
	var movieArray = new Array(
		{encryptName:"iuuq;00xxx/bhjmjuzwjefpt/dpn0Npwjft0171617.spvoe3.pwfsmbz.tufmmbs.hbups/xnw", title:"AKC/USA WT Tryouts", handlerIdx:0, dogName:"Stellar", breedIdx:0, heightIdx:3, typeIdx:0, date:"05/06/06"},
		{encryptName:"iuuq;00xxx/bhjmjuzwjefpt/dpn0Npwjft0171618.spvoe6.tnbsu.tct.nbswfm.mvlb/xnw", title:"AKC/USA WT Tryouts", handlerIdx:1, dogName:"Marvel/Luka", breedIdx:2, heightIdx:1, typeIdx:0, date:"05/07/06"},
		{encryptName:"iuuq;00xxx/bhjmjuzwjefpt/dpn0Npwjft0171617.dbscsfz.kxx/xnw", title:"English Cocker Sp. Nationals", handlerIdx:2, dogName:"Winston", breedIdx:1, heightIdx:1, typeIdx:0, date:"05/06/06"},
		{encryptName:"iuuq;00xxx/bhjmjuzwjefpt/dpn0Npwjft0171617.18.FDT.ibwjohgvo/xnw", title:"ECS having fun :)", handlerIdx:4, dogName:"", breedIdx:1, heightIdx:1, typeIdx:2, date:"05/07/06"},
		{encryptName:"iuuq;00xxx/bhjmjuzwjefpt/dpn0Npwjft0171233.fc35.kxx.cpxfst.tmjdl/xnw", title:"Portland, OR", handlerIdx:5, dogName:"Slick", breedIdx:0, heightIdx:3, typeIdx:1, date:"01/22/06"},
		{encryptName:"iuuq;00xxx/bhjmjuzwjefpt/dpn0Npwjft0171219.nboufmm.xbwf.kxx/xnw", title:"Marcy Mantell & Wave", handlerIdx:6, dogName:"Wave", breedIdx:2, heightIdx:0, typeIdx:1, date:"01/08/06"}
		//{encryptName:"", title:"", handlerIdx:, dogName:"", breedIdx:, heightIdx:, typeIdx:, date:""}
		);

	var theTable;
	var theTableHead;
	var theTableBody;
	var movieArraySortedByHandler;
	var movieArraySortedByDogName;
	var movieArraySortedByBreed;
	var movieArraySortedByHeight;
	var movieArraySortedByType;
	var idxMoviesShownInTableArray;
	var sortIndexActive = 0; // By default we do not sort the information

	// The window where the movie is going to be shown
	var movieWnd;
	var movieContent;

	// To avoid calling unnecesary functions, we track the screen state
	var isShowingMovie = false;

// Redraws the table of movies on screen
function updateMovieTable (recordRestriction){

	clearTable(theTable);
	buildTableHead(theTable);

	var myTr;
	var rowNum = 0;
	// reset indices shown
	idxMoviesShownInTableArray.length = 0;
	for (var i = 0; i < movieArray.length; i++){
		if ((recordRestriction == -1 || i == recordRestriction) && doesMeetSearchCriteria(i)){
			// Add index to the list of indeces shown in table
			idxMoviesShownInTableArray[rowNum] = i;
			myTr = theTableBody.insertRow(rowNum);
			// Any 'cell' in this record should trigger 'launchMovie()'
			myTr.onclick = launchMovie;
			myTr.onmouseover = setCursorToHand;
			myTr.onmouseout  = setCursorToNormal;

			myTr.movieIndexToLaunch = i;

			// Change background color to 'even' rows
			if (2*Math.round(rowNum/2) != rowNum)
				myTr.style.backgroundColor = "#eeeeee";

			fillRow(myTr, i);
			rowNum++;
		}
	}
	return;
}

// Gets the index that correspond to 'encryptedFile' or -1 if none is found
function getIdxOfEncryptedTitle (encryptedTitle) {
	for (var i=0; i < movieArray.length; i++){
		var movieRecord = movieArray[i];
		if (movieRecord.encryptName == encryptedTitle)
			return i;
	}
	return -1;
}

// When movies.html loads up, it might or might not contains parameters
// if a parameter is found, it should indicate the 'encrypted' name of the movie
// the user wants to see
function processParameters () {
	var idx;
	var q = location.search.substring(1);
	if (q.length == 0){
		idx = -1;
	} else {
		var idx = getIdxOfEncryptedTitle(q);
	}
	if (idx != -1){
		var movieRecord;
		// Get the movieRecord according to current sort
		movieRecord = getSortedRecord(idx);
	
		setUpMovieShowMode(idx);
		playOnMediaPlayer(decryptMovieName(movieRecord.encryptName));
	} 
	updateMovieTable(idx);
}

function decryptString (str) {
		var result = "";
		for (var i=0; i<str.length; i++){
				var codeAt = str.charCodeAt(i);
				var fromCharCode = String.fromCharCode(codeAt-1);
				result = result+fromCharCode;
		}
		return result;
}

function decryptMovieName (movieName){
	return decryptString(movieName);
}

function showMovie (encryptedName) {
	var videoHandler = document.getElementById("videoScreen");
	var decryptedName = decryptMovieName(encryptedName);
	var newHtml = '<object data="'+ decryptedName +'" type="video/x-ms-wmv" width="320" height="240"> <param name="ShowStatusBar" value="True"> <param name="src" value="'+ decryptedName +'"> <param name="autostart" value="True"> <param name="volume" value="0"> </object>';
	videoHandler.innerHTML = newHtml;
}

function hideMovieScreen () {
	var videoHandler = document.getElementById("videoScreen");
	var newHtml = '';
	videoHandler.innerHTML = newHtml;
}


// Initially the 'movieScreen' is not visible, and has no dimensions
// Just before the user starts watching the video, we need to make the
// movieString as big as the video
function setMovieScreenStyle () {
	var videoHandler = document.getElementById("videoScreen");
	videoHandler.style.width = "320px";
	videoHandler.style.height = "240px";
	videoHandler.style.border = "solid thin #cccc00";
	isShowingMovie = true;
}

function resetMovieScreenStyle () {
	var videoHandler = document.getElementById("videoScreen");
	videoHandler.style.width = "0px";
	videoHandler.style.height = "0px";
	videoHandler.style.border = "0";
	isShowingMovie = false;
	updateMovieTable(-1);

	var table = document.getElementById("watchTable");
	clearTable(table);
	setCursorToNormal();
	hideMovieScreen();
}

// When the user is watching a movie, [s]he can several things
// - Send the link to a friend
// - Do another search
function showUserWatchControl (idx) {
	var myTr;
	var myTd;
	var mailBody;
	var movieRecord;
	// Get the movieRecord according to current sort
	movieRecord = getSortedRecord(idx);
	mailBody = "Click on the link to watch the video: ";
	mailBody = mailBody + "http://www.AgilityInMotion.com/movies.html?";
	mailBody = mailBody +movieRecord.encryptName;

	var table = document.getElementById("watchTable");
	clearTable(table);
	var tableWatchHead = document.getElementById("watchTableHead");
	myTr = tableWatchHead.insertRow(0);
	myTd = myTr.insertCell(0);
	myTd.style.width = "250px";
	myTd.innerHTML = '<a href="mailto:?subject=Great agility video&body='+mailBody+'">Send link to a friend</a>';
	myTd = myTr.insertCell(1);
	myTd.innerHTML = "New Search";
	myTd.onclick   = resetMovieScreenStyle;
	myTd.onmouseover = setCursorToHand;
	myTd.onmouseout  = setCursorToNormal;

	return;
}

// Set the cursor to 'hand'
function setCursorToHand () {
	document.body.style.cursor = "pointer";
}

// Restores the cursor to 'normal'
function setCursorToNormal () {
	document.body.style.cursor = "default";
}

// When the user selects a movie to watch, the screen is modified like:
// - The title of the movie being watched will be highlighted
// - the movieScreen will appear
// - the controls to 'send the link' and 'restart a search' will appear
function setUpMovieShowMode (idx) {
	updateMovieTable(idx);
	showUserWatchControl(idx);
	if (!isShowingMovie){
		setMovieScreenStyle();
	}
}

function playOnMediaPlayer (movieTitle) {
	var videoHandler = document.getElementById("videoScreen");
	var newHtml = '<object data="'+ movieTitle +'" type="video/x-ms-wmv" width="320" height="240"> <param name="ShowStatusBar" value="False"> <param name="src" value="'+ movieTitle +'"> <param name="autostart" value="True"> <param name="volume" value="0"> </object>';
	videoHandler.innerHTML = newHtml;
}

// Launches a window where the user can watch the movie
function launchMovie (){
	setCursorToNormal();
	var movieRecord;
	// Get the movieRecord according to current sort
	movieRecord = getSortedRecord(this.movieIndexToLaunch);

	setUpMovieShowMode(this.movieIndexToLaunch);
	playOnMediaPlayer(decryptMovieName(movieRecord.encryptName));

	return;
}

// Checks against all search criteria established in the HTML page
function doesMeetSearchCriteria (idx) {

	// Check boundaries
	if (idx >= movieArray.length)
		return false;

	var movieRecord;
	movieRecord = getSortedRecord(idx);


	return doesMeetHandlerCriteria(movieRecord) && doesMeetBreedCriteria(movieRecord) && doesMeetHeightCriteria(movieRecord) && doesMeetTypeCriteria(movieRecord);
}

// Check if a form 'select' index, matches a field (handler or breed..etc)
function selectOptionIdxCheck (formSelectedIdx, recordFieldIdx){
	// When 'All' option is selected, 'idx' meets criteria
	if (formSelectedIdx == 0)
		return true;

	if (recordFieldIdx == formSelectedIdx-1)
		return true;
	else 
		return false;
}

// Check whether or not record  satisfies 
// the 'handler' criteria established by the user
function doesMeetHandlerCriteria (movieRecord) {
	var formSelectedIdx = document.movieForm.optHandler.selectedIndex;
	return selectOptionIdxCheck(formSelectedIdx, movieRecord.handlerIdx);
}

// Check whether or not record  satisfies 
// the 'breed' criteria established by the user
function doesMeetBreedCriteria (movieRecord) {
	var formSelectedIdx = document.movieForm.optBreed.selectedIndex;
	return selectOptionIdxCheck(formSelectedIdx, movieRecord.breedIdx);
}

// Check whether or not record  satisfies 
// the 'height' criteria established by the user
function doesMeetHeightCriteria (movieRecord) {
	var formSelectedIdx = document.movieForm.optHeight.selectedIndex;
	return selectOptionIdxCheck(formSelectedIdx, movieRecord.heightIdx);
}

// Check whether or not record  satisfies 
// the 'type' criteria established by the user
function doesMeetTypeCriteria (movieRecord) {
	var formSelectedIdx = document.movieForm.optType.selectedIndex;
	return selectOptionIdxCheck(formSelectedIdx, movieRecord.typeIdx);
}

// Builds the head of table dinamically, according
// to user specification
function buildTableHead (table) {
	var myTr;
	myTr = theTableHead.insertRow(0);

	fillRow(myTr, -1);

	return;
}

// Redraws the table, but indexed by Handler Name
function rebuildSortByHandler () {
	sortIndexActive = 1;
	// Do not rebuild if showing a movie (looses the movie that is showing)
	if (isShowingMovie)
		return;
	updateMovieTable(-1);
}
// Redraws the table, but indexed by Dog Name
function rebuildSortByDogName () {
	sortIndexActive = 2;
	// Do not rebuild if showing a movie (looses the movie that is showing)
	if (isShowingMovie)
		return;
	updateMovieTable(-1);
}
// Redraws the table, but indexed by Breed
function rebuildSortByBreed () {
	sortIndexActive = 3;
	// Do not rebuild if showing a movie (looses the movie that is showing)
	if (isShowingMovie)
		return;
	updateMovieTable(-1);
}
// Redraws the table, but indexed by Height
function rebuildSortByHeight () {
	sortIndexActive = 4;
	// Do not rebuild if showing a movie (looses the movie that is showing)
	if (isShowingMovie)
		return;
	updateMovieTable(-1);
}
// Redraws the table, but indexed by Type
function rebuildSortByType () {
	sortIndexActive = 5;
	// Do not rebuild if showing a movie (looses the movie that is showing)
	if (isShowingMovie)
		return;
	updateMovieTable(-1);
}
// Redraws the table, but indexed by Date (which is 'no index')
function rebuildSortByDate () {
	sortIndexActive = 0;
	// Do not rebuild if showing a movie (looses the movie that is showing)
	if (isShowingMovie)
		return;
	updateMovieTable(-1);
}

// returns 'recordNumber' from movieArray according to current sortIndex
function getSortedRecord (recordNumber){
	switch (sortIndexActive){
	case 0:
		return movieArray[recordNumber];
	case 1:
		return movieArray[movieArraySortedByHandler[recordNumber]];
	case 2:
		return movieArray[movieArraySortedByDogName[recordNumber]];
	case 3:
		return movieArray[movieArraySortedByBreed[recordNumber]];
	case 4:
		return movieArray[movieArraySortedByHeight[recordNumber]];
	case 5:
		return movieArray[movieArraySortedByType[recordNumber]];
	default:
		alert("Error: please inform fspadaro@agilityinmotion.com");
		return undefined;
	}
}

// Fills a row with the content of movieArray[recordNumber]
// if sortIndexActive > 0, then it uses index to sort the information
function fillRow (tr, recordNumber) {
	var movieRecord;
	var cellNum = 0;
	var myTd;

	myTd = tr.insertCell(cellNum);

	// If the recordNumber is -1, that it means is the title
	if (recordNumber != -1){
		movieRecord = getSortedRecord(recordNumber);
	}

	// Movie 'description'
	if (recordNumber == -1) {
		myTd.innerHTML = "Description";
	} else {
		myTd.innerHTML = movieRecord.title;
	}

	// Movie 'handlerIdx'
	cellNum++;
	myTd = tr.insertCell(cellNum);
	if (recordNumber == -1) {
		myTd.onclick = rebuildSortByHandler;
		myTd.innerHTML = "Handler Name";
	} else {
		//myTd.style.textAlign = "right";
		myTd.innerHTML = handlerArray[movieRecord.handlerIdx];
	}

	// Movie 'dogName'
	cellNum++;
	myTd = tr.insertCell(cellNum);
	if (recordNumber == -1) {
		myTd.onclick = rebuildSortByDogName;
		myTd.innerHTML = "Dog Name";
	} else {
		myTd.innerHTML = movieRecord.dogName;
	}

	// Movie 'breed'
	cellNum++;
	myTd = tr.insertCell(cellNum);
	if (recordNumber == -1) {
		myTd.onclick = rebuildSortByBreed;
		myTd.innerHTML = "Breed";
	} else {
		myTd.innerHTML = breedArray[movieRecord.breedIdx];
	}

	// Movie 'height'
	cellNum++;
	myTd = tr.insertCell(cellNum);
	if (recordNumber == -1) {
		myTd.onclick = rebuildSortByHeight;
		myTd.innerHTML = "Ht.";
	} else {
		myTd.innerHTML = heightArray[movieRecord.heightIdx];
	}

	// Movie 'Type'
	cellNum++;
	myTd = tr.insertCell(cellNum);
	if (recordNumber == -1) {
		myTd.onclick = rebuildSortByType;
		myTd.innerHTML = "Type";
	} else {
		myTd.innerHTML = typeArray[movieRecord.typeIdx];
	}

	// Movie 'date'
	cellNum++;
	myTd = tr.insertCell(cellNum);
	if (recordNumber == -1) {
		myTd.onclick = rebuildSortByDate;
		myTd.innerHTML = "Date";
	} else {
		myTd.innerHTML = movieRecord.date;
	}

	return;
}

function clearTable(table) {
	var numOfRows = table.rows.length;
	for (var i = 0; i < numOfRows; i++){
		// JavaConsole gave an error when using deleteRow()
		// so I will delete always the first element
		table.deleteRow(0);
	}
}

// Init global variables and data
function initData () {
	theTable = document.getElementById("myTable");
	theTableHead = document.getElementById("myTableHead");
	theTableBody = document.getElementById("myTableBody");

	initIndexes();
	initFilters();
}

function initFilters () {
	initHandlerOptions();
	initBreedOptions();
	initHeightOptions();
	initTypeOptions();
}
function initHandlerOptions () {
	var theOptions = document.movieForm.optHandler;
	initGenericOptions(theOptions, handlerArray);
}
function initBreedOptions () {
	var theOptions = document.movieForm.optBreed;
	initGenericOptions(theOptions, breedArray);
}
function initHeightOptions () {
	var theOptions = document.movieForm.optHeight;
	initGenericOptions(theOptions, heightArray);
}
function initTypeOptions () {
	var theOptions = document.movieForm.optType;
	initGenericOptions(theOptions, typeArray);
}

function initGenericOptions (theOptions, dataArray) {
	for (var i=0; i < dataArray.length; i++){
		var h = new Option (dataArray[i], dataArray[i], false, false);
		theOptions.options[theOptions.options.length] = h;
	}
}

// Initialize the array of indexes that will point to movieArray
// by different criterias
function initIndexes () {
	movieArraySortedByHandler  = new Array(movieArray.length);
	movieArraySortedByDogName  = new Array(movieArray.length);
	movieArraySortedByBreed    = new Array(movieArray.length);
	movieArraySortedByHeight   = new Array(movieArray.length);
	movieArraySortedByType     = new Array(movieArray.length);
	idxMoviesShownInTableArray = new Array(movieArray.length);

	for (var i = 0; i < movieArray.length; i++){
		movieArraySortedByHandler[i] = i;
		movieArraySortedByDogName[i] = i;
		movieArraySortedByBreed[i]   = i;
		movieArraySortedByHeight[i]  = i;
		movieArraySortedByType[i]    = i;
	}

	var idxI;
	var idxJ;
	var tmpVar;
	for (var i = 0; i < movieArray.length-1; i++){
		for (var j = i+1; j < movieArray.length; j++){
			// By handlerIdx
			idxI = movieArraySortedByHandler[i];
			idxJ = movieArraySortedByHandler[j];
			if (handlerArray[movieArray[idxJ].handlerIdx] < handlerArray[movieArray[idxI].handlerIdx]){
				tmpVar = movieArraySortedByHandler[i];
				movieArraySortedByHandler[i] = movieArraySortedByHandler[j];
				movieArraySortedByHandler[j] = tmpVar;
			}
			// By dogName
			idxI = movieArraySortedByDogName[i];
			idxJ = movieArraySortedByDogName[j];
			if (movieArray[idxJ].dogName < movieArray[idxI].dogName){
				tmpVar = movieArraySortedByDogName[i];
				movieArraySortedByDogName[i] = movieArraySortedByDogName[j];
				movieArraySortedByDogName[j] = tmpVar;
			}
			// By breed
			idxI = movieArraySortedByBreed[i];
			idxJ = movieArraySortedByBreed[j];
			if (breedArray[movieArray[idxJ].breedIdx] < breedArray[movieArray[idxI].breedIdx]){
				tmpVar = movieArraySortedByBreed[i];
				movieArraySortedByBreed[i] = movieArraySortedByBreed[j];
				movieArraySortedByBreed[j] = tmpVar;
			}
			// By height
			idxI = movieArraySortedByHeight[i];
			idxJ = movieArraySortedByHeight[j];
			if (heightArray[movieArray[idxJ].heightIdx] < heightArray[movieArray[idxI].heightIdx]){
				tmpVar = movieArraySortedByHeight[i];
				movieArraySortedByHeight[i] = movieArraySortedByHeight[j];
				movieArraySortedByHeight[j] = tmpVar;
			}
			// By type
			idxI = movieArraySortedByType[i];
			idxJ = movieArraySortedByType[j];
			if (typeArray[movieArray[idxJ].typeIdx] < typeArray[movieArray[idxI].typeIdx]){
				tmpVar = movieArraySortedByType[i];
				movieArraySortedByType[i] = movieArraySortedByType[j];
				movieArraySortedByType[j] = tmpVar;
			}
		}
	}
}

-->

