/*------------------------------------------------------------------*
 controls anchors with rel attribute of this.relAttributeOfTabs.
 on mouse over each tab moves from its starting position to 
 this.openPositionX , this.openPositionY and the remaining tabs
 return to their starting position.
 Anchor tags cannot have '#' in the href. Must be '#name' or link.
 Thomas Elmore
*------------------------------------------------------------------*/
var TabController = function(){
	
	// edit if you change the rel attribute that you want the script to control
	this.relAttributeOfTabs = 'bedfont_tabs';
	
	// position when open (relative to start point)
	this.openPositionX = 0;
	this.openPositionY = -145;
	
	// more may ensure all tabs are returned but may also slow down action. 1 seems to work fine
	this.allowableNumberOfLoops = 1;
	
	
	this.allTabs = new Array();
	this.allEffects = new Array();
	
	this.restingPositions = new Array();
	
	this.tabWithFocus = null;
	
	this.loopCount = 0;
	
	this._innit_();
	
};
TabController.prototype._innit_ = function(){

	//Detect IE5.5+
	version = 0;
	
	var is_ie = false;
	
	if(navigator.appVersion.indexOf("MSIE") != -1 ) {
	
		temp=navigator.appVersion.split("MSIE");
	
		version=parseFloat(temp[1]);
	
	}

	if( version >= 5.5 ) is_ie = true;

	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName('a');

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
			
		var relAttribute = String(anchor.getAttribute('rel'));
			
		// use the string.match() method to catch 'gallery' references in the rel attribute
		if ( relAttribute.toLowerCase().match( this.relAttributeOfTabs ) ) {
				
			if( is_ie == true )	anchor.style.marginTop = "13em";
			else anchor.style.marginTop = "15em";
				
			this.attachEvent( anchor, this.updatePositions );
				
			this.recordStartingPosition( anchor );
			
			anchor.setAttribute( "status", "closed" );
			
			// add to array for use in closing all tabs
			this.allTabs.push( anchor );
			
			this.allEffects[ anchor ] = null;
			
		}
	}
	
};
TabController.prototype.updatePositions = function( element ){
	
	// set property so that tabWithFocus is always the most recently 'moused' element
	this.tabWithFocus = element;
	
	// menu tabs have 4 states which define the action upon mouse over:
	//
	// correct action already in progress or complete. make sure all others are closed or closing
	if( element.getAttribute( "status" ) == "moving_up" || element.getAttribute( "status" ) == "open" ){ 
		
		this.closeAllOtherTabs();
	
	// element moving wrong way. stop it, start the correct movement and make sure all others are closed or closing
	} else if( element.getAttribute( "status" ) == "moving_down" ) {
		
		this.stopMovement( element );
		this.openTab( element );
		this.closeAllOtherTabs();
	
	// element stationary in closed position. open it and make sure all others are closed or closing
	} else if( element.getAttribute( "status" ) == "closed" ) {
		
		this.openTab( element );
		this.closeAllOtherTabs();
	
	}
	
};
TabController.prototype.attachEvent = function( element, action ){
	
	// bind and attach mouse over event so that a method can be used
	var onMouseOver = action.bind( this );
	element.onmouseover = function(){
		onMouseOver( this );
	};
	
};
// make array of all starting points so can return to exact spot
TabController.prototype.recordStartingPosition = function( element ){
	
	var newRecord = new Array();
	
	newRecord.top = parseFloat(element.style.top || '0');
	newRecord.left = parseFloat(element.style.left || '0');
	
	this.restingPositions[ element ] =  newRecord;

};
TabController.prototype.moveToPosition = function( element, newTop, newLeft, callback ){
	
	// get current position of element. if moving a half open element this could be any value
	var currentTop = parseFloat(element.style.top || '0');
	var currentLeft = parseFloat(element.style.left || '0');
	
	// calculate amount of movement needed to get from current position to desired position
	var movementTop = newTop - currentTop;
	var movementLeft = newLeft - currentLeft;

	// bind the call back method. create move effect and assign effect to the lookup table against the element.
	var bindedCallback = callback.bind( this );
	this.allEffects[ element ] = null;
	this.allEffects[ element ] = new Effect.Move( element, { x: movementLeft, y: movementTop, afterFinish: bindedCallback  } );

};
TabController.prototype.openTab = function( element ){
	
	element.setAttribute( "status", "moving_up" );
	
	this.moveToPosition( element, this.openPositionY, this.openPositionX, this.markAsOpen );
	
};
// closing the tab simply means returning it to its resting position (noted in initialisation)
TabController.prototype.closeTab = function( element ){
	
	element.setAttribute( "status", "moving_down" );
	
	var restTop = this.restingPositions[element].top;
	var restLeft = this.restingPositions[element].left;
	
	this.moveToPosition( element, restTop, restLeft, this.markAsClosed );
	
};
TabController.prototype.stopMovement = function( element ){
	
	// find and stop any active effect pertaining to element
	if( this.allEffects[ element ] != null ){
		
		this.allEffects[ element ].cancel();
		this.allEffects[ element ] = null;
	
	}
	
};
TabController.prototype.markAsOpen = function( obj ){

	this.stopMovement( obj.element );
	obj.element.setAttribute( "status", "open" );
	
};
TabController.prototype.markAsClosed = function( obj ){
	
	this.stopMovement( obj.element );
	obj.element.setAttribute( "status", "closed" );
	
};
// close any tab that isnt the tabWithFocus and isnt already closing or closed. Uses the global 
// property of last 'moused' tab so always closes correct tabs even if the actual call to the 
// method is out of date. Can quickly build up lots of loops so monitor the number of loops going on
//  and only add a new one if there are few enough
TabController.prototype.closeAllOtherTabs = function(){
	
	this.loopCount++;
	if( this.loopCount <= this.allowableNumberOfLoops ){
	
		for( var i=0; i<this.allTabs.length; i++ ){
			var element = this.allTabs[i];
			
			if( element != this.tabWithFocus && element.getAttribute( "status" ) != "closed" && element.getAttribute( "status" ) != "moving_down" ){
				
				this.stopMovement( element );
				this.closeTab( element );
			}
			
		}
	
	}
	this.loopCount--;
	
};
