/*
 * jQuery bgFade
 * Copyright 2010 Jake Lauer with Clarity Design (isthatclear.com)
 * Released under the MIT and GPL licenses.
 */
( function( $ ){
	$.fn.bgFade = function( action, options ) {
	
		if ( action == 'fadeIn' ) {
			var defaults = {
				top: "",
				left: "",
				height: "",
				width: "",
				fadeSpeed: 200,
				fadeToOpacity: 1,
				animate: false
			};
		} else if ( action == 'fadeOut' ) {
			var defaults = {
				top: "",
				left: "",
				height: "",
				width: "",
				fadeSpeed: 200,
				fadeToOpacity: 0,
				animate: false
			};
		}

		var options = $.extend( defaults, options );

		return this.each( function() {
			
			if ( action=='fadeIn' ) {
				if ( $( this ).css( 'position' ) == "static" ) {
					$( this ).css( {position: 'relative'} );
				}
				if ( (options.fadeToOpacity == 1) || ( (options.fadeToOpacity < 1) && $(this).find( '.bg_fade' ).length == 0 ) ){
					$( this ).append( '<span class="bg_fade"></span>' )
				}
			}
			var fader = $( this ).find( '.bg_fade' );
			var aThis = $( this );

			if ( options.top == "" ) {
							if ( fader.css( 'top' ) == '0px' ) {
								fTop = 0;
							} else {
								fTop = fader.css( 'top' );
							}
						} else {
							fTop = options.top;
						}
	
						if ( options.top != "" && action != 'fadeIn' ) {
							fTop = options.top;
						} else if ( options.top == "" && action != 'fadeIn' ) {
							fTop = 0;
						}
				
			if ( options.left == 0 ) {
							if ( fader.css( 'left' ) == '0px' ) {
								fLeft = 0;
							} else {
								fLeft = fader.css( 'left' );
							}
						} else {
							fLeft = options.left;
						}
	
						if ( options.left != "" && action != 'fadeIn' ) {
							fLeft = options.left;
						} else if ( options.left == "" && action != 'fadeIn' ) {
							fLeft = 0;
						}

			if ( options.width == "" ) {
							if ( fader.css( 'width' ) == '0px' ) {
								fWidth = 0;
							} else {
								fWidth = fader.css( 'width' );
							}
						} else {
							fWidth = options.width;
						}
	
						if ( options.width != "" && action != 'fadeIn' ) {
							fWidth = options.width;
						} else if ( options.width == "" && action != 'fadeIn' ) {
							fWidth = $( this ).width();
						}
						
						
			if ( options.height == "" ) {
							if ( fader.css( 'height' ) == '0px' ) {
								fHeight = 0;
							} else {
								fHeight = fader.css( 'height' );
							}
						} else {
							fHeight = options.height;
						}

						if ( options.height != "" && action != 'fadeIn' ) {
							fHeight = options.height;
						} else if ( options.height == "" && action != 'fadeIn' ) {
							fHeight = $( this ).height();
						}
						
			if ( $(this).find('a').length > 0 && fader.size() == 1) {
				var linkText = $(this).find('a').text();
				var linkHTML = $(this).find('a').html( $(this).find('a').clone() ).html().replace(linkText,'');
				$(this).find('a').not(':first').remove();
				linkHTML = linkHTML.replace('href', 'class="appendedLink" style="display:block;width:100%;height:100%;" href');
				if ($(this).find('.bg_fade').html() == ""){
						fader.html('<a class="appendedLink" href="http://google.com" style="display:block;width:' + fWidth + ';height:' + fHeight + ';"></a>');
				}
			}
			
			if ( action == 'fadeIn') {
				fader.css({opacity: 0});
			}
			
			if ( options.animate == false ) {
				fader.css( {
					top:fTop, 
					left:fLeft, 
					width: fWidth, 
					height: fHeight
				} );
			}
			
			if( action == 'fadeIn' ) {
				bgFadeOn( options, fader, aThis );
			} else if ( action == 'fadeOut' ){
				bgFadeOff( options, fader, aThis );
			}
		} );
		
		function bgFadeOn ( options, fader, aThis ) {
			fader.siblings().not('a').remove();
			if ( options.animate == true ) {
				if ($.browser.opera) {
					aThis.css({
						top:fader.css('top'),
						left:fader.css('left')
					});
				}
				aThis
					.stop()
					.animate( {
						top:fTop,
						left:fLeft,
						width: fWidth,
						height: fHeight
						},
					options.fadeSpeed );
				fader
					.stop()
					.animate( {
						opacity: options.fadeToOpacity,
						width: fWidth,
						height: fHeight
						}, 
					options.fadeSpeed );
			} else {
				fader
					.stop()
					.animate( {opacity: options.fadeToOpacity}, options.fadeSpeed );
			}
		}

		function bgFadeOff ( options, fader, aThis ) {
			if ( options.animate == true ) {
				aThis
					.stop()
					.animate( {
						top:fTop, 
						left:fLeft, 
						width: fWidth, 
						height: fHeight 
						}, 
					options.fadeSpeed );
				fader
					.stop()
					.animate( {
						opacity: options.fadeToOpacity
						},
					options.fadeSpeed, 
						function() { 
							if( options.fadeToOpacity == 0 ){
								$( this ).remove(); 
							} else {
								$( this ).parent().find( '.bg_fade' ).not( $( this ) ).remove();
							}
						}
					 );
			} else {
				fader
					.stop()
					.animate( {
						opacity: options.fadeToOpacity
						},
					options.fadeSpeed, 
						function() { 
							if( options.fadeToOpacity == 0 ){
								$( this ).remove(); 
							} else {
								$( this ).parent().find( '.bg_fade' ).remove();
							}
						}
					 );
			}
		}

	};

} )( jQuery );



