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

/*
 * 	PictureBox Interface
 * 	
 *  
 *  
 */

// Create namespace

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 = {};}

// Picturebox object declaration

com.medialab.sg.gallery1.beforeAfterPicturebox = function(pictureboxXMLIn, docentIn){


	////
	////		"PRIVATE" Variables
	////
	
	var that = this;  		// store for when object scope isn't available
	var debug = false;  	// use to turn on or off debug alerts
	var docent = docentIn;  // Actual docent object
	docent.sgconsole("Constructing sg before/after picturebox");
	var xml = pictureboxXMLIn;
	
	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";

	//
	//   ADD ANY ADDITIONAL MEMBER VARIABLES YOU NEED HERE
	//

	
	// sgalert()
	// Conditional wrapper for debug alerts
	// This was in docent but for some reason I decided each routine needed it's own/
	// 
	// docent.sgconsole() is less obtrusive, but only works in Firefox
	//
	var sgalert = function(msg){
		if (debug) {
			alert(msg);
		}
	};
	
	
	////
	////		"PRIVATE" functions
	////
	
	
	//
	// waitForFlashLoad
	//
	// Uses some docent utility routines to check if Flash movie is loaded
	//
	
 	function waitForFlashLoad () {
		
		if (docent.flashMovieIsLoaded(docent.getFlashMovie(flashName))) {
			clearInterval(timerID);
			that.flashLoaded();
		}
		else { docent.sgconsole("Flash not loaded yet!"); }
	}

	//
	// initPictureBox
	//
	// Create the picturebox element and inject it into the DOM
	//
	//  YOU WON'T NEED TO CHANGE THIS
	//

	function initPictureBox() {
		
		
		// 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('#'+myDivID).html('<div id="'+flashName+'"><p></p></div>');

		var attributes = {};
		attributes.id = flashName;
		attributes.NAME=flashName;
					
		swfobject.embedSWF(flashFileName, flashName, myWidth, myHeight, "9.0.0", false, flashvars, params, attributes);
		
		// Set up timer to intialize Flash movie once it loads
		timerID = setInterval ( waitForFlashLoad, intervalTicks );// hey
	}
	
	// init()
	//
	// Check environment to make sure everything we need is available
	//    If something isn't right give a useful error
	//    If everything is good hand off to processXML() and initPictureBox()
	//
	function init() {
		sgalert("SG Flash Before After Picturebox: Init", this);
		
		if(xml==undefined || xml==null) { docent.sgconsole("Picturebox got bad xml!!")};
		
		if(!window.swfobject) { docent.sgconsole("Error - Missing javascript: swfobject")};
		processXML();
		if (numExhibits > 0) {
			if (docent.hasViewSheet()) {
				initPictureBox();
			}
			else {
				$ml('#' + myDivID).html('<p style="color:#000;">To use the before/after picturebox you will need to add a viewsheet to your design.  See the gallery documentation for details.</p><p style="color:#fff;">To use the before/after picturebox you will need to add a viewsheet to your design.  See the gallery documentation for details.</p>');
			}
		}
	}
	
	// processXML
	//
	// Slurp all options and other useful stuff out of the xml so we don't have
	// to parse every time we want something
	//
	//  YOU WILL WANT TO ADD ANY CUSTOM OPTIONS THAT YOU WISH TO KEEP LOCALLY
	//
	function processXML () {
		sgalert("SG Flash Picturebox: processXML", this);
		
		numExhibits = docent.getExhibitCount();
		
		flashFileName = getOption("filename");
		myDivID = getOption("divname");
		myWidth = getOption("width");
		myHeight = getOption("height");
		myClickAction = getOption("clickaction");
		myHoverAction = getOption("hoveraction");
	
		return;
	}
	
	// getOption
	//	
	// Get a picturebox option from the XML.
	// If xml format changes just change this.
	//
	function getOption(opt) {
		return docent.getPictureBoxOption(opt);
	}

	
	
	////
	////		"PUBLIC" methods
	////
	
	
	// displayByUrl()
	//
	// Tell pbox to display a certain URL, provided as a string
	//	
	
	this.displayByUrl = function(imageUrl) { /* flashBrowserRef.displayFromURL(imageUrl); */	}
	
	// displayByNum()
	//
	// Tell pbox to display an image by finding it in the XML
	// using Zero-Based exhibit and view ordinals.
	//	
	
	this.displayByNum = function(exhibitNum, viewNum) {
		docent.sgconsole("flash PICTUREBOX displayByNum("+exhibitNum+","+viewNum+")");
		//var dispUrl = getUrlFromNum(exhibitNum,viewNum);
		//docent.sgconsole("flash PICTUREBOX displayByNum get url " + dispUrl);
		flashBrowserRef.update(exhibitNum, viewNum);
	}
	
	
	// 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.
	//
	// YOU WILL NEED TO CUSTOMIZE THIS FUNCTION UNLESS YOUR
	// FLASH MOVIE IS DESIGNED TO ACCEPT THE STANDARD SITEGRINDER XML FORMAT
	// AND USES THE FLASHDOCENT TO PROCESS IT
	
	this.flashLoaded = function () {
		flashBrowserRef = docent.getFlashMovie(flashName);
		docent.sgconsole("Flashpbox : found movie " + flashName + " " + flashBrowserRef);
		flashBrowserRef.initWithXML($ml(xml).toXML(), docent.getName(), flashName, docent.getCurrentExhibit(), docent.getCurrentView());
	}
	
	// Start the whole house of cards going!
	init();
}

