﻿package captivate.veela_as3
{
	import flash.net.*;
	import flash.events.*;
	import flash.display.*;
	import flash.ui.*;
	import flash.system.fscommand;
	import flash.system.Security;
	import flash.utils.setInterval;
	import flash.utils.clearInterval;
	
	import flash.external.ExternalInterface;
	import flash.geom.Rectangle;
	
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	
public dynamic class cpSkinMainTimeline extends MovieClip
{
	public var isCPMovie:Boolean = true;
	public var CPMovieType:Number = 0;
    public var  cpCaptivateSkinSWF;
    public var  cpContentLoadStart;
    public var  cpContentPositioned;
    public var  cpContentScaled;
    public var  cpContentLoaded = false;
    public var  cpOrgSWFPath;
    public var  __loadbase;
    public var  skinWidth:int;
    public var  skinHeight:int;
    public var  contentLeft:int;
    public var  contentTop:int;
    public var  contentWidth:int;
    public var  contentHeight:int;
    public var  contentSWF:String;
	public var	loadedFromAggregator:Boolean = false;
	public var 	DoNotRegisterRightClickBecauseOfAggregator:Boolean = false;	
	public var	needToMuteAudioForAggregator:Boolean = false;


	public var  cpPbcBar_mc:MovieClip;//cpPlaybar;
	public var  playbar_mc:MovieClip;//rdPlaybar
	public var  cpSkinBorder_mc:MovieClip;
	public var  cpSkinLoader_mc:MovieClip;
	public var  contentShade_mc:MovieClip;
	public var  cpCC_mc:MovieClip;
	
	public var isCustomizable:Boolean=false;
	var intervalID :Number;

	//***************************************************
   /* var pbcBtnTips = pbcBtnTips_ENU;
    var pbcBtnFaceColor = 13624027;
    var pbcBtnGlowColor = 5548975;
    var pbcBtnIconColor = 2960685;
    var pbcBkColor = 15529201;
    var pbcBkAlpha = 100;
    var pbcBarPos = 3;
    var pbcBarAlign = 3;
    var movieWidth = 641;
    var movieHeight = 512;
    var hasRewind = true;
    var hasBack = true;
    var hasPlayPause = true;
    var hasSlider = true;
    var hasForward = true;
    var hasCC = true;
    var hasMute = true;
    var hasExit = true;
    var pbcApplyColor = false;
    var hasInfo = true;*/



		//RK The properties below here are also present 
		//in CaptivateMainTimeLine for embedding playbar.
		//For Localization, need to modify there also.
		public var PlaybarProperties:XML = CPSKINXMLSTR;
		
	var pbcBtnTips_ENU:Array = new Array("Rewind ",
									 "Back ",
									 "Play ",
									 "Pause ",
									 "Forward ",
									 "Closed Captioning ",
									 "Audio On ",
									 "Audio Off ",
									 "Exit ",
									 "Information ",
									 "Dummy for scrubber",
									 "Table of Contents",
									 "2x Fast Forward Speed",
									 "4x Fast Forward Speed",
									 "Normal Speed");

	public var pbcBtnTips:Array = pbcBtnTips_ENU;
	
	public var isPreviewSkin;
	private var isPreviewForAudioDialog;
	private var isPreview;
	CPSKINTIMELINESTR

	function cpSkinMainTimeline()
	{
		trace("*********** cpPbcBar_mc *********** "+ cpPbcBar_mc);
		addFrameScript(0, frame1);
		this.addEventListener(Event.ENTER_FRAME, onenterframe);
		cpCaptivateSkinSWF = true;
		
	}
	

	function frame1()
	{		
		myTrace("frame1");
		cpCaptivateSkinSWF = true;
		cpContentLoaded = false;
		__loadbase = undefined;
		
		createCpContextMenu();
	
		CPSKINFRAME1STR
		
		myTrace("cpSkinBorder_mc " + cpSkinBorder_mc + " ; isPreviewSkin " + isPreviewSkin);
		if(cpSkinBorder_mc != null)	
			cpSkinBorder_mc.visible = Boolean(isPreviewSkin);		
		
		
		// for External Interface -- Needed only for preview.
		try{
		//if(( (isPreview == 1) ||(isPreviewSkin == 1) ||  (isPreviewForAudioDialog == 1 ) ) && (ExternalInterface.available == true) && (parent==stage))
			if((ExternalInterface.available == true) && (parent==stage))
			{
				myTrace("ExternalInterface is available");
				ExternalInterface.addCallback("cpEISetValue", cpEISetValue);
				ExternalInterface.addCallback("cpEIGetValue", cpEIGetValue);
				ExternalInterface.addCallback("cpGetValue", cpGetValue);
			}
		}catch(e)
		{
			myTrace("Security sandbox violation error " + e);
		}
	
	}


	public function IsOrgSWFContent():Boolean
	{
		return (cpOrgSWFPath == contentSWF);
	}
	
	public function LoadAnotherSWF(swfPath:String)
	{
		if (contentSWF != swfPath)
		{
			// test //unloadMovie("cpSkinLoader_mc");
			this.removeChild(cpSkinLoader_mc);
			contentSWF = swfPath;
			cpContentLoaded = false;
			cpContentScaled = false;
			cpContentPositioned = false;
			cpContentLoadStart = false;	
		}
	}
	
	public function getLoadBase()
	{
		// test // add
		var _url:String = loaderInfo.url;
		
		if (__loadbase == undefined) {
			var q = _url.indexOf("?");           // skip query string
	
			// remove main movie filename
			if (q > 0)
			__loadbase = _url.substring(0, Math.max(_url.lastIndexOf("/", q), _url.lastIndexOf("\\", q)) + 1);
			else
			__loadbase = _url.substring(0, Math.max(_url.lastIndexOf("/"), _url.lastIndexOf("\\")) + 1);
		}
		return __loadbase;
	}
	
	/*function loadCompleted(a1:Event)
	{
		cpSkinLoader_mc = lm.content;
		ref.addChild(lm);
	}*/
	
	public function ContentStartLoad()
	{
		//Security.allowDomain("*");
		if (cpOrgSWFPath == null)
			cpOrgSWFPath = contentSWF;
		
			//GS 08-Nov-2006 BUG#189575. We should always use absolute paths to load a SWF.
			var contentSWFFullPath:String = getLoadBase() + contentSWF;
			trace("contentSWFFullPath = " + contentSWFFullPath);
			
			// test // loadMovie(contentSWFFullPath, "cpSkinLoader_mc");
			var lm:Loader = new Loader();
			var ref = this;
			
			lm.contentLoaderInfo.addEventListener(Event.COMPLETE,function(a1:Event)
			{
				
				trace(" @@@@@@@ COMPLETE @@@@@@@ ");
				ref.cpSkinLoader_mc = lm.content;
				
				//var pbcIndex:int = ref.getChildIndex(cpPbcBar_mc);
				//ref.addChildAt(lm, pbcIndex - 1);
				ref.addChild(lm);
				if(cpCC_mc != null)
					ref.addChild(cpCC_mc);
				if(cpPbcBar_mc != null)
					ref.addChild(cpPbcBar_mc);//Keep cpPbcBar_mc on top of CPMainSwf
				if(playbar_mc != null)
					ref.addChild(playbar_mc);
				
				if(cpSkinLoader_mc != null)
				{
					cpSkinLoader_mc.x = contentLeft;
					cpSkinLoader_mc.y = contentTop;
				}
				
				if(contentShade_mc != null)
				{
					contentShade_mc.x = contentLeft;
					contentShade_mc.y = contentTop;
					contentShade_mc.width = contentWidth;
					contentShade_mc.height = contentHeight;
				}
				if(ref.loadedFromAggregator)
				{
					ref.cpSkinLoader_mc.DoNotRegisterRightClickBecauseOfAggregator = DoNotRegisterRightClickBecauseOfAggregator;
				}
				
			});
			
			lm.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,function(aevent:Event)
			{
				myTrace("Load Error - Captivate Movie Not Loaded");
			});
			
			//RK Need to add the loadercontext as TOC has UIScrollbar classes 
			//in skin swf which are also present in case of questions.Bug: 1823651
			var cpLoaderContext:LoaderContext = new LoaderContext();
			//cpLoaderContext.applicationDomain = new ApplicationDomain();
			cpLoaderContext.applicationDomain = ApplicationDomain.currentDomain;
			addChild(lm);

			lm.load(new URLRequest(contentSWFFullPath),cpLoaderContext);
			
		//}
		/*
		else
		{
			loadMovie(contentSWF, "cpSkinLoader_mc");
		}
		*/
	
		cpContentLoadStart = true;
	
	}
	
	public function ContentPosition()
	{
		if (contentWidth > cpSkinLoader_mc.width)
			cpSkinLoader_mc.x = contentLeft + (contentWidth - cpSkinLoader_mc.width) / 2;
		else 
			cpSkinLoader_mc.x = contentLeft;
			
		if (contentHeight > cpSkinLoader_mc.height)
			cpSkinLoader_mc.y = contentTop + (contentHeight - cpSkinLoader_mc.height) / 2;
		else
			cpSkinLoader_mc.y = contentTop;
	
		cpContentPositioned = true;
	}
	
	public function ContentLoaded()
	{
		
		trace("content is ready");
		cpContentLoaded	= true;
		if (contentShade_mc!=null)
		{
			contentShade_mc.x = cpSkinLoader_mc.x;
			contentShade_mc.y = cpSkinLoader_mc.y;
			contentShade_mc.width = cpSkinLoader_mc.width;
			contentShade_mc.height = cpSkinLoader_mc.height;
		}
		fscommand("cpSkinReady", 1);//to tell the UI it's ready
	
		this._slideContent2 = cpSkinLoader_mc._slideContent2;
		this._slideContent = cpSkinLoader_mc._slideContent;
		
	}
	
	function initTOC(temp:cpSkinMainTimeline)
	{
		
	}
	
	public function ContentScaleDown()
	{
		if ( (contentWidth < cpSkinLoader_mc.width) || (contentHeight < cpSkinLoader_mc.height) )
		{
			var ratioW:Number = contentWidth / cpSkinLoader_mc.width;
			var ratioH:Number = contentHeight / cpSkinLoader_mc.height;
			var ratio = (ratioW < ratioH) ? ratioW : ratioH;
			if ((ratio < 0.99) || (ratio > 1.01))
			{
				cpSkinLoader_mc.width *= ratio;
				cpSkinLoader_mc.height *= ratio;
			}
		}
		
		ContentPosition();
		cpContentScaled = true;
	}
	
	public function ContentProcess()
	{
		if (cpContentLoadStart != true)
			ContentStartLoad();
		else if (cpSkinLoader_mc!=null && cpSkinLoader_mc.framesLoaded >= 1)  
		{
			/*SW since width and height of the loaded movie is the same as content width and height
			scaling and positioning are not required. Moreover cpSkinLoader_mc.height and cpSkinLoader_mc.width
			varies.
			if (cpContentPositioned != true)
				ContentPosition();
			else if (cpContentScaled != true)
				ContentScaleDown();
			else*/ if (cpContentLoaded != true)
			{
				var l2:LoaderInfo = cpSkinLoader_mc.loaderInfo;
				if (l2.bytesTotal * 0.7 <= l2.bytesLoaded)
					ContentLoaded();
			}
		}
	}
	
	// test //function onEnterFrame()
	function onenterframe(aevent:Event)
	{
		//SM Bug #176840
		if(currentFrame == 2)
			stop();
		ContentProcess();
		if(cpSkinLoader_mc != null)
		{
			this._slideContent2 = cpSkinLoader_mc._slideContent2;
			this._slideContent = cpSkinLoader_mc._slideContent;
		}
	//	trace("this = " + this + " cpSkinLoader_mc._slideContent2 = " + cpSkinLoader_mc._slideContent2 );
	}

	
	public function createCpContextMenu() 
	{
		var l1:LoaderInfo = this.loaderInfo;
		l1.addEventListener(Event.COMPLETE,function(a1:Event)
		{
			var my_cm:ContextMenu = new ContextMenu();
			// test //
			//var menuItem_cmi:ContextMenuItem = new ContextMenuItem("Created with Adobe Captivate", adobeHomeMenuItemHandler);
			var menuItem_cmi:ContextMenuItem = new ContextMenuItem("Created with Adobe Captivate");
			menuItem_cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,adobeHomeMenuItemHandler);

			my_cm.customItems.push(menuItem_cmi);
			my_cm.hideBuiltInItems();
			this.menu = my_cm;
		});
	}
	
	public function adobeHomeMenuItemHandler(a:ContextMenuEvent) {
		navigateToURL(new URLRequest("http://www.adobe.com/products/captivate"), "_blank");
	}
	
		//For External Interface
	public function cpEISetValue(variable:String, val):void
	{
		//myTrace("cpSetValue variable " + variable + " ; val  " + val);
		
		var arr:Array = variable.split(".");
		var ref = this;
		
		for(var i=0; i < arr.length -1; i++)
		{
			ref = ref[arr[i]];
		}
		var nVal:Number = Number(val);
		if(isNaN(nVal))
			ref[ arr[arr.length -1] ] = val;		//For Strings
		else
			ref[ arr[arr.length -1] ] = nVal;		//For Numbers and booleans
	}
	
	public function cpEIGetValue(variable:String)
	{
		var arr:Array = variable.split(".");
		var ref = this;
		
		for(var i=0; i < arr.length -1; i++)
		{
			ref = ref[arr[i]];
		}
		ref = ref[ arr[arr.length -1] ];

		//myTrace("cpGetValue variable " + variable + " ; val " + ref  + ' cpSkinLoader_mc.rdcmndPause '+  cpSkinLoader_mc.rdcmndPause );
		
		return ref;		
	}
	
	public function cpGetValue(variable:String)
	{
		return cpEIGetValue(variable);
	}
	
	private function myTrace(str:String)
	{
		trace("cpSkineMainTimeline::"+str);
	}
	
}

}
