/** 
 * Initialization method for a tab mechanic. Example usage:
 * 
 * 	
    var obj={
    	_selectedIdStr: 'secondTab',
		_idStr: "tab1", 
		_buttonIdArr: {
			},
		_contentIdArr:{
			},
		_activeStyleStr:'activeTab',
		_noStartingStateBln: false,
		_hideContentBln: true
		
	};		
	var tabCls = new TabController(obj);
	
	obj._buttonIdArr : Array of id for each tab button, each button should have a unique ID.
	obj._contentIdArr : (optional) Array of id for each content section connected to tab buttons, should be respected to obj._contentIdArr.
	obj._noStartingStateBln : (optional) Boolean to indicate no starting state. By default will be set false, selecting the first id in the _buttonIdArr
	obj._selectedIdStr : (optional) button to be selected
	obj._hideContentBln : (optional) hides inactive content on load.
 */
function TabController(obj) {
	
	// ------------------------------------------------------------------------------------------
	// DEFINE PROPERTIES
	// ------------------------------------------------------------------------------------------
	this._contentIdArr = new Array();
	this._buttonIdArr;
	this._idStr;
	this._activeStyleStr;
	this._selectedIdStr;
	this._previousSelectedIdStr;
	
	this._propsObj;
	
	// ------------------------------------------------------------------------------------------
	// INTIALIZATION
	// ------------------------------------------------------------------------------------------
	this.init = function(obj) {
		//Common.tracer("TabController.init");
		this.setupProps(obj);
		this.setupListeners(obj);
		this.generate(obj);
		
	};
	
	/** Calls primary initialization methods. */
	this.setupProps = function(obj) {
		//Common.tracer("TabController.setupProps");
		//save the id of the property
		if(obj._idStr) this._idStr = obj._idStr;	
		if(obj._contentIdArr) this._contentIdArr = obj._contentIdArr;	
		if(obj._buttonIdArr) this._buttonIdArr = obj._buttonIdArr;	
		if(obj._activeStyleStr) this._activeStyleStr = obj._activeStyleStr;	
		
		this._selectedIdStr = this._buttonIdArr[0];
		if(obj._selectedIdStr) this._selectedIdStr = obj._selectedIdStr;	
		
		
	};
	
	/** Sets up listeners on prev and next button */
	this.setupListeners = function(obj){
		//Common.addEventListener({_idStr: "click", _targetStr: ".ev_"+this._idStr+"_paginator", _eventObj: {_context: this, _fn: this.paginatorRelease} } ); //_weakBln: false
		
		//Loop through this._buttonIdArr and set up listener for each id.  Weak.
		for(i=0; i< this._buttonIdArr.length; i++){
			Common.addEventListener({_idStr: "click", _targetStr: "#"+this._buttonIdArr[i], _eventObj: {_context: this, _fn: this.tabRelease} } ); //_weakBln: false
		}
		
		
	};
	
	/** removes listeners */
	this.removeListeners = function(){
		//Loop through _buttonIdArr and remove listeners. Weak.
		for(i=0; i< this._buttonIdArr.length; i++){
			Common.removeEventListener({_idStr: "click", _targetStr: "#"+this._buttonIdArr[i]} ); //_weakBln: false
		}
	};
	

	// ------------------------------------------------------------------------------------------
	// GENERATION
	// ------------------------------------------------------------------------------------------
	/** Calls all primary generation methods. */
	this.generate = function(obj) {
		//Common.tracer("TabController.generate");
		
		//If noStartingStateBln is true, return.
		if(obj._noStartingStateBln) return;
		
		//Sets _selectedIdStr tab to active
		$('#'+this._selectedIdStr).addClass(this._activeStyleStr);	
		
		if(obj._contentIdArr && obj._hideContentBln){
			this.hideInactiveContent(obj);
		}
	
	};
	// ------------------------------------------------------------------------------------------
	// PUBLIC METHODS 
	// ------------------------------------------------------------------------------------------
	/** tab button has been released */
	this.tabRelease = function(obj){
		//Common.tracer("TabController.tabRelease");
		
		//Get id from button and calls changeTab.
		var idStr = obj._eventObj.currentTarget.id;
		
		this.changeTab({_idStr:idStr});
		
	};
	
	this.changeTab = function(obj) {
		//Common.tracer("TabController.changeTab");
		
		//remove style from _selectedIdStr
		$('#'+this._selectedIdStr).removeClass(this._activeStyleStr);
		
		//Save _previousSelectedIdStr
		this._previousSelectedIdStr = this._selectedIdStr;
		
		//update _selectedIdStr
		this._selectedIdStr = obj._idStr;
		
		//Add active state to _selectedIdStr
		$('#'+this._selectedIdStr).addClass(this._activeStyleStr);
		
		//Dispatch event indicating tab has switched include this._idStr
		Common.dispatchDynamicEvent({_idStr: "TabController"+this._idStr, _paramsObj: {_idStr:this._selectedIdStr} });
	
		// call showContent
		this.showContent();
	};
	
	
	
	this.showContent = function(obj){
		//Common.tracer("TabController.showContent");
		
		//If this._contentIdArr.length is not 0,
		if(this._contentIdArr.length > 0 ){
			var indexToHideStr = this.getIndexOfButton({_idStr:this._previousSelectedIdStr});
			//Hide content of that index.'
			Common.change({_spr: "#"+this._contentIdArr[indexToHideStr], _propStr: "alpha", _newValNum: 0, _successObj: {_fn: this.hideContentSuccess, _context: this  }  });
		}
	};
	
	this.hideContentSuccess = function(obj){
		//Common.tracer("TabController.hideContentSuccess");
		
		//set the display of the current content to none
		var indexToHideStr = this.getIndexOfButton({_idStr:this._previousSelectedIdStr});
		$('#'+this._contentIdArr[indexToHideStr]).css('display','none');
		//show current active content
		this.completeShowContent();
	};
	
	
	this.hideInactiveContent = function(obj){
		Common.tracer("TabController.hideInactiveContent");
		//Loop through contentIdArr and hide all content
		for(i=0; i<this._contentIdArr.length; i++){
			$('#'+this._contentIdArr[i]).css('opacity','0');
			$('#'+this._contentIdArr[i]).css('display','none');
		}
		
		//show current active content
		this.completeShowContent();
	};
	
	this.completeShowContent = function() {
		Common.tracer("TabController.completeShowContent");
		var indexToShowStr = this.getIndexOfButton({_idStr:this._selectedIdStr});
		$('#'+this._contentIdArr[indexToShowStr]).css('display','block');
		Common.change({_spr: "#"+this._contentIdArr[indexToShowStr], _propStr: "alpha", _newValNum: 1 });
		
		Common.dispatchDynamicEvent({_idStr: "K_Site_resize"});
		
		
	};
	
	
	this.getIndexOfButton = function(obj){
		//Common.tracer("TabController.getIndexOfButton");
		
		//get index of obj._idStr
		var indexNum = $.inArray(obj._idStr, this._buttonIdArr);
		return indexNum;
	};

	
	

	// ------------------------------------------------------------------------------------------
	// DESTROY METHOD
	// ------------------------------------------------------------------------------------------
	/** Prepares for garbage collection by clearing all properties and deregistering all listeners. */
	this.destroy = function() {
		//Common.tracer("TabController.destroy");
		
		this.removeListeners();
		
		var destroyArr = [this._buttonIdArr, this._contentIdArr];
		Common.scorch(destroyArr);
		this._buttonIdArr=null; this._contentIdArr=null; 
		
	};
	
	// ------------------------------------------------------------------------------------------
	// CALL TO CONSTRUCTOR - MUST BE AT THE END
	// ------------------------------------------------------------------------------------------
	/** Prepares for garbage collection by clearing all properties and deregistering all listeners. */
	this.init(obj);
	

}




