﻿//Flash HT Class Def
function FlashHT( _mmPath , _guid , _templateID ){
	FlashHT.prototype.mmPath = _mmPath;
	FlashHT.prototype.guid = _guid;
	FlashHT.prototype.templateID = _templateID;
	//Object State 
	FlashHT.prototype.animation = 'on';
	FlashHT.prototype.currentSlide = 1;
	FlashHT.prototype.totalSlides = 1;
	FlashHT.prototype.elementType = '';
	FlashHT.prototype.locked = 0;
	FlashHT.prototype.url = '';
	FlashHT.prototype.caption = '';
	FlashHT.prototype.text = '';
	FlashHT.prototype.previewing = false;
	FlashHT.prototype.sound = '';
	//Template parameters for forming XML
	FlashHT.prototype.template = new Object();
	FlashHT.prototype.template.name = 'Untitled';
	FlashHT.prototype.template.href = '';
	FlashHT.prototype.template.id = 1;
	FlashHT.prototype.template.store = 'flash';
	FlashHT.prototype.template.update = 'false';
	
	if( typeof FlashHT._initialized == 'undefined'){
		FlashHT.prototype.loadTools = function( toolset ){
			switch ( toolset ){
				case 'FlashHTPresentationImageTools':
					document.getElementById('tools').src = 'FlashHTPresentationImageTools.flx';
					help( "PresentationImage" );
					changeButtonStatus( true );
				break;
				case 'FlashHTImageTools':
					document.getElementById('tools').src = 'FlashHTImageTools.flx';
					help( "Image" );
					changeButtonStatus( true );
				break;
				case 'FlashHTTextTools':
					document.getElementById('tools').src = 'FlashHTTextTools.flx';
					help( "Text" );
					changeButtonStatus( true );
				break;
				case 'Save':
					document.getElementById('tools').src = 'FlashHTSave.flx';
					help( "SaveAs" );
					changeButtonStatus( true );
				break;
				case 'FlashHTSoundTools':
				    document.getElementById('tools').src = 'FlashHTSoundTools.flx';
				    help( "Sound" );
				    changeButtonStatus( true );
				break;
				default:
					document.getElementById('tools').src = 'FlashHTBlankTools.flx';
					help( "Default" );
					changeButtonStatus( false );
			    break;
			}
		};
		//**************************************************************************
		FlashHT.prototype.setUrl = function( url ){this.elementType = url;};
		FlashHT.prototype.getUrl = function(){return this.url;};		
		FlashHT.prototype.setCaption = function( caption ){this.caption = caption;};
		FlashHT.prototype.getCaption = function(){return this.caption;};		
		FlashHT.prototype.setText = function( text ){this.text = text;};
		FlashHT.prototype.getText = function(){return this.text;};		
		FlashHT.prototype.setLocked = function( locked ){this.locked = 1;};
		FlashHT.prototype.getLocked = function(){return this.locked;};		
		FlashHT.prototype.setElementType = function( type ){this.elementType = type;};
		FlashHT.prototype.getElementType = function(){return this.elementType;};		
		FlashHT.prototype.setAnimation = function( onoff ){	this.animation = onoff;};
		FlashHT.prototype.getAnimation = function(){return this.animation;};
		FlashHT.prototype.getCurrentSlide = function(){	return this.currentSlide;};
		FlashHT.prototype.getTotalSlides = function(){return this.totalSlides;};
		FlashHT.prototype.getName = function(){return this.template.name;};
		FlashHT.prototype.setName = function( _name ){ this.template.name = _name; }; 
		FlashHT.prototype.getId = function(){return this.template.id;};
		FlashHT.prototype.setId = function( _id ){ this.template.id = _id; }; 
		FlashHT.prototype.getStore = function(){return this.template.store;};
		FlashHT.prototype.setStore = function( _store ){ this.template.store = _store; }; 
		FlashHT.prototype.getUpdate = function(){return this.template.update;};
		FlashHT.prototype.setUpdate = function( _update ){ this.template.update = _update; }; 
		FlashHT.prototype.getHref = function() {return this.template.href;};
		FlashHT.prototype.setHref = function( _href) { this.template.href = _href; };
		FlashHT.prototype.getSound = function() {return this.sound;};
		FlashHT.prototype.setSound = function(_sound) { this.sound = _sound; };
		/***************************************************************************
		 * updateTotal is called whenever the Flash movie adds, removes or changes 
		 * location to a different slide.  Internal counters are used for the UI 
		 * to let users know where they are and if they can add / remove a slide
		 * from the position they are in (only allowed at end of movie)
		 */
		FlashHT.prototype.updateTotal = function( _thisSlide ,  _newTotal ){
			this.totalSlides = _newTotal;
			this.currentSlide = _thisSlide;
		};
		/***********************************************************************
		 * Standard way or returning a reference to a movie.  Once this reference
		 * is obtained, calls can be made to it as though it were a JavaScript 
		 * Object
		 */
		FlashHT.prototype.getMovie = function(){
			var isIE = navigator.appName.indexOf("Microsoft") != -1;
 			return (isIE) ? window[this.templateID] : document[this.templateID];
		};
		//***********************************************************************
		// Wrappers for Flash File functions 
		// Most of these functions handle internal house-keeping regarding state
		// and then call the Flash movie. 
		//***********************************************************************
		/**
		 * Set Image Accepts the image ID from the image selector and the builds the 
		 * URL to pass into the Flash movie.  Along with the URL the ImageID and Store
		 * are passed into the movie so the URLs can be re-created on the back-end. 
		 * ElementType is part of the Flash - JavaScript API and determins what kind
		 * of image is being edited
		 */
		FlashHT.prototype.SetImage = function( _imageID  ){
			var xml = encodeURI(build_element('File',build_attribute('id', _imageID ),''));

			var url = this.mmPath + '/stores/GetPersonal.flx?xml=' + xml + '&amp;guid=' + this.guid;
			//alert(url);
			this.getMovie().fSetImage(  this.getElementType() , url  , _imageID , 'personal');
		};
		/*************************************************************************
		 * Sets the sound for this flash file
		 * accepts a fileId, which is the id of the sound file that has been selected in the file tree
		 * then calls GetSoundClipInfo and pulls the url and store for that sound file and calls
		 * fSetSound with that information.
		 */
		FlashHT.prototype.SetSound = function( fileId ){
            var xml = build_element('SoundClip', build_attribute('id', fileId),'');
            var response = load_xml_response_xml('GetSoundClipInfo.flx?flxnodeco',xml);
            var url = escape(response.getElementsByTagName('SoundClip')[0].getAttribute('url'));
            var store = response.getElementsByTagName('SoundClip')[0].getAttribute('store');
            this.setSound(url);
            this.getMovie().fSetSound( url, fileId, store );
            alert('Sound Set.');
		}
		/*************************************************************************
		 * Play Sound starts playing the selected sound through the flash movie
		 * It does not take in any params but pulls the sound from the flash object
		 * where it was set when it was clicked on in the fileTree
         */
        FlashHT.prototype.playSound = function( ) {
            //alert(unescape(this.getSound()));
            this.getMovie().fPreviewSound(this.getSound());
        }
		/*************************************************************************
		 * Stop sound stops playing the sound in the flash movie
		 * It does not take any params, it just stops playing whatever sound is currently
		 * playing in the flash movie
         */
        FlashHT.prototype.stopSound = function( ) {
            this.getMovie().fStopSound();
        }
		/*************************************************************************
		 * Set Text accepts both a target and a text string.
		 * The target tells the Flash movie what type of text object should receive
		 * the text.  The text itself is stored in the FlashHT object for multi-part
		 * forms (such as Presentation images where an image, caption and description are
		 * all set).  The two empty strings passed to the movie represent the ID and Store
		 * which aren't used for text nodes but are included for consistency in the XML built
		 * by Flash.
		 */
		FlashHT.prototype.SetText = function( target , text ){
            //switching the size and the customSize attributes
            text = text.replace(/customsize="([0-9]+)"/gi,'middle="$1"');
            text = text.replace(/size="([0-9]+)"/gi,'customsize="$1"');
            text = text.replace(/middle="([0-9]+)"/gi,'size="$1"');
            
            //text = text.replace(/size="([0-9]+)" customsize="([0-9]+)"/gi,'size="$2" customsize="$1"');

			if(target == 'caption')
				this.setCaption( text );
			else{
				this.setText( text );
				target = 'text';
			}

			this.getMovie().fSetText( target , text ,'' , '');

		};
		/**************************************************************************
		 * DoneWithPresentationMode resets the editor UI to the blank tools
		 * and tells the movie to close the thumb nail slider and go back to 
		 * the normal image edit mode.
		 */
		FlashHT.prototype.DoneWithPresentationMode = function(){
			this.getMovie().fDone(  );
			this.loadTools('blank');
			help('Default');
		};
		/**************************************************************************
		 * Toggle Animation keeps track of the animation preference within the 
		 * FlashHT object.  Depending on what mode is selected, it calls the 
		 * appropriate function with the appropriate setting to set the movie to 
		 * the right animation mode.  This setting only affects new slides being 
		 * added in or edited. 
		 */
		FlashHT.prototype.ToggleAnimation = function(){
			if( this.getAnimation() == 'on' )
				this.setAnimation( 'off' );
			else
				this.setAnimation( 'on' );
			
			var setting = this.getAnimation() == 'on' ? 1 : 0 ;
      
			this.getMovie().fSetAnimation( setting );
		};
		/**************************************************************************
		 * MoveForward first checks to see if the current slide is less than the 
		 * total number of slides.  If it is, then it tells the movie to move forward
		 * which in turn will trigger a call from Flash updating the Total slides
		 * and the Current Slide.  
		 */
		FlashHT.prototype.MoveForward = function(){
			if( this.currentSlide < this.totalSlides ){
				this.getMovie().fSetSlide( "forward" );
			}
			else{
				alert( 'You are already at end of your presentation.' );
			}
		};
		/**************************************************************************
		 * MoveBack is similar to MoveForward in how it checks and updates the movie
		 */
		FlashHT.prototype.MoveBack = function(){
			if( this.currentSlide > 1 ){
				this.getMovie().fSetSlide( "back" );
			}
			else{
				alert( 'You are already at the beginning of your presentation.' );
			}
		};
		/**************************************************************************
		 * INITIALIZE is called after the movie alerts the browser that it has been 
		 * loaded.  Initialize will go grab the XML for the template based on the 
		 * ID that is passed from the navigator.  The XML is then loaded into the 
		 * movie via fSetXML.  Then the movie is set to "edit" mode.  Animiation 
		 * preference is then turned on. 
		 */
		FlashHT.prototype.Initialize = function( mode ){

			var name = this.getName();
			var href = this.getHref();
			var flashxml =  build_element( 'File' , build_attribute('href', href ) , '');
//alert(flashxml);
			var xml = load_xml( 'GetFlashXml.flx?flxnodeco', flashxml);
//alert(xml);
            document.getElementById('xmloutput').value = xml;  //putting xml to be passed in into the textarea
			this.getMovie().fSetXML( xml );
			this.getMovie().fSetControl( mode );
			this.setAnimation( 'on' );
			this.getMovie().fSetAnimation( 1 );
			document.getElementById('title').innerHTML = this.getName();
	     };
	     /***************************************************************************
	      * Initialize Function for preview only.  Dependent on local call to 
	      * getPreviewXML which allows a given stylesheet to decide how to get the
	      * XML to preview.  In most cases it will be a call the parent window (flash editor)
	      * where the XML should be stored locally.  This keeps us from doing a round trip to
	      * the database and also let's us preview the movie before saving the XML
	      */
	     FlashHT.prototype.InitializeForPreview = function(){
			var xml = getPreviewXML();
			this.getMovie().fSetXML( xml );
			this.getMovie().fSetControl( 'play' );
			this.setAnimation( 'on' );
			this.getMovie().fSetAnimation( 1 );
	     }
		/**************************************************************************
		 * AddSlide does a check to see if the movie is at the last slide in the set
		 * Slides can only be added at the end.  If the movie is at the last slide, 
		 * the Flash movie is instructed to add a new slide which will in turn update
		 * the number of slides and current slide through a separate call from the Flash
		 * movie.  If you are not at the end, the editor will confirm that you want to add
		 * one to the end.  Quirky UI but eventually we want to have an editor that will 
		 * allow you to add slides anywhere.
		 */
		FlashHT.prototype.AddSlide = function( check ){
			if( (this.currentSlide == this.totalSlides) || (check == true) ){// If at last slide OR if they have confirmed
				this.getMovie().fEditSlideArray( "AddSlide" );
			}
			else{
				var conf = confirm( '     Slides can only be added to the end of the movie.\nDo you want to add a slide to the end of the movie now?' );
				if( conf )
					this.AddSlide( true );
				else 
					return false;
			}
		};
		/***********************************************************************************
		 * DeleteSlide.  Just like AddSlide, you can only perform this operation at the end
		 * of the slide array.  
		 * First checks to see if there are slides left.
		 * Second checks to see if only 1 left. If only one left, remove it and move to summary
		 * If You are AT the last slide, Remove it.
		 * If You aren't at the last slide, remind user that you can only remove the last and upon
		 * Confirm, remove it.
		 */
		FlashHT.prototype.DeleteSlide = function( check ){
			if( this.totalSlides == 0 ) {
				alert( 'There are no more slides left' );
				return false;
			} else if( this.totalSlides == 1 ) {
                if(confirm('Are you sure you want to remove this slide?'))
                {
				    this.getMovie().fEditSlideArray( "RemoveLastSlide" );
                    help( "RemoveLastSlide" );
				    this.MoveToSummary();
				    return false;
				}
			} else if( (this.currentSlide == this.totalSlides) || (check == true) ){//If at last slide OR if they have confirmed
                if(this.currentSlide == this.totalSlides)
                {
                    if(confirm('Are you sure you want to remove this slide?'))
                    {
				        this.getMovie().fEditSlideArray( "RemoveLastSlide" );
              	        help( "RemoveLastSlide" );
				        this.totalSlides--;  //updating total slide count
                    }
				}
				else
				{
				    this.getMovie().fEditSlideArray( "RemoveLastSlide" );
				    help( "RemoveLastSlide" );
				    this.totalSlides--;
				}
			} else {
				var conf = confirm( 'Slides can only be deleted from the end of the movie.\n Do you want to remove the last slide of this movie?');
				if( conf )
					this.DeleteSlide( true );
				else
					return false;
			}
		};
		/***********************************************************************************
		 * MoveToSummary instructs the movie to go to the summary slide.  This is a special
		 * slide and is not counted in the slide array which is why you can't call move forward
		 * to get to the Summary Slide
		 */
		FlashHT.prototype.MoveToSummary = function(){
			this.getMovie().fSetSlide( "summary" );		
		};
		/***********************************************************************************
		 * Preview grabs the XML out of the Flash movie and stores it locally. It ignores
		 * the return value from the internal call because the XML is grabbed from the hidden
		 * field by a call from the child window to the parent 
		 * Set the movie state to "previewing" in order to grab the XML from the Flash movie
		 * without saving it to the system.  Work-around because I didn't think about this
		 * until after creating the whole save routine. 
		 */
		FlashHT.prototype.Preview = function(){
			this.previewing = true;
			this.SaveToSystem( false );
			this.previewing = false;
			var templateXml = build_element('Template', build_attribute('id',id), '');
      var templateDims = load_xml_response_xml( 'GetDimensionsForTemplateAndUser.flx?flxnodeco', templateXml);
      var templateNode = templateDims.getElementsByTagName("Template")[0];
      var w = templateNode.getAttribute("width");
      var h = templateNode.getAttribute("height");
			var url = 'FlashHTPreview.flx?href='+this.getHref()+'&w='+w+'&h='+h;
			window.open( url ,'LinkLocation','alwaysRaised=yes,resizable=yes,scrollbars=yes,location=no,menubar=yes,width=850,height=650');

		};
		/***********************************************************************************
		 * Save checks internally to see if the movie has already been saved.  If it was 
		 * loaded from personal then obviously it was saved.  In addition it may have been 
		 * loaded from deep freeze and then saved while editing was in progress.  If a save
		 * has already been performed then it will simply do an update.  If it has not yet
		 * been saved it will open the "save" tool set.
		 */
		FlashHT.prototype.Save = function(){
			if(( this.getName() == 'Untitled' || this.getName() == '') && !this.getHref().match(/personal/gi)){
				this.loadTools('Save');
				return false;
			}
			else{
				this.setUpdate( 'true' );
				this.SaveToSystem( false , false);
				help( "Save" );
			}
		};
		/************************************************************************************
		 * Will always open up the Save dialog tool set
		 * Set update to false (in case it was set to true before) so that a new version of
		 * the template is saved instead of updating the old one.
		 */
		FlashHT.prototype.SaveAs = function(){
			this.setUpdate( 'false' );
			this.loadTools('Save');	
		};
		/*************************************************************************************
		 * Save to system.  xml param can be "false" or valid XML.
		 * If xml is false it means you haven't gotten the XML from the Flash movie yet so 
		 * go get it.
		 * If xml is not false it means it is really XML and you call the internal function 
		 * to put it into the right XML for the back end call
		 * If in previewing state, you don't save the movie, you just get the XML.
		 * If not in previewing state, you save the movie using the back end call.
		 * Grab the new href off of the response and change the internal state of the FlashHT object
		 * set the title of the movie in the editor and tell the user that the movie was saved 
		 * successfully.
		 */
		FlashHT.prototype.SaveToSystem = function( xml ){
			if( xml === false ){
				this.getMovie().fSave(); //go grab the latest XML from the system
			}
			else if( this.previewing === false )
			{
				var flashxml = this.CreateFlashXMLForDB( xml );
//alert(flashxml);
//document.getElementById('xmloutput').value = flashxml;  //putting xml to be passed in into the textarea
				var response = load_xml_response_xml( "SaveFlash.flx?flxnodeco" , flashxml );
//alert('response.xml: ' + response.xml);
				var FlashTemplate = response.getElementsByTagName( 'FlashTemplate' );
				if( FlashTemplate.length > 0 ){
					var newHref = FlashTemplate[0].getAttribute("href");
					this.setHref( newHref );
					document.getElementById('title').innerHTML = this.getName();
					id = FlashTemplate[0].getAttribute("href").split(":")[1].replace(/\//gi,'');  //setting the id to the id of the saved piece for return to editor selection
					store = 'personal';  //setting store to personal because now it's a saved piece
                    var unedited = FlashTemplate[0].getAttribute("unedited");
                    if(unedited > 0)
                        alert('Your presentation has been saved.\r\n\r\nNote: you have ' + unedited + ' slide(s) in your presentation that have not been edited.\r\nIf you do not want default images to appear in your presentation, make\r\nsure you edit all of your slides.');
                    else
    					alert('Your presentation has been saved.');
				}
				else
				{
					alert('There was a problem with your request.' + response.xml);
//alert(response.xml);
				}
			}
			else{
				this.CreateFlashXMLForDB( xml );
			}
		};
		/*******************************************************************************
		 * Internal use only.  Takes the XML from the Flash movie and generates the 
		 * proper XML to insert into the database. 
		 */
		FlashHT.prototype.CreateFlashXMLForDB = function( xml ){
				var name = this.getName();
				var href = this.getHref();
				var update = this.getUpdate();
				var flashxml = build_element( 'FlashTemplate' , build_attribute('href', href ) , build_attribute('name', name ), build_attribute('update', update ), xml);
				flashxml = flashxml.replace(/<\?.+?>/,""); //Gets rid of the XML doc declaration//
				xml = xml.replace(/<\?.+?>/,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"); //gets rid of xml declaration only to add it back in with a line break after it for the preview
				document.getElementById('xmloutput').value = xml;	//Puts the Flash into the hidden field for preview
				globalXML = xml;  //puts the flash xml into a global var for the preview
				return flashxml;
		};
	////////////////////////////////
	FlashHT._initialized = true;
	}
}
