package {
	import flash.events.*;
	import flash.display.*;
	import flash.net.*;
	import flash.media.*;
	public class NavEvents {
		var framework:Framework;
		var navigation:MovieClip;
		
		public var navData:XML;
		public var loader:URLLoader;
		
		public var strOrient:String;
		public var strWrapup:String;
		public var p2:String;
		
		public var strModule:String;
		public var strLesson:String;
		public var strOf:String;
		
		/*
		*  @public
		*  calling events of all interface navigation system  
		*/
		public function NavEvents(ArgFramework:Framework,ArgNavigation:MovieClip) {
			framework=ArgFramework;
			navigation=ArgNavigation;
			
				navigation.addEventListener("onNext",onNavigatorNext);
				navigation.addEventListener("onBack",onNavigatorBack);
				navigation.addEventListener("onPause",onNavigatorPause);
				navigation.addEventListener("onAudioVolume",onNavigatorAudioVolume);
				navigation.addEventListener("onReplay",onNavigatorReplay);
				navigation.addEventListener("onMenu",onNavigatorMenu);
				navigation.addEventListener("onPressAction",onNavigatorPressAction);
				navigation.addEventListener("onPressResources",onNavigatorPressResources);
				navigation.addEventListener("onPressGlossary",onNavigatorPressGlossary);
				navigation.addEventListener("onPressHelp",onNavigatorPressHelp);
				navigation.addEventListener("onPressExit",onNavigatorPressExit);
			
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,completeHandler);
			 
			var request:URLRequest= new URLRequest("component/content_en/interface.xml");
			
			
			try 
			{
				loader.load(request);
			} 
			catch(error:Error) 
			{
			}
		
		}

		/*
		*  @public
		*  set the data from interface xml file to objects after loading complete
		*/
		private function completeHandler(e:Event):void
		{
			navData = new XML(e.target.data);
			navigation.setNextHint(navData.btn.(@id=="next"));
			navigation.setBackHint(navData.btn.(@id=="back"));
			navigation.setPlayPauseHint(navData.btn.(@id=="play"),navData.btn.(@id=="pause"));
		    navigation.setTitle(navData.p.(@id=="title"));
			navigation.setAudioHint(navData.btn.(@id=="audio_off"));
			navigation.setReplayHint(navData.btn.(@id=="replay"));
			navigation.setMenuHint(navData.btn.(@id=="menu_off"));
			navigation.setAction(navData.btn.(@id=="action"));
			navigation.setResources(navData.btn.(@id=="resources"));
			navigation.setGlossary(navData.btn.(@id=="glossary"));
			navigation.setHelp(navData.btn.(@id=="help"));
			navigation.setExit(navData.btn.(@id=="exit"));
			
			navigation.setpp(navData.p.(@id=="pp"));
			p2 = navData.p.(@id=="about");
			
			loader.removeEventListener(Event.COMPLETE, completeHandler);
			
			 strOrient =  navData.p.(@id=="orientation");
			 strWrapup =  navData.p.(@id=="wrapup");
			 strModule =  navData.p.(@id=="module");
			 strLesson =  navData.p.(@id=="lesson");
			 strOf =  navData.p.(@id=="of");
			
		}
		//--------------------------------------
		//  Navigator Events
		//--------------------------------------
		function onNavigatorNext(e:Event) {
			
			framework.nextScreen();
		}
		function onNavigatorBack(e:Event) {
			framework.backScreen();
		}
		
		function onNavigatorPause(e:Event) {

			if(framework.playMode == true){
			  framework.pauseContent();
			}
			else{
              framework.playMode = true; // dummy setting for toggling Quiz Screens
			  if (CGlobal.islAppRoot.checkFinished && CGlobal.content_mc.screenAudio == "on"){
				framework.ReplayScreen();
			  }
			  else if (!CGlobal.islAppRoot.checkFinished ){
    			framework.playContent();
			  }
			  else{
			    updateBtns();
				}
			}

			
		}
		
		function onNavigatorAudioVolume(e:Event) {
			
			if(framework.audioMode == true){
			  
			  navigation.buttonMuteOn();
			  navigation.setAudioHint(navData.btn.(@id=="audio_off"));
			  framework.audioMode = false;
		     SoundMixer.soundTransform = new SoundTransform(1);
			  }else{

		     SoundMixer.soundTransform = new SoundTransform(0);
			  navigation.buttonMuteOff();
			  navigation.setAudioHint(navData.btn.(@id=="audio_on"));
			  framework.audioMode = true;
			}
			
			
		}
		function onNavigatorReplay(e:Event) {
			
			framework.ReplayScreen();
		}
		function onNavigatorMenu(e:Event) {
			framework.OpenComponant("course_menu");
		}
		function onNavigatorPressAction(e:Event) {
			framework.OpenComponant("action_plan");
		}
		function onNavigatorPressResources(e:Event) {
			framework.OpenComponant("resources");
		}
		function onNavigatorPressGlossary(e:Event) {
			framework.OpenComponant("glossary");
		}
		function onNavigatorPressHelp(e:Event) {
			framework.OpenComponant("help");
		}
		function onNavigatorPressExit(e:Event) {
			
			navigateToURL(new URLRequest(p2));
			
			
		}
		/////////////////////////////////////////////////////
		//--------------------------------------
		//  Methods
		//--------------------------------------
		public function updateBtns(){
			
			if(framework.playMode == true){
		      navigation.buttonPauseState();
			}else{
			  navigation.buttonPlayState();
			}

			
			}
		function getItemNumInList(Item:XML,Arra:XMLList)
		{
			for(var i:int=0;i<Arra.length();i++)
			{
				
				if(Arra[i].@src.toString()==Item.@src.toString())
				{
					return i;
				}
			}
			return -1;
		}
			
		public function updateNavData() {

			//setting the track bar width
			//framework.CurrentScreenID
			
			var TotalPages:int=framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().parent().groupnode.groupnode.page.length();
			var myNum:int=getItemNumInList(framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID],framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().parent().groupnode.groupnode.page)+1;
			
			navigation.setTrackRatio(((myNum*100)/TotalPages));
			
			 var m = CGlobal.currentPage.parent().parent().parent().title;
			 navigation.setLesson(m);
			 
			 var Mod = framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().parent().(@id);
			 var ModN = Mod.childIndex() - 1;
			 var ModT = framework.Structure_code.XMLData.groupnode.length() - 1;
			 			
			var n  = CGlobal.currentPage.parent().parent().parent().children().length() - 2;
			
			navigation.setTopic(framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().title + ", " + framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().title);
			
			
			
			if(ModN == 0){
				
				navigation.setpage_counter("<b>" + strOrient + " " + strLesson + " " +  getParentIndex()  + " " + strOf + " " + n + "</b>");
			}
			
			else if(ModN == ModT){
				navigation.setpage_counter("<b>" + strWrapup + "</b>");
			}else{
			
			    navigation.setpage_counter("<b>" + strModule +  " " + ModN + " :"  + " " + strLesson + " " +  getParentIndex()  + " " + strOf + " " + n + "</b>");
			}
			//----------------------------------------------------------------------
			if(!CGlobal.currentPage)
			{
				return;
			}
			if (CGlobal.currentPage.@back != undefined && CGlobal.currentPage.@back.toString() == "null")
			{
				navigation.disableBackButton();
			}
			else if (CGlobal.currentPage.parent.@type && CGlobal.currentPage.parent.@type.toString().indexOf("exam") != -1 && CGlobal.currentPage.@type.toString() == "question")
			{
				navigation.disableBackButton();
			}
			else
			{
				navigation.enableBackButton();
			} // end else if
			if (!CGlobal.currentPage.@next || CGlobal.currentPage.@next.toString() == "null" || CGlobal.islAppRoot.islSettings.forcedNavigation == "on")
			{
				navigation.disableNextButton();
			}
			
			
			
			
			else if (CGlobal.currentPage.parent.@type && CGlobal.currentPage.parent.@type.toString().indexOf("exam") != -1 && CGlobal.currentPage.@type.toString() == "question")
			{
				navigation.disableNextButton();
			}
			else if (CGlobal.currentPage.@type && CGlobal.currentPage.@type.toString() == "interactive")
			{
				navigation.disableNextButton();
			}
			else
			{
				navigation.enableNextButton();
			} // end else if
			if (CGlobal.currentPage.parent.@type && CGlobal.currentPage.parent.@type.toString().indexOf("exam") != -1 && CGlobal.currentPage.@type.toString() == "question")
			{
				
				navigation.disableReplayButton();
			}
			else
			{
				navigation.enableReplayButton();
			} // end else if
		}
		public function moduleNumber(modCN:Number =0/*module number*/):String {

			if ((modCN) == 0) {
				return framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().parent().title;
			} else {
				return ("Module " + modCN);
			}
		}
		 public function getMyIndex(Node:XML):int {

			 var curNodeId:String = Node.@src.toString();
			for (var ppC:Number=0; ppC <= Node.parent().page.length(); ppC++) {
				if (Node.parent().page[ppC].@src.toString() == curNodeId) {
					return ppC;
					
				}
			}
			return -1;
		}
		 public function getParentIndex() {
			var curNodeParentId:String = framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().@id;
			for (var ppC:Number=0; ppC <= framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().parent().children().length(); ppC++) {
				var ppID:String = framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().parent().children()[ppC].@id;
				if (framework.Structure_code.XMLData.groupnode.groupnode.groupnode.page[framework.CurrentScreenID].parent().parent().@id == ppID) {
					return (ppC - 1);
					
				}
			}
		}

		
		

	}
}