/*
######################################################################################
######                                                                        #######
######     LAYOUT / POSITION SCRIPTS                                         #######
######                                                                      #######
##################################################################################
*/

/********************************************************************************/
/***** FULL WINDOW DIMENSIONS AND VALUES ***************************************/
/******************************************************************************/

	var getWindowWidth = 	function() { return window.offsetWidth || document.documentElement.clientWidth; };
	var getWindowHeight = 	function() { return window.offsetHeight || document.documentElement.clientHeight; };
	var getTotalWidth = 	function() { return (window.innerWidth + window.scrollMaxX) || document.body.clientWidth; };
	var getTotalHeight = 	function() { return (window.innerHeight + window.scrollMaxY) || document.body.clientHeight; };
	var getScrollWidth = 	function() { return getTotalWidth() - getWindowWidth(); };
	var getScrollHeight =  	function() { return getTotalHeight() - getWindowHeight(); };
	var getScrollTop = 		function() { return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; };
	var getScrollLeft = 	function() { return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft; };


/********************************************************************************/
/***** FULL WINDOW DIMENSIONS AND VALUES ***************************************/
/******************************************************************************/
	
	var setScrollTop = function(value) {
		var y = value;
		var x = getScrollTop();
		window.scrollTo(x,y);
	};
	var setScrollLeft = function(value) {
		var y = getScrollLeft();
		var x = value;
		window.scrollTo(x,y);
	};
	var setScroll = function(x,y) {
		window.scrollTo(x,y);
		return true;
	};


/********************************************************************************/
/***** ELEMENT OFFSET DIMENSIONS ***********************************************/
/******************************************************************************/

	var getOffsetWidth = function(obj) {
		return obj.offsetWidth;
	};
	var getOffsetHeight = function(obj) {
		return obj.offsetHeight;
	};
	

/********************************************************************************/
/***** GET ALL MOUSE COORDINATES ***********************************************/
/******************************************************************************/

/***** MOUSE POSITION WITHIN A WINDOW *****/

	var getMousePositionX = function(evt) {
		return Event.pointerX(evt);
	}
	var getMousePositionY = function(evt) {
		return Event.pointerY(evt);
	}

/***** MOUSE POSITION WITHIN AN ELEMENT ****/

	var getMouseOffsetX = function(obj) {
		var mousePos = getMouseOffset(obj);
		return mousePos.x
	};
	var getMouseOffsetY = function(obj) {
		var mousePos = getMouseOffset(obj);
		return mousePos.y
	};
	var getMouseOffset = function(obj) {
		var posX = obj.offsetLeft;
		var posY = obj.offsetTop;
		while(obj.offsetParent) {
			if(obj == document.getElementsByTagName('body')[0]) {
				break
			} else {
				posX	= posX + obj.offsetParent.offsetLeft;
				posY	= posY + obj.offsetParent.offsetTop;
				obj		= obj.offsetParent;
			}
		}
		var mouseOffset = {
			x: posX,
			y: posY
		};
		return mouseOffset;
	}



/*
######################################################################################
######                                                                        #######
######     GENERIC SCRIPTS			                                         #######
######                                                                      #######
##################################################################################
*/

	var Generic = new Object();

