/******************************************
 * @author Hope M Dady
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 2/1/11
 * 
 * Full Glossary class
 * CS5 version of com.digitec.cee.fullGlossary
 *  
*******************************************/

package com.digitec.cee{
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.EventDispatcher;
	
	public class fullGlossary extends MovieClip {
		
		private var glossary:Glossary;
		private var fGlos:XMLList;
		public var fGlosStatus:Boolean=false;
		private var abc:Array=new Array();
		private var glosGroups:XMLList;
		private var pageInfo:Object=new Object();
		private var Select0:mcSelectItem=new mcSelectItem();
		private var Select1:mcSelectItem=new mcSelectItem();
		private var Select2:mcSelectItem=new mcSelectItem();
		private var Select3:mcSelectItem=new mcSelectItem();
		private var Select4:mcSelectItem=new mcSelectItem();
		private var Select5:mcSelectItem=new mcSelectItem();
		private var Select6:mcSelectItem=new mcSelectItem();
		private var Select7:mcSelectItem=new mcSelectItem();
		private var Select8:mcSelectItem=new mcSelectItem();
		private var Select9:mcSelectItem=new mcSelectItem();
		private var Select10:mcSelectItem=new mcSelectItem();
		private var Select11:mcSelectItem=new mcSelectItem();
		private var Select12:mcSelectItem=new mcSelectItem();
		private var Select13:mcSelectItem=new mcSelectItem();
		private var Select14:mcSelectItem=new mcSelectItem();
		private var Select15:mcSelectItem=new mcSelectItem();
		private var Select16:mcSelectItem=new mcSelectItem();
		private var Select17:mcSelectItem=new mcSelectItem();
		private var Select18:mcSelectItem=new mcSelectItem();
		private var Select19:mcSelectItem=new mcSelectItem();
		private var Select20:mcSelectItem=new mcSelectItem();
		private var Select21:mcSelectItem=new mcSelectItem();
		private var Select22:mcSelectItem=new mcSelectItem();
		private var Select23:mcSelectItem=new mcSelectItem();
		
		public function fullGlossary() {
			glossary = new Glossary();
			abc=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
			setButtons();
			
			pageInfo.currPage=0
			pageInfo.totalPage=0
		}
		private function setButtons():void{
			for(var ct:Number=0;ct<26;ct++){
				this["lBtn"+ct].txtButton.htmlText="<b>"+abc[ct]+"</b>"
				this["lBtn"+ct].myvar=abc[ct]
				this["lBtn"+ct].addEventListener(MouseEvent.MOUSE_UP, letterSelect);
				this["lBtn"+ct].mouseChildren=false;
				this["lBtn"+ct].buttonMode=true;
			}
			
			btnNext.txtButton.htmlText="next page &gt;&gt;"
			btnPrev.txtButton.htmlText="&lt;&lt; previous page"
			
			btnNext.addEventListener(MouseEvent.MOUSE_UP, nextPage);
			btnNext.mouseChildren=false;
			btnNext.buttonMode=true;
			btnNext.visible=false;
			btnPrev.addEventListener(MouseEvent.MOUSE_UP, prevPage);
			btnPrev.mouseChildren=false;
			btnPrev.buttonMode=true;
			btnPrev.visible=false;
			
			//on screen text buttons
			
			//create textfields
			var yloc:Number=70
			var xloc:Number=12
			var yshift:Number=25
			for(ct=0;ct<24;ct++){
				this["Select"+ct].x=xloc
				this["Select"+ct].y=yloc
				this["Select"+ct].name="Select"+ct
				this["Select"+ct].txtButton.htmlText=""
				this["Select"+ct].mouseChildren=false;
				yloc+=yshift;
				if(ct==11){
					yloc=70;
					xloc=250
				}
				addChild(this["Select"+ct]);
			}
			
		}
		private function clickLink(e:MouseEvent):void{
			dispatchEvent(new HCustomEvent(HCustomEvent.ADD_GLOSSARY, e.target.myvar) )
		}
		private function nextPage(e:MouseEvent):void{
			pageInfo.currPage++
			btnPrev.visible=true;
			
			if(pageInfo.currPage==pageInfo.totalPage){
				btnNext.visible=false;
			}
			changeFoot()
			setPage()
		}
		private function prevPage(e:MouseEvent):void{
			pageInfo.currPage--
			btnNext.visible=true;
			
			if(pageInfo.currPage==1){
				btnPrev.visible=false;
			}
			changeFoot()
			setPage();
		}
		private function changeFoot(){
			txtFooter.htmlText=String(glosGroups.length())+ " terms  |  page " + pageInfo.currPage + " of " + pageInfo.totalPage
		}
		private function letterSelect(e:MouseEvent):void{
			glosGroups=fGlos.(@name.substr(0,1) == e.target.myvar);
			
			pageInfo.currPage=1
			btnPrev.visible=false;
			
			setFooter();
			
			if(pageInfo.currPage==pageInfo.totalPage){
				btnNext.visible=false;
			}
			
			
			setPage();
		}
		private function setPage():void{
			
			var howMany:Number;
			var testnum:Number=pageInfo.currPage*24
			var compnum:Number=glosGroups.length()
			
			if(testnum<=compnum){
				//full page
				howMany=24
			}else{
				howMany=24-(testnum-compnum)
			}
			
			for(var ct=0;ct<24;ct++){
				if(howMany>ct){
					this["Select"+ct].txtButton.htmlText="<u>"+glosGroups[((pageInfo.currPage-1)*24)+ct].@name+"</u>"
					this["Select"+ct].myvar={titleText:glosGroups[((pageInfo.currPage-1)*24)+ct].@name, contentText:glosGroups[((pageInfo.currPage-1)*24)+ct].def}
					this["Select"+ct].addEventListener(MouseEvent.MOUSE_UP, clickLink);
					this["Select"+ct].buttonMode=true;
				}else{
					this["Select"+ct].myvar={titleText:"", contentText:""}
					this["Select"+ct].txtButton.htmlText=""
					if(this["Select"+ct].hasEventListener(MouseEvent.MOUSE_UP))this["Select"+ct].removeEventListener(MouseEvent.MOUSE_UP, clickLink);
					this["Select"+ct].buttonMode=false;
				}
			}
						
		}
		private function clearPage():void{
			for(var ct=0;ct<24;ct++){
				this["Select"+ct].txtButton.htmlText=""
				if(this["Select"+ct].hasEventListener(MouseEvent.MOUSE_UP))this["Select"+ct].removeEventListener(MouseEvent.MOUSE_UP, clickLink);
				this["Select"+ct].buttonMode=false;
			}
		}
		private function setFooter(){
			if(glosGroups.length()<25){
				pageInfo.totalPage=1
			}else{
				pageInfo.totalPage=Math.floor(glosGroups.length()/24)
				var p=(glosGroups.length())%24
				if(p>0){
					pageInfo.totalPage++;
				}
				btnNext.visible=true;
			}
			changeFoot()
		}
		//FUNCTIONS CALLED FROM CEE_LESSONS
		public function init(b:Boolean):void{
			btnNext.visible=false;
			btnPrev.visible=false;
			txtFooter.htmlText=""
			
			fGlosStatus=b;
			
			if(!fGlos){
				fGlos=glossary.getFullGlossary();
			}else{
				clearPage();
			}
			
		}
	}
	
}
