Ext.namespace('Altius');


Altius.Tabset = Ext.extend(Ext.Component,{
	activeTab: 0,
	initialTab: null,
	tabs:[],
	bodies:[],
	historyToken:null,
	history: false,
	initComponent: function() {

		var name = Ext.fly(this.renderTo).getAttribute('name')||'';
		this.history = Ext.fly(this.renderTo).getAttribute('history');
//		alert(this.history);
		if(this.history)
			this.historyToken=Ext.History.RegisterComponent(name);

//		console.log('historytoken',this.historyToken);


		Altius.Tabset.superclass.initComponent.call(this);
	},
	render: function(ts) {

		this.el = ts=Ext.get(ts);

		ts.replaceClass('tabSet','tabSetLive');
		ts.setStyle('display','block');
		var nav = ts.createChild({tag: 'ul', cls:'tabNav'},ts.first());
		var tabs = ts.select('> div.tabContent');

		this.tabs={};				// These initializer are required to ensure that the array literals are reset.
		this.bodies={};

		tabs.each(function(tab,c,index) {
			var header = tab.first('h3');
			var name;
			if (header)	{

				var li = nav.createChild({tag: 'li', html: String.format('<a href="javascript:void(null);">{0}</a>',header.dom.innerHTML)});
				name = header.getAttribute('name') || index;
				console.log('name',name);

//				var li = nav.createChild({tag: 'li', html: header.dom.innerHTML});
				header.remove();
				this.tabs[name]=li.dom;
				this.bodies[name]=tab.dom;

				li.on('click', this.tabClick,this,{index: name});
				if(this.activeTab==index)
					this.setActive(name);

			} 
		},this);
		console.log('hello');
//		TJ.ExpandColumns({heights:true});
//		this.setActive(this.activeTab);			// do not set with history.

		if(this.history) {
			Ext.History.on('change', this.historyChange,this);
			this.historyChange(Ext.History.getToken());
		}

	},
	tabClick: function(e,el,obj) {
		this.setActive(obj.index,true);
//		console.log('cmp',Ext.getCmp(this.id), this.id, Ext.ComponentMgr.all);

	},
	setActive: function(tab,hist) {
//		console.log('active',tab);
// this ends up being called twice because the Ext.History.add causes the historyChange event to trigger as well.  Doesn't make sense!!!!
		if(this.tabs[tab]) {
			if(this.initialTab===null)
				this.initialTab=tab;
			Ext.fly(this.tabs[tab]).radioClass('tabActive');
			Ext.fly(this.bodies[tab]).radioClass('tabActive');
//			if(this.tabs[activeTab])
//			Ext.fly(this.bodies[tab]).fadeIn();
			if(this.activeTab !=tab){  // new tab!!
				TJ.ExpandColumns({heights:true});
				this.activeTab=tab;
				console.log('active',this.tabs[tab]);
				if(hist && this.history) {
					Ext.History.AltiusAdd(this.historyToken,tab);
//					Ext.History.add(this.historyToken + ':' + tab);

				}
			}
		}
	},
	historyChange: function(token) {
//		console.log('history',token,this);
		if(token === null) {
//			console.log('init',this.initialTab);
			this.setActive(this.initialTab);
			return;
		}
		var parts = token.split(':');
		if(parts.length==1)
			parts.unshift('');
		
		if( this.historyToken==parts[0]) {
			this.setActive(parts[1]);
		}
	}


});


TJ.Twitter.FeedPreviewer = Ext.extend(Ext.Component,{
	refreshInterval: 5000,
   initComponent : function(){
        TJ.Twitter.FeedPreview.superclass.initComponent.call(this);
		return;
		/*
        this.addEvents("update");
		this.callbackName = 'TwitterCallback_' + this.conf.id;

		console.log(this.callbackName);
		window[this.callbackName] = function(data) {
			console.log('hello');
			Yourfunction(this,data);


		};
	*/

    },
    onRender: function() {
        TJ.Twitter.FeedPreview.superclass.onRender.apply(this, arguments);
		console.log('TJ.Twitter.Feedpreview',this,this.container,this.getEl());
		this.list = this.container.createChild({cls:'content',tag:'ol'});
		if(this.data)
			this.doPaint(this.data);
		
		this.task = {
			run: function() {
//				console.log('dorun');
//				TJ.LoadScript(String.format('http://twitter.com/status/user_timeline/{0}.json?count={2}&callback={1}',this.conf.userid,this.callbackName,this.conf.count));
//				console.log(String.format('http://twitter.com/status/user_timeline/{0}.json?count={2}&callback={1}',this.conf.userid,this.callbackName,this.conf.count));
			},
			scope: this,
			interval: 20000
		};

//		Ext.TaskMgr.start(this.task);

    },
	doPaint: function(data) {
		for(var i =0 ; i < data.length ; i++){
			var li = this.list.createChild({tag:'li'});
			li.createChild({tag:'p',style:"word-wrap:break-word;"} ).update(data[i].text.parseURL().parseUsername().parseHashtag());
			li.createChild({tag:'p',style:"word-wrap:break-word;", cls:"byline", title:data[i].created_at}).update(TJ.RelativeDate(data[i].created_at));
		}
	},
	TwitterCallback: function(obj){
		console.log('Update Twitter',obj,this);


	}
});