/*!
 * Color animation jQuery-plugin
 * http://www.bitstorm.org/jquery/color-animation/
 * Copyright 2011 Edwin Martin <edwin@bitstorm.org>
 * Released under the MIT and GPL licenses.
 */

(function($) {
	/**
	 * Check whether the browser supports RGBA color mode.
	 *
	 * Author Mehdi Kabab <http://pioupioum.fr>
	 * @return {boolean} True if the browser support RGBA. False otherwise.
	 */
	function isRGBACapable() {
		var $script = $('script:first'),
				color = $script.css('color'),
				result = false;
		if (/^rgba/.test(color)) {
			result = true;
		} else {
			try {
				result = ( color != $script.css('color', 'rgba(0, 0, 0, 0.5)').css('color') );
				$script.css('color', color);
			} catch (e) {
			}
		}

		return result;
	}

	$.extend(true, $, {
		support: {
			'rgba': isRGBACapable()
		}
	});

	var properties = ['color', 'backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'outlineColor'];
	$.each(properties, function(i, property) {
		$.fx.step[property] = function(fx) {

			if (!fx.init) {
				fx.begin = parseColor($(fx.elem).css(property));
				fx.end = parseColor(fx.end);
				fx.init = true;
			}

			fx.elem.style[property] = calculateColor(fx.begin, fx.end, fx.pos);
		}
	});

	// borderColor doesn't fit in standard fx.step above.
	$.fx.step.borderColor = function(fx) {
		if (!fx.init) {
			fx.end = parseColor(fx.end);
		}
		var borders = properties.slice(2, 6); // All four border properties
		$.each(borders, function(i, property) {
			if (!fx.init) {
				fx[property] = {begin: parseColor($(fx.elem).css(property))};
			}

			fx.elem.style[property] = calculateColor(fx[property].begin, fx.end, fx.pos);
		});
		fx.init = true;
	}

	// Calculate an in-between color. Returns "#aabbcc"-like string.
	function calculateColor(begin, end, pos) {
		var color = 'rgb' + ($.support['rgba'] ? 'a' : '') + '('
				+ parseInt((begin[0] + pos * (end[0] - begin[0])), 10) + ','
				+ parseInt((begin[1] + pos * (end[1] - begin[1])), 10) + ','
				+ parseInt((begin[2] + pos * (end[2] - begin[2])), 10);
		if ($.support['rgba']) {
			color += ',' + (begin && end ? parseFloat(begin[3] + pos * (end[3] - begin[3])) : 1);
		}
		color += ')';
		return color;
	}

	// Parse an CSS-syntax color. Outputs an array [r, g, b]
	function parseColor(color) {
		var match, triplet;

		// Match #aabbcc
		if (match = /#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(color)) {
			triplet = [parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16), 1];

			// Match #abc
		} else if (match = /#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(color)) {
			triplet = [parseInt(match[1], 16) * 17, parseInt(match[2], 16) * 17, parseInt(match[3], 16) * 17, 1];

			// Match rgb(n, n, n)
		} else if (match = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) {
			triplet = [parseInt(match[1]), parseInt(match[2]), parseInt(match[3]), 1];

		} else if (match = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([01\.]*)\s*\)/.exec(color)) {
			triplet = [parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10),parseFloat(match[4])];

			// No browser returns rgb(n%, n%, n%), so little reason to support this format.
		}
		return triplet;
	}
})(jQuery);
