dojo.require("dijit.layout.StackContainer");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");

/**
 * @author DREDroot
 *
 */
dojo.declare("dojom.classes.TabClass", null, 
{
	tabIds	: null,
	sTabIds : null,
	controller : null,
	logger : null,
	mainTabContainer : null,
	log: null,
	
	constructor: function (mainTabNode)
	{
		this.log = LOG.addClass('TabClass');
		var obj = this;
		this.tabIds = -2;
		this.sTabIds = 
		[	
			true,
			true,
			true,
			true,
			true
		];
		this.mainTabContainer = dijit.byId(mainTabNode);
		
	},
	
	getNextTabId: function()
	{
		return this.tabIds--;
	},
	
	getNextSearchTabId: function()
	{
		var obj = this;
		try{
			for(var i=0; i<this.sTabIds.length;i++)
			{
				obj.log.debug('getNextSearchTabId. tabId: '+i+','+this.sTabIds[i]);
				if(this.sTabIds[i])
				{				
					this.sTabIds[i] = false;
					return i;
				}
			}	
			return null;
		}catch(ex){obj.log.debug('getNextTabId.'+JSON.stringify(ex))}
	},
	
	createNewTab: function(params)	
	{
		var obj = this;
		
		try{		
			var title = params['title'];
			var tabId = (params['tabId'] != null ? params['tabId'] : this.getNextTabId);
			var id = (params['id'] != null ? params['id'] : tabId);
			var insertIndex = params['insertIndex'];
			var parameters = (params['parameters'] ? params['parameters'] : {});
			var toolbar = params['toolbar'];
			var grid = params['grid'];
			var pages = params['pages'];
			var sentity = params['sentity'];
			var sepanel = params['sepanel'];
			var cssClass = params['cssClass'];
						
			
			obj.log.debug('createNewTab. id='+id);
			if(id == null)
			{
				obj.log.error('CreateNewTab. id is null');
				return false;
			}
		
			var node = document.createElement('div');
			//dojo.body().appendChild(node);
		
			
			var tbl = document.createElement("table"); 
			tbl.className = "vs_container";
			
       	 	var tblBody = document.createElement("tbody");
						
			var row1 = document.createElement("tr");
			var row2 = document.createElement("tr");
			
			/*table_area*/

			var cell11 = document.createElement("td");	
			cell11.className = "vs_container_left";
			var cell12 = document.createElement("td");
			cell12.className = "vs_container_right";
			
			var gridNode = document.createElement("div");
			gridNode.className = 'vs_container_left_div';
			
			if(!toolbar) 
				toolbar = new dijit.layout.ContentPane({id: 'toolbar_'+id, style:"overflow-x: hidden;"},document.createElement('div'));					
			gridNode.appendChild(toolbar.domNode);
			
			if(!grid)
				grid = new dijit.layout.ContentPane({id: 'grid_' + id},document.createElement('div'));			
			gridNode.appendChild(grid.domNode);			
			
			cell11.appendChild(gridNode);	
			
			if(!sentity)
				sentity = new dijit.layout.ContentPane({id: 'sentity_'+id, className:'vs_container_right_div'},document.createElement('div'));
			cell12.appendChild(sentity.domNode);
			
			
			row1.appendChild(cell11);
			row1.appendChild(cell12);
			tblBody.appendChild(row1);			
			
			/*table_area_footer*/
			var cell21 = document.createElement("td");	
			cell21.className = "vs_container_left";
			var cell22 = document.createElement("td");
			cell22.className = "vs_container_right";
			
			if(!pages)
				pages = new dojom.widgets.PagesNode({id: 'pages_'+id},document.createElement('div'));
			cell21.appendChild(pages.domNode);
			
			if(!sepanel)
				sepanel = new dijit.layout.ContentPane({id: 'sepanel_'+id},document.createElement('div'));
			cell22.appendChild(sepanel.domNode);
			
			row2.appendChild(cell21);
			row2.appendChild(cell22);
			tblBody.appendChild(row2);	
			
			tbl.appendChild(tblBody);	
			
			node.appendChild(tbl);			
			

			/*CREATE TAB*/		
			this.mainTabContainer.addChild(new dijit.layout.ContentPane({
					id: (params['id'] !=null ? id : "tab_"+id), 
					title: (title !=null ? title : (parameters['keywords'] ? parameters['keywords'] : '_')),
					closable: true,
					tabId: tabId,
					parameters: {},
					toolbar: toolbar,
					grid: grid,
					pages: pages,
					sentity: sentity,
					sepanel: sepanel,
					
					setTitle: function(title)
					{	
						try{	
							if(title==null || title=='')			
								return;
							obj.log.debug('setTitle :'+title);
							
							this.controlButton.focusNode.innerHTML = title;							
							
							//obj.log.debug(this.controlButton.innerDiv.innerHTML);
							this.title = title;
						}catch(ex){obj.log.error('createNewTab /setTitle/.'+JSON.stringify(ex))}
					}
				}, 
				node),
				insertIndex
			);	
				
			/*set tab params*/
			var tabChild = dijit.byId(params['id'] !=null ? id : "searchTab_"+id);
			
			obj.log.debug('createNewTab. tabChild:'+tabChild);	
			tabChild.parameters['keywords'] = parameters['keywords'];
			tabChild.parameters['category'] = parameters['category'];										
			tabChild.parameters['page'] = parameters['page'];										
			tabChild.parameters['entity'] = parameters['entity'];
			tabChild.parameters['sortOption'] = parameters['sortOption'];
			tabChild.parameters['byAuthor'] = parameters['byAuthor'];
			obj.log.debug('createNewTab. tabChild.parameters:'+JSON.stringify(tabChild.parameters));			
			
			/*set css CLASS*/
			tabChild.controlButton.innerDiv.className += ' '+ cssClass;						
			
			/*EVENTS*/
			dojo.connect(tabChild,'destroy',function(){
				try{				
					if(grid!= null) grid.destroy();
					if(toolbar!= null) toolbar.destroy();
					if(pages!= null) pages.destroy();
					if(sentity!= null) sentity.destroy();
					if(sepanel!= null) sepanel.destroy();			
					if(tabId !=null && tabId >=0)
						obj.sTabIds[tabId] = true;
					
					obj.log.debug('createNewTab /tabChild destroy/.'+tabChild+' tabId:'+tabId);
				}catch(ex){obj.log.error('createNewTab /destroy tabChild: '+tabChild+'/.'+JSON.stringify(ex))}
			});
			
			this.mainTabContainer.selectChild(tabChild);
		}catch(ex){obj.log.error('createNewTab.'+JSON.stringify(ex))}
		
		return tabChild;
	},
	
	changeTab: function(tabChild,params)
	{
		var obj = this;			
		//obj.log.debug('changeTab.tabChild:'+tabChild+', params:'+JSON.stringify(params));		
		try{
			for(var key in params)	
			{									
				if(key == 'sentityContent')
				{
					if(params[key].nodeName != null)
					{
						if(tabChild.sentity.childDomNode != null)
							tabChild.sentity.domNode.replaceChild(params[key],tabChild.sentity.childDomNode);
						else
							tabChild.sentity.domNode.appendChild(params[key]);
						tabChild.sentity.childDomNode = params[key];
					}
					else 
					{
						if(tabChild.sentity.childDomNode != null)
							tabChild.sentity.domNode.removeChild(tabChild.sentity.childDomNode);
						tabChild.sentity.setContent(params[key]);					
					}
				}
				else if(key == 'keywords')
				{
					tabChild.parameters['keywords'] = params[key];
					tabChild.setTitle(params[key]);
				}
				else if(key == 'title')
				{
					tabChild.setTitle(params[key]);
				}
				else if(key ==  'category')
				{ 
					tabChild.parameters['category'] = params[key];
				}
				else if(key ==  'sortOption')
				{
					tabChild.parameters['sortOption'] = params[key];
				}
				else if(key == 'byAuthor')
				{
					tabChild.parameters['byAuthor'] = params[key];
				}
				else if(key == 'page')
				{
					tabChild.parameters['page'] = params[key];
					tabChild.pages.setCurrentPage(params[key]);
				}
				else if(key == 'entity')
				{
						tabChild.parameters['entity'] = params[key];
				}
				else if(key == 'gridData')
				{
					tabChild.grid.setData(params[key]);
				}
				else if(key == 'pagesNum')
				{
					tabChild.pages.setPagesNum(params[key]);
				}
				else if(key == 'toolbarContent')
				{
					if(params[key].nodeName != null)
					{
						if(tabChild.toolbar.childDomNode != null)
							tabChild.toolbar.domNode.replaceChild(params[key],tabChild.toolbar.childDomNode);
						else
							tabChild.toolbar.domNode.appendChild(params[key]);
						tabChild.toolbar.childDomNode = params[key];
					}
					else 
					{
						if(tabChild.toolbar.childDomNode != null)
							tabChild.toolbar.domNode.removeChild(tabChild.toolbar.childDomNode);
						tabChild.toolbar.setContent(params[key]);					
					}
				}
				else if(key == 'sepanelContent')
				{
					if(params[key].nodeName != null)
					{
						if(tabChild.sepanel.childDomNode != null)
							tabChild.sepanel.domNode.replaceChild(params[key],tabChild.sepanel.childDomNode);
						else
							tabChild.sepanel.domNode.appendChild(params[key]);
						tabChild.sepanel.childDomNode = params[key];
					}
					else 
					{
						if(tabChild.sepanel.childDomNode != null)
							tabChild.sepanel.domNode.removeChild(tabChild.sepanel.childDomNode);
						tabChild.sepanel.setContent(params[key]);					
					}
				}	
				else if(key == 'cssClass')
				{
					tabChild.controlButton.innerDiv.className += ' '+ params[key];	
				}						
			}						
		}catch(ex){obj.log.error('changeTab.'+JSON.stringify(ex))}							
	}
	
});
