//This needs to be turned into an Object

var current_tab = -1;//set our current tab
//function used to start ourtab cycler
function info_nav_init(){
	new PeriodicalExecuter(function(r){if(current_tab != -1){ info_nav_select($$('img.tab_select')[current_tab]); current_tab++;current_tab = current_tab > ($$('img.tab_select').length-1) ? 0 : current_tab;}else{r.stop(); }}, 3);
}
//stop cycling through
function stopCycle(){
	//current_tab=
	current_tab = -1;
}
//choose a particular tab
function info_nav_select(e){
	if( $(e.id) ){
		var i = 1; while( $('info_nav_seperator_'+i) ){ $('info_nav_seperator_'+i).src ="/web_images/home/main_ad_menu_sep_line.gif"; i++};
		$$('img.tab_select').each( function (x,index){ 
			if(x.id == e.id){
				x.src = x.src.replace(/_off/,'_on');
				x.src = x.src.replace(/_hover/,'_on');
				
				if( $('info_nav_seperator_'+ (index + 1 ) ) ){
					$('info_nav_seperator_'+ (index + 1 )).src = "/web_images/home/main_ad_menu_shadow_left.gif"; 
				}else if( $('info_nav_seperator_'+ index) ){ $('info_nav_seperator_'+index).src = "/web_images/home/main_ad_menu_shadow_right.gif"; }
				var idx = index;
				$('home_animation_items').childElements().each(function(c,index){ if(idx==index){ c.show() }else{ c.hide()} });
				
				adjustBG();
				//$('home_animation').childElements()[index].show();
			}else{
				x.src = x.src.replace(/_on/,'_off');
			}
		});
	}
}
//Mouseover function -> no hover if current state is 'on'
function info_nav_hover(e){
	if( $(e.id) ){
		$$('img.tab_select').each( function (x,index){ if(x.id == e.id){ x.src = x.src.replace(/_off/,'_hover'); } });
	}
}
//Mouseout function
function info_nav_leave(e){
	if( $(e.id) ){
		$$('img.tab_select').each( function (x,index){ 
			var p = current_tab - 1; if( p == -1 ){ p = 4;}
			//Flip the state to off unless it is the item that is currently selected
			if(x.id == e.id){ x.src = x.src.replace(/_hover/,(p != index ? '_off' : '_on')); }				
		});
	}
}
//info_nav_init();//start cycling through our tabs->no longer being used

/*
//OBJECT Work In Progress(WIP)
var BoxTabNavigator  = Class.create();

BoxTabNavigator.prototype = {
	 curTab:0
	,tabClass:"tab_select"
	,seperators:true
	,seperatorImages:{root:"main_ad_menu_",left:"shadow_left.gif",right:"shadow_right.gif",std:"sep_line.gif"}
	,contentId:"home_animation_items"
	,actionImageSufx:{on:'on',off:'off',hover:'hover'}
};
*/

//inside selector
function rotateNav(e){
	if(navigator.userAgent.match('MSIE')){ var e = $(e) }//IE fix
	e.siblings().each(function(c){	var i = c.down().src; c.down().src = i.replace('_on.png','_off.png'); });//remove all the ON state of our elements siblings
	e.down().src = e.down().src.replace('_off.png','_on.png');//set our ON state
	var n = e.previousSiblings().length;//find the position of our element within its siblings
	e.up().nextSiblings().each(function(divs,index){ if(n==index){ divs.show();}else{divs.hide();} });//hide and show the corect div
	adjustBG();

}
/*
Adjusts the background image of our container
*/
function adjustBG(){
	//var path = "/web_images/home/";
	//var bgimg = new Template("#{path}main_ad_#{section}.jpg");
	var e = $('main_ad_bg');
	var r = new RegExp('(\\d+)-(\\d+)');	
	var i=1;var j=1;
	var items = $('home_animation_items').down();
	while(!items.visible()){items =	items.next(); i++;}
	items = items.down().down().next();
	if(items.down()){
		items = items.down();
		while(!(items.down().src.match('_on.png'))){ items = items.next(); j++; }
	}
	var m = e.style.background.match(r);
	if(m){ e.style.background = e.style.background.replace(r,i+"-"+j);}
	
}