DAN ZEN EXPO - CODE EXHIBIT - HANGY
package {
	
	import com.danzen.interfaces.invisibleInterface.IIEvent;
	import com.danzen.interfaces.invisibleInterface.IISet;
	import com.danzen.utilities.DynamicObject;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.Font;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemIdleMode;
	
	public class Hangy extends MovieClip {
		
		private var myIISet:IISet;		
		private var current:MovieClip; // MovieClip to show viewer
		private var currentTip:MovieClip;
		private var sW:Number;
		private var sH:Number;
		private var myFormat:TextFormat;
		
		private var assets:MovieClip;
		private var backing:MovieClip;
		private var logo:MovieClip;
		private var setupBut:MovieClip;
		private var menuTip:MovieClip;
		private var menuTipV:MovieClip;
		private var autoTouch:MovieClip;
		private var autoTouchV:MovieClip;
		private var myMoods:MovieClip;
		private var myText:MovieClip;
		private var myLights:MovieClip;
		private var myAds:MovieClip;
		private var myAdsV:MovieClip;
		private var myPics:MovieClip;
		private var myPicsV:MovieClip;
		private var menus:MovieClip;
		private var textWindow:MovieClip;
		private var picWindow:MovieClip;
		private var thumbs:MovieClip;
		private var picInterface:MovieClip;
		private var samplePic:MovieClip
		private var samplePicTest:MovieClip
		private var playing:MovieClip;
		
		private var picsFitLast:Boolean = true;
		private var adsFitLast:Boolean = true;
		
		private var firstCheck:Boolean = true;
		
		private var ps:PhotoStore;
		
		private var verticalCheck:Boolean;
		private var lastVerticalCheck:Boolean;
		
		private var greetsDefaultText:String = "How's it hanging?\n'Sup bro, dude\nLong time no see!\nCiao for now!\nHasta la vista\n";
		private var phrasesDefaultText:String = "Don't worry\nBaby - hands up!\nMeddling kids!\nAnd the point is...\nzzzzz zzzz zzzzzzz...\n";
		private var defaultPicsFit:Array = [true, true, false, false, false]; // true if don't want to lose any of picture
		private var defaultAdsFit:Array = [true, true, true, true, true];		
		private var gridSetup:Array = [[1,1],[2,1],[3,1],[2,2],[3,2],[3,2],[4,2],[4,2],[3,3],[4,3],[4,3],[4,3]];
		
		private var myData:MyData;		
		
		private var lastText:String;
		private var currentTextType:String;
		
		private var lastPic:Object;
		private var currentPicType:String;
		private var currentPicFit:Boolean = true;
		private var lastType:String; // for custom pic or ad when resizing
		private var lastPosition:Number; // for custom pic or ad when resizing
		
		private var currentPic:MovieClip;
		private var otherPic:MovieClip; // either vertical or horizontal thumb pic
		private var currentHit:MovieClip;
		private var picTimer:Timer;
		private var picsCheck:Boolean = false;
		private var adsCheck:Boolean = false;
		private var otherHit:MovieClip;
		
		private var picLocX:Number;
		private var picLocY:Number;
		private var picTime:Number;
		
		private var overlay:MovieClip;
				
		private var autoTimer:Timer;
		private var autoTipTimer:Timer;
		private var autoPlayingTimer:Timer;
		private var picsAutoTimerSecs:Number = 4;
		private var adsAutoTimerSecs:Number = 4;
		private var currentAuto:MovieClip;
		private var autoShield:MovieClip;
		private var autoToggle:Boolean = false;
		private var autoPause:Boolean = false;
		private var currentAutoPic:Number = 1;
		private var currentAutoAd:Number = 1;
		private var picsAutoCheck:Boolean = false;
		private var adsAutoCheck:Boolean = false;
		private var lastSecs:Number;
		private var overlayOkay:Boolean = true;
		
		private var myFont:Font;
		
		[SWF(backgroundColor='#000000')]
		
		public function Hangy() {
			trace("hi from Hangy");
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void {
			
			NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;	
			
			assets = new Assets();			
			myFont = new WillFont();	
						
			backing = assets.backing;
			backing.gotoAndStop(1);
			logo = assets.logo;	
			logo.mouseEnabled = false;
			logo.mouseChildren = false;
			setupBut = assets.setupBut;
			
			menuTip = assets.menuTip;
			menuTipV = assets.menuTipV;
			menuTip.mouseEnabled = false;
			menuTip.mouseChildren = false;
			menuTipV.mouseEnabled = false;
			menuTipV.mouseChildren = false;
			autoTouch = assets.autoTouch;
			autoTouchV = assets.autoTouchV;			
			myMoods = assets.myMoods;
			myText = assets.myText;
			myLights = assets.myLights;
			myAds = assets.myAds;
			myAdsV = assets.myAdsV;
			myPics = assets.myPics;
			myPicsV = assets.myPicsV;
			menus = assets.menus;
			textWindow = assets.textWindow;
			picWindow = assets.picWindow;
			picInterface = assets.picInterface;
			samplePicTest = assets.samplePic;
			
			stopSwoodle(); // has sounds...
			
			
			autoTimer = new Timer(picsAutoTimerSecs*1000);
			autoTimer.addEventListener(TimerEvent.TIMER, doAuto);
			autoTipTimer = new Timer(3000);
			autoTipTimer.addEventListener(TimerEvent.TIMER, doAutoTip);
			autoPlayingTimer = new Timer(1500);
			autoPlayingTimer.addEventListener(TimerEvent.TIMER, doAutoPlaying);
			
			addChild(backing);
			
			picTimer = new Timer(200);
			picTimer.addEventListener(TimerEvent.TIMER, doPicCheck);			
			
			myData = new MyData(gridSetup.length-1);
			if (!myData.phrases) {myData.phrases = phrasesDefaultText;}
			if (!myData.greets) {myData.greets = greetsDefaultText;}
						
			current = setupBut;					
					
			stage.addEventListener(Event.RESIZE, doResize);
			doResize(null);
			prepareMenus();	
						
			addChild(menus);
						
		}
		
		///////////////////////  AUTO SHOW   ///////////////////////
		
		private function doAutoTip(e:TimerEvent):void {
			removeTip();
		}
		
		private function removeTip():void {
			if (contains(currentAuto)) {
				removeChild(currentAuto); // keeps buttons up three seconds
			}
			autoTipTimer.stop();
		}
		
		private function doAutoPlaying(e:TimerEvent):void {
			removePlaying();
		}
		
		private function showPlaying(w:String="playing..."):void {
			trace (w);
			if (playing && contains(playing)) {
				removeChild(playing);
			}			
			playing = currentAuto.playing;
			playing.myText.text = w;
			playing.mouseEnabled = false;
			playing.mouseChildren = false;
			addChild(playing);		
			playing.x = sW - playing.width/2 - 20;
			playing.y = sH - playing.height/2 - 20;			
			autoPlayingTimer.reset();
			autoPlayingTimer.start();
		}
	
		private function removePlaying():void {
			autoPlayingTimer.stop();
			if (contains(playing)) {
				removeChild(playing);
			}
		}
		
		private function showShield():void {
			if (!autoShield) {
				autoShield = new MovieClip();		
				autoShield.graphics.beginFill(0,0);
				autoShield.graphics.drawRect(0,0,100,100);
				autoShield.addEventListener(MouseEvent.CLICK, doAutoClick);
				autoShield.buttonMode = true;
				
				autoTouch.autoBut.addEventListener(MouseEvent.CLICK, doAutoBut);
				autoTouch.touchBut.addEventListener(MouseEvent.CLICK, doTouchBut);
				autoTouch.autoBut.buttonMode = true;
				autoTouch.touchBut.buttonMode = true;
				autoTouchV.autoBut.addEventListener(MouseEvent.CLICK, doAutoBut);
				autoTouchV.touchBut.addEventListener(MouseEvent.CLICK, doTouchBut);
				autoTouchV.autoBut.buttonMode = true;
				autoTouchV.touchBut.buttonMode = true;
				
			}
			autoShield.width = sW;
			autoShield.height = sH;		
			addChild(autoShield);
		}
		
		private function showAuto():void {
			autoTouch.alpha = .9;
			autoTouchV.alpha = .9;
			fit(currentAuto);
			addChild(currentAuto);	
			showPlaying("paused...");
		}
		
		private function doAutoBut(e:MouseEvent):void {
			removeChild(currentAuto);
			autoTimer.reset();
			autoTimer.delay = this[currentPicType+"AutoTimerSecs"] * 1000; 
			trace (this[currentPicType+"AutoTimerSecs"] * 1000);
			autoTimer.start();
			autoPause = false;
			showPlaying();
		}
		
		private function doTouchBut(e:MouseEvent):void {
			removeChild(currentAuto);
			removeChild(autoShield);
			removePlaying();
			autoPause = false;
			if (currentTip.menuText.text == "pics") {
				picsAutoCheck = false;				
			} else {
				adsAutoCheck = false;
			}
		}
		
		private function doAutoClick(e:MouseEvent):void {
			if (sW > sH) {
				if (mouseX > sW*2/3 && mouseY > sH/2) {					
					removeAuto();
				} else {
					if (!autoPause) {						
						showAuto();
						autoPause = true;
						autoTimer.stop();
						autoTipTimer.reset();
						autoTipTimer.start();						
					} else {
						autoPause = false;
						autoTimer.reset();
						autoTimer.delay = this[currentPicType+"AutoTimerSecs"] * 1000; 
						autoTimer.start();
						autoNext();
						autoTipTimer.stop();
						showPlaying();
						removeTip();
					}
				}
			} else {
				if (mouseX > sW/2 && mouseY > sH*2/3) {
					removeAuto();
				} else {					
					if (!autoPause) {
						showAuto();
						autoPause = true;
						autoTimer.stop();
						autoTipTimer.reset();
						autoTipTimer.start();
					} else {
						autoPause = false;
						autoTimer.reset();
						autoTimer.delay = this[currentPicType+"AutoTimerSecs"] * 1000; 
						autoTimer.start();
						autoNext();
						autoTipTimer.stop();
						showPlaying();
						removeTip();
					}
				}
			}
		}
		
		private function removeAuto():void {
			trace ("remove auto");
			overlayOkay = false;
			if (contains(currentAuto)) {removeChild(currentAuto);}
			removeChild(autoShield);
			backing.gotoAndStop(2);
			if (contains(logo)) {removeChild(logo); removeChild(currentTip);}
			autoTimer.stop();
			autoTipTimer.stop();
			autoPause = false;
			removePlaying();
			mainMenu();
			overlayOkay = true;
		}
		
		private function doAuto(e:TimerEvent):void {
			autoNext();			
		}
		
		private function autoNext():void {
			if (currentTip.menuText.text == "pics") {
				loadPic(currentAutoPic);
				currentAutoPic++;
				if (currentAutoPic > defaultPicsFit.length) {
					currentAutoPic = 1;
				}
			} else {
				loadAd(currentAutoAd);
				currentAutoAd++;
				if (currentAutoAd > defaultAdsFit.length) {
					currentAutoAd = 1;
				}
			}
		}
		
		private function startAuto():void {	
			myIISet.hide();
			autoTimer.reset();
			autoTimer.delay = this[currentPicType+"AutoTimerSecs"] * 1000; 
			autoTimer.start();			
			showShield();
			showPlaying();
		}

		///////////////////////  RESIZE & GRIDS   ///////////////////////
		
		private function doResize(e:Event):void {
			
			// this function is filled with things that need to go in the order listed - be careful
			// some code looks like it can be grouped but it can't - be extra careful
			
			if (stage.fullScreenWidth > 0) {
				sW = stage.fullScreenWidth;
				sH = stage.fullScreenHeight;
			} else {
				sW = stage.stageWidth;
				sH = stage.stageHeight;
			}
			/*
			if (sW > 1024) {
				logo.gotoAndStop(1);
				setupBut.gotoAndStop(1);
				trace ("bigger");
			} else*/
				
			if (sW > 760) {
				logo.gotoAndStop(2);	
				setupBut.gotoAndStop(2);
			} else if (sW >= 600) {
				logo.gotoAndStop(3);
				setupBut.gotoAndStop(3);
			} else if (sW > 400) {
				logo.gotoAndStop(4);	
				setupBut.gotoAndStop(4);
			} else {
				logo.gotoAndStop(5);	
				setupBut.gotoAndStop(5);
			} 			

			
			if (sW < sH) { // portrait
				verticalCheck = true;
			} else {
				verticalCheck = false;
			}
					
			if (verticalCheck === lastVerticalCheck && !firstCheck) {
				return;
			}			
			lastVerticalCheck = verticalCheck;
			
			var currentP:String;
			var currentB:Number = 1;
						
			if (verticalCheck) {	
				if (contains(backing)) {
					currentB = backing.currentFrame;
					removeChild(backing);
				}
				backing = assets.backingV;	
				addChildAt(backing,0);
				backing.stop();
				backing.gotoAndStop(currentB);
				// code looks awkward but need to swap vertical and horizontal clips
				if (current.name == "myAds") {
					currentP = current.getChildAt(0).name;
					removeChild(current);
					current = myAdsV;					
					addChild(current);
					while (current.numChildren > 0) {
						current.removeChildAt(0);
					}
					current.addChild(current[currentP]);
					if (currentP == "ad2") { // swoodle sound
						assets.myAds.ad2.swoodle.stop();
						assets.myAds.ad2.swoodle.gotoAndStop(1);
						if (overlay && contains(overlay)) {
							assets.myAdsV.ad2.swoodle.stop();
							assets.myAdsV.ad2.swoodle.gotoAndStop(1);
						} else {
							assets.myAdsV.ad2.swoodle.play();
						}
					}
				} else if (current.name == "myPics") {	
					currentP = current.getChildAt(0).name;
					removeChild(current);
					current = myPicsV;					
					addChild(current);
					while (current.numChildren > 0) {
						current.removeChildAt(0);
					}
					current.addChild(current[currentP]);			
				}
			} else {
				if (contains(backing)) {
					currentB = backing.currentFrame;
					removeChild(backing);
				}
				backing = assets.backing;	
				addChildAt(backing,0);
				backing.stop();
				backing.gotoAndStop(currentB);
				if (current.name == "myAdsV") {
					currentP = current.getChildAt(0).name;
					removeChild(current);
					current = myAds;					
					addChild(current);
					while (current.numChildren > 0) {
						current.removeChildAt(0);
					}
					current.addChild(current[currentP]);
					if (currentP == "ad2") { // swoodle sound
						assets.myAdsV.ad2.swoodle.stop();
						assets.myAdsV.ad2.swoodle.gotoAndStop(1);
						trace ("stopping swoodle v")
						if (overlay && contains(overlay)) {
							assets.myAds.ad2.swoodle.stop();
							assets.myAds.ad2.swoodle.gotoAndStop(1);
						} else {
							assets.myAds.ad2.swoodle.play();
							trace ("playing swoodle")
						}
					}					
				} else if (current.name == "myPicsV") {					
					currentP = current.getChildAt(0).name;
					removeChild(current);
					current = myPics;					
					addChild(current);
					while (current.numChildren > 0) {
						current.removeChildAt(0);
					}
					current.addChild(current[currentP]);					
				}
			}
								
			fill(backing);
										
			if (!myIISet) {
				myIISet = new IISet(this); // last is doubleClick
				myIISet.addEventListener(IIEvent.PRESS, IIPress);
				myIISet.activeGrid = "main";
			}
			resetGrids();
			
			if (contains(setupBut)) { // setUpBut seems to be in setuoWindow somewhere - so must do this first
				addChild(setupBut); 
			}
						
			var tempText:String;
			if (verticalCheck) { // portrait
				// code looks awkward but must be done this way
				if (contains(menus)) {
					removeChild(menus);
					menus = assets.menusV;
					addChild(menus);
				} else {
					menus = assets.menusV;
				}
				if (contains(textWindow)) {
					stage.focus = null;
					tempText = textWindow.textBox.myText.text;
					removeChild(textWindow);
					textWindow = assets.textWindowV;
					addChild(textWindow);
					textWindow.textBox.myText.text = tempText;		
					textWindow.headText.text = "You can have up to 11 "+currentTextType+",\none per line - keep them short!";
					addTextListeners();
				} else {
					textWindow = assets.textWindowV;
				}
				if (contains(picWindow)) {
					removeChild(picWindow);
					picWindow = assets.picWindowV;
					addChild(picWindow);
					addPicListeners();					
				} else {
					picWindow = assets.picWindowV;
				}
				
			} else { // landscape
				// code looks awkward but must be done this way
				if (contains(menus)) {
					removeChild(menus);
					menus = assets.menus;
					addChild(menus);
				} else {
					menus = assets.menus;
				}
				if (contains(textWindow)) {
					stage.focus = null;
					tempText = textWindow.textBox.myText.text;
					removeChild(textWindow);
					textWindow = assets.textWindow;
					addChild(textWindow);
					textWindow.textBox.myText.text = tempText;
					textWindow.headText.text = "You can have up to 11 "+currentTextType+",\none per line - keep them short!";
					addTextListeners();					
				} else {
					textWindow = assets.textWindow;
				}
				if (contains(picWindow)) {
					removeChild(picWindow);
					picWindow = assets.picWindow;
					addChild(picWindow);
					addPicListeners();					
				} else {
					picWindow = assets.picWindow;
				}				
			}			
			
			if (firstCheck) {				
				logo.alpha = 1;
				setupBut.alpha = .9;
				//addChild(logo);		
				//addChild(setupBut); 
				current = setupBut;				
				firstCheck = false;			
			}
			if (contains(logo)) {addChild(logo);}
								
			center(logo);
			center(setupBut);			
			if (sW > sH) {
				fit (myText, 1.2);
			} else {
				fit (myText, 1);
			}
			fill(myLights,1.3);	
			
			if (picsFitLast) {
				fit(myPics);
				fit(myPicsV);
			} else {
				fill(myPics);
				fill(myPicsV);	
			}
			if (adsFitLast) {
				fit(myAds);
				fit(myAdsV);
			} else {
				fill(myAds);
				fill(myAdsV);
			}
			
			fill(myMoods, 1.1);
			fixMoods();			
			fill(menus,1);	
			fill(textWindow,1);
			fill(picWindow,1);
			fit(picInterface);
			
			// code looks awkward but must be done this way
			if (currentTip && contains(currentTip)) {				
				removeChild(currentTip);
				if (verticalCheck) { 
					currentTip = menuTipV;
				} else {
					currentTip = menuTip;
				}
				addChild(currentTip);				
			} else {
				if (verticalCheck) { 
					currentTip = menuTipV;
				} else {
					currentTip = menuTip;
				}
			}
			fit(currentTip, .9);
					
			if (samplePic && contains(samplePic)) {
				removeChild(samplePic);
				var myPic:MovieClip = preparePic(ps.bitmapData);
				samplePic = new MovieClip();
				samplePic.graphics.beginFill(0x000000,1);
				samplePic.graphics.drawRect(0,0,sW,sH);
				if (currentPicFit) {
					fit(myPic);
				} else {
					fill(myPic);
				}
				samplePic.addChild(myPic);
				addChild(samplePic);
				setChildIndex(picInterface, numChildren-1);
			}	
			
			if (overlay && contains(overlay)) {
				removeChild(overlay);
				addCustomPic(lastType, lastPosition)
			}
						
			if (autoShield && contains(autoShield)) {	
				showShield();
			}			
			
			if (currentAuto && contains(currentAuto)) {					
				removeChild(currentAuto);
				if (verticalCheck) { 
					currentAuto = autoTouchV;
				} else {
					currentAuto = autoTouch;
				}				
				showAuto();
			} else {
				if (verticalCheck) { 
					currentAuto = autoTouchV;
				} else {
					currentAuto = autoTouch;
				}				
			}		
			
		}
		
		private function resetGrids():void {
			myIISet.clear();
			if (verticalCheck) { // portrait - usually 2 columns, 3 rows
				myIISet.addGrid("main",2,3);
				myIISet.addGrid("moods",2,3);
				myIISet.addGrid("lights",2,4);
				myIISet.addGrid("greets",gridSetup[myData.numGreets][1],gridSetup[myData.numGreets][0]);
				myIISet.addGrid("phrases",gridSetup[myData.numPhrases][1],gridSetup[myData.numPhrases][0]);	
				myIISet.addGrid("pics",2,3);
				myIISet.addGrid("ads",2,3);
								
				trace ("portrait");
			} else { // landscape - usually 3 columns 2 rows
				myIISet.addGrid("main",3,2);
				myIISet.addGrid("moods",3,2);				
				myIISet.addGrid("greets",gridSetup[myData.numGreets][0],gridSetup[myData.numGreets][1]);
				myIISet.addGrid("pics",3,2);
				myIISet.addGrid("lights",4,2);				
				myIISet.addGrid("phrases",gridSetup[myData.numPhrases][0],gridSetup[myData.numPhrases][1]);
				myIISet.addGrid("ads",3,2);
				trace ("landscape");
			}				
			myIISet.setActiveGrid(myIISet.activeGrid);
		}
		
		private function fixMoods():void {
			if (current.name == "myMoods") {
				var clip:MovieClip = MovieClip(current.getChildAt(0));
				if (sW < sH) {
					clip.face.scaleX = clip.face.scaleY = .55;
				} else {
					clip.face.scaleX = clip.face.scaleY = 1;					
				}
			}
		}
		
		private function fill(w:MovieClip, p:Number=1):void { // makes w fill stage stretching or shrinking width or height and centering
			
			w.scaleX = w.scaleY = 1;	// in some cases does not seem to reset in time - oh well - minor bug	
				
			var wid:Number = w.width;
			var hei:Number = w.height;				
	
			if (wid / sW > hei / sH) {			
					w.height = sH;
					w.width = w.height * wid / hei;				
			} else {
				w.width = sW;
				w.height = w.width * hei / wid;
			}
						
			w.width *= p;
			w.height *= p;
			
			w.x = sW / 2;
			w.y = sH / 2;
		
		}
				
		private function fit(w:MovieClip, p:Number=1):void { // makes w fit in stage stretching or shrinking width or height and centering
			
			w.scaleX = w.scaleY = 1;
			
			var wid:Number = w.width;
			var hei:Number = w.height;
						
			if (wid / sW < hei / sH) {
				w.height = sH;
				w.width = w.height * wid / hei;
			} else {
				w.width = sW;
				w.height = w.width * hei / wid;
			}
			w.width *= p;
			w.height *= p;
			w.x = sW / 2;
			w.y = sH / 2;		
			
		}
		
		///////////////////////   MENUS   ///////////////////////
		
		private function prepareMenus():void {
						
			setupBut.addEventListener(MouseEvent.CLICK, doSetup); // setup but on home page
			setupBut.buttonMode = true;
			
			var pannels:Array = [assets.menus, assets.menusV];
			var clip:MovieClip;
			for (var i:uint=0; i<pannels.length; i++) {
				clip = pannels[i];				
				
				clip.backing.addEventListener(MouseEvent.CLICK, closeMenu);
				clip.backing.buttonMode = true;		
				
				// mainTabs				
				clip.mainTabs.addEventListener(MouseEvent.CLICK, doMainTabs);
				clip.mainTabs.buttonMode = true;
				clip.mainTabs.homeBut.mouseChildren = false;
				clip.mainTabs.setupBut.mouseChildren = false;
				clip.mainTabs.helpBut.mouseChildren = false;
				clip.mainTabs.introBut.mouseChildren = false;
				clip.mainTabs.tabText.mouseEnabled = false;
				clip.mainTabs.tabText.mouseChildren = false;
				
				// setup screen
				clip.setupWindow.setupButs.greetsBut.mouseChildren = false;
				clip.setupWindow.setupButs.phrasesBut.mouseChildren = false;
				clip.setupWindow.setupButs.adsBut.mouseChildren = false;
				clip.setupWindow.setupButs.picsBut.mouseChildren = false;
				clip.setupWindow.setupButs.addEventListener(MouseEvent.CLICK, doSetupButs);
				clip.setupWindow.setupButs.buttonMode = true;
				clip.setupWindow.setupButs.topicText.mouseEnabled = false;
				clip.setupWindow.setupButs.topicText.mouseChildren = false;
									
				// intro screen
				clip.introWindow.visit.addEventListener(MouseEvent.CLICK, function():void {navigateToURL(new URLRequest("http://hangy.mobi"));});
				clip.introWindow.visit.buttonMode = true;
				clip.introWindow.dzLogo.addEventListener(MouseEvent.CLICK, function():void {navigateToURL(new URLRequest("http://danzen.com"));});
				clip.introWindow.dzLogo.buttonMode = true;
			}		
			
			myFormat = new TextFormat(null, 30, 0xAAAAAA);			
			setupPics("ads");			
			setupPics("pics");			
			picInterface.buts.addEventListener(MouseEvent.CLICK, doPicInterface);
			picInterface.buts.buttonMode = true;	
			
			setMenu("intro");
			
		}
		
		private function doMainTabs(e:MouseEvent):void {
			trace ("doMainTabs");				
			switch (e.target.name) {
				case "homeBut":
					closeMenu(null);
					break;				
				case "setupBut":	
					setMenu("setup");
					break;
				case "helpBut":
					setMenu("help");
					break;
				case "introBut":			
					setMenu("intro");
					break;
			}			
		}	
		
		private function setMenu(m:String):void {
			var menuList:Array = [assets.menus.introWindow,assets.menus.helpWindow,assets.menus.setupWindow,assets.menusV.introWindow,assets.menusV.helpWindow,assets.menusV.setupWindow];
			var win:MovieClip;
			for (var i:uint=0; i<menuList.length; i++) {
				win = menuList[i];
				if (win.parent) {win.parent.removeChild(win);}
			}			
			assets.menus.addChild(assets.menus[m+"Window"]);
			assets.menusV.addChild(assets.menusV[m+"Window"]);
			
			var pannels:Array = [assets.menus, assets.menusV];
			var clip:MovieClip;
			for (i=0; i<pannels.length; i++) {
				clip = pannels[i];	
				clip.mainTabs.homeBut.backing.alpha = .2;
				clip.mainTabs.setupBut.backing.alpha = .2;
				clip.mainTabs.helpBut.backing.alpha = .2;
				clip.mainTabs.introBut.backing.alpha = .2;
			}
			assets.menus.mainTabs[m+"But"].backing.alpha = 1;
			assets.menusV.mainTabs[m+"But"].backing.alpha = 1;
		}
				
		private function doSetup(e:MouseEvent):void {
			trace ("setup");
			setMenu("setup");
			addChild(menus);
			removeChild(logo);
			removeChild(setupBut);
		}
		
		private function doSetupButs(e:MouseEvent):void {
			trace ("setupbuts");				
			switch (e.target.name) {
				case "greetsBut":
					trace("greets");
					currentTextType = "greets";
					addChild(textWindow);
					lastText = textWindow.textBox.myText.text = myData.greets;
					textWindow.headText.text = "You can have up to 11 greets,\none per line - keep them short!";
					addTextListeners();
					break;
				case "phrasesBut":
					trace("phrases");		
					currentTextType = "phrases";
					addChild(textWindow);
					lastText = textWindow.textBox.myText.text = myData.phrases;
					textWindow.headText.text = "You can have up to 11 phrases,\none per line - keep them short!";
					addTextListeners();
					break;
				case "adsBut":
					trace("ads");
					currentPicType = "ads";
					if (assets.picWindow.contains(assets.picWindow.picsBox)) {						
						assets.picWindow.removeChild(assets.picWindow.picsBox);
						assets.picWindow.addChild(assets.picWindow.adsBox);						
						assets.picWindowV.removeChild(assets.picWindowV.picsBox);
						assets.picWindowV.addChild(assets.picWindowV.adsBox);
					}
					picWindow.menuText.text = "PROMOS";
					if (adsAutoCheck) {
						assets.picWindow.autoBut.myText.text = "TOUCH";
						assets.picWindowV.autoBut.myText.text = "TOUCH";
						trace ("ads but to touch");
					} else {
						assets.picWindow.autoBut.myText.text = "AUTO";
						assets.picWindowV.autoBut.myText.text = "AUTO";
						trace ("ads but to auto");
					}
					addChild(picWindow);
					lastPic = DynamicObject.copy(myData.ads);
					lastSecs = adsAutoTimerSecs;
					addPicListeners();
					break;
				case "picsBut":
					trace("pics");					
					currentPicType = "pics";
					if (assets.picWindow.contains(assets.picWindow.adsBox)) {						
						assets.picWindow.removeChild(assets.picWindow.adsBox);
						assets.picWindow.addChild(assets.picWindow.picsBox);						
						assets.picWindowV.removeChild(assets.picWindowV.adsBox);
						assets.picWindowV.addChild(assets.picWindowV.picsBox);
					}
					picWindow.menuText.text = "PICS";
					if (picsAutoCheck) {
						assets.picWindow.autoBut.myText.text = "TOUCH";
						assets.picWindowV.autoBut.myText.text = "TOUCH";
						trace ("pics but to touch");
					} else {
						assets.picWindow.autoBut.myText.text = "AUTO";
						assets.picWindowV.autoBut.myText.text = "AUTO";
						trace ("pics but to auto");
					}
					addChild(picWindow);
					lastPic = DynamicObject.copy(myData.pics);
					lastSecs = picsAutoTimerSecs;
					addPicListeners();
					break;
			}			
		}	
			
	
		///////////////////////// PICS ///////////////////////////
		
		private function setupPics(type:String, r:Boolean=false):void { // do once and to reset // for setup window not invisible grid pictures
			
			if (type != "pics" && type != "ads") {return;}
			
			var hit:MovieClip;
			var pic:MovieClip;
			var bmd:BitmapData;
			
			if (!this[type + "Check"] || r) {
				this[type + "Check"] = true;		
								
				var picList:Array = [[assets[type+"ThumbsV"], assets.picWindowV], [assets[type+"Thumbs"], assets.picWindow]];
				var picsW:MovieClip;
				var thumbLookup:Array = [];
				var myPic:MovieClip;
				var fitfill:String;
				var picData:Array = myData[type];	
				var aw:MovieClip; // alertWindow
				
				for (var k:uint=0; k<picData.length; k++) {
					thumbLookup[picData[k].position] = picData[k].thumb;
				}
				
				for (var j:uint = 0; j<picList.length; j++) {
					
					if (!r && !(picsCheck && adsCheck)) {
						aw = picList[j][1].alertWindow;
						picList[j][1].removeChild(aw);
						aw.backBut.addEventListener(MouseEvent.CLICK, closeAlert);
						aw.backBut.buttonMode = true;
						aw.backing.addEventListener(MouseEvent.CLICK, closeAlert);
						aw.backing.buttonMode = true;
						aw.removeChild(aw.secondsPanel);
						aw.secondsPanel.secondsText.mouseEnabled = false;
						aw.secondsPanel.secondsText.mouseChildren = false;
						aw.secondsPanel.secondsButs.addEventListener(MouseEvent.CLICK, doSeconds);
						aw.secondsPanel.secondsButs.buttonMode = true;						
						aw.secondsPanel.secondsButs.secs2.mouseChildren = false;
						aw.secondsPanel.secondsButs.secs4.mouseChildren = false;
						aw.secondsPanel.secondsButs.secs6.mouseChildren = false;
						picList[j][1].autoBut.mouseChildren = false;						
					}
				
					thumbs = picList[j][0];
					
					picsW = picList[j][1];
					for (var i:uint=0; i<picsW[type+"Box"].hits.numChildren; i++) {
						hit = MovieClip(picsW[type+"Box"].hits["hit"+(i+1)]);
						pic = MovieClip(picsW[type+"Box"].pics["pic"+(i+1)]);
						hit.pic = pic;
						pic.hit = hit;
						pic.x = hit.x;
						pic.y = hit.y;												
						if (pic.picHolder.numChildren == 1) {
							pic.picHolder.removeChildAt(0);
						}
						bmd = myData.getPicBMD(type, i+1);
						if (bmd) {
							// get picture data from database here					
							myPic = getIcon(bmd, pic.picMask, myData.getPicFitfill(type, i+1));
							pic.picHolder.addChild(myPic);
						} else {
							pic.picHolder.addChild(thumbs["pic"+(thumbLookup[i+1])]);
						}
						pic.picHolder.mask = pic.picMask;
						pic.mouseChildren = false;
					}
				}				
			}
		}

		private function doSeconds(e:MouseEvent):void {
			var num:Number = Number(e.target.name.substr(4,1));
			setSecs(num);
			this[currentPicType+"AutoTimerSecs"] = num;		
		}
		
		private function setSecs(n:Number):void {
			var picList:Array = [assets.picWindow.alertWindow, assets.picWindowV.alertWindow];
			var aw:MovieClip;
			for (var i:uint=0; i<picList.length; i++) {
				aw = picList[i];
				aw.secondsPanel.secondsButs.secs2.backing.alpha = .2;
				aw.secondsPanel.secondsButs.secs4.backing.alpha = .2;
				aw.secondsPanel.secondsButs.secs6.backing.alpha = .2;
				var but:MovieClip = aw.secondsPanel.secondsButs["secs"+n];
				but.backing.alpha = 1;	
			}			
		}
		
		private function addPicListeners():void {			
			picWindow.picsBox.pics.addEventListener(MouseEvent.CLICK, doPicTap);
			picWindow.picsBox.pics.addEventListener(MouseEvent.MOUSE_DOWN, doPicDown);
			picWindow.adsBox.pics.addEventListener(MouseEvent.CLICK, doPicTap);
			picWindow.adsBox.pics.addEventListener(MouseEvent.MOUSE_DOWN, doPicDown);
			picWindow.saveBut.addEventListener(MouseEvent.CLICK, doPicSave);
			picWindow.cancelBut.addEventListener(MouseEvent.CLICK, doPicCancel);
			picWindow.resetBut.addEventListener(MouseEvent.CLICK, doPicReset);
			picWindow.autoBut.addEventListener(MouseEvent.CLICK, doPicAuto);
			picWindow.autoBut.buttonMode = picWindow.picsBox.pics.buttonMode = picWindow.adsBox.pics.buttonMode = picWindow.saveBut.buttonMode = picWindow.cancelBut.buttonMode = picWindow.resetBut.buttonMode = true;			
		}
		
		private function removePicListeners():void {			
			picWindow.picsBox.pics.removeEventListener(MouseEvent.CLICK, doPicTap);
			picWindow.picsBox.pics.removeEventListener(MouseEvent.MOUSE_DOWN, doPicDown);
			picWindow.adsBox.pics.removeEventListener(MouseEvent.CLICK, doPicTap);
			picWindow.adsBox.pics.removeEventListener(MouseEvent.MOUSE_DOWN, doPicDown);
			picWindow.saveBut.removeEventListener(MouseEvent.CLICK, doPicSave);
			picWindow.cancelBut.removeEventListener(MouseEvent.CLICK, doPicCancel);
			picWindow.resetBut.removeEventListener(MouseEvent.CLICK, doPicReset);
			picWindow.autoBut.removeEventListener(MouseEvent.CLICK, doPicAuto);
		}		
						
		private function doPicAuto(e:MouseEvent):void {
			var but:MovieClip = MovieClip(e.target);			
			if (but.myText.text == "AUTO") { // button in TOUCH mode going to AUTO
				if (this[currentPicType + "AutoCheck"]) { // memory in AUTO mode
					autoToggle = false;
				} else {
					autoToggle = true;
				}
				but.myText.text = "TOUCH";
				picWindow.addChild(picWindow.alertWindow);
				picWindow.alertWindow.addChild(picWindow.alertWindow.secondsPanel);				
				setSecs(this[currentPicType+"AutoTimerSecs"]);
				if (currentPicType == "pics") {
					picWindow.alertWindow.myText.text = "Pics will play automatically";
				} else {
					picWindow.alertWindow.myText.text = "Promos will play automatically";
				}
			} else { // button in AUTO mode going to TOUCH
				if (this[currentPicType + "AutoCheck"]) { // memory in AUTO mode
					autoToggle = true;
				} else {
					autoToggle = false;				
				}
				but.myText.text = "AUTO";
				picWindow.addChild(picWindow.alertWindow);
				if (currentPicType == "pics") {
					picWindow.alertWindow.myText.text = "Pics will not play automatically";
				} else {
					picWindow.alertWindow.myText.text = "Promos will not play automatically";
				}			
			}
			
		}
				
		private function doPicDown(e:MouseEvent):void {
			currentHit = null;
			currentPic = MovieClip(e.target);
			
			// handle both horizontal and vertical at same time even if hidden
			
			if (picWindow.name == "picWindow") {
				otherPic = assets.picWindowV[currentPicType+"Box"].pics[currentPic.name];
			} else {
				otherPic = assets.picWindow[currentPicType+"Box"].pics[currentPic.name];
			}
			
			picWindow[currentPicType+"Box"].pics.setChildIndex(currentPic, picWindow[currentPicType+"Box"].pics.numChildren-1);
			currentPic.startX = currentPic.hit.x;
			currentPic.startY = currentPic.hit.y;
			otherPic.startX = otherPic.x;
			otherPic.startY = otherPic.y;
			currentPic.startDrag();
			stage.addEventListener(MouseEvent.MOUSE_UP, doPicUp);
			picTimer.start();
			picLocX = currentPic.x;
			picLocY = currentPic.y;
			picTime = new Date().time;
		}
				
		private function doPicCheck(e:TimerEvent):void {
			var pp:MovieClip;
			
			if (currentHit) {
				currentHit.alpha = 1;
			}
			currentHit = null;
			
			for each (var h:MovieClip in picWindow[currentPicType+"Box"].hits) {
				
				if (h.pic == currentPic) {continue;}
				
				if (h.hitTestObject(currentPic)) {					
					currentHit = h.pic;									
					currentHit.alpha = .5;
					currentHit.startX = currentHit.x;
					currentHit.startY = currentHit.y;	
					
					if (picWindow.name == "picWindow") {
						otherHit = assets.picWindowV[currentPicType+"Box"].pics[currentHit.name];
					} else {
						otherHit = assets.picWindow[currentPicType+"Box"].pics[currentHit.name];
					}	
					otherHit.startX = otherHit.x;
					otherHit.startY = otherHit.y;
					
					break;
				}
				
			}
		}
		
		private function doPicUp(e:MouseEvent):void {
			trace ("up " + currentPic.name);
			picTimer.stop();
			currentPic.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, doPicUp);
						
			if (currentHit) {
				
				var firstPic:MovieClip = currentPic;
				var firstHit:MovieClip = currentPic.hit;	
				var secondPic:MovieClip = currentHit;
				var secondHit:MovieClip = currentHit.hit;
							
				var otherFirstPic:MovieClip = otherPic;
				var otherFirstHit:MovieClip = otherPic.hit;	
				var otherSecondPic:MovieClip = otherHit;
				var otherSecondHit:MovieClip = otherHit.hit;
				
				myData.swapPics(currentPicType, Number(firstHit.name.substr(3,1)), Number(secondHit.name.substr(3,1)));
				
				TweenLite.to(firstPic, .4, {x:secondPic.startX, y:secondPic.startY, easing:Sine.easeInOut});
				TweenLite.to(secondPic, .4, {x:firstPic.startX, y:firstPic.startY, easing:Sine.easeInOut});
				
				otherFirstPic.x = otherSecondPic.startX;
				otherFirstPic.y = otherSecondPic.startY;
				otherSecondPic.x = otherFirstPic.startX;
				otherSecondPic.y = otherFirstPic.startY;
								
				firstPic.hit = secondHit;
				firstHit.pic = secondPic;
				secondPic.hit = firstHit;
				secondHit.pic = firstPic;
				
				otherFirstPic.hit = otherSecondHit;
				otherFirstHit.pic = otherSecondPic;
				otherSecondPic.hit = otherFirstHit;
				otherSecondHit.pic = otherFirstPic;
				
				picWindow[currentPicType+"Box"].pics.setChildIndex(secondPic, picWindow[currentPicType+"Box"].pics.numChildren-1);
				picWindow[currentPicType+"Box"].pics.setChildIndex(firstPic, picWindow[currentPicType+"Box"].pics.numChildren-1);				
				
				currentHit.alpha = 1;				
				currentHit = null;
				
			} else {
				TweenLite.to(currentPic, .4, {x:currentPic.startX, y:currentPic.startY, easing:Sine.easeInOut});
			}
		}
		
		private function doPicTap(e:MouseEvent):void {			
			if (Math.abs(picLocX - e.target.x) < 20 && Math.abs(picLocY - e.target.y) < 20 && Math.abs(picTime-new Date().time) < 500) {			
				ps = new PhotoStore();
				ps.getPhoto();
				ps.addEventListener(Event.CANCEL, picUploadCancel);
				ps.addEventListener(Event.COMPLETE, picUploadComplete);
			}
		}
		
		private function picUploadCancel(e:Event):void {
			ps = null;
		}
		
		private function picUploadComplete(e:Event):void {
			if (ps.error == "success") {
				// grab new pic data and adjust the thumb too
				//ps.bitmapData = getSampleBitmapData();				
				var myPic:MovieClip = preparePic(ps.bitmapData);
				samplePic = new MovieClip();
				samplePic.graphics.beginFill(0x000000,1);
				samplePic.graphics.drawRect(0,0,sW,sH);
				fit(myPic);
				samplePic.addChild(myPic);
				addChild(samplePic);				
				addChild(picInterface);
			} else {				
				//trace (ps.error);
				picWindow.addChild(picWindow.alertWindow);
				picWindow.alertWindow.myText.text = ps.error;
			}
			
		}
		
		private function doPicInterface(e:MouseEvent):void {
			trace ("picInterface");	
			var bmd:BitmapData;
			var myMatrix:Matrix;
			var myPic:MovieClip;
			
			switch (e.target.name) {
				case "fit":
					trace("fit");
					currentPicFit = true;
					fit(MovieClip(samplePic.getChildAt(0)));
					break;
				case "fill":
					trace("fill");
					currentPicFit = false;
					fill(MovieClip(samplePic.getChildAt(0)));
					break;
				case "left":
					trace("left");
					bmd = new BitmapData(ps.bitmapData.height, ps.bitmapData.width);
					myMatrix = new Matrix();
					myMatrix.translate(-ps.bitmapData.width/2, -ps.bitmapData.height/2);
					myMatrix.rotate(-90*Math.PI/180);		
					myMatrix.translate(ps.bitmapData.height/2, ps.bitmapData.width/2);
					bmd.draw(ps.bitmapData, myMatrix);
					ps.bitmapData = bmd;
					samplePic.removeChildAt(0);
					myPic = preparePic(bmd);
					samplePic.addChild(myPic);					
					if (currentPicFit) {
						fit(MovieClip(samplePic.getChildAt(0)));
					} else {
						fill(MovieClip(samplePic.getChildAt(0)));
					}
					break;
				case "right":
					trace("right");
					bmd = new BitmapData(ps.bitmapData.height, ps.bitmapData.width);
					myMatrix = new Matrix();
					myMatrix.translate(-ps.bitmapData.width/2, -ps.bitmapData.height/2);
					myMatrix.rotate(90*Math.PI/180);		
					myMatrix.translate(ps.bitmapData.height/2, ps.bitmapData.width/2);
					bmd.draw(ps.bitmapData, myMatrix);
					ps.bitmapData = bmd;
					samplePic.removeChildAt(0);
					myPic = preparePic(bmd);
					samplePic.addChild(myPic);		
					if (currentPicFit) {
						fit(MovieClip(samplePic.getChildAt(0)));
					} else {
						fill(MovieClip(samplePic.getChildAt(0)));
					}
					break;
				case "cancel":
					trace("cancel");
					removeChild(samplePic);
					samplePic = null;
					ps = null;
					removeChild(picInterface);
					break;
				case "ok":
					trace("ok");
					// write ps.bitmapData to database at correct selected position
					
					myData.setPicBMD(currentPicType, ps.bitmapData, Number(currentPic.hit.name.substr(3,1)), String(currentPicFit));					
					
					// write a bitMap to both icons at selected position and fit or fill
					
					currentPic.picHolder.removeChildAt(0);					
					myPic = getIcon(ps.bitmapData, currentPic.picMask, String(currentPicFit));
					currentPic.picHolder.addChild(myPic);
					myPic.mask = currentPic.picMask;
					
					otherPic.picHolder.removeChildAt(0);
					myPic = getIcon(ps.bitmapData, otherPic.picMask, String(currentPicFit));
					otherPic.picHolder.addChild(myPic);
					myPic.mask = otherPic.picMask;
					
					removeChild(samplePic);
					samplePic = null;
					ps = null;
					removeChild(picInterface);
					break;
			}			
		}	
				
		private function getIcon(bmd:BitmapData, maskClip:MovieClip, fitfill:String):MovieClip { // reads bmd of pic and returns icon image for fill or fit
						
			var myMatrix:Matrix;
			var myPic:MovieClip;
			var bm:Bitmap;
			var newBmd:BitmapData;
			var scale:Number;
			
			var targetW:Number;
			var targetH:Number;			
			
			targetW = maskClip.width;
			targetH = maskClip.height;				
			
			newBmd = new BitmapData(targetW, targetH, false, 0x00000000);
			
			var wid:Number = bmd.width;
			var hei:Number = bmd.height;
			
			if (fitfill == "true") { // fit								
				if (wid / targetW < hei / targetH) {
					scale = hei / targetH;
				} else {
					scale = wid / targetW;
				}				
			} else { // fill
				if (wid / targetW > hei / targetH) {
					scale = hei / targetH;
				} else {
					scale = wid / targetW;
				}
			}			
			scale = 1/scale;
			var newW:Number = wid * scale;
			var newH:Number = hei * scale;
			
			myMatrix = new Matrix();			
			myMatrix.scale(scale,scale);
			myMatrix.translate((targetW-newW)/2,(targetH-newH)/2);
			newBmd.draw(bmd, myMatrix);					
			bm = new Bitmap(newBmd);
			bm.x = -bm.width/2;
			bm.y = -bm.height/2;
			myPic = new MovieClip();
			myPic.addChild(bm);					
			return myPic;
			
			
		}
							
		private function preparePic(bmd:BitmapData):MovieClip {
			var bm:Bitmap = new Bitmap(bmd);
			var myPic:MovieClip = new MovieClip();
			myPic.addChild(bm);
			bm.x = -bm.width/2;
			bm.y = -bm.height/2;			
			return myPic;
		}
		
		private function getSampleBitmapData():BitmapData {
			var bmd:BitmapData = new BitmapData(samplePicTest.width, samplePicTest.height,false,0x00000000);
			bmd.draw(assets.samplePic);
			return bmd;
		}
				
		private function doPicCancel(e:MouseEvent):void {
			removePicListeners();
			myData[currentPicType] = DynamicObject.copy(lastPic);
			this[currentPicType+"AutoTimerSecs"] = lastSecs;
			setupPics(currentPicType, true);
			removeChild(picWindow);	
			autoToggle = false;
		}		
		
		private function closeAlert(e:MouseEvent):void {			
			var aw:MovieClip = MovieClip(MovieClip(e.currentTarget).parent);
			var pan:MovieClip = MovieClip(MovieClip(MovieClip(e.currentTarget).parent).parent); // goodness me...
			pan.removeChild(aw);
			if (aw.contains(aw.secondsPanel)) {aw.removeChild(aw.secondsPanel);}
		}
		
		private function doPicSave(e:MouseEvent):void {
			// only place to write to database
			myData.flushPics(currentPicType);
			removePicListeners();			
			removeChild(picWindow);	
			if (autoToggle) {
				autoToggle = false;
				this[currentPicType + "AutoCheck"] = (picWindow.autoBut.myText.text == "TOUCH");
			}
		}
		
		private function doPicReset(e:MouseEvent):void {
			myData.resetPics(currentPicType);
			setupPics(currentPicType, true);
		}		
			
		
		/////////////// TEXT ///////////////////////
		
		
		private function addTextListeners():void {			
			textWindow.saveBut.addEventListener(MouseEvent.CLICK, doTextSave);
			textWindow.cancelBut.addEventListener(MouseEvent.CLICK, doTextCancel);
			textWindow.resetBut.addEventListener(MouseEvent.CLICK, doTextReset);	
			textWindow.saveBut.buttonMode = textWindow.cancelBut.buttonMode = textWindow.resetBut.buttonMode = true;
		}
				
		private function removeTextListeners():void {			
			textWindow.saveBut.removeEventListener(MouseEvent.CLICK, doTextSave);
			textWindow.cancelBut.removeEventListener(MouseEvent.CLICK, doTextCancel);
			textWindow.resetBut.removeEventListener(MouseEvent.CLICK, doTextReset);				
		}
				
		private function closeMenu(e:MouseEvent):void {
			removeChild(menus);	
			addChild(logo);
			addChild(setupBut);
		}
		private function doTextCancel(e:MouseEvent):void {
			removeTextListeners();
			textWindow.textBox.myText.text = lastText;
			removeChild(textWindow);	
		}		
		
		private function doTextSave(e:MouseEvent):void {
			removeTextListeners();
			var t:String = textWindow.textBox.myText.text;
			t = t.replace (/\s*\R/g, "\n");
			t = t.replace (/^\s*|[\t ]+$/gm, "");
			t = t.replace (/\R$/, "");			
			textWindow.textBox.myText.text = t;
			myData[currentTextType] = t;
			removeChild(textWindow);	
			resetGrids();
			setChildIndex(getChildAt(numChildren-1), 1);
		}
	
		private function doTextReset(e:MouseEvent):void {
			textWindow.textBox.myText.text = this[currentTextType + "DefaultText"];
		}
				
		
		////////////////////////////  GENERAL  //////////////////////////////
		
		private function mainMenu():void {
			trace ("main");
			autoTimer.stop();
			if (overlay && contains(overlay)) {
				removeChild(overlay);
			}
			myIISet.show();
			myIISet.setActiveGrid("main");
			backing.gotoAndStop(1);
			if (contains(current)) {removeChild(current);}
			current = setupBut;			
			addChild(logo);
			addChild(setupBut);
			addChild(current);
			stopSwoodle();				
		}
		
		private function stopSwoodle():void {
			assets.myAds.ad2.swoodle.stop();
			assets.myAdsV.ad2.swoodle.gotoAndStop(1);
			assets.myAds.ad2.swoodle.stop();
			assets.myAdsV.ad2.swoodle.gotoAndStop(1);			
		}
		
		private function center(w:DisplayObject):void {
			w.x = sW/2;
			w.y = sH/2;
		}
			
		private function IIPress(e:IIEvent):void {
			
			trace (e.grid, e.num);	
			
			if (overlay && contains(overlay)) {
				removeChild(overlay);
			}
			
			if (e.grid != "main") {
				myIISet.hide();
			}
			
			var t:String;
			
			switch (e.grid) {
				case "main":		
					
					if (sW < sH) { 
						switch (e.num) {
							case 1:
								myIISet.setActiveGrid("moods");
								menuTip.menuText.text = menuTipV.menuText.text = "moods";
								break;
							case 2:
								myIISet.setActiveGrid("lights");
								menuTip.menuText.text = menuTipV.menuText.text = "lights";
								break;
							case 3:
								myIISet.setActiveGrid("greets");
								menuTip.menuText.text = menuTipV.menuText.text = "greets";
								break;
							case 4:
								myIISet.setActiveGrid("phrases");
								menuTip.menuText.text = menuTipV.menuText.text = "phrases";
								break;									
							case 5:
								myIISet.setActiveGrid("pics");
								menuTip.menuText.text = menuTipV.menuText.text = "pics";
								break;
							case 6:
								myIISet.setActiveGrid("ads");
								menuTip.menuText.text = menuTipV.menuText.text = "promos";
								break;										
						}						
						currentTip = menuTipV;
						currentAuto = autoTouchV;
						
					} else {
					
						switch (e.num) {
							case 1:
								myIISet.setActiveGrid("moods");
								menuTip.menuText.text = menuTipV.menuText.text = "moods";
								break;
							case 2:
								myIISet.setActiveGrid("greets");
								menuTip.menuText.text = menuTipV.menuText.text = "greets";
								break;
							case 3:
								myIISet.setActiveGrid("pics");
								menuTip.menuText.text = menuTipV.menuText.text = "pics";								
								break;
							case 4:
								myIISet.setActiveGrid("lights");
								menuTip.menuText.text = menuTipV.menuText.text = "lights";
								break;
							case 5:
								myIISet.setActiveGrid("phrases");
								menuTip.menuText.text = menuTipV.menuText.text = "phrases";
								break;	
							case 6:
								myIISet.setActiveGrid("ads");
								menuTip.menuText.text = menuTipV.menuText.text = "promos";
								break;
						}									
						currentTip = menuTip;
						currentAuto = autoTouch;
					}					
					
					if (contains(logo)) {addChild(logo);}
					
					addChild(currentTip);
					fit(currentTip, .9);
					removeChild(setupBut);
					
					if (picsAutoCheck && currentTip.menuText.text == "pics") {
						autoNext();
						startAuto();
					} else if (adsAutoCheck && currentTip.menuText.text == "promos") {
						autoNext();
						startAuto();
					}
					
					break;
				
				case "moods":
					
					backing.gotoAndStop(2);
					if (contains(logo)) {removeChild(logo); removeChild(currentTip);}
					if (e.num == 6) {mainMenu(); return;}					
					current = myMoods;
					addChildAt(current,1);

					while (current.numChildren > 0) {
						current.removeChildAt(0);
					}
					current.addChild(current["face"+e.num]);
					current["face"+e.num].gotoAndPlay(1);					
					
					break;
				
				case "ads":
					
					loadAd(e.num);
					
					
					break;				
				
				case "greets":
					
					if (contains(logo)) {removeChild(logo); removeChild(currentTip);}
					current = myText;
					addChildAt(current,1);
					backing.gotoAndStop(2);
					
					if (e.num == gridSetup[myData.numGreets][0]*gridSetup[myData.numGreets][1]) {mainMenu(); return;}	
					
					var tArray:Array = myData.greets.split(/\n|\r/g);					
					t = tArray[Math.min(e.num-1, myData.numGreets-1)];
					if (myFont.hasGlyphs(t)) {
						if (myText.contains(myText.textHolder2)) {
							myText.removeChild(myText.textHolder2);
						}
						myText.addChild(myText.textHolder);
						myText.textHolder.myText.text = t;			
						myText.textHolder.maskBack.mask = myText.textHolder.myText;
					} else {
						if (myText.contains(myText.textHolder)) {
							myText.removeChild(myText.textHolder);
						}
						myText.addChild(myText.textHolder2);
						t = t.toUpperCase();
						myText.textHolder2.myText.text = t;		
						myText.textHolder2.maskBack.mask = myText.textHolder2.myText;
					}
					break;
				
				case "lights":
					
					if (sW < sH) { 
						if (e.num == 2) {
							e.num = 4;
						} else if (e.num == 4) {
							e.num = 2;
						}
					}					
					backing.gotoAndStop(2);
					if (contains(logo)) {removeChild(logo); removeChild(currentTip);}
					if (e.num == 8) {mainMenu(); return;}					
					current = myLights;
					addChildAt(current,1);
					while (current.numChildren > 0) {
						current.removeChildAt(0);
					}
					current.addChild(current["light"+e.num]);
					current["light"+e.num].gotoAndPlay(1);
					break;
				
				case "pics":
					
					loadPic(e.num);
					break;
				
				case "phrases":
					
					backing.gotoAndStop(2);
					if (contains(logo)) {removeChild(logo); removeChild(currentTip);}
					current = myText;
					addChildAt(current,1);
					
					if (e.num == gridSetup[myData.numPhrases][0]*gridSetup[myData.numPhrases][1]) {mainMenu(); return;}	
										
					var teArray:Array = myData.phrases.split(/\n|\r/g);					
				
					t = teArray[Math.min(e.num-1, myData.numPhrases-1)];
					if (myFont.hasGlyphs(t)) {
						if (myText.contains(myText.textHolder2)) {
							myText.removeChild(myText.textHolder2);
						}
						myText.addChild(myText.textHolder);
						myText.textHolder.myText.text = t;			
						myText.textHolder.maskBack.mask = myText.textHolder.myText;
					} else {
						if (myText.contains(myText.textHolder)) {
							myText.removeChild(myText.textHolder);
						}
						myText.addChild(myText.textHolder2);
						t = t.toUpperCase();
						myText.textHolder2.myText.text = t;		
						myText.textHolder2.maskBack.mask = myText.textHolder2.myText;
					}
					break;		
				
				
			}
			
			myText.textHolder.myText.y = - myText.textHolder.myText.textHeight / 2
			myText.textHolder.maskBack.y = 0;
			myText.textHolder2.myText.y = - myText.textHolder2.myText.textHeight / 2
			myText.textHolder2.maskBack.y = 0;
		
			if (sW > sH) {
				fit (myText, 1.2);
			} else {
				fit (myText, 1);
			}
			fill(myLights,1.3);
			fill(myMoods, 1.1);	
			fixMoods();
		
		}
		
		private function loadPic(n:Number):void {
			
			var bmd:BitmapData;
			var myPic:MovieClip;
			var picPos:Number;
			
			currentPicType = "pics";
			backing.gotoAndStop(2);
			if (contains(logo)) {removeChild(logo); removeChild(currentTip);}
			if (n == 6) {mainMenu(); return;}					
			if (sW < sH) { // portrait
				current = myPicsV;			
			} else {
				current = myPics;
			}		
			addChildAt(current,1);
			
			// check data for bytes at n position and use that for pic at fit or fill				
			
			if (!addCustomPic("pics", n)) {
				picPos = myData.getPicPosition("pics", n);						
				
				while (current.numChildren > 0) {
					current.removeChildAt(0);
				}
				current.addChild(current["pic"+picPos]);
				current["pic"+picPos].gotoAndPlay(1);
				
				if (defaultPicsFit[picPos-1] == true) {
					fit(current);
					picsFitLast = true;
				} else {
					fill(current);
					picsFitLast = false;
				}
				
			}
			
		}
		
		private function loadAd(n:Number):void {
			
			var bmd:BitmapData;
			var myPic:MovieClip;
			var picPos:Number;
			
			currentPicType = "ads";			
			backing.gotoAndStop(2);
			if (contains(logo)) {removeChild(logo); removeChild(currentTip);}
			if (n == 6) {mainMenu(); return;}					
			if (sW < sH) { // portrait
				current = myAdsV;			
			} else {
				current = myAds;
			}		
			addChildAt(current,1);
			stopSwoodle();
			
			// check data for bytes at n position and use that for pic at fit or fill 					
			if (!addCustomPic("ads", n)) {
				
				picPos = myData.getPicPosition("ads", n);
				//current.gotoAndStop(picPos);
				
				if (picPos == 2) { // swoodle
					current.ad2.swoodle.play();
				}
				
				while (current.numChildren > 0) {
					current.removeChildAt(0);
				}
				current.addChild(current["ad"+picPos]);
				current["ad"+picPos].gotoAndPlay(1);	
				
				if (defaultAdsFit[picPos-1] == true) {
					fit(current);
					adsFitLast = true;
				} else {
					fill(current);
					adsFitLast = false;
				}
				
			}
		}
		
		private function addCustomPic(type:String, num:Number):Boolean {
			var bmd:BitmapData = myData.getPicBMD(type, num);
			if (overlay && contains(overlay)) {removeChild(overlay); overlay = null;}
			if (bmd) {
				lastType = type;
				lastPosition = num;
				var myPic:MovieClip = preparePic(bmd);
				overlay = new MovieClip();
				overlay.graphics.beginFill(0x000000,1);
				overlay.graphics.drawRect(0,0,sW,sH);
				if (myData.getPicFitfill(type, num) == "true") {
					fit(myPic);
					this[type + "FitLast"] = true;
				} else {
					fill(myPic);
					this[type + "FitLast"] = false;
				}				
				overlay.addChild(myPic);
				if (overlayOkay) {
					addChild(overlay);	
					overlay.mouseChildren = false;
					overlay.mouseEnabled = false;
				}
				return true;
			} else {				
				return false;
			}
		}
		
	}
	
}