
$(document).ready( function() {

	$('.load_fade, .l_press img').loadFade({'time':400, 'delay':400});
	
	$('#l_home_buttons a').swapOver();
		
});

$(window).load( function () {
	
	setTimeout( function() {
		$('#cycle_left').tkCycle({slides:[
			'<img src="http://media.first-thought.com/provide/home/1_left.jpg" />',
			'<img src="http://media.first-thought.com/provide/home/2_left.jpg" />',
			'<img src="http://media.first-thought.com/provide/home/3_left.jpg" />'
		]});
		$('#cycle_right').tkCycle({slides:[
			'<img src="http://media.first-thought.com/provide/home/1_right.jpg" />',
			'<img src="http://media.first-thought.com/provide/home/2_right.jpg" />',
			'<img src="http://media.first-thought.com/provide/home/3_right.jpg" />'
		]});
	}, 700);
	
});


(function() {
	jQuery.fn.loadFade = function(settings) {

		var config = {
			'time':400,
			'delay':100
		};
		if( settings ) {
			$.extend(config, settings);
		}

		return this.each( function() {

			var $obj = $(this);
			
			// if this is not an img element
			if( $obj[0].nodeName.toUpperCase() !== 'IMG' ) {
				return;
			}

			$obj.
				css({opacity: 0.0}). // set the img opacity to transparent
				bind('load readystatechange', function() { // bind to the load and readystatechange events
					setTimeout( function() { // wait for config.delay miliseconds after the event to prevent glitching
						$obj.animate({opacity: 1.0}, config.time, function() { // fade the img to opaque over config.time miliseconds
							$obj.unbind('load readystatechange'); // the image has been fadded in so unbind the events
						});
					}, config.delay);
				});
	
			this.src = this.src; // give browsers (IE) another chance to trigger event handlers for the img
	
		});
		
	};
})(jQuery);


(function() {
	jQuery.fn.tkCycle = function(settings) {

		if( $.browser.msie && (parseInt($.browser.version, 10) < 8) ) {
			return;
		}

		var config = {
			delay: 7000,
			fadeTime: 1000,
			slides: []
		};
		if( settings ) {
			$.extend(config, settings);
		}

		return this.each( function() {

			var slides = config.slides;
			var index = 0;
			var timer = false;
			
			var $obj = $(this);
			
			//slides.unshift( $obj.html() );
			
			var width = $obj.width();
			var height = $obj.height();
			
			var $markup = $(
				
			);			
		
			var $top = $('<div />');
			var $bottom = $('<div />');
		
			$top.width(width).height(height).
				css({'position': 'absolute', 'z-index':'201', 'opacity':0.0});
			$bottom.width(width).height(height).
				css({'position': 'absolute', 'z-index':'200', 'opacity':1.0});
			if( config.css ) {
				$top.css(config.css);
				$bottom.css(config.css);
			}
			$bottom.html( slides[0] );
			$top.html( slides[1] );
			$obj.
				css({'visibility':"hidden"}).
				before($bottom).
				before($top);
			
			function nextIndex() {
				return ( index < (slides.length - 1) ? index + 1 : 0 );
			}
			function prevIndex() {
				return ( index > 0 ? index - 1 : slides.length - 1 );
			}
			
			function advance() {
				$top.stop().animate({opacity:1.0}, config.fadeTime, function() {
					index = nextIndex();
					$bottom.html( slides[index] );  // copy the top image to the bottom
					setTimeout(function() { // prevent glitch
						$top.css({opacity:0.0});  // hide the top image
						$top.html( slides[ nextIndex() ] );  // preload the next slide into the hidden top container
					}, 1);
				});
			}
			
			function goBack() {
				$top.html( slides[ prevIndex() ] );  // preload the slide into the hidden top container
				$top.stop().animate({opacity:1.0}, config.fadeTime, function() {
					index = prevIndex();
					$bottom.html( slides[index] );  // copy the top image to the bottom
					setTimeout(function() { // prevent glitch
						$top.css({opacity:0.0});  // hide the top image
						$top.html( slides[ nextIndex() ] );  // preload the next slide into the hidden top container
					}, 1);
				});
			}
			
			timer = setInterval(function() { advance(); }, config.delay);
			
		});
		
	};
})(jQuery);


(function() {
	jQuery.fn.swapOver = function(settings) {

		var
			config = {
				suffix:'_over'
			},
			cache = [];
			
		if( settings ) {
			$.extend(config, settings);
		}

		return this.each( function() {
															 
			var $obj = $(this);
			if( !$obj.is('a') ) {
				return;
			}
			
			// preload images
			$obj.find('img').each(function() {
				var
					srcPath = $(this).attr('src'),
					overPath = srcPath.substr(0, srcPath.lastIndexOf('.')) + config.suffix + srcPath.substr(srcPath.lastIndexOf('.')),
					cacheImage = document.createElement('img');
				cacheImage.src = overPath;
				cache.push(cacheImage);
			});
			
			$obj.
				bind('mouseover', function() {
					$(this).find('img').each(function() {
						var
							srcPath = $(this).attr('src'),
							overPath =
								srcPath.substr(0, srcPath.lastIndexOf('.')) +
								config.suffix +
								srcPath.substr(srcPath.lastIndexOf('.'));
						$(this).attr('src', overPath);
					});
				}).
				bind('mouseout', function() {
					$(this).find('img').each(function() {
						var
							srcPath = $(this).attr('src'),
							pos = srcPath.lastIndexOf(config.suffix),
							origPath = pos ?
								srcPath.substr(0, pos) + srcPath.substr(srcPath.lastIndexOf('.')) :
								srcPath;
						$(this).attr('src', origPath);
					});
				});

		}); // each match
		
	};
})(jQuery);