/********************************************************************************/
/***** CONTENT SUPPORT FUNCTIONS ***********************************************/
/******************************************************************************/
	
	var genericTransition 		= 'off';	// Records the state of the transition. On = running, off = ready
	var genericLinkElement;					// Records the element that was clicked to call the action. Used to reset INPUTs
	var switcherCloser			= 'true';	// Decides if an active DIV can be closed using the Generic.SwitcherCloser() function
	
	Generic.Core = {

/***** HUB FOR THE TRANSITIONS *****/

		Switcher: function(Opener,Closer,Container) {	
		// Get all of the DIVs styles set for the transition
			$(Container).setStyle({
				height: $(Closer).getHeight() + 'px', 
				width: $(Closer).getWidth() + 'px', 
				position: 'relative', 
				overflow: 'hidden'
			});
			Generic.Core.Layout(Opener,Closer);
		
		// Specify transition effects
			var transition1 = new Effect.Fade($(Closer), { sync: true });
			var transition2 = new Effect.Appear($(Opener), { sync: true });
			var transition3 = new Effect.Morph($(Container), { style: { height: $(Opener).getHeight() + 'px' }, sync: true })
		
		// If the opening and closing DIVs have the same height, DON'T use the morph effect (var transition3)
			if($(Opener).getHeight() != $(Closer).getHeight()) {
				var effectList	= [transition1,transition2,transition3];
			} else {
				var effectList	= [transition1,transition2];
			}

		// The content transition itself (fade out the active DIV, fade in the next)
			new Effect.Parallel(effectList, { duration: 0.7,  beforeStart: function() {
					genericTransition = 'on'
				}, afterFinish: function() {
					genericTransition = 'off';
					Generic.inputReset();
					switcherCloser = 'true';

				// Change the DIVs back to normal positioning. Set the container height to auto for closing
					$(Opener).style.position 	= '';
					$(Closer).style.position 	= '';
					$(Container).style.overflow = 'visible';
					$(Container).style.height 	= 'auto';
				}
			})
		},

/***** DOM CHANGER FOR ALL DIVS IN PLAY *****/

		Layout: function(Opener,Closer) {
		// Prepare the Closing DIV
			$(Closer).setStyle({ position: 'absolute', left: '0', top: '0' });

		// Prepare the Opening DIV
			$(Opener).show();
			$(Opener).setStyle({
				visibility: 'hidden',
				position: 	'absolute',
				left: 		'0',
				top: 		'0',
				visibility: 'visible'
			})
		
		// Re-hide the opening DIV for the transition
			$(Opener).hide();
		},

/***** OPEN THE DIV FOR THE FIRST TIME *****/

		Open: function(divElement) {
		// Specify transition effects
			var transition1 = new Effect.BlindDown($(divElement), { sync: true });
			var transition2 = new Effect.Appear($(divElement), { sync: true });
			var effectList	= [transition1,transition2];
		
		// Opening transition
			new Effect.Parallel(effectList, { duration: 0.7, beforeStart: function() {
					genericTransition = 'on'
				}, afterFinish: function() {
					genericTransition = 'off';
					Generic.inputReset();
					switcherCloser = 'true';
				}
			})
		},

/***** CLOSE THE DIV AND RESET VARIABLES *****/

		Close: function(divElement) {
		// Specify transition effects
			var transition1 = new Effect.Fade($(divElement), { sync: true });
			var transition2 = new Effect.BlindUp($(divElement), { sync: true });
			var effectList	= [transition1,transition2];

		// Closing transition
			new Effect.Parallel(effectList, { duration: 0.7, beforeStart: function() {
					genericTransition = 'on'
				}, afterFinish: function() {
					genericTransition = 'off';
					Generic.inputReset();
					switcherCloser = 'true';
				}
			});
		}
	}



/*
######################################################################################
######                                                                        #######
######     GENERIC LINK SCRIPTS	                                             #######
######                                                                      #######
##################################################################################
*/

/********************************************************************************/
/***** ACTIVE LINK MANAGER *****************************************************/
/******************************************************************************/

/*
This function will allow you to manage active links in a menu. It can be run alongside virtually any
other script when specified in an event handler.
It works by targetting to container and seeing how many links are within it. It then removes the active
class from all before assigning the active state to the link that was clicked.

Has been scripted to support A tags and INPUTS as activators.
*/

	Generic.activeLinks = function(linkElement) {
		linkElement 		= $(linkElement);
		genericLinkElement 	= linkElement;
		
	// Identify activator type
		switch(linkElement.tagName) {
			case 'INPUT':
				switch(linkElement.type) {
					case 'checkbox':
						elementType = 'checkbox';
					break;
					case 'radio':
						elementType = 'radio';
					break;
				}
			break;
			case 'A':
				elementType = 'link';
			break;
		}

	// Switch between the element types
		switch(elementType) {
			case 'checkbox':
			case 'radio':
			// Disable clickability when the transition is running
				if(genericTransition == 'on') {
					linkElement.disabled = true;
				} else {
					linkElement.disabled = false;
				}
			break;
			case 'link':
				if(linkElement.up().tagName == 'LI') {
					var Container = linkElement.up().up();
				} else {
					var Container = linkElement.up();
				}
				var linkList = $(Container).getElementsByTagName('a');
			
			
			// If the link is alone it should switch between active and inactive onclick
				if(linkList.length == 1) {
					if(linkElement.className == 'active') {
						linkElement.className = '';
					} else {
						linkElement.className = 'active';
					}
				} else {
					if(linkElement.className != 'active') {
						for(var i = 0; i < linkList.length; i++) {
							linkList[i].className = '';
						}
						linkElement.className = 'active';

					// if Generic.SwitcherCloser() is being run, make sure all links finish inactive
						if(switcherCloser == 'true') {
							if(linkElement.className == 'active') {
								linkElement.className = '';
							}
						}
					}
				}
			break;
		}
	}


/***** INPUT RESET *****/

/*
If the activator is a CHECKBOX or RADIO the above script will disable them so they
cant be clicked over and over during the transition.
This script reenables the button.
It is called once the transition has completed
*/

	Generic.inputReset = function() {
		if(genericLinkElement) {
			if(genericLinkElement.tagName == 'INPUT') {
				switch(genericLinkElement.type) {
					case 'checkbox':
					case 'radio':
						genericLinkElement.disabled = false;
					break;
				}
			}
			genericLinkElement = '';
		}
	}



/*
######################################################################################
######                                                                        #######
######     GENERIC CONTENT SWITCHING SCRIPTS	                             #######
######                                                                      #######
##################################################################################
*/

/********************************************************************************/
/***** CONTENT SWITCHING FUNCTIONS *********************************************/
/******************************************************************************/

/***** CONTENT SWITCHER WHICH ALLOWS THE ACTIVE DIV TO BE CLOSED *****/

/*
This function will allow you to switch between different DIVs that are associated through
a specified containing DIV. This script also allows you to close the active DIV when actioned.
A variation of this script, Generic.SwitcherOnly(), functions in the same way but will prevent the
active DIV from closing when actioned.
*/

	Generic.SwitcherCloser = function(divElement) {
		
		if(genericTransition == 'off') {
			genericTransition = 'on';
			var Container = $(divElement).up();
			var ContainerChildren = $(Container).childElements();
			var status = '';
			for(var i = 0; i < ContainerChildren.length; i++) {

				if(ContainerChildren[i].className == 'div_cell') {
					if(ContainerChildren[i].style.display != 'none') {
						var Closer		= ContainerChildren[i];
						if(ContainerChildren[i] == $(divElement)) {
							if(switcherCloser == 'true') {
								status = 'closing';
							} else {
								genericTransition = 'off';
							}
						} else {
							var divActive = ContainerChildren[i];
							status = 'switching';	
						}
						break;
					} else {
						if(switcherCloser == 'true') {
							status = 'opening';
						}
					}
				}
			}
			switch(status) {
				case 'opening':
					Generic.Core.Open(divElement);
				break;
				case 'closing':
					Generic.Core.Close(divElement);
				break;
				case 'switching':
					Opener = divElement;
					Closer = divActive;
					Generic.Core.Switcher(Opener,Closer,Container);
				break;
			}
		}
	}
	

/***** CONTENT SWITCHER WHICH DOESNT CLOSE THE ACTIVE DIV *****/

/*
Identical to the Generic.SwitcherCloser() function. The only difference is this
function will not allow the active DIV to be closed when actioned again
*/

	Generic.SwitcherOnly = function(divElement) {
		switcherCloser = 'false';
		Generic.SwitcherCloser(divElement);
	}


/********************************************************************************/
/***** SIMPLE OPENER / CLOSER **************************************************/
/******************************************************************************/

/*
This script will allow you to open and close a single DIV and its contents. It isnt
designed for multiple DIV content.
*/

	Generic.OpenerCloser = function(divElement) {

		if(genericTransition == 'off') {
			genericTransition = 'on';

			divElement	= $(divElement);
			var elementType;

		// Set the activator and div states
			if(divElement.style.display == 'none') {
				Generic.Core.Open(divElement);
			} else {
				Generic.Core.Close(divElement);
			}
		}
	}


