/*
Author: jcyuan.space@gmail.com
the Tab control
@2011 1.13

usage:
   TabCtrl - the main manager
   TabPage - the page to be added to the TabCtrl class
*/

var TabCtrl = new Class({
	pages : null,
	activePage : null,
	defaultIndex : 0,
	Implements: [Events],
	initialize : function(defaultOpenIndex)
	{
		this.pages = [];
		this.setDefaultOpenIndex(defaultOpenIndex);
	},
	setDefaultOpenIndex : function(idx)
	{
		this.defaultIndex = (typeOf(idx) == "number") ? idx : 0;
	},
	addPage : function(page)
	{
		if(instanceOf(page, TabPage))
		{
			if(instanceOf(page.parent, TabCtrl))
				page.parent.removePage(page);
			page.parent = this;
			this.pages.include(page);
		}
		return this;
	},
	removePage : function(page)
	{
		page.destroy();
		this.pages.erase(page);
		page.parent = null;
		return this;
	},
	activate : function(page)
	{
		if(!this.pages.contains(page))return;
		if(this.activePage)
		{
			if(this.activePage == page)return;
			
			this.activePage.deactivate();
			this.fireEvent('onDeactive', this.activePage);
		}
		page.activate();
		this.activePage = page;
		this.fireEvent('onActive', page);
	},
	activateAt : function(idx)
	{
		if(idx < 0 || idx > this.pages.length - 1)return;
		var page = this.pages[idx];
		if(!page)return;
		this.activate(page);
	},
	getActivatedPage : function()
	{
		return this.pages.filter(function(item)
		{
			return item.isActivated();
		});
	},
	draw : function()
	{
		if(this.pages.length <= 0)return;
		if(this.defaultIndex > this.pages.length - 1 || this.defaultIndex < 0)
			this.defaultIndex = 0;
		this.activateAt(this.defaultIndex);
	}
});

var TabPage = new Class({
	name : "",
	status : "deactive",
	header : null,
	body : null,
	parent : null,
	headereventtype : null,
	initialize : function(name, headerID, bodyID, headerevttype, parentCtrl)
	{
		this.name = name;
		this.header = $(headerID);
		this.headerHandler = this._defaultHeaderBehaviroHandler.bind(this);
		this.setHeaderEventType(headerevttype || TabPage.Behavior_CLICK);  //click -> default
		this.body = $(bodyID);
		this.status = "deactive";
		this.setParent(parentCtrl);
	},
	setHeaderEventType : function(evt)
	{
		if(typeOf(this.header) != "element" || !evt) return;
		
		if(this.headereventtype != null)
			this.header.removeEvent(this.headereventtype, this.headerHandler);
		
		this.headereventtype = evt;
		this.header.addEvent(evt, this.headerHandler);
	},
	setParent : function(parent)
	{
		if(instanceOf(parent, TabCtrl))
		{
			if(instanceOf(this.parent, TabCtrl))
				this.parent.removePage(this);
			parent.addPage(this);
		}
		return this;
	},
	activate : function()
	{
		if(typeOf(this.header) == 'element' && typeOf(this.body) == 'element')
		{
			this.header.addClass('active');
			this.body.removeClass('hidden').addClass('detail');
		}
		this.status = "active";
	},
	deactivate : function()
	{
		if(typeOf(this.header) == 'element' && typeOf(this.body) == 'element')
		{
			this.header.removeClass('active');
			this.body.removeClass('detail').addClass('hidden');
		}
		this.status = "deactive";
	},
	isActivated : function()
	{
		return this.status == "active";
	},
	_defaultHeaderBehaviroHandler : function(e)
	{
		e.stop();
		if(instanceOf(this.parent, TabCtrl))
			this.parent.activate(this);
	},
	//clear etc.  called by its parent, not call it anywhere yourself.
	destroy : function()
	{
		if(typeOf(this.header) == "element" && this.headereventtype != null)
			this.header.removeEvent(this.headereventtype, this.headerHandler);
	}
});
TabPage.Behavior_CLICK = 'click';
TabPage.Behavior_DBLCLICK = 'dblclick';
TabPage.Behavior_MOUSEOVER = 'mouseenter';
TabPage.Behavior_MOUSEOUT = 'mouseleave';
