/***** demotive sliding puffs object *************************
REQUIREMENTS: jquery.easing.1.3.js - easing equations - http://gsgd.co.uk/sandbox/jquery/easing/

OPTIONS:
Can be sent as an object in the function call to override defaults i.e.: DEMOTIVE.slidepuffs.init($j(element), {});

easeType:
	an easing name - see http://gsgd.co.uk/sandbox/jquery/easing/ - defaults to jquery standard 'swing'
position:
	the four css values - top, right, bottom, left
offset:
	the offset from the chosen direction - ie bottom with an offset of 20 == bottom: 20px
speed :
	speed of transition in ms
*********************************************************/

// namespace demotive:
if (!DEMOTIVE) {
	var DEMOTIVE = {};
}

// demotive puff object
DEMOTIVE.slidepuffs = {
	opts : {
		easeType : 'swing',
		position : 'bottom',
		offset : 0,
		speed : 300
	},
	init : function($els, opts) {
		// $els is a jQuery collection
		$els.each(function(i) {
			$el = jQuery(this);
			// set position defaults
			$el.css({
				'position' : 'relative',
				'overflow' : 'hidden'
			});
			// grab a collection of .dem-puff-unit children
			var $unitEls = $el.find('.dem-slidepuff-unit');
			// set properties for each element
			var propsObj = {};
			propsObj.easeType = DEMOTIVE.slidepuffs.opts.easeType;
			propsObj.position = DEMOTIVE.slidepuffs.opts.position;
			propsObj.offset = DEMOTIVE.slidepuffs.opts.offset;
			propsObj.speed = DEMOTIVE.slidepuffs.opts.speed;
			if (opts) {
				if (opts.easeType) {
					propsObj.easeType = opts.easeType;
				}
				if (opts.position) {
					propsObj.position = opts.position;
				}
				if (opts.offset) {
					propsObj.offset = opts.offset;
				}
				if (opts.speed) {
					propsObj.speed = opts.speed;
				}
			}
			// bottom catch - FF2 needs a set height
			if ((propsObj.position == 'bottom') || (propsObj.position == 'top')) {
				$el.css({ 'height' : $el.innerHeight() });
			}
			$el.data('slide-props', propsObj);
			// set child unit defaults (just in case)
			$unitEls.each(function(i) {
				var cssObj = {};
				cssObj['position'] = 'absolute';
				if (propsObj.position == 'top') {
					cssObj[propsObj.position] = '-' + (jQuery(this).outerHeight(true) - propsObj.offset) + 'px';
				} else if (propsObj.position == 'bottom') {
					// if the value is 'bottom' reset the puff to work from the top instead, due to FF2 not being quite up on it without a fixed container height
					cssObj['bottom'] = 'auto';
					cssObj['top'] = (jQuery(this).parents('.dem-slidepuff').innerHeight() - propsObj.offset) + 'px';
				} else {
					cssObj[propsObj.position] = '-' + (jQuery(this).outerWidth(true) - propsObj.offset) + 'px';
					cssObj['width'] = jQuery(this).width();
				}
				jQuery(this)
					.css(cssObj);
			});
			$el.bind('mouseover.slidepuff focus.slidepuff', function() {
				DEMOTIVE.slidepuffs.activate(jQuery(this), 'in');
			}).bind('mouseout.slidepuff blur.slidepuff', function() {
				DEMOTIVE.slidepuffs.activate(jQuery(this), 'out');
			});
			if ((jQuery.browser.msie) && (jQuery.browser.version == "6.0") && (this.nodeName.toUpperCase() == "A")) {
				$el.bind('click.slidepuff', function() {
					window.location.href = jQuery(this).attr('href');
				});
			}
		});
	},
	activate : function($el, dir) {
		// $el is the .dem-slidepuff object
		var animObj = {};
		if (dir == 'in') {
			if ($el.data('slide-props').position == 'bottom') {
				animObj['top'] = ($el.innerHeight() - $el.find('.dem-slidepuff-unit:first').outerHeight(true)) + 1 + 'px';
			} else {
				animObj[$el.data('slide-props').position] = 0;
			}
		} else {
			if ($el.data('slide-props').position == 'top') {
				animObj[$el.data('slide-props').position] = '-' + ($el.find('.dem-slidepuff-unit:first').outerHeight(true) - $el.data('slide-props').offset) + 'px';
			} else if ($el.data('slide-props').position == 'bottom') {
				animObj['top'] = ($el.innerHeight() - $el.data('slide-props').offset) + 'px';
			} else {
				animObj[$el.data('slide-props').position] = '-' + ($el.find('.dem-slidepuff-unit:first').outerWidth(true) - $el.data('slide-props').offset) + 'px';
			}
		}
		$el.find('.dem-slidepuff-unit')
			.stop()
			.animate(animObj, $el.data('slide-props').speed, $el.data('slide-props').easeType );
	}
}

/***** demotive fading puffs object *************************
REQUIREMENTS: jquery.easing.1.3.js - easing equations - http://gsgd.co.uk/sandbox/jquery/easing/

OPTIONS:
Can be sent as an object in the function call to override defaults i.e.: DEMOTIVE.fadepuffs.init($j(element), {});

easeType:
	an easing name - see http://gsgd.co.uk/sandbox/jquery/easing/ - defaults to jquery standard 'swing'
fadeTo:
	the opacity value to fade up to - 0-1
speed :
	speed of transition in ms
*********************************************************/
DEMOTIVE.fadepuffs = {
	opts : {
		easeType : 'swing',
		fadeTo : 1,
		speed : 300
	},
	init : function($els, opts) {
		// $els is a jQuery collection
		$els.each(function(i) {
			$el = jQuery(this);
			// grab a collection of .dem-puff-unit children
			var $unitEls = $el.find('.dem-fadepuff-unit');
			// set properties for each element
			var propsObj = {};
			propsObj.easeType = DEMOTIVE.fadepuffs.opts.easeType;
			propsObj.fadeTo = DEMOTIVE.fadepuffs.opts.fadeTo;
			propsObj.speed = DEMOTIVE.fadepuffs.opts.speed;
			if (opts) {
				if (opts.easeType) {
					propsObj.easeType = opts.easeType;
				}
				if (opts.fadeTo) {
					propsObj.fadeTo = opts.fadeTo;
				}
				if (opts.speed) {
					propsObj.speed = opts.speed;
				}
			}
			$el.data('fade-props', propsObj);
			// set child unit defaults (just in case)
			$unitEls.each(function(i) {
				var cssObj = {};
				cssObj['opacity'] = 0;
				cssObj['visibility'] = 'visible';
				jQuery(this)
					.css(cssObj);
			});
			$el.bind('mouseover.fadepuff focus.fadepuff', function() {
				DEMOTIVE.fadepuffs.activate(jQuery(this), 'in');
			}).bind('mouseout.fadepuff blur.fadepuff', function() {
				DEMOTIVE.fadepuffs.activate(jQuery(this), 'out');
			});
			if ((jQuery.browser.msie) && (jQuery.browser.version == "6.0") && (this.nodeName.toUpperCase() == "A")) {
				$el.bind('click.fadepuff', function() {
					window.location.href = jQuery(this).attr('href');
				});
			}
		});
	},
	activate : function($el, dir) {
		// $el is the .dem-slidepuff object
		var animObj = {};
		if (dir == 'in') {
			animObj['opacity'] = $el.data('fade-props').fadeTo;
		} else {
			animObj['opacity'] = 0;
		}
		$el.find('.dem-fadepuff-unit')
			.stop()
			.animate(animObj, $el.data('fade-props').speed, $el.data('fade-props').easeType );
	}
}
