(function () {
	
	var fade_slider = (function (window, document, undefined) {
		var data = [
			{
				'button': 'images/c_button.png',
				'slider_img': 'images/corporate-slider.jpg',
				'background_left': 'images/blank.gif',
				'background_right': 'images/corporate-background-right.jpg',
				'background_color': '#275589',
				'href': '/Global/Default.asp'
			}, {
				'button': 'images/w_button.png',
				'slider_img': 'images/woodworking-slider.jpg',
				'background_left': 'images/woodworking-background-left.jpg',
				'background_right': 'images/woodworking-background-right.jpg',
				'background_color': '#3f2516',
				'href': '/Woodworking/Spotlights/'
			}, {
				'button': 'images/i_button.png',
				'slider_img': 'images/industrial-slider.jpg',
				'background_left': 'images/industrial-background-left.jpg',
				'background_right': 'images/industrial-background-right.jpg',
				'background_color': '#062867',
				'href': '/Industrial/Spotlights/'
			}, {
				'button': 'images/o_button.png',
				'slider_img': 'images/oem-slider.jpg',
				'background_left': 'images/oem-background-left2.jpg',
				'background_color': '#718095',
				'background_right': 'images/blank.gif',
				'href': '/OEM-Direct/About/Default.asp'
			}
		];
			
		return {
			'data': data,
			'timer': null,
			'moving': false,
			'imagesLoaded': 0,
			'cycleSpeed': 1100,
			'fadeOutSpeed': 600,
			'fadeInSpeed': 800,
			'slide_width': 232,
			'slide_position': 0,
			'slide': function (index) {
				if (this.moving) {
					return false;
				}
				
				var that			= this;
				var theSlide		= jQuery('#fade_slider_slide');
				var theSlideImage	= jQuery('#fade_slider_slide img');
				var leftWidth		= this.slide_width * (index);
				var leftSide		= jQuery('#fade_slider_left');
				var leftSideImage	= jQuery('#fade_slider_left img');
				var rightWidth		= ((data.length - 1) * this.slide_width) - (this.slide_width * (index));
				var rightSide		= jQuery('#fade_slider_right');
				var rightSideImage	= jQuery('#fade_slider_right img');
				
				//clear backgrounds.
				leftSide.stop(true, true).css({
					'background': 'url("images/blank.gif")'
				});
				rightSide.stop(true, true).css({
					'background': 'url("images/blank.gif")'
				});
				
				// set background color
				jQuery('#fade_slider').css({
					'background': data[index].background_color
				});
				
				// make the right side the correct length
				rightSide.css({
					'left': leftWidth + that.slide_width  - 1 + 'px',
					'width': rightWidth + 'px'
				});

				// get new images ready to fade in
				rightSideImage.stop(true, true).animate({
					'opacity': '0.0'
				}, 0);
				leftSideImage.stop(true, true).animate({
					'opacity': '0.0'
				}, 0);
				rightSideImage.attr('src', data[index].background_right);
				leftSideImage.attr('src', data[index].background_left);

				// set the moving flag.
				that.moving = true;
				
				// get the new slider images ready
				theSlide.css('background', 'url("' + data[index].slider_img + '")');
				
				// fade the new slider image in.
				theSlideImage.stop().animate({
					'opacity': '0.0'
				}, that.cycleSpeed, function () {
					jQuery(this).attr('src', data[index].slider_img);
					jQuery(this).css('opacity', '1.0');
				});
				
				// resize left side and at the same time... ------+
				leftSide.stop(false, true).animate({	//        |
					'width': leftWidth					//        |  
				}, that.cycleSpeed);					//        |
				// move the slider while the left side expands  <-+
				theSlide.stop(true, true).animate({
					'left': leftWidth
				}, that.cycleSpeed, function () {
					// the slider is done moving... turn off moving flag
					that.moving = false;
					that.slide_position = index;
					// start fading the background images in
					leftSideImage.stop(true, true).animate({
						'opacity': '1.0'
					}, that.fadeInSpeed);
					rightSideImage.stop(true, true).animate({
						'opacity': '1.0'
					}, that.fadeInSpeed, function () {
						// trigger the "full_stop" event
						jQuery('#fade_slider').trigger('full_stop');
					});
					
				});
			},
			'preload': function () {
				var that = this;
				var i;
				for (i = 0; i < data.length; i++) {
					jQuery('#preloader').append('<img src="' + data[i].slider_img + '" />');
					jQuery('#preloader').append('<img src="' + data[i].background_left + '" />');
					jQuery('#preloader').append('<img src="' + data[i].background_right + '" />');
				}
				jQuery('#preloader img').each(function () {
					jQuery(this).load(function () {
						that.imagesLoaded++;
					});
				});
				this.load();
			},
			'load': function () {
				// each slide has three images so we need this many...
				var totalImages = data.length * 3;
				// keep repeating until we get all images loaded
				if (this.imagesLoaded !== totalImages) {
					var that = this;
					// lets not go crazy, at lease wait .1 seconds
					setTimeout(function () {
						that.load();
					}, 100);
				} else {
					// we're done loading
					jQuery('#fade_slider').trigger('loaded');
					this.init();
				}
			},
			'init': function () {
				var that = this;
				var leftWidth = this.slide_width * this.slide_position;
				var rightWidth = ((data.length - 1) * this.slide_width) - (this.slide_width * this.slide_position);
				jQuery('#fade_slider').css({
					'width': ((data.length) * this.slide_width)
				});
				jQuery('#fade_slider_left').css({
					'width':  leftWidth + 'px'
				});
				jQuery('#fade_slider_right').css({
					'width': rightWidth + 'px',
					'left': this.slide_width - 1 + 'px'
				});
				jQuery('#fade_slider_right img').attr('src', data[0].background_right);
				jQuery('#fade_slider_slide').css({
					'left': this.slide_width * this.slide_position + 'px'
				});
				jQuery('#fade_slider_slide img').attr('src', data[0].slider_img);
				
				this.createButtons();
				this.showButton(0);
				
				jQuery('.fade_button').click(function () {
					var id = jQuery(this).attr('id').substr(11);
					
					if (that.slide_position === id) {
						window.location = data[id].href;
					} else {
						that.slide(id);
						that.showButton(id);
					}
				});
			},
			'showButton': function (x) {
				var that = this;
				if (!jQuery.browser.msie) {
					jQuery('.fade_button').stop(true,false).animate({
						'opacity': '0.5'
					}, this.cycleSpeed, function () {
						jQuery('#fade_button' + x).stop(true,false).animate({
							'opacity': '1.0'
						}, 400);
					});
				}
			}, 
			'createButtons': function () {
				var y;
				var that = this;
				var thisButton;
				var leftButton;
				var topPadding;
				for (y = 0; y < data.length; y++) {
					leftButton = (y * this.slide_width) + ((this.slide_width / 2) - 75);
					//jQuery('#fade_slider').append('<div class="fade_button" id="fade_button' + y + '"><p>' + data[y].button + '</p></div>');
					jQuery('#fade_slider').append('<div class="fade_button" id="fade_button' + y + '"><img src="' + data[y].button + '" /></div>');
					topPadding = (jQuery('#fade_button' + y).height() - jQuery('#fade_button' + y + ' p').height()) / 2 - 3;
					
					jQuery('#fade_button' + y).css({
						'left':  leftButton + "px"
					});
					
					jQuery('#fade_button' + y + ' p').css({
						'padding-top': topPadding + "px"
					});
				}
				jQuery('.fade_button').each(function () {
					jQuery(this).bind('mouseenter', function () {
						clearTimeout(that.timer);
						var button_index = jQuery(this).attr('id').substr(11);
						if (button_index !== that.slide_position && !that.moving) {
							that.slide(button_index);
							that.showButton(button_index);
						} else {
							var me = this;
							that.timer = setTimeout(function () {
								jQuery(me).trigger('mouseenter');
							}, 1);
						}
					});
				});
			}
		};
	}(this, document));
	
	jQuery(function () {
		fade_slider.preload();
		
		jQuery('#fade_slider').bind('full_stop', function () {
			var x = 0;
			var subAnimationTimer;
			if (fade_slider.slide_position === '3') {
				(function subAnimation() {
					if (fade_slider.slide_position === '3') {
						if (x === 0) {
							jQuery('#fade_slider_left img').stop(false,true).animate({
								'opacity': '1.0'
							}, 1000, function () {
								x = 1;
								subAnimationTimer = setTimeout(function () {
									subAnimation();
								}, 2000);
							});
						} else {
							jQuery('#fade_slider_left').css({
								'opacity': '1.0',
								'background': 'url("' + 'images/oem-background-left.jpg' + '")'
							});
							jQuery('#fade_slider_left img').stop(false, true).animate({
								'opacity': '0.0'
							}, 1000, function () {
								x = 0;
								subAnimationTimer = setTimeout(function () {
									subAnimation();
								}, 2000);
							});
						}
					} else {
						clearTimeout(subAnimationTimer);
					}
				}());
			}
		});
	});
}());
