var Redbar = new Object();

Redbar.TYPE_SEARCH 				= "search";
Redbar.TYPE_MYPAGE 				= "mypage";
Redbar.TYPE_TOPIC				= "topic";
Redbar.TYPE_CUSTOMER_CENTER 	= "cutomcenter";

Redbar.mypageTab = [
	{id : 'myfeed', 	name : '피드관리' 	, url : '/service/MyFeed.action'		, action :'url'	},
	{id : 'myinfo', 	name : '회원정보수정' , url : '/service/ModifyMyInfo.action'	, action :'url'}
];
 
Redbar.topicTab = [
	{id : 'topictrend', 	name : '토글뱅크' 	, url : '/service/TopicTrend.action'		, action :'url'}
];

Redbar.customerCenterTab = [
	{id : 'infomanage', 	name : '회원정보관리' 		, url : '/service/MyFeed.action'	, action :'url'},
	{id : 'qna', 			name : '장애신고 및 문의' 	, url : '/service/TopicReal.action'	, action :'url'}
];

Redbar.Displayer = function(type,parentdiv,selectedId,options){

	this.setOptions(options);
	
	this.selectedTabType	= type;
	this.action				= '';
	this.parentDivId		= parentdiv;
	this.parentDiv			= $(this.parentDivId);
	this.selectedId			= selectedId;
	this.selectedIndex		= -1;
	this.id					= 'redbar';
	
	this.tabInfo = [];
	this.setTabInfo(type);

	//this.addEventListener();
}



Redbar.Displayer.prototype = {

	setOptions: function(options){
		this.options = {
			tabClassName		: 'tab',
			selectedTabClassName: 'tab_on',
			clickScript 		: null
		};
		
		for (var x in options) {
			this.options[x] = options[x];
			if (typeof this.options[x] != 'function') 
				continue;
		}
	},
	
	setTabInfo : function (type,tabinfo) {
		
		if (type && !tabinfo) {
			if (this.selectedTabType == Redbar.TYPE_MYPAGE)
				this.tabInfo[this.selectedTabType] = Redbar.mypageTab;
			else if (this.selectedTabType == Redbar.TYPE_TOPIC)
				this.tabInfo[this.selectedTabType] = Redbar.topicTab;
			else if (this.selectedTabType == Redbar.TYPE_TOPIC)
				this.tabInfo[this.selectedTabType] = Redbar.customerCenterTab;
		} else if (type && tabinfo) {
			
			this.tabInfo[type] 		= tabinfo;
		}
		
		this.selectedTabType	= type;
		
		if (this.tabInfo[type]) {
			this.setDisplay();			
		}
	},
	
	getSelectedTabInfo : function() {
		var stabinfo = this.tabInfo[this.selectedTabType];
		return stabinfo[this.selectedIndex];
	},
	
	setSelectedId : function (id) {
		this.selectedId			= id;
		this.setDisplay();	
	},
	
	setDisplay: function(){
	
		var selectInfo = this.tabInfo[this.selectedTabType];
		
		if (this.parentDiv.childNodes.length == 0) {
			
			for (var i = 0; i < selectInfo.length; i++) {
			
				var li = document.createElement("li");
				li.id = selectInfo[i].id;
				
				var aTag = document.createElement("a");
				aTag.actionType = selectInfo[i].action;
				aTag.clickURL = selectInfo[i].url;
				aTag.onclick = this.tabClickHandler.bindAsEventListener(aTag);
				
				if (i == 0) {
					aTag.className = "all";
				}
					
				if (selectInfo[i].id == this.selectedId) {
					li.className = this.options.selectedTabClassName;
					this.selectedIndex = i;
				}
			
				var tabSpan = document.createElement("span");
				tabSpan.appendChild(document.createTextNode(selectInfo[i].name));
				tabSpan.style.zIndex = "0";
				
				aTag.appendChild(tabSpan);
				li.appendChild(aTag);
				this.parentDiv.appendChild(li);
			}
		} else {
			
			var child = this.parentDiv.childNodes;
			for (var i = 0; i < child.length; i++) {
				
				if (this.selectedId == child[i].id) {
					child[i].className = this.options.selectedTabClassName;
					this.selectedIndex = i;
				} else {
					child[i].className = "";
				}
				
			}
		}
	},

	tabClickHandler : function(e) {
		if (this.actionType == "url") {
			document.location.href = this.clickURL;
		}
		else 
		if (this.actionType == "script") {
			eval(this.clickURL);
		}
	
	}
	
}
