var autoRotateTimer = false;
var autoRotateDelay = 10000;
function autoRotate() {
	if (autoRotateTimer) clearTimeout(autoRotateTimer);
	var tabs = $$('#feature .tab-content.selected .news-item-select a.selected');
	if (tabs.length <= 0) return;
	
	var next = tabs[0].next()
	if (!next) {
		next = $$('#feature .tab-content.selected .news-item-select a')[0];
	}
	if (next) fireEvent(next, 'click');
	
	
	autoRotateTimer = setTimeout(autoRotate, autoRotateDelay);
}
function fireEvent(element, e){
    if (document.createEventObject){
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+e,evt)
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(e, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}

Event.onReady(function() {
	var tabs = $$('#feature .tabs li a').each(function(obj,i) {
		var tab = obj.up(); // should be $('tab-nav-'+i)
		Event.observe(obj, 'click', function(event) {
			if (Element.hasClassName(tab, 'selected')) {
				Event.stop(event);
				return false;
			}
			$$('#feature .tabs li').each(function(elem) {Element.removeClassName(elem, 'selected');});
			Element.addClassName(tab, 'selected');
			
			$$('#feature .tab-content').each(function(elem) {Element.removeClassName(elem, 'selected');});
			$('tab-'+i).addClassName('selected').hide();
			Effect.Appear($('tab-'+i), {duration:0.3});
			Event.stop(event);
			
			if (autoRotateTimer) clearTimeout(autoRotateTimer);
			autoRotateTimer = setTimeout(autoRotate, autoRotateDelay);
			return false;		
		});
	});
	
}); //onReady
Event.onReady(function() {
	var featureTabs = $$('#feature .tab-content');
	featureTabs.each(function(tabobj,tabID) {
		tabobj.id = 'tab-'+tabID;
		var selected = 0;
		var newsItems = Element.select(tabobj, '.news-item');
		var newsItemsNav = document.createElement('div');
		newsItemsNav.className = 'news-item-select';
		//for (var i=0,item;item=newsItems[i];i++) {
		newsItems.each(function(item,i) {
			item.id = 'tab-'+tabID+'-'+i;
			var sel = document.createElement('a');
			sel.id = 'sel-'+tabID+'-'+i;
			sel.href='#'+item.id;
			sel.innerHTML = (i+1);
			if (i==selected) {
				Element.addClassName(sel, 'selected');
				Element.addClassName(item, 'selected');
			}
			Event.observe(sel, 'click', function(event) {
				if (selected == i) return false;
				
				Element.removeClassName($('sel-'+tabID+'-'+selected), 'selected');
				Element.addClassName($('sel-'+tabID+'-'+i), 'selected');
				Element.removeClassName($('tab-'+tabID+'-'+selected), 'selected').hide();
				Element.addClassName($('tab-'+tabID+'-'+i), 'selected').hide();

				Effect.Appear($('tab-'+tabID+'-'+i), {duration:0.3})
				selected=i
				Event.stop(event);
				if (autoRotateTimer) clearTimeout(autoRotateTimer);
				return false;
			});
			newsItemsNav.appendChild(sel);
		})
		if (newsItems.length > 0) tabobj.appendChild(newsItemsNav);
	});
	
	if (autoRotateTimer) clearTimeout(autoRotateTimer);
	autoRotateTimer = setTimeout(autoRotate, autoRotateDelay);
	
});