/*
	gRSSTab
	display RSS Ajax with tab
	design by http://www.goragod.com (goragod wiriya)
	3-9-52
*/

gRSSTab = GClass.create();
gRSSTab.prototype = {
	initialize: function(tab, div, interval, loadingClass){
		this.tab = $E(tab);
		this.tab.innerHTML = '';
		this.div = $E(div);
		this.div.innerHTML = '';
		this.div.style.position = 'relative';
		this.options = new Array();
		this.feeds = new Array();
		this.texts = new Array();
		this.loadingClass = loadingClass || 'rsstab_loading';
		this.selectIndex = -1;
		this.interval = 1000 * (interval || 0);
		this.ajax = new GAjax;
	},

	add: function(feedurl, text, options){
		this.feeds.push(feedurl);
		this.texts.push(text);
		var _options = {
			rows: 2,
			cols: 2,
			imageWidth: 75,
			imageHeight: 75,
			className: 'table_rsstab_class',
			reader: 'widgets/rss/reader.php',
			topiclen: 20,
			detaillen: 90
		};
		Object.extend(_options,options || { });
		this.options.push(_options);
	},

	show: function(id){
		var ul = document.createElement('ul');
		this.tab.appendChild(ul);
		var temp = this;
		var tab = this.tab.id + '_';
		forEach(this.feeds, function(item,index){
			var li = document.createElement('li');
			ul.appendChild(li);
			var a = document.createElement('a');
			a.innerHTML = temp.texts[index];
			a.href = '#' + index;
			a.id = tab + index;
			li.appendChild(a);
			a.onclick = function(){
				temp.interval = 0;
				var ids = this.href.split('#');
				temp.select(parseFloat(ids[1]));
				return false;
			};
		});
		var li = document.createElement('li');
		ul.appendChild(li);
		li.className = this.loadingClass;
		li.style.display = 'none';
		this.loading = li;
		this.select(id);
	},

	select: function(id){
		this._showLoading();
		var temp = this;
		this.ajax.send(this.options[id].reader, this._query(id), function(xhr){
			temp._hideLoading();
			temp.div.innerHTML = xhr.responseText;
			temp._setselect(id);
			if(temp.interval > 0){
				window.setTimeout(function(){
					temp.selectIndex++;
					if(temp.selectIndex >= temp.feeds.length){
						temp.selectIndex = 0;
					};
					temp.select(temp.selectIndex);
				},temp.interval);
			};
		});
	},

	_showLoading: function(){
		this.loading.style.display = '';
	},

	_hideLoading: function(){
		this.loading.style.display = 'none';
	},

	_query: function(id){
		var query = 'url=' + encodeURIComponent(this.feeds[id]);
		query += '&rows=' + this.options[id].rows;
		query += '&cols=' + this.options[id].cols;
		query += '&imageWidth=' + this.options[id].imageWidth;
		query += '&imageHeight=' + this.options[id].imageHeight;
		query += '&className=' + this.options[id].className;
		query += '&topiclen=' + this.options[id].topiclen;
		query += '&detaillen=' + this.options[id].detaillen;
		return query;
	},

	_setselect: function(id){
		this.selectIndex = id;
		var tab = this.tab.id + '_';
		var as = this.tab.getElementsByTagName('a');
		var flag = '';
		forEach(as, function(item, index){
			if(index == 0){
				flag = ' first';
			}else if(index < as.length - 1){
				flag = '';
			}else{
				flag = ' last';
			};
			if(item.id == tab + id){
				item.className = 'select' + flag;
			}else{
				item.className = '';
			};
		});
	}
};