var rotateTimeout=false;

function vbpri_register_rotating_control (object_id)
{
	var object = new YAHOO.widget.TabView(object_id, {orientation:'bottom'});
    
    object.set('activeIndex', 0);
    
    object.contentTransition=vbpri_transition;
    
	var i=0;
	while (true)
	{
		var tab=object.getTab(i);
		if(!tab)
		{
			break;
		}
		
		tab.addListener('mouseover', vbpri_select_tab, object);
		i++;
	}
	
	rotateTimeout=window.setInterval(function(){vbpri_rotate_image(object)}, vbpri_rotate_timeout);
}

function vbpri_register_video_control (object_id)
{	
	var object = new YAHOO.widget.TabView(object_id, {orientation:'bottom'});
	
	object.set('activeIndex', 0);
}

function vbpri_rotate_image(tabcontrol)
{
	var activeIndex=tabcontrol.get('activeIndex');
	
	var tabs=tabcontrol.get('tabs');
	while(true)
	{
		activeIndex++;
		try {
			var tabid=tabs[activeIndex].get('contentEl').id;
		} catch (e)	{
			tabcontrol.set('activeIndex', 0);
			return;
		}
		var is_video_tab=false;
		if(is_video_tab==false)
		{
			break;
		}
	}
	
	try {
		tabcontrol.set('activeIndex', activeIndex);
	}
	catch(e) {
		tabcontrol.set('activeIndex', 0);
	}
}

function vbpri_select_tab(evt, object)
{
	if(rotateTimeout)
	{
		clearTimeout(rotateTimeout);
		rotateTimeout=false;
	}
	var tabs=object.get('tabs');
	var selectedIndex=object.get('activeIndex');
	var activeTab=tabs[selectedIndex];
	var elemId=activeTab.get('contentEl').id;
	
	var is_video_tab=PHP.match_all(elemId, 'video_tab([0-9]+)');
	if(is_video_tab)
	{
		return false;
	}
	
	var index=object.getTabIndex(this);
	object.set('activeIndex', index);
}

function vbpri_transition(newTab, oldTab) 
{
	if (oldTab) {
    	var oldTab_attributes = {opacity:{from:1, to:0}};
    	var oldTab_anim = new YAHOO.util.Anim(
    			oldTab.getAttributeConfig("contentEl").value, 
    			oldTab_attributes,  
                0.2,
                YAHOO.util.Easing.easeOut
            );
    	oldTab_anim.onComplete.subscribe(vbpri_apply_transition, {tab:oldTab, visible:false});
    	oldTab_anim.animate();
    }
	if (newTab) {
    	var newTab_attributes = {opacity:{from:0, to:1}};
    	var newTab_anim = new YAHOO.util.Anim(
    			newTab.getAttributeConfig("contentEl").value, 
    			newTab_attributes,  
                0.2,
                YAHOO.util.Easing.easeOut
            );
    	newTab.set('contentVisible', true);
    	newTab_anim.onComplete.subscribe(vbpri_apply_transition, {tab:newTab, visible:true});
    	newTab_anim.animate();
    }
}

function vbpri_apply_transition(evt, anim_info, tab_info)
{
	if (tab_info.tab) {
		tab_info.tab.set('contentVisible', tab_info.visible);
    }
}
