/*
* This class belongs to quesions receives button 
* actions and evaluation results.
*/
package {
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.Timer;


	public class Interaction_Code {
		var targetX;
		public function Interaction_Code() {
			
		}

		/*
		*  @public
		*  initiating interaction screen types 
		*/
		public function initInteractionsScreen(type) {
			if (!CGlobal.islAppRoot.islMainPlayerRunning) {
				
			} else {
				contentLevel = CGlobal.content_mc;
			}// end else if
			switch (type) {


				case "carousel" :
					{

						init_carousel();
						break;

					};

				case "graphicalTimeline" :
					{
						init_graphicalTimeline();
						break;

				}
			};


			


		}// End of the public function
		var contentLevel;
		

		/*
		*  @public
		*  Generating objects which isn't initiated or 
		*  ceated manually on the stage.
		*/
		public function autoCreate() {
			var _loc5 = 0;
			for (var _loc2 = 1; _loc2 <= contentLevel.slider_items; ++_loc2) {
				
				
				var _loc1 = contentLevel.graphic_mc["click_" + _loc2];
				_loc1.x = _loc5;
				_loc1.visited = 0;
				_loc5 = _loc5 + (contentLevel.item_spacer + _loc1.width);
				_loc1.indicator.indicator_gr.gotoAndPlay(contentLevel["indicator_type_" + _loc2]);
				if (_loc1.roll_ != undefined) {
					_loc1.roll_.name = "roll_" + _loc2;
				}// end if
				initItems(_loc1);
			}// end of for
			contentLevel.graphic_mc.click_item.visible = false;
			contentLevel.graphic_mc.item.visible = 0;
			contentLevel.graphic_mc.x = contentLevel.slider_viewable_area.x;
			if (contentLevel.item_grouping > 0) {
				var _loc7 = contentLevel.graphic_mc.item.width * contentLevel.item_grouping;
				_loc7 = _loc7 + contentLevel.item_spacer * (contentLevel.item_grouping - 1);
				var _loc6 = 0;
				var _loc8 = Math.floor(contentLevel.slider_items / contentLevel.item_grouping);
				for (var d:int = 1; d <= _loc8; d++) {
					
					
					var _loc3 = contentLevel.graphic_mc["group_bkground_" + d];
					var _loc4 = contentLevel.graphic_mc["group_graphic_" + d];
					_loc3.width = _loc7;
					_loc3.x = _loc6;
					_loc6 = _loc6 + (contentLevel.item_spacer + _loc3.width);
					var centrePoint = _loc3.width / 2 - _loc4.width / 2;
					_loc4.x = _loc3.x + centrePoint;
				}// end of for
				contentLevel.graphic_mc.grouping_gr.visible = false;
				contentLevel.graphic_mc.grouping_bkg.visible = false;
			} else {
				contentLevel.graphic_mc.grouping_gr.visible = false;
				contentLevel.graphic_mc.grouping_bkg.visible = false;
			}// end else if
			initSlider();
		}// End of the public function

		/*
		*  @public
		*  initaiting the properties of the slider.
		*  
		*/
		public function initSlider() {
			var sliderMoving = false;
			var sliderBegX = 0;
			var sliderDistance = 0;
			graphicBegX = 0;
			graphicDistance = 0;
			var backgroundBegX = 0;
			var backgroundDistance = 0;



			contentLevel.slider.hitArea_mc.addEventListener(MouseEvent.MOUSE_UP ,onpress);

			// dragging slider.
			function onpress(e:MouseEvent) {
				contentLevel.slider.startDrag(false, contentLevel.slider_bar.x, contentLevel.slider.y, contentLevel.slider_bar.x + contentLevel.slider_bar.width - contentLevel.slider.hitArea_mc.width, contentLevel.slider.y);
				sliderMoving = true;
				contentLevel.extra_content.gotoAndStop(1);
			}

			contentLevel.slider.hitArea_mc.addEventListener(MouseEvent.MOUSE_UP ,onrelease);

			//Dropping Slider
			function onrelease(e:MouseEvent) {
				contentLevel.slider.stopDrag();
				sliderMoving = false;
			}


			contentLevel.slider.hitArea_mc.onReleaseOutside = function ()
			{
			contentLevel.slider.stopDrag();
			sliderMoving = false;
			};


			contentLevel.slider.addEventListener(Event.ENTER_FRAME , onEFFunc);

			// Adjusting the positions of the slider on ENTER_FRAME
			function onEFFunc(e:Event) {
				if (e.target.moveme == true) {
					var dx = e.target.targetX - e.target.x;
					e.target.x = e.target.x + dx/contentLevel.graphic_speed;
					if (Math.abs(dx) <= 1) {
						e.target.moveme = false;
						e.target.x = targetX;
					}// end if
				}// end if
			}
			sliderBegX = contentLevel.slider_bar.x;
			sliderDistance = contentLevel.slider_bar.width - contentLevel.slider.hitArea_mc.width;
			graphicBegX = contentLevel.graphic_mc.x;
			graphicDistance = contentLevel.graphic_mc.width - contentLevel.slider_viewable_area.width;
			backgroundBegX = contentLevel.background_mc.x;
			backgroundDistance = contentLevel.background_mc.width - contentLevel.slider_viewable_area.width - 800;

			contentLevel.graphic_mc.addEventListener(Event.ENTER_FRAME , onEFFunc2);

			
			function onEFFunc2(e:Event) {
				var perMoved = (contentLevel.slider.x - sliderBegX) / sliderDistance;
				var grMoved = -(contentLevel.graphic_mc.x - graphicBegX) / graphicDistance;
				if (sliderMoving) {
					var _loc2 = graphicBegX - graphicDistance * perMoved;
					var _loc3 = backgroundBegX - backgroundDistance * perMoved;
					e.target.targetX = _loc2;
					e.target.moveme = true;
					contentLevel.background_mc.targetX = _loc3;
					contentLevel.background_mc.moveme = true;
				}// end if
				if ( e.target.moveme == true) {
					if ( e.target.popTargetX != undefined) {
						dx =  e.target.popTargetX -  e.target.x;
					} else {
						dx =  e.target.targetX -  e.target.x;
					}// end else if
					e.target.x =  e.target.x + dx / contentLevel.graphic_speed;
					if (grMoved > 0 && grMoved < 1 &&  e.target.popTargetX != undefined) {
						_loc3 = backgroundBegX - backgroundDistance * grMoved;
						contentLevel.background_mc.targetX = _loc3;
						contentLevel.background_mc.moveme = true;
						var _loc4 = sliderBegX + sliderDistance * grMoved;
						contentLevel.slider.targetX = _loc4;
						contentLevel.slider.moveme = true;
					}// end if
					if (Math.abs(dx) <= 1) {
						e.target.moveme = false;
						e.target.x = targetX;
						e.target.popTargetX = undefined;
					}// end if
				}// end if
			}
			contentLevel.background_mc.addEventListener(Event.ENTER_FRAME , onEFFunc3);

			function onEFFunc3(e:Event) {
				if (e.target.moveme == true) {
					dx = e.target.targetX - e.target.x;
					e.target.x = e.target.x + dx / contentLevel.background_speed;
					if (Math.abs(dx) <= 1) {
						e.target.moveme = false;
						e.target.x = targetX;
					}// end if
				}// end if
			}
		}// End of the public function


		/*
		*  @public
		*  Initiating Items when event case is fired
		*  
		*/
		public function initItems(obj) {
			graphicBegX = contentLevel.graphic_mc.x;
			if (obj.hitArea_mc != undefined) {

				obj.hitArea_mc.buttonMode = true;
				obj.hitArea_mc.mouseChildren = false;

				obj.hitArea_mc.addEventListener(MouseEvent.MOUSE_OVER ,item_over);
				obj.hitArea_mc.addEventListener(MouseEvent.MOUSE_OUT  ,item_out);
				obj.hitArea_mc.addEventListener(MouseEvent.MOUSE_DOWN ,item_release);

				
			} else {
				obj.buttonMode = true;
				obj.mouseChildren = false
				;
				obj.addEventListener(MouseEvent.MOUSE_OVER ,item_over);
				obj.addEventListener(MouseEvent.MOUSE_OUT  ,item_out);
				obj.addEventListener(MouseEvent.MOUSE_DOWN ,item_release);

				
			}// end else if
		}// End of the public function

		/*
		*  @public
		*  Executing the Mouse_Over event specifications
		*  which belonging to screen items (optins, buttons...etc).
		*/
		public function item_over(e:MouseEvent) {
			if (e.target.name.indexOf("hitArea_mc") > -1) {
				curItemStr = String(e.target.parent);
				curItemStr = curItemStr.substring(curItemStr.lastIndexOf("_") + 1, curItemStr.length);
				e.target.parent.gotoAndPlay("rollover");
				e.target.parent.indicator.gotoAndPlay("rollover");
				if (contentLevel["roll_" + curItemStr] != undefined) {
					e.target.parent["roll_" + curItemStr].gotoAndPlay("open");
				}// end if
			} else {
				curItemStr = String(this);
				curItemStr = curItemStr.substring(curItemStr.lastIndexOf("_") + 1, curItemStr.length);
				e.target.gotoAndPlay("rollover");
				e.target.indicator.gotoAndPlay("rollover");
				if (contentLevel["roll_" + curItemStr] != undefined) {
					e.target["roll_" + curItemStr].gotoAndPlay("open");
				}// end if
			}// end else if
		}// End of the public function

		/*
		*  @public
		*  Executing the Mouse_Out event specifications
		*  which belonging to screen items (optins, buttons...etc).
		*/
		public function item_out(e:MouseEvent) {
			if (e.target.name.indexOf("hitArea_mc") > -1) {
				e.target.parent.gotoAndPlay("rollout");
				e.target.parent.indicator.gotoAndPlay("rollout");
				if (contentLevel["roll_" + curItemStr] != undefined) {
					e.target.parent["roll_" + curItemStr].gotoAndPlay("close");
				}// end if
			} else {
				e.target.gotoAndPlay("rollout");
				e.target.indicator.gotoAndPlay("rollout");
				if (contentLevel["roll_" + curItemStr] != undefined) {
					e.target["roll_" + curItemStr].gotoAndPlay("close");
				}// end if
			}// end else if
		}// End of the public function

		/*
		*  @public
		*  Executing the Mouse_Click event specifications
		*  which belonging to screen items (optins, buttons...etc).
		*/
		public function item_release(e:MouseEvent) {
			if (e.target.name.indexOf("hitArea_mc") > -1) {
				e.target.parent.gotoAndPlay("down");
				e.target.parent.indicator.gotoAndPlay("down");
				e.target.parent.viewed_mc.visible = true;
			} else {
				e.target.gotoAndPlay("down");
				e.target.indicator.gotoAndPlay("down");
				e.target.viewed_mc.visible = true;
			}// end else if
			showInfo();
		}// End of the public function

		/*
		*  @public
		*  
		*  
		*/
		public function showInfo() {
			
			currentAction = contentLevel.item_action;
			currentAction = currentAction.toLowerCase();
			if (currentAction == "popup") {
				currentItemWidth = (contentLevel.graphic_mc["click_" + curItemStr].width + contentLevel.item_spacer) * (curItemStr - 1) + contentLevel.graphic_mc["click_" + curItemStr].item_center.x;
				contentLevel.graphic_mc.popTargetX = contentLevel.popup_position.x - currentItemWidth;
				contentLevel.graphic_mc.moveme = true;
				button_code_lib.sharedfunctionality(contentLevel.graphic_mc["click_" + curItemStr]);
			} else if (currentAction == "loadpage") {
				branchPage = CGlobal.islAppRoot.findPageBySrc(CGlobal.Course, contentLevel["loadPage_" + curItemStr]);
				loadPage(branchPage);
			}// end else if
		}// End of the public function

		/*
		*  @public
		*  check if the slider exist call initSlider to initialize it
		*  or creat it by calling autoCreate function
		*/

		public function init_graphicalTimeline() {
			if (contentLevel.slider_autoCreate == "on") {
				autoCreate();
			} else {
				initSlider();
			}// end else if
		}// End of the public function
		var difference;
		var graphicBegX;
		var graphicDistance;
		var curItemStr;

		public function loadExternal(fileName:String) {
			if (fileName.indexOf("mailto") == -1) {
				var request1:URLRequest = new URLRequest(fileName);
				navigateToURL(request1, "_blank");
			} else {
				var request2:URLRequest = new URLRequest(fileName);
				navigateToURL(request2, "_blank");
			}// end else if
		}// End of the public function

		//Loading Captivate files
		public function loadCaptivate(launchName) {
			CGlobal.islAppRoot.captivate_btn = CGlobal.islAppRoot.captivate_btn == undefined ? ("") : ("");
			contentLevel.capPlayState = true;
			if (!CGlobal.islAppRoot.islMainPlayerRunning) {
				var _loc3 = CGlobal.PathBack+"captivates/" + contentLevel.myFile + "_" + launchName + ".swf";
			} else {

				_loc3 = CGlobal.PathBack + "movies/captivates/" + contentLevel.myFile + "_" + launchName + ".swf";

				_loc3 = CGlobal.PathBack + "movies/captivates/" + contentLevel.myFile + "_" + launchName + ".swf";

			}// end else if
			switch (launchName) {
				case "captivate_1" :
					{
						captivate.captivate_type = "Guide Me";
						break;

						} ;
					case "captivate_2" :
						{
							captivate.captivate_type = "Let Me Try";
							break;

							} ;
						case "captivate_3" :
							{
								captivate.captivate_type = "Guide Me";
								break;

								} ;
							case "captivate_4" :
								{
									captivate.captivate_type = "Let Me Try";
									break;

							}
							} ;// End of switch
							var Preloader:FileLoader=new FileLoader(_loc3);
							Preloader.addEventListener("onFileLoad",onPreloaderLoad);

							// data loading process of Capivate child movies 
							function onPreloaderLoad() {
								captivate.holder.addChild(Preloader.loader.content);
							}
							if (contentLevel.screenAudio == "on") {
								
							}// end if
							var easeInterval:Timer=new Timer(1000,0);
							easeInterval.addEventListener(TimerEvent.TIMER,easeInCap(launchName));
							easeInterval.start();
							
							
							contentLevel[launchName].buttonMode = false;
							CGlobal.islAppRoot.captivate_btn = launchName;
							CGlobal.islDebug("contentLevel.captivate_btn: " + CGlobal.islAppRoot.captivate_btn);
						}// End of the public function

						
						public function easeInCap(launchName) {
							easeInterval.stop();
							captivate.initProgressBar(captivate.holder.rdinfoSlideCount);
							CGlobal.islAppRoot.easeInComponent("captivate");
						}// End of the public function

						// Initiate the carousel items.
						public function init_carousel() {
							var a = new Number();
							var depthCounter = 0;
							var rad = 1.745329E-002;
							var _loc5 = 360 / contentLevel.carousel_items;
							for (var _loc3 = 1; _loc3 <= contentLevel.carousel_items; ++_loc3) {
								contentLevel.carousel["click_" + _loc3].angle = _loc5 * _loc3;
								contentLevel.carousel["click_" + _loc3].y = 0;
							}// end of for
							contentLevel.carousel.x = contentLevel.dead_zone.x;

							contentLevel.carousel.addEventListener(Event.ENTER_FRAME , EFFunc);


							// animate Items on Enter_Frame according to mouse movement in a carousel style
							function EFFunc(e:Event) {
								if (contentLevel.xmouse <= contentLevel.dead_zone.x - contentLevel.dead_zone.width / 2 || contentLevel.xmouse >= contentLevel.dead_zone.x + contentLevel.dead_zone.width / 2) {
									for (var _loc3 = 1; _loc3 <= contentLevel.carousel_items; ++_loc3) {
										var _loc2 = e.target["click_" + _loc3].angle + a;
										_loc2 = _loc2 > 360 ? ((_loc2 - 360) % 360) : (_loc2 % 360);
										_loc2 = _loc2 < 0 ? ((_loc2 + 360) % 360) : (_loc2 % 360);
										var _loc4 = 1.500000E+000 - Math.sin(_loc2 * rad);
										e.target["click_" + _loc3].x = contentLevel.carousel_spacer * Math.cos(_loc2 * rad) * 1.800000E+000;
										e.target["click_" + _loc3].scaleX = -45 * Math.sin(_loc2 * rad) * _loc4;
										e.target["click_" + _loc3].scaleY = 42 * _loc4;
										e.target["click_" + _loc3].alpha = 10 + 42 * _loc4;
										if (e.target["click_" + _loc3].xscale > 50) {
											
											++depthCounter;
										}// end if
									}// end of for
									a = a - e.target.mouseX / contentLevel.carousel_speed_decreaser;
								}// end if
								TimerEvent.updateAfterEvent();
								
							}
						}// End of the public function
					}
				}