/*************************************************************************
* classGeneral.js
* A collection of small classes used throughout the site.  Too small to have it's
* own file.
* Usage:
*
* Dependencies: 		Mootools 1.2.1 Core
*
*
* v.1 (11/16/2008) 		- 	Initial release of matchDim
* v.1.1 (12/16/2008) 	-	Initial release of verticalCenter
*
*
* PLEASE KEEP CLASS NAMES IN ALPHABETICAL ORDER
*	-infoHover
*	-matchDim
*	-verticalCenter
*
*
* Class instantiations at the bottom!
**************************************************************************/


/**************************************************************************
*  infoHover
*  @param: 		targetParent (class name for the target parent)
*
*  Description:	Will use element delegation to add an event to the parent
*				of target element, event to each el will be added on the fly
*				to create the hover box effect
*
*  Structure: <div id="targetParent">
*					<div class="customClass box_target">
*						<div class="customClass inactive">
*					</div>
*				</div>
*
*  Dependencies: classElDelegation.js, global-functions.js (parent sniffers)
*
*  To Do:		Generalize this more el = this.getChildren()[1];
*
*  Author: 		Tri Doan
*
***************************************************************************/
var infoHover = new Class({
	Implements: [Options, Events],

	options: {
		targetParent: 'hoverParent'
	},
	initialize: function(options) {
		this.setOptions(options);

		//basic error checking
		if(!$(this.options.targetParent)) {
			return false;
		}
		$(this.options.targetParent).addEvents({
		    //monitor an element for mouseover
		    'mouseover(.box_target)': function(e){
				el = this.getChildren()[1];
				$$('.hover_active').tween('opacity','0').removeClass('hover_active');
				el.setStyle('opacity',1).addClass('hover_active');
			},
			'mouseout(.box_target)': function(e) {
		        el = this.getChildren()[1];
				if(mouseLeaves(this, e)) {el.morph('.inactive').removeClass('hover_active');}
		    }
		});
	}
});

/**************************************************************************
*  matchDim
*  @param: 		elements ID (array)
*				match (array)
*  Description:	Will match 2 or more element's dimensions (width or height)
*				based on the element with the largest dimension
*
*  Author: 		Tri Doan
*
*  To Do:		Add ability to specify changing both width and height
***************************************************************************/
var matchDim = new Class({

	Implements: [Options],

	options: {
		elementID: [],				// The ID's of the elements of evaluate
		matchLarger: true,			// true, for using the larger value
		matchProperty: 'height'		// Default to match height only?  'height', 'width', 'both'
	},

	initialize: function(options) {
		this.setOptions(options);

		//basic error checking
		if(this.options.elementID.length == 0) {
			return false;
		}

		this.dim1 = 0;		//primary
		this.dim2 = 0;		//secondary

		this.options.elementID.each(function(el,i) {
			if(!(el)[0]) {
				return false;
			} else {
				switch(this.options.matchProperty){
					case 'height':
						if(this.dim1 <= 0) {
							this.dim1 = (el)[0].getSize().y;
						} else {
							if(this.options.matchLarger) {
								this.dim2 = (el)[0].getSize().y;
								this.dim1 = (this.dim1 >= this.dim2) ? this.dim1 : this.dim2;
							} else {
								this.dim1 = (this.dim2 >= this.dim1) ? this.dim2 : this.dim1;
							}
							this.setDim();
						}
						break;
					case 'width':
						if(this.dim1 <= 0) {
							this.dim1 = (el)[0].getSize().x;
						} else {
							if(this.options.matchLarger) {
								this.dim2 = (el)[0].getSize().x;
								this.dim1 = (this.dim1 >= this.dim2) ? this.dim1 : this.dim2;
							} else {
								this.dim1 = (this.dim2 >= this.dim1) ? this.dim2 : this.dim1;
							}
							this.setDim();
						}
						break;
					case 'both':
						break;
				}
			}
		}.bind(this));

	},

	setDim: function() {
		if(this.options.matchProperty == 'height' || this.options.matchProperty == 'width') {
			this.options.elementID.each(function(el) {
				(el)[0].setStyle(this.options.matchProperty,this.dim1);
			}.bind(this));
		}
	}

});

/**************************************************************************
*  verticalCenter
*  @param: 		optional: thisItem (class name for targeted elements)
*
*  Description:	Will vertically center an element relative to it's parent
*
*
*  Author: 		Tri Doan
*
***************************************************************************/
var verticalCenter = new Class({
	Implements: [Options],

	options: {
		container: document,
		thisItem: '.vA'		// The global class name reserved to trigger this class
	},
	items: [],
	initialize: function(options) {
		this.setOptions(options);

		//get the el from the options
		var items = ($type(this.options.thisItem) == "string")	? this.container.getElements(this.options.thisItem) : this.options.thisItem;

		//basic error checking
		if(items.length == 0) {
			return false;
		}

		//loop through each one
		items.each(function(el,i) {
			this.centerThis(el);
		}, this);

	},

	centerThis: function(item) {
		itemHeight = $(item).getSize().y;
		parHeight = $(item).getParent().getSize().y;
		marginHeight = Math.round(((parHeight-itemHeight)/2));

		//alert(itemHeight+', '+parHeight+', '+marginHeight);

		if(marginHeight > 0) {
			$(item).setStyle('margin-top',marginHeight);
		}
		return true;
	}
});




/******************************************************
*
*					INSTANTIATIONS
*
*******************************************************/
