/**
 *    Copyright (c) 2009-2010, As Is Software, dba Media Lab Inc.
 *    http://www.medialab.com
 */

/*
 * 	Notes:
 * 	1. Nice HTML thumbsheet here: http://plt-scheme.org/screenshots/
 */

var com;
if(!com) { com = {}; } else if(typeof(com) != "object") { throw new Error("can't initialize...com is not an object."); }
if(!com.medialab) { com.medialab = {}; } else if(typeof(com.medialab) != "object") { throw new Error("can't initialize...com.medialab is not an object."); }
if(!com.medialab.sg) { com.medialab.sg = {}; }
if (!com.medialab.sg.gallery1) { com.medialab.sg.gallery1 = {};}


com.medialab.sg.gallery1.thumbsheetFlashBasic =
function (thumbSheetXMLIn, modeIn, docentIn) { 
	var that=this;
	var debug = false;
	var sgalert = function(msg) { if (debug) {
		alert(msg);
	} };
	var docent= docentIn;

	//alert(docentIn);
	var xml = thumbSheetXMLIn;
	
	var thumbWidth = 0;
	var thumbHeight = 0;
	var sheetWidth = 0;
	var sheetHeight = 0;
	var sheetMode = modeIn;
	var linkClass = "not set yet";
	var changeimageonhover = "false";
	
	var timerID = 0;		  // used for async waiting for Flash movie to load
	var intervalTicks = 1000; // frequency of checking.  Shorten this for final build.
	
	//
	// var flashName:
	//
	// Flash embed object name/id for swfobject to set.
	// It is then used to get the browser object reference.
	// (Why isn't that just returned from swfobject.embedSWF()?  I have no idea.)
	// It is initialized in initPictureBox() to be "flashmovie" + a unique ID. 
	//
	
	var flashName = "not set yet";			

	//
	// Below options will come from xml.  See processXML()
	//
	
	var myWidth = 0;
	var myHeight = 0;
	var flashFileName = "not set yet";		// Actual file path of Flash movie for swfobject
	var flashBrowserRef = "not set yet";	// cached reference to Flash browser object to use for function calling
	var myDivID = "not set yet";
	var myClickAction = "not set yet";
	var myHoverAction = "not set yet";

	//***
	//***   		SET-UP FUNCTIONS
	//***
	
	function init() {
		sgalert("SG Standard Thumbsheet: Init", this);
		//if(getOption("type")!="sitegrinderhorizontalflash") { alert("Type mismatch between thumbsheet javascript (sitegrinderhorizontalflash) and data ("+ getOption('type') +") !"); return;}
		if(getOption("version") > 1.0) { alert("Thumbsheet javascript version older than data version!"); return;}
		processXML();
		if (numExhibits > 0) {
			initSheet();
		}
	}
	
	function processXML () {
		sgalert("SG Standard Thumbsheet: processXML", this);
		/*
		 * we'll need to know:
		 * 
		 * 		- thumb div name prefix
		 * 		- options (scroll? change on hover?)
		 * 
		 *     1. count the thumbs (to figure out how to go page to page
		 */
		docent.sgconsole("filename " + getOption("filename"));
		flashFileName = getOption("filename");
		numExhibits = docent.getExhibitCount();
//		sheetMode = getOption("mode");
		sheetWidth = getOption("sheetwidth");
		sheetHeight = getOption("sheetheight");
		thumbWidth = getOption("thumbwidth");
		thumbHeight = getOption("thumbheight");
		sheetDivID = getOption("sheetdivname");
		linkClass = getOption("linkclassname");
		changeimageonhover = getOption("changeimageonhover");

		return;
	}
	
	// initSheet
	//
	// Create the sheet element and inject it into the DOM.  
	// Load external files, ie Flash if required.
	//
	function initSheet() {
		
		
		// Set up Flash container uing swfobject()
		var flashvars = {};
		
		var params = {};
		params.allowScriptAccess = "always";
		params.scaleMode = "noScale";
		params.wmode = "transparent";
		
		var d = new Date(); // for constructing a unique name
		flashName = "flashmovie" + d.getTime();
		docent.sgconsole("naming picturebox move " + flashName);
		
		// add internal div for flash embed to replace
		$ml('#'+sheetDivID).html('<div id="'+flashName+'"><p></p></div>');
		var attributes = {};
		attributes.id = flashName;
		attributes.NAME=flashName;
					
		swfobject.embedSWF(flashFileName, flashName, sheetWidth, sheetHeight, "9.0.0", false, flashvars, params, attributes);
		
		// Set up timer to intialize Flash movie once it loads
		timerID = setInterval ( waitForFlashLoad, intervalTicks );// hey
	}

/* This version works but doesn't insert the a new div
	function initSheet() {
		docent.sgconsole("Initializing Flash Thumbsheet");
		// Set up Flash container uing swfobject()
		var flashvars = {};
		
		var params = {};
		params.allowScriptAccess = "always";
		params.scaleMode = "noScale";
		params.wmode = "transparent";
		
		var d = new Date(); // for constructing a unique name
		flashName = "flashmovie" + d.getTime();
		docent.sgconsole("naming thumb move " + flashName);
		
		var attributes = {};
		attributes.id = flashName;
		attributes.NAME=flashName;
	    docent.sgconsole("Embedding " + flashFileName);
		swfobject.embedSWF(flashFileName, sheetDivID, sheetWidth, sheetHeight, "9.0.0", false, flashvars, params, attributes);		
		// Set up timer to intialize Flash movie once it loads
		timerID = setInterval ( waitForFlashLoad, intervalTicks );// hey
	}
*/	
	//
	// waitForFlashLoad
	//
	// Use some docent utility routines to check if Flash movie is loaded
	//
	
 	function waitForFlashLoad () {
		//docent.sgconsole("getFlashMovie returning " + docent.getFlashMovie(flashName));
		if (docent.flashMovieIsLoaded(docent.getFlashMovie(flashName))) {
			clearInterval(timerID);
			that.flashLoaded();
		}
		else { /*docent.sgconsole("Flash movie "+ flashFileName +" not loaded into "+ sheetDivID + " yet!"); */}
	}

	
	// 		flashLoaded()
	//
	// Called when Flash movie is finally loaded to
	// finish setting it up.  Should probably be private,
	// but not sure if timer could call it if it were.
	//	
	this.flashLoaded = function () {
		flashBrowserRef = docent.getFlashMovie(flashName);
		docent.sgconsole(flashBrowserRef);
		//flashBrowserRef.newImage(null);
		//var preparedXML = prepareCoverFlowXML();
		flashBrowserRef.initWithXML($ml(xml).toXML(), docent.getName(), flashName, sheetMode, docent.getCurrentExhibit(), docent.getCurrentView());
		$ml('#'+flashName).css('outline','none');
		docent.thumbSheetReady();
	}
	 

	//***
	//***   		CALLBACK FUNCTIONS
	//***

	
	// 		select()
	//
	// called whenever a new image is selected to sync
	// could be called becuase a thumb (including this one) is clicked
	// or for any other reason the docent wants, like next/prev buttons.
	//
	// don't do anything if already synced 
	//
	// If not synced then hilite apropriate thumb if necessary
	// and dehilite others if necessary
	//
	this.select = function (thumbnum) {
		flashBrowserRef = docent.getFlashMovie(flashName);
		if(flashBrowserRef) { flashBrowserRef.selectThumb(thumbnum); }
	}
			
	// 		updateSheet()
	//
	// Generally caled on a view sheet when the active exhibit changes.
	// Usually implemented as a complete reset with new images...
	// but without, for example, having to reload the Flash movie if
	// it doesn't have to be.
	//
	this.updateSheet = function(){
		docent.sgconsole(sheetMode + " sheet UpdateSheet " + docent.getCurrentExhibit() + " " + docent.getCurrentView());
//		initSheet();
		flashBrowserRef.updateSheet(docent.getCurrentExhibit(),docent.getCurrentView());
//		flashBrowserRef.initWithXML(newXml,docent.getName(),sheetMode);
	}

	//***
	//***   		UTILITY FUNCTIONS
	//***

	function getOption(opt) {
		return docent.getThumbSheetOption(sheetMode,opt);
	}
		
	init();
}
