/*
@@@BUILDINFO@@@ VCStartPage.jsx 1.0.0.0 4.0.0.0client99 4-January-2008
*/
//------------------------------------------------------------------------------
//	ADOBE SYSTEMS INCORPORATED
//	Copyright 2007 Adobe Systems Incorporated 
//	All Rights Reserved
//	NOTICE: Adobe permits you to use, modify, and distribute 
//			this file in accordance with the terms of the Adobe license 
//			agreement accompanying it. If you have received this file 
//			from a source other than Adobe, then your use, modification, 
//			or distribution of it requires the prior written permission of Adobe.
//------------------------------------------------------------------------------

#target bridge
/**
* This script should be run only in Adobe Bridge.
*/

if(BridgeTalk.appName == "bridge") 
{	
		
	/**
	* Get StartPage swf file
	*/
	VCStartPage.getStartPageFile = function()
	{
	    StartPageFilePath = "";
	    
	    try
	    {
			StartPageFilePath = Folder.decode( versioncue.extensionPath + "/resources/flash/Connect.swf" );	

			var StartPageFile = new File(StartPageFilePath);
		    
			if(StartPageFile.exists)
	    		return StartPageFile;
			else
    		{
    			return null;
    		}
    	}catch(error)
    	{
    		alert(error);
    		return null;
    	}
	}
		
	/**
	* This method will return a referance to the Tab 
	* provided in argument. Else it will return null.
	**/
	VCStartPage.getTabByID = function(tabID)
	{
		var resultTab = null;

		try
		{
			var palettesArray = app.document.palettes;
			for ( i=0; i < palettesArray.length; i++ )
			{
				tabbedPalette = palettesArray[i];
				if( tabbedPalette != undefined && tabbedPalette.id == tabID) 
				{
					resultTab = tabbedPalette;
					break;
				}
			}		
		}
		catch(error)
		{
			alert(error);
		}

		return resultTab;
	}
	
	/**
	 Get the flashplayer if added otherwise null
	*/
	VCStartPage.getVCFlashPlayer = function()
	{
		try
		{
			var VCFlashPlayer = null
			if((VCStartPageTab = VCStartPage.getTabByID( VCStartPage.StartPageTabID )) != null)
			{
				VCFlashPlayer =  VCStartPageTab.content.children[0];
			}
		}catch(error)
		{
			alert(error);
		}
		return VCFlashPlayer;		
	}
	
	
	/**
	* This method will remove VCFlashPlayer from VCStartPage Tab
	*/
	VCStartPage.clearVCStartPageTab = function( startPageTab )
	{
		try
		{
			if( startPageTab != null)
			{
				var startPagePanel = startPageTab.content;
				//Remove handler
				startPagePanel.removeEventListener("onResize");
				startPagePanel.removeEventListener("onResizing");
				var childArrayLength = startPagePanel.children.length;
				for( var i=0; i < childArrayLength; i++)
				{					
					// remove flash element
					if(startPagePanel.children[0].type == "flashplayer")
					{
						startPagePanel.remove( 0 );						
					}
				}			
			}
		}
		catch(error) 
		{ 
			alert(error); 
		}
	}
	
	/**
	*  This Method Will remove VC Start Page Tab	
	**/
	VCStartPage.removestartPageTab = function(canRemove)
	{
		try
		{
			var startPageTab = VCStartPage.getTabByID( VCStartPage.StartPageTabID );
			if( startPageTab != null)
			{
				var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
				if(vcFlashPlayer != null)
				{
					

//					if(vcFlashPlayer.invokePlayerFunction('IsDontShowAgainCallback'))
//					{					    				    
						//alert("Please use help menu to again open VC Welcome Screen");
//						VCSession.setSettingsValue("Version_Cue_Welcome_Screen","DontShowAgain","1");
//					}
//					else
//					{
						// Dont change last value logged by any Bridge window.
						//VCSession.setSettingsValue("Version_Cue_Welcome_Screen","DontShowAgain","0");
//					}									
				
					vcFlashPlayer.invokePlayerFunction('OnCloseCallback');
					//Stop Movie.
					vcFlashPlayer.stopMovie();
				}
								
				VCStartPage.clearVCStartPageTab(startPageTab);
				
								
				if(startPageTab.visible)
					startPageTab.visible = false;
								
				if(canRemove)
				{
					startPageTab.remove();					
				}																								
			}
		}
		catch(error) 
		{ 
			alert(error); 
		}
	}
	
	
	
	VCStartPage.loadStartPage = function( startPageTab )
	{
		
		try
		{
			var IsSuccess = false;
			if(startPageTab != null)
			{				
				var VCStartPagePanel = startPageTab.content;
				var vcStartPageFile = VCStartPage.getStartPageFile ();
				if(vcStartPageFile != null)
				{
					VCStartPagePanel.alignChildren = ['fill','fill'];

					VCFlashPlayer = VCStartPagePanel.add('flashplayer');
					
					/**
					* METHODS TO COMMUNICATE WITH ACTIONSCRIPT
					*/
					
					
					/**
					* Localization Method
					*/
					VCFlashPlayer.getLocalizedString = function(dString)
					{
						try
						{
							var VCFlashPlayer = VCStartPage.getVCFlashPlayer();
							var lString  = "";
							if(VCFlashPlayer != null)
							{									
								lString  = versioncue.getLocalizedString( dString );								
							}
							return lString;							
							
						}catch(error)
						{
		 					alert(error);
						}
					}
					
					/**
					* Help Center
					*/
/*					VCFlashPlayer.versionCueHelp = function(topic)
					{
						try
						{
							var HelpObj = new Object;
							HelpObj.product = "Bridge";
							HelpObj.topic =   topic;
							HelpObj.page =    "0"; //Ignored
							HelpObj.locale =  app.locale;
							HelpObj.version = app.version;
						    app.launchHelp(HelpObj);
						}catch(error)
						{
							alert(error);
						}
					}
*/										
					/************************************************************************************/
					
					var PanelWidth  =  VCStartPagePanel.size.width;	
					var PanelHeight = VCStartPagePanel.size.height;
					
					VCFlashPlayer.preferredSize = [PanelWidth, PanelHeight];
					
					VCFlashPlayer.alignment =  ['fill', 'fill']

					VCStartPagePanel.onResizing = function()
					{
						this.layout.resize();
					}
				
					VCStartPagePanel.onResize = function()
					{
						this.layout.resize();						
					}
					
					VCFlashPlayer.loadMovie (vcStartPageFile);
					VCStartPagePanel.layout.layout(true);
					IsSuccess = true;					
				}// Swf File not found														
			
			}// startPageTab is null
		
		}// Exception
		catch(error)
		{
			alert(error);			
		}		
		return IsSuccess;
	}
		
	/**
	*  This Method Will Show VC Start Page Tab	
	**/
	VCStartPage.showStartPageTab = function( document )
	{
		try
		{			
			var IsSuccess = false;
			if( typeof( VersionCueSDKLoader ) != "undefined" && typeof VersionCueSDKLoader == "object" && VersionCueSDKLoader.loadSDK() != null )
			{							
				// Create the palette
				var paletteName = versioncue.getLocalizedString( "$$$/Bridge/VCAdapter/Menu_WelcomeTab=Connect to Server" );
				if (document == undefined)
					var startPageTab = new TabbedPalette( app.document, paletteName, "startPage" ,"script", "center" );
				else
					var startPageTab = new TabbedPalette( document, paletteName, "startPage" ,"script", "center" );

				
				IsSuccess = VCStartPage.loadStartPage( startPageTab );
				if(!IsSuccess)
					VCStartPage.removestartPageTab(true);
				else
				{
					var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
					if( vcFlashPlayer != null )
					{
						//We can stop movie at this point
//						vcFlashPlayer.stopMovie();
					}
						
				}//Dont show again				
			}//Unable to load VCSDK
			
		}//Exception
		catch(error) 
		{ 			
			alert(error); 
			VCStartPage.removestartPageTab( true );
		}
		return 	IsSuccess;
	}
	
	
	VCStartPage.GetCustomProjectIcon = function(projectObj)
	{
		try
		{
			var CustomIconPath = null;
			var projectNode = VCNode.getInterface(projectObj);
			if(projectNode != null)
			{
				var resultGetPropertyIconData = projectNode.getPropertyIconData(VCPropertyConstants.NAME,16);
				if(resultGetPropertyIconData.isSuccess)
				{
					var iconData = resultGetPropertyIconData.iconData;
					var prjServerName = projectObj.server.name;
					CustomIconPath = Folder.decode( versioncue.extensionPath + "/resources/images/" + prjServerName + "_" + projectObj.name + "_customIcon.png");
					iconFile = new File (CustomIconPath);
					iconFile.encoding = "BINARY";
					iconFile.open("w");
					iconFile.write(iconData.data);
					iconFile.close();				
					return CustomIconPath;
				}
			}			
		}
		catch(error)
		{
			alert(error); 
		}
		return CustomIconPath;
	}	
}
