﻿package TableWidget{
	

	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import fl.containers.ScrollPane;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	import TableWidget.comp.textformatter.TextPropertiesComponent;
	
	/**
	* This class is used to create Row level assets and keeps the reference of all rows.
	* @author Devender Gupta
	*/
	public class RowClass extends EventDispatcher{
		
		private static var instance:RowClass;
		
		private var objWidgetController:WidgetController;
		private var objTextPropertiesComponent:TextPropertiesComponent;
		private var mcRef:MovieClip;
		private var objConfigXML:XML;
		private var xmlCaptivateParams:XML;
		private var wm:String = "Stage";
		
		private var mcCells:MovieClip;
		private var mcGrid:MovieClip;
		private var rowsMc:MovieClip;
		
		public var arrRowsAssets:Array;
		private var arrRowReferences:Array;
		private var arrColumnReferences:Array;
		
		private var selectedRow:Number = -1;
		private var completeRowSelected:Number = -1;
		private var oldDepthForMenu:int;
		private var oldMenuDisplayingRow:MovieClip;
		private var oldDepthForSelection:int;
		
		private var objColumnClass:ColumnClass;
		public var addRowBar:AddRowColumnBar;
		
		public function RowClass(){
			
		}
				
		public static function getInstance():RowClass{
			if(instance == null){
				instance =  new RowClass();
			}
			return instance;
		}
		
		public function killSingleton(){
			instance = null;
		}
		
		/**
		* This function set the references of the required objects like, controller instance, configuration XML, data XML, 
		* and widget mode.
		* Also, it takes refrence of the main movieclip in which the whole table will be created.
		*/
		public function setReferences(mc:MovieClip):void{
			objWidgetController = WidgetController.getInstance();
			objConfigXML = objWidgetController.objConfigXML;
			xmlCaptivateParams = objWidgetController.xmlCaptivateParams;
			wm = objWidgetController.wm;
			objTextPropertiesComponent = objWidgetController.objTextPropertiesComponent;
			mcRef = mc;
			mcCells = MovieClip(mc.getChildByName("mcCells"))
			mcGrid = MovieClip(mc.getChildByName("mcGrid"))
		}
		
		/**
		* This function is used to initiate the rows creation logic of the class. It generate default rows also.
		*/
		public function init():void{
			arrRowsAssets = new Array();
			arrRowReferences = new Array();
			arrColumnReferences = new Array();
			
			if(addRowBar != null){
				mcGrid.removeChild(addRowBar);
			 	addRowBar = null;
			}
			addRowBar = new AddRowColumnBar();
			addRowBar.name = "addRowBar"
			mcGrid.addChild(addRowBar);
			
			if(rowsMc != null){
				mcRef.removeChild(rowsMc);
			 	rowsMc = null;
			}
			rowsMc = new MovieClip();
			rowsMc.name = "rows";
			mcGrid.addChild(rowsMc);
			
			addRowBar.alpha = 0;
			addRowBar.addEventListener(MouseEvent.MOUSE_OVER, displayAddRowBar);
			addRowBar.addEventListener(MouseEvent.MOUSE_OUT, hideAddRowBar);
			addRowBar.bg.addEventListener(MouseEvent.CLICK, addRowAtLast);
			objColumnClass = ColumnClass.getInstance();
			createDefaultRows();
		}
		
		
		private function createDefaultRows():void{
			
			if(xmlCaptivateParams != null){
				//dumb block or now in the left top corner.
				var bg = new MovieClip();
				bg.name = "bg";
				bg.graphics.beginFill(objConfigXML.rowColumnGrid.@bgColor);
				bg.graphics.lineStyle(objConfigXML.rowColumnGrid.@borderThickness, objConfigXML.rowColumnGrid.@borderColor);
				bg.graphics.drawRect(0, 0, objConfigXML.rowColumnGrid.@rowWidth, objConfigXML.row.@height);
				bg.graphics.endFill()
				bg.x = objConfigXML.rowColumnGrid.@xPos;
				bg.y = objConfigXML.rowColumnGrid.@yPos;
				mcGrid.addChild(bg);
				//mcGrid.setChildIndex(bg, mcGrid.getChildIndex(rowsMc));
				
				var rows = xmlCaptivateParams.table.column[0].row.length();
				for(var i=0; i<rows; i++){
					createRowAt(i, true);	
					arrRowReferences[i].yPos = ((i+1)*objConfigXML.row.@height) + int(objConfigXML.rowColumnGrid.@yPos)
				}
				
			}
		}
		
		/**
		* This function is used to create row at any index value. This will be used while creating default rows and
		* while inserting new rows on user's demand.
		*/
		public function createRowAt(index:int, isDefault:Boolean=false):void{
			var selectNewRow = false;
			
			/*if(objWidgetController.creationComplete)
			{
				objTextPropertiesComponent.initializeFormatter();
			}*/
			if(completeRowSelected != -1){
				selectNewRow = true
				deselectRowSelection()
			}
			if(!isDefault){
				objColumnClass.deselectColumnSelection();
				objColumnClass.deselectAllColumns()
				deselectAllRows();
			}
			
			createAssets(index);
			arrColumnReferences = objColumnClass.getColumnReferences()
			if(!isDefault){
				if(arrColumnReferences!=null){
					for(var i=0; i<arrColumnReferences.length; i++){
						if(index == arrRowReferences.length){
							xmlCaptivateParams.table.column[i].insertChildAfter(xmlCaptivateParams.table.column[i].row[index-1], <row><cell bgColor='' borderColor=''></cell></row>)			
						}else{
							xmlCaptivateParams.table.column[i].insertChildBefore(xmlCaptivateParams.table.column[i].row[index], <row><cell bgColor='' borderColor=''></cell></row>)
						}
					}
					trace(xmlCaptivateParams+"<><><><><")
					for(i=0; i<arrRowReferences.length; i++){
						if(i>=index){
							arrRowReferences[i].shift(objConfigXML.row.@height);
						}
					}
				}			
			}
			var objRowReference = new RowReference();
			if(isDefault == false){
				if(arrRowReferences.length > index){
					if(arrRowReferences[index].currentSelectedCell == -1){
						objRowReference.currentSelectedCell = arrRowReferences[index-1].currentSelectedCell
					}else{
						objRowReference.currentSelectedCell = arrRowReferences[index].currentSelectedCell
					}
					
				}else if(arrRowReferences.length == index){
					objRowReference.currentSelectedCell = arrRowReferences[index-1].currentSelectedCell
				}
			}
			objRowReference.setReferences(mcCells, arrRowReferences);
			objRowReference.setColumnReferences(arrColumnReferences);
			objRowReference.addEventListener(objRowReference.UPDATEHEIGHT, rowResized);
			objRowReference.init(index);
			
			if(!isDefault){
				objRowReference.createRowAt(index);
			}
			arrRowReferences.splice(index, 0, objRowReference)
			for(i=index+1; i<arrRowReferences.length; i++){
				arrRowReferences[i].index = i;
			}
			if(!isDefault){
				objColumnClass.updateCellReferences(index, objRowReference.arrCellsReference)
			}
			ScrollPane(mcRef.parent.parent).update();
			if(!isDefault){
				if(index <= selectedRow){
					selectedRow++
				}
				objRowReference.rowChanged();
			}
			shiftAddRowBar();
			if((isDefault == false) && (selectedRow == -1)){
				objRowReference.arrCellsReference[0].setSelection(true);
			}
			if(selectNewRow){
				selectionRowAt(index)
			}
			
		}
		
		public function deleteRowAt(index:int):void{
			if(arrRowReferences.length ==2){
				return
			}
			var selectNewRow = false;
			if(completeRowSelected != -1){
				selectNewRow = true
				deselectRowSelection()
			}
			objColumnClass.deselectColumnSelection();
			objColumnClass.deselectAllColumns()
			deselectAllRows();
				
			oldDepthForSelection--;
			deleteAssets(index);
			arrColumnReferences = objColumnClass.getColumnReferences()
			for(var i=0; i<arrColumnReferences.length; i++){
				delete xmlCaptivateParams.table.column[i].row[index]
			}
			
			for(i=0; i<arrRowReferences.length; i++){
				if(i>index){
					arrRowReferences[i].shift(-arrRowReferences[index].rowHeight);
				}
			}
			var objRowReference = arrRowReferences[index];
			var newIndex:int;
			if(objRowReference.currentSelectedCell != -1){
				if(arrRowReferences.length > index+1){
					arrRowReferences[index+1].currentSelectedCell = objRowReference.currentSelectedCell
					newIndex = index
				}else if(arrRowReferences.length == index+1){
					arrRowReferences[index-1].currentSelectedCell = objRowReference.currentSelectedCell
					newIndex = index-1;
				}
				selectedRow = -1;
			}
			
			objRowReference.deleteCellsAt(index);
			arrRowReferences.splice(index, 1)

			for(i=0; i<arrRowReferences.length; i++){
				arrRowReferences[i].index = i;
			}
			
			arrRowReferences[newIndex].rowChanged();
			shiftAddRowBar();
			ScrollPane(mcRef.parent.parent).update();
			var selectionIndex = (arrRowReferences.length > index) ? index : (index - 1)
			selectionRowAt(selectionIndex)
			arrRowReferences[selectionIndex].arrCellsReference[0].setSelection(true);
			
		}
		
		private function displayAddRowBar(evt:MouseEvent){
			evt.currentTarget.alpha = 1
			
		};

		private function hideAddRowBar(evt:MouseEvent){
			evt.currentTarget.alpha = 0
		}
		
		private function addRowAtLast(evt:MouseEvent){
			objColumnClass.hideDisplayedColumn();
			hideDisplayedRow();
			objColumnClass.deselectColumnSelection();
			objColumnClass.deselectAllColumns()
			createRowAt(arrRowReferences.length);
			arrRowReferences[arrRowReferences.length-1].arrCellsReference[0].setSelection(true);
		}
		
		private function shiftAddRowBar(){
			addRowBar.y = int(objConfigXML.rowColumnGrid.@columnHeight) + mcCells.y + mcCells.height;
			addRowBar.x = objConfigXML.rowColumnGrid.@rowWidth;
			addRowBar.bg.width = mcCells.width;
			if(objColumnClass.addColumnBar != null){
				objColumnClass.addColumnBar.bg.height = mcCells.height;
			}
		}
		/**
		* This function will set the references of all the rows in ColumnClass so that every columnReference object
		* can contain the same for their logic.
		*/
		public function getRowReferences():Array{
			return arrRowReferences;
		}
		
		public function setRowReferences(arrRef:Array):void{
			arrRowReferences = arrRef;
		}
		
		
		public function updateCellReferences(index:int, arrCells:Array){
			for(var i=0; i<arrRowReferences.length; i++){
				arrRowReferences[i].arrCellsReference.splice(index, 0, arrCells[i]);
				arrRowReferences[i].addEvents();
			}
		}		
		
		/**
		* This function is used to create row level assets like Row Number field, cell, drop down menu etc.
		*/
		private function createAssets(index){
			var rowMc = new MovieClip();
			arrRowsAssets.splice(index, 0, rowMc);
			rowsMc.addChild(rowMc)
			
			var bg = new MovieClip();
			bg.name = "bg";
			bg.graphics.beginFill(objConfigXML.rowColumnGrid.@bgColor);
			bg.graphics.drawRect(0, 0, objConfigXML.rowColumnGrid.@rowWidth, objConfigXML.row.@height);
			bg.graphics.endFill()
			bg.graphics.lineStyle(objConfigXML.rowColumnGrid.@borderThickness, objConfigXML.rowColumnGrid.@borderColor, 1, false, "none");
			
			bg.graphics.moveTo(objConfigXML.rowColumnGrid.@rowWidth, 0);
			bg.graphics.lineTo(0, 0);
			bg.graphics.lineTo(0, objConfigXML.row.@height);
			bg.graphics.lineTo(objConfigXML.rowColumnGrid.@rowWidth, objConfigXML.row.@height);
			bg.graphics.endFill()
			
			if(index != 0){
				bg.addEventListener(MouseEvent.MOUSE_OVER, showDDMenu)
				bg.addEventListener(MouseEvent.MOUSE_OUT, hideDDMenu)
			}
			bg.addEventListener(MouseEvent.CLICK, clickedRowBg)

			rowMc.addChild(bg);
			rowMc.bg = bg;
			
			var bgRollover = new MovieClip();
			bgRollover.name = "bgRollover";
			bgRollover.graphics.beginFill(objConfigXML.rowColumnGrid.@hoverBgColor);
			bgRollover.graphics.drawRect(0, 0, objConfigXML.rowColumnGrid.@rowWidth, objConfigXML.row.@height);
			bgRollover.graphics.endFill()
			bgRollover.graphics.lineStyle(objConfigXML.rowColumnGrid.@borderThickness, objConfigXML.rowColumnGrid.@borderColor, 1, false, "none");
			
			bgRollover.graphics.moveTo(objConfigXML.rowColumnGrid.@rowWidth, 0);
			bgRollover.graphics.lineTo(0, 0);
			bgRollover.graphics.lineTo(0, objConfigXML.row.@height);
			bgRollover.graphics.lineTo(objConfigXML.rowColumnGrid.@rowWidth, objConfigXML.row.@height);
			bgRollover.graphics.endFill()
			if(index != 0){
				bgRollover.addEventListener(MouseEvent.MOUSE_OVER, showDDMenu)
				bgRollover.addEventListener(MouseEvent.MOUSE_OUT, hideDDMenu)
			}
			bgRollover.addEventListener(MouseEvent.CLICK, clickedRowBg)
			bgRollover.visible = false
			rowMc.addChild(bgRollover);
			rowMc.bgRollover = bgRollover;
			
			var addButtonAbove = new AddRowButton();
			addButtonAbove.name = "addButtonLeft";
			addButtonAbove.x = bg.x
			addButtonAbove.y = bg.y
			addButtonAbove.visible = false
			rowMc.addChild(addButtonAbove);
			rowMc.addButtonAbove = addButtonAbove;
			addButtonAbove.addEventListener(MouseEvent.CLICK, addRowAbove)
			
			var addButtonBelow = new AddRowButton();
			addButtonBelow.name = "addButtonRight";
			addButtonBelow.x = bg.x 
			addButtonBelow.y = bg.y + bg.height
			addButtonBelow.visible = false
			rowMc.addChild(addButtonBelow);
			rowMc.addButtonBelow = addButtonBelow;
			addButtonBelow.addEventListener(MouseEvent.CLICK, addRowBelow)
			
			var tf:TextField = new TextField();
			tf.width = bg.width;
			tf.autoSize = TextFieldAutoSize.CENTER;
			tf.selectable = false;
			//tf.embedFonts = true;
			tf.mouseEnabled = false;
			rowMc.tf = tf;
			
			var dropDownMenuButton = new RowDropDownMenuButton();
			dropDownMenuButton.useHandCursor = false
			dropDownMenuButton.x = bg.width
			dropDownMenuButton.y = bg.y + (bg.height /2)
			dropDownMenuButton.visible = false;
			rowMc.dropDownMenuButton = dropDownMenuButton;
			dropDownMenuButton.addEventListener(MouseEvent.MOUSE_OVER, showMenu)
			dropDownMenuButton.addEventListener(MouseEvent.MOUSE_OUT, hideMenu)
			dropDownMenuButton.addEventListener(MouseEvent.MOUSE_DOWN, showCompleteMenu)
			rowMc.addChild(dropDownMenuButton)
			
			var dropDownMenu:MovieClip = new RowDropDownMenu();
			dropDownMenu.x = dropDownMenuButton.x
			dropDownMenu.y = dropDownMenuButton.y
			dropDownMenu.visible = false;
			//dropDownMenu.closeMenu.buttonMode = true;
			dropDownMenu.closeMenu.addEventListener(MouseEvent.MOUSE_DOWN, hideCompleteMenu)
			dropDownMenu.menuBtn1.addEventListener(MouseEvent.CLICK, insertRowMenu);
			dropDownMenu.menuBtn1.useHandCursor = false
			dropDownMenu.menuBtn2.addEventListener(MouseEvent.CLICK, deleteRowMenu);
			dropDownMenu.menuBtn2.useHandCursor = false
			rowMc.dropDownMenu = dropDownMenu;
			rowMc.addChild(dropDownMenu)
			
			rowMc.addChild(tf);
			
			//code to select the column
			
			for(var i=0; i<arrRowsAssets.length; i++){
				if(i>0){
					arrRowsAssets[i].y = arrRowsAssets[i-1].y + arrRowsAssets[i-1].bg.height;
					arrRowsAssets[i].tf.htmlText = objWidgetController.htmlize(i, "rowColumnGrid");
				}else{
					arrRowsAssets[i].y = int(objConfigXML.rowColumnGrid.@yPos) + int(objConfigXML.rowColumnGrid.@columnHeight)
					addButtonAbove.visible = false
					addButtonBelow.visible = false
				}
				
				arrRowsAssets[i].tf.y = (arrRowsAssets[i].bg.height - arrRowsAssets[i].tf.height)/2
				arrRowsAssets[i].x = objConfigXML.rowColumnGrid.@xPos
			}
		}
		
		private function deleteAssets(index:int){
			var rowMc = arrRowsAssets.splice(index, 1)[0];
			rowsMc.removeChild(rowMc)
			for(var i=0; i<arrRowsAssets.length; i++){
				if(i>0){
					arrRowsAssets[i].y = arrRowsAssets[i-1].y + arrRowsAssets[i-1].bg.height;
				}else{
					arrRowsAssets[i].y = int(objConfigXML.rowColumnGrid.@yPos) + int(objConfigXML.rowColumnGrid.@columnHeight)
				}
				arrRowsAssets[i].tf.htmlText = objWidgetController.htmlize(i+1, "rowColumnGrid");
				arrRowsAssets[i].tf.y = (arrRowsAssets[i].bg.height - arrRowsAssets[i].tf.height)/2
				arrRowsAssets[i].x = objConfigXML.rowColumnGrid.@xPos
			}
		}
		
		private function showMenu(evt:MouseEvent):void{
			evt.currentTarget.visible = true
		}
		private function hideMenu(evt:MouseEvent):void{
			evt.currentTarget.visible = false
		}
		private function showDDMenu(evt:MouseEvent):void{
			evt.currentTarget.parent.dropDownMenuButton.visible = true
		}
		private function hideDDMenu(evt:MouseEvent):void{
			evt.currentTarget.parent.dropDownMenuButton.visible = false
		}
		private function showCompleteMenu(evt:MouseEvent):void {
			
			var toDisable = false;
			if(selectedRow != -1){
				var currentSelectedRow = arrRowReferences[selectedRow]
				
				if(currentSelectedRow.currentSelectedCell!=-1){
					var currentSelectedRowCell = currentSelectedRow.arrCellsReference[currentSelectedRow.currentSelectedCell]
					if (currentSelectedRowCell.cellText.type == "input") {
						toDisable = true
					}
				}
			}
			if(!toDisable){
				if(oldMenuDisplayingRow){
					rowsMc.setChildIndex(oldMenuDisplayingRow, oldDepthForMenu)
					oldMenuDisplayingRow.dropDownMenu.visible = false;
					oldMenuDisplayingRow.dropDownMenuButton.visible = false;
				}
				oldMenuDisplayingRow = evt.currentTarget.parent;
				if(selectedRow!=-1){
					arrRowsAssets[selectedRow].addButtonBelow.visible = false;
					arrRowsAssets[selectedRow].addButtonAbove.visible = false;
				}
				oldDepthForMenu = rowsMc.getChildIndex(oldMenuDisplayingRow);
				rowsMc.setChildIndex(evt.currentTarget.parent, arrRowsAssets.length-1)
			//}
			
			evt.currentTarget.parent.dropDownMenu.visible = true;
			mcRef.cellBlocker.reference = evt.currentTarget.parent;
			mcRef.cellBlocker.width = mcRef.width; 
			mcRef.cellBlocker.height = mcRef.parent.height;
			mcRef.cellBlocker.visible = true;
			mcRef.setChildIndex(mcRef.cellBlocker, 1);
			mcRef.cellBlocker.addEventListener(MouseEvent.CLICK, clickedOutSideMenu);
			//objColumnClass.deselectColumnSelection();
			//deselectRowSelection();
			dispatchEvent(new Event("hideopenedmenu"));
			}
				
		}
		
		private function hideCompleteMenu(evt:MouseEvent):void{
			mcRef.cellBlocker.width = 0
			mcRef.cellBlocker.height = 0
			if(oldMenuDisplayingRow){
				rowsMc.setChildIndex(oldMenuDisplayingRow, oldDepthForMenu)
				oldMenuDisplayingRow = null;
				oldDepthForMenu = 0
			}
			if(selectedRow!=-1){
				arrRowsAssets[selectedRow].addButtonBelow.visible = true;
				arrRowsAssets[selectedRow].addButtonAbove.visible = true;
			}
			evt.currentTarget.parent.visible = false;
		}
		
		private function clickedOutSideMenu(evt:MouseEvent):void{
			mcRef.cellBlocker.removeEventListener(MouseEvent.CLICK, clickedOutSideMenu);
			mcRef.cellBlocker.visible = false;
			mcRef.cellBlocker.width = 0
			mcRef.cellBlocker.height = 0
			mcRef.cellBlocker.reference.dropDownMenu.visible = false;
			mcRef.cellBlocker.reference.dropDownMenuButton.visible = false;
			if(selectedRow!=-1){
				arrRowsAssets[selectedRow].addButtonBelow.visible = true;
				arrRowsAssets[selectedRow].addButtonAbove.visible = true;
			}
			if(oldMenuDisplayingRow){
				rowsMc.setChildIndex(oldMenuDisplayingRow, oldDepthForMenu)
				oldMenuDisplayingRow = null;
				oldDepthForMenu = 0
			}
		}
		
		private function clickedRowBg(evt:MouseEvent):void{
			mcRef.cellBlocker.width = 0
			mcRef.cellBlocker.height = 0
			if(selectedRow!=-1){
				arrRowsAssets[selectedRow].addButtonBelow.visible = true;
				arrRowsAssets[selectedRow].addButtonAbove.visible = true;
			}
			objColumnClass.hideDisplayedColumn();
			hideDisplayedRow();
			if(evt != null){
				selectCompleteRow(evt);
			}
		}
		
		private function insertRowMenu(evt:MouseEvent):void{
			mcRef.cellBlocker.width = 0
			mcRef.cellBlocker.height = 0
			hideDisplayedRow();
			var mc = evt.currentTarget.parent.parent;
			mc.dropDownMenu.visible = false;
			var index = getRowNumber(mc);
			if(index == 0){
				index = 1;
			}
			createRowAt(index);
		}		
		private function deleteRowMenu(evt:MouseEvent):void{
			mcRef.cellBlocker.width = 0
			mcRef.cellBlocker.height = 0
			hideDisplayedRow();
			var mc = evt.currentTarget.parent.parent;
			mc.dropDownMenu.visible = false;
			deleteRowAt(getRowNumber(mc));
		}
		public function hideDisplayedRow(){
			if(oldMenuDisplayingRow){
				oldMenuDisplayingRow.dropDownMenu.visible = false;
				oldMenuDisplayingRow.dropDownMenuButton.visible = false;
				rowsMc.setChildIndex(oldMenuDisplayingRow, oldDepthForMenu)
				oldMenuDisplayingRow = null;
				oldDepthForMenu = 0
			}
		}
		
		private function getRowNumber(mc:MovieClip):int{
			var num = 0;
			for(var i=0; i<arrRowsAssets.length; i++){
				if(mc == arrRowsAssets[i]){
					num = i;
					break;
				}
			}
			return num;
		}
		
		
		public function setSelection(index:int, columnIndex:int):void{
			deselectRowSelection();
			deselectAllRows();
			if(selectedRow != -1){
				rowsMc.setChildIndex(arrRowsAssets[selectedRow], oldDepthForSelection)
				arrRowsAssets[selectedRow].bgRollover.visible = false;
				arrRowsAssets[selectedRow].addButtonBelow.visible = false;
				arrRowsAssets[selectedRow].addButtonAbove.visible = false;
				arrRowReferences[selectedRow].currentSelectedCell = -1
			}
			
			if(columnIndex == -2){
				deselectAllRows();
				objColumnClass.selectAllColumns();
			}else{
				arrRowReferences[index].currentSelectedCell = columnIndex
			}
			arrRowsAssets[index].bgRollover.visible = true;
			if(index != 0){
				arrRowsAssets[index].addButtonBelow.visible = true;
				arrRowsAssets[index].addButtonAbove.visible = true;
			}
			oldDepthForSelection = rowsMc.getChildIndex(arrRowsAssets[index]);
			rowsMc.setChildIndex(arrRowsAssets[index], arrRowsAssets.length-1)
			selectedRow = index
			
		}
		
		public function selectAllRows(){
			deselectAllRows();
			selectedRow = -1;
			arrRowsAssets[1].addButtonAbove.visible = true;
			arrRowsAssets[arrRowsAssets.length-1].addButtonBelow.visible = true;
			for(var i=0; i<arrRowsAssets.length; i++){
				arrRowsAssets[i].bgRollover.visible = true;
			}
		}
		public function deselectAllRows(){
			for(var i=0; i<arrRowsAssets.length; i++){
				if(arrRowsAssets[i].bgRollover.visible){
					//rowsMc.setChildIndex(arrRowsAssets[i], oldDepthForSelection)
					arrRowsAssets[i].bgRollover.visible = false;
					arrRowsAssets[i].addButtonBelow.visible = false;
					arrRowsAssets[i].addButtonAbove.visible = false;
					arrRowReferences[i].currentSelectedCell = -1
					selectedRow = -1;
				}
			}
		}
		
		private function selectCompleteRow(evt:MouseEvent){
			var mc = evt.currentTarget.parent;
			objColumnClass.deselectColumnSelection();
			selectionRowAt(getRowNumber(mc));
		}
		
		private function addRowBelow(evt:Event):void{
			var mc = evt.currentTarget.parent;
			createRowAt(getRowNumber(mc)+1);
		}
		
		private function addRowAbove(evt:Event):void{
			var mc = evt.currentTarget.parent;
			var index = getRowNumber(mc);
			if(index != 0){
				createRowAt(index);
			}
		}
		
		private function rowResized(evt:Event):void{
			var index = evt.currentTarget.index
			arrRowsAssets[index].bg.height = (arrRowReferences[index].rowHeight > objConfigXML.row.@height)? arrRowReferences[index].rowHeight : objConfigXML.row.@height;
			arrRowsAssets[index].bgRollover.height = arrRowReferences[index].rowHeight;
			arrRowsAssets[index].addButtonBelow.y = arrRowsAssets[index].bg.y + arrRowsAssets[index].bg.height
			arrRowsAssets[index].tf.y = (arrRowsAssets[index].bg.height - arrRowsAssets[index].tf.height)/2
			arrRowsAssets[index].dropDownMenuButton.y = arrRowsAssets[index].bg.y + (arrRowsAssets[index].bg.height /2)
			arrRowsAssets[index].dropDownMenu.y = arrRowsAssets[index].dropDownMenuButton.y
			var colRef = arrRowReferences[index].currentSelectedColumn
			xmlCaptivateParams.table.column[colRef.index].row[index].cell[0] = evt.currentTarget.arrCellsReference[colRef.index].txtContent;
			if(objWidgetController.currentSelectedCell == null){
				if(colRef.arrCellsReference.length > (index+1)){
					colRef.arrCellsReference[index+1].setSelection(true);
				}else{
					createRowAt(index+1);
					colRef.arrCellsReference[index+1].setSelection(true);
				}
			}
			for(var i=index+1; i<arrRowsAssets.length; i++){
				arrRowsAssets[i].y = arrRowsAssets[i-1].y + arrRowsAssets[i-1].bg.height
			}
			shiftAddRowBar();
		}
		
		
		public function setDefaultRowHeights(){
			var arrHeights = new Array();
			for(var i=0; i<arrRowReferences.length; i++){
				var arrCells = arrRowReferences[i].arrCellsReference
				for(var j=0; j<arrCells.length; j++){
					arrCells[j].wrapText();
					if(arrHeights[i] == undefined){
						arrHeights[i] = arrCells[j].Height;
					}else{
						arrHeights[i] = Math.max(arrHeights[i], arrCells[j].Height);
					}
				}
				arrRowReferences[i].rowHeight = arrHeights[i];
				
				arrRowsAssets[i].bg.height = (arrRowReferences[i].rowHeight > objConfigXML.row.@height)? arrRowReferences[i].rowHeight : objConfigXML.row.@height;
				arrRowsAssets[i].bgRollover.height = arrRowReferences[i].rowHeight;
				arrRowsAssets[i].addButtonBelow.y = arrRowsAssets[i].bg.y + arrRowsAssets[i].bg.height
				arrRowsAssets[i].tf.y = (arrRowsAssets[i].bg.height - arrRowsAssets[i].tf.height)/2
				arrRowsAssets[i].dropDownMenuButton.y = arrRowsAssets[i].bg.y + (arrRowsAssets[i].bg.height /2)
				arrRowsAssets[i].dropDownMenu.y = arrRowsAssets[i].dropDownMenuButton.y
				
				for(j=0; j<arrRowsAssets.length; j++){
					if(j>i){
						arrRowsAssets[j].y = arrRowsAssets[j-1].y + arrRowsAssets[j-1].bg.height
					}
				}
				var heightIncreased = false;
				for(j=0; j<arrCells.length; j++){
					if(arrCells[j].Height != arrHeights[i]){
						arrCells[j].Height = arrHeights[i];
						heightIncreased = true;
					}
				}
				var newPosition = arrRowReferences[i].yPos + arrHeights[i];
				var prevPosition;
				if(arrRowReferences[i+1] != null){
					prevPosition = arrRowReferences[i+1].yPos;
				}else{
					prevPosition = arrRowReferences[i].yPos;
				}
				
				for(j=0; j<arrRowReferences.length; j++){
					if(j>i){
						arrRowReferences[j].shift(newPosition - prevPosition);
					}
				}
			}	
			shiftAddRowBar();
		}
		
		private function selectionRowAt(index:int):void{
			if(selectedRow != -1){
				var selectedRowRef = arrRowReferences[selectedRow]
				if(selectedRowRef.currentSelectedCell != -1){
					selectedRowRef.arrCellsReference[selectedRowRef.currentSelectedCell].setSelection(false);
				}
			}
			var rowCellsRef = arrRowReferences[index].arrCellsReference
			var selXPos = rowCellsRef[0].x;
			var selYPos = rowCellsRef[0].y;			
			var lastCell = rowCellsRef[rowCellsRef.length-1]
			var selWidth = lastCell.x + lastCell.width - selXPos - 1;
			var selHeight = rowCellsRef[0].height - 1;
			setSelection(index, -2);
			deselectRowSelection();
			var mc:MovieClip
			if(mcGrid.getChildByName("rowSelection") == null){
				mc = new MovieClip();
				mc.name = "rowSelection"
				mcGrid.addChild(mc);
				mcGrid.setChildIndex(mc, 0)
			}else{
				mc = MovieClip(mcGrid.getChildByName("rowSelection"));
				mc.graphics.clear();
				mcGrid.setChildIndex(mc, 0)
				mc.visible = true;
			}
			mc.graphics.lineStyle(2,objConfigXML.cell.@selectedBorderColor);
			mc.graphics.drawRect(0, 0, selWidth, selHeight);
			mc.graphics.endFill();
			mc.x = selXPos+1;
			mc.y = selYPos+1
			var cellRef
			arrRowReferences[index].rowSelected = true;
			cellRef = arrRowReferences[index].arrCellsReference
			for(var i=0; i<cellRef.length; i++){
				cellRef[i].changeToSelection(true);
			}
			completeRowSelected = index;
			
		}
		
		public function deselectRowSelection(){
			if(completeRowSelected != -1){
				var prevSelectedRow = arrRowReferences[completeRowSelected];
				prevSelectedRow.rowSelected = false;
				var cellRef = prevSelectedRow.arrCellsReference
				for(var i=0; i<cellRef.length; i++){
					cellRef[i].changeToSelection(false);
				}
				completeRowSelected = -1;
				if(mcGrid.getChildByName("rowSelection") != null){
					var mc:MovieClip = MovieClip(mcGrid.getChildByName("rowSelection"));
					mc.visible = false;
				}
			}
		}
	}
}