/*
@@@BUILDINFO@@@ VCStartPage.jsx 1.0.0.0 3.1.0.0client51 6-July-2007
*/
//------------------------------------------------------------------------------
//	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
	    {
			if ( versioncue.isWin )
			{
				// Win
				StartPageFilePath = Folder.decode( versioncue.extensionPath + "/resources/flash/Vc_Start_Page.swf" );				
			}
			else if( versioncue.isMac )
			{
				// Mac
				StartPageFilePath = 
				Folder.decode( versioncue.extensionPath + "/resources/flash/Vc_Start_Page.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
					*/
					
					/**
					* This Method Will Be Called By Actionscript In The Start To Display Recent
					* Projects. Later VCAapterLib Can Call This Method To Change Icons Or Projects 
					* Names Dynamically.
					*/
					VCFlashPlayer.DisplayRecentProjects = function()
					{
						try
						{
							var vcFlashPlayer = VCStartPage.getVCFlashPlayer();			
							if( vcFlashPlayer == null)
							{
								// Return if vcFlashPlayer is not loaded
								return;
							}
							var flags = new Array;
							flags[0] = VCFlagConstants.NONE;
							var resultExploreMountedProjects = VCSession.exploreMountedProjects(flags);

							if(resultExploreMountedProjects.status.isSuccess)
							{
								var RecentProjects = resultExploreMountedProjects.projectList;
								var RecentProjectsString = "";
								
								var maxProjects;
								//We are intersted in 15 recent projects only

								if(RecentProjects.size > 15)
									maxProjects = 15;
								else
									maxProjects = RecentProjects.size;

								var IsCustomProject = false;
								for(var cnt = 0; cnt < maxProjects; cnt++)
								{
									IsCustomProject = false;
									var projObj = RecentProjects.getAt(cnt);

									var CustomIconPath = VCStartPage.GetCustomProjectIcon(projObj);						
									if(CustomIconPath != null)
									{
										IsCustomProject = true;
										var IconURL = CustomIconPath;
										if( versioncue.isWin )
										{
											IconURL = IconURL.substr(1);
											IconURL = IconURL.replace(/\//, ":/");
										}
										RecentProjectsString += 4 + ";"						
									}
									else if(projObj.projectStructure == VCProjectStructureConstants.PREDEFINED)
									{
										RecentProjectsString += 3 + ";";						
									}
									else if(projObj.isShared == false)
									{
										RecentProjectsString += 2 + ";";					
									}
									else if(projObj.online == false)
									{
										RecentProjectsString += 1 + ";";												
									}
									else
									{
										RecentProjectsString += 0 + ";";						
									}

									RecentProjectsString += projObj.name + ";";

									var projectURLObj = projObj.projectURL;
									var projectURL = projectURLObj.urlString;					

									if (cnt != maxProjects - 1)
									{
										if(IsCustomProject)
										{
											RecentProjectsString += projectURL + ";"  + IconURL + ",";							
										}
										else															
											RecentProjectsString += projectURL + ",";
									}
									else
									{
										if(IsCustomProject)
										{
											RecentProjectsString += projectURL + ";" + IconURL;
										}
										else							
											RecentProjectsString += projectURL;
									}
								}
								
								var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
								if(vcFlashPlayer != null)
								{					
									if(RecentProjectsString == "")
									{
										vcFlashPlayer.invokePlayerFunction('DisplayRecentProjectsCallback');
									}
									else	
										vcFlashPlayer.invokePlayerFunction('DisplayRecentProjectsCallback', RecentProjectsString);
								}				
							}								
						}
						catch(error)
						{
										
						}
							
					}
						
						
					/**
					* This Method Will Be Called By Actionscript In The Start To Display 
					* Local Servers. Later VCAapterLib Can Call This Method To Change Status 
					* Dynamically.
					*/
					VCFlashPlayer.UpdateMyServer = function(IsFirstCall)
					{			
						try
						{	
							var vcFlashPlayer = VCStartPage.getVCFlashPlayer();			
							if( vcFlashPlayer == null )
							{
								// Return if vcFlashPlayer is not loaded yet
								return;
							}
							var MyCS2Server  = null;
							var MyCS3Server  = null;
							var IsCS2Online = false;
							var IsCS2Available = false;
							var CS2Name = "";
							var CS2URL = "";
							var CS2Status = "";
							var CS2Version = "";
							
							var IsCS3Online = false;
							var IsCS3Available = false;
							var CS3Name = "";
							var CS3URL = "";
							var CS3Status = "";
							var CS3Version = "";
							var browseServersNodeName = "";
							var recentProjectName = ""
							
							var urlWorkspacesObj = VCSession.getSpecialNodeURL(VCNodeConstants.WORKSPACES);
							var urlWorkspaces = urlWorkspacesObj.urlString;
							var thumbnailBrowseServers = new Thumbnail("bridge:vc:" + urlWorkspaces);
							browseServersNodeName =  thumbnailBrowseServers.name;
							
							var urlProjectsObj = VCSession.getSpecialNodeURL(VCNodeConstants.PROJECTS);
							var urlProjects = urlProjectsObj.urlString;
							var thumbnailRecentProjects = new Thumbnail("bridge:vc:" + urlProjects);
							recentProjectNodeName = thumbnailRecentProjects.name;
							
							var resultObj = VCStartPage.getLocalServers(IsFirstCall);
							var localServerList  = resultObj.localServerList;
							var isStartMyServer  = resultObj.isStartMyServer;
							var startMyServerNodeName = resultObj.StartMyServerNodeName;
														

							for(var i = 0; i < localServerList.size; i++)
							{
								var serverObj = localServerList.getAt(i);
								if(serverObj.version < 3.0)
									MyCS2Server = serverObj;
								else
									MyCS3Server = serverObj;

							}
							if(MyCS2Server != null)
							{
								
								var flags = new Array;
								flags[0] = VCFlagConstants.NONE;
								MyCS2Server.verifyConnection(flags);
								IsCS2Online = MyCS2Server.online;
								IsCS2Available = true;
								CS2Name = MyCS2Server.name;
								CS2URL = MyCS2Server.serverURL;
								var thumbnailCS2Server = new Thumbnail("bridge:vc:" + MyCS2Server.serverURL);
								CS2Status =  thumbnailCS2Server.VersionCue.common.statusText;
								CS2Version = thumbnailCS2Server.VersionCue.common.version;								
							}
							
							if(MyCS3Server != null)
							{
								var flags = new Array;
								flags[0] = VCFlagConstants.NONE;
								MyCS3Server.verifyConnection(flags);
								IsCS3Online = MyCS3Server.online;
								IsCS3Available = true;
								CS3Name = MyCS3Server.name;
								CS3URL = MyCS3Server.serverURL;
								var thumbnailCS3Server = new Thumbnail("bridge:vc:" + MyCS3Server.serverURL);								
								CS3Status =  thumbnailCS3Server.VersionCue.common.statusText;
								CS3Version = thumbnailCS3Server.VersionCue.common.version;
							}
							
							var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
							if(vcFlashPlayer != null)
							{
								vcFlashPlayer.invokePlayerFunction('UpdateMyServerCallback',IsCS3Online, IsCS3Available,IsCS2Online,IsCS2Available,CS3Name,CS2Name,CS3URL,CS2URL,CS3Status,CS3Version,CS2Status,CS2Version,browseServersNodeName,recentProjectNodeName,startMyServerNodeName,isStartMyServer);
								//Redraw recent projects.
								VCStartPage.DisplayRecentProjects();				
							}			
						}
						catch(error)
						{
							
						}
					}

					/**
					* Open Local Server In Content Pane Of Bridge & Close Startup Page if dont show again is clicked
					*/
					VCFlashPlayer.JumpToMyServer = function(serverURL)
					{
						try
						{
							if(serverURL != null && serverURL != typeof "undefined")
							{				
								  var vcStartPageTab = VCStartPage.getTabByID(VCStartPage.StartPageTabID);
								  var thumbnail = new Thumbnail("bridge:vc:" + serverURL);
								  thumbnail.open();
								  var contentTab = VCStartPage.getTabByID('contentTab');
								  var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
								  var dontShowAgain = false;
								  if( vcFlashPlayer != null )
								  {
									dontShowAgain = vcFlashPlayer.invokePlayerFunction('IsDontShowAgainCallback');
									vcFlashPlayer.stopMovie();
								  }
								  if(contentTab == null || dontShowAgain)
								  {
		 							var task = "VCStartPage.removestartPageTab(true);";
			 						app.scheduleTask(task, 0, false );
			 						vcStartPageTab.visible = false;
			 						contentTab.visible = true;				 
								  }
								  else
								  {
			 						contentTab.visible = true;
								  }			
							}
						}
						catch(error)
						{
							alert(error);
						}
					}

					/**
					* Open Servers In Content Pane Of Bridge & Close Startup Page if dont show again is clicked
					*/
					VCFlashPlayer.JumpToBrowseServers = function()
					{
						try
						{
							var urlWorkspacesObj = VCSession.getSpecialNodeURL(VCNodeConstants.WORKSPACES);
							var urlWorkspaces = urlWorkspacesObj.urlString;
							if(urlWorkspaces != null && urlWorkspaces != typeof "undefined")
							{
								var vcStartPageTab = VCStartPage.getTabByID(VCStartPage.StartPageTabID);
								var thumbnail = new Thumbnail("bridge:vc:" + urlWorkspaces);
								thumbnail.open();
								var contentTab = VCStartPage.getTabByID('contentTab');
								var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
								var dontShowAgain = false;
								if( vcFlashPlayer != null )
								{
								  dontShowAgain = vcFlashPlayer.invokePlayerFunction('IsDontShowAgainCallback');
								  vcFlashPlayer.stopMovie();
								}
								if(contentTab == null || dontShowAgain)
								{
			 					  var task = "VCStartPage.removestartPageTab(true);";
			 					  app.scheduleTask(task, 0, false );
			 					  vcStartPageTab.visible = false;
			 					  contentTab.visible = true;			 	  			 	  
								}
								else
								{
									contentTab.visible = true;					
								}					
							}
						}
						catch(error)
						{
							alert(error);
						}
					}


					/**
					* Open Recent Projects In Content Pane Of Bridge & Close Startup Page if dont show again is clicked
					*/
					VCFlashPlayer.JumpToRecentProjects = function()
					{
					  try
					  {
						  var urlProjectsObj = VCSession.getSpecialNodeURL(VCNodeConstants.PROJECTS);
						  var urlProjects = urlProjectsObj.urlString;
						  if(urlProjects != null && urlProjects != typeof "undefined")
						  {
							  var vcStartPageTab = VCStartPage.getTabByID(VCStartPage.StartPageTabID);
							  var thumbnail = new Thumbnail("bridge:vc:" + urlProjects);
							  thumbnail.open();
							  var contentTab = VCStartPage.getTabByID('contentTab');
							  var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
							  var dontShowAgain = false;
							  if( vcFlashPlayer != null )
							  {
								dontShowAgain = vcFlashPlayer.invokePlayerFunction('IsDontShowAgainCallback');
								vcFlashPlayer.stopMovie();
							  }
							  if(contentTab == null || dontShowAgain)
							  {
		 						var task = "VCStartPage.removestartPageTab(true);";
			 					app.scheduleTask(task, 0, false );
			 					vcStartPageTab.visible = false;
			 					contentTab.visible = true;
							  }				
							  else
							  {
			 					contentTab.visible = true;
							  }			
						   }
					  }
					  catch(error)
					  {
						alert(error);
					  }
					}


					/**
					* Open Recent Project In Content Pane Of Bridge & Close Startup Page if dont show again is clicked
					*/
					VCFlashPlayer.JumpToRecentProject = function(projectURL)
						{
						  try
						  {			 
							  if(projectURL != null && projectURL != typeof "undefined")
							  {
								  var vcStartPageTab = VCStartPage.getTabByID(VCStartPage.StartPageTabID);
								  var thumbnail = new Thumbnail("bridge:vc:" + projectURL);
								  thumbnail.open();
								  var contentTab = VCStartPage.getTabByID('contentTab');
								  var vcFlashPlayer = VCStartPage.getVCFlashPlayer();
								  var dontShowAgain = false;
								  if( vcFlashPlayer != null )
								  {
									dontShowAgain = vcFlashPlayer.invokePlayerFunction('IsDontShowAgainCallback');
									vcFlashPlayer.stopMovie();
								  }
								  if(contentTab == null || dontShowAgain)
								  {
		 							var task = "VCStartPage.removestartPageTab(true);";
			 						app.scheduleTask(task, 0, false );
			 						vcStartPageTab.visible = false;
			 						contentTab.visible = true;
								  }				
								  else
								  {
			 						contentTab.visible = true;
								  }							  
							   }
						  }
						  catch(error)
						  {
							alert(error);
						  }
					}
					
					/**
					* Start my CS3 server
					*/
					VCFlashPlayer.JumpToStartMyServer = function()
					{
						try
						{
							var path = "intrinsic://localhost/startserver/";
							var pathURL = new VCURL( path );
							if( pathURL.isValid)
							{
								var node = VCSession.getKnownNode( pathURL );
								if( node.isValid )
								{
									var flags = new Array
									var statusResolveNode = node.resolveAlias( flags );
								}
							}			
						}
						catch(error)
						{
							alert(error);
						}
					}
					
					/**
					* 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 )
			{							
				var resultGetSettingValue = VCSession.getSettingsValue("Version_Cue_Welcome_Screen","DontShowAgain","0");
				
				if(!(resultGetSettingValue.isSuccess && resultGetSettingValue.value == "1"))
				{
					// Create the palette
					var paletteName = versioncue.getLocalizedString( "$$$/Bridge/VCAdapter/Menu_WelcomeTab=Version Cue Welcome Screen" );				
					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;
	}
	
	
	
	/**
	*  This Method Will Return List Of Local Servers (VC2 & VC3) If There.
	**/
	VCStartPage.getLocalServers = function(IsFirstCall)
	{
		try
		{
			var resultObj = new Object;
			resultObj.isStartMyServer = false;
			resultObj.StartMyServerNodeName = "";
			resultObj.localServerList = new VCServerContainer;
			var sessionNode = VCNode.getInterface(VCSession);			
			if(sessionNode.isValid)
			{
				var flags = new Array;
				flags[0] = VCFlagConstants.ALL_LEVELS;
				flags[1] = VCFlagConstants.EXPLORE_ICON
				var resultExploreNodes = sessionNode.exploreNodes(flags);
				if (resultExploreNodes.status.isSuccess)
				{
					var nodeList = resultExploreNodes.nodeList;
					for(var i = 0 ; i < nodeList.size; i++)
					{						
						var nodeObj = nodeList.getAt(i);
						var serverObj = nodeObj.server;
						if( nodeObj.isAlias )
						{
							resultObj.isStartMyServer = true;
							var pathStartMyServer = "intrinsic://localhost/startserver/";
							var thumbnailsSartMyServer = new Thumbnail("bridge:vc:" + pathStartMyServer);
							resultObj.StartMyServerNodeName = thumbnailsSartMyServer.name;
						}
						if(serverObj != null)
						{
							resultObj.localServerList.add(serverObj);
						}
					}
				}
				// Synchronous call to detect local server in case we dont found it above. Call it only for first call
				// to UpdateMyServer JS callback, which is from ActionScript.
				if(IsFirstCall && resultObj.localServerList.size == 0)
				{					
					var flags = new Array;
					flags[0] = VCFlagConstants.BROADCAST_SERVERS;
					var resultExploreServers = VCSession.exploreServers(flags);
					if (resultExploreServers.status.isSuccess)
					{
						var serverList = resultExploreServers.serverList;
						for(var count = 0 ; count < serverList.size ; count++)
						{
							var serverObj = serverList.getAt(count);
							if(serverObj.isLocal)
							{
								resultObj.localServerList.add(serverObj);
							}							
						}
					}
				}		
			}
			return resultObj;
		}
		catch(error)
		{
			alert(error);
		}
	}
	
	
	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;
					if ( versioncue.isWin )
					{
						// Win
						CustomIconPath = Folder.decode( versioncue.extensionPath + "/resources/images/" + prjServerName + "_" + projectObj.name + "_customIcon.png");
					}
					else if( versioncue.isMac )
					{
						// Mac
						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;
	}	
}
