// JavaScript Document

$.fn.parallax2 = function(options) {
	return this.each(function(){
		settings2 = $.extend({
			id: 'light-holder2',
     		width2: 1900, // default width of the parallax
     		height2: 1000, // default height of the parallax
			minScroll2: 3000, // beginning offset of the parallax
			maxScroll2: 4500, // ending offset of the parallax

			Image1x2: -.3, // how far in the x direction it should travel. Approx. -1 to 1
			Image1y2: -.3, // how far in the y direction it should travel. Approx. -1 to 1
			Image1s2: 1, // how fast overall it should travel. 0 to 3.
			Image1o2: 0, // how fast the opacity should change. Should be 0 to 1.
			Image1d2: 0, // how fast the change of degree should be. -3 to 3.
			Image1r2: 0, // how fast the image scales. Should be 0 to 1.
			
			Image2x2: 0,
			Image2y2: -.3,
			Image2s2: 1,
			Image2o2: 0,	
			Image2d2: 0,
			Image2r2: 0,
			
			Image3x2: .3,
			Image3y2: -.3,
			Image3s2: 1,
			Image3o2: 0,	
			Image3d2: 0,
			Image3r2: 0,
			
			Image4x2: .3,
			Image4y2: 0,
			Image4s2: 1,
			Image4o2: 0,	
			Image4d2: 0,
			Image4r2: 0,
			
			Image5x2: .3,
			Image5y2: .3,
			Image5s2: 1,
			Image5o2: 0,	
			Image5d2: 0,
			Image5r2: 0,
			
			Image6x2: 0,
			Image6y2: .3,
			Image6s2: 1,
			Image6o2: 0,
			Image6d2: 0,
			Image6r2: 0,
			
			Image7x2: -.3,
			Image7y2: .3,
			Image7s2: 1,
			Image7o2: 0,
			Image7d2: 0,
			Image7r2: 0,
			
			Image8x2: -.3,
			Image8y2: 0,
			Image8s2: 1,
			Image8o2: 0,
			Image8d2: 0,
			Image8r2: 0,
			
	 	}, options);

		var $this = $('#' + settings2.id);
		
		$('img', $this).css('opacity', 0);
		$($this).css({
			width: settings2.width2,
			height: settings2.height2
		});
		
		$('> *',$this).css({
			position: 'relative',
			width: settings2.width2,
			height: settings2.height2
		});
		var imgCount = $('img', $this).length;
		
		for (i=1; i<=imgCount; i++)
		{
			window[settings2.id + 'startTop' + i] = $(('.img' + i), $this).css('bottom');
			window[settings2.id + 'startLeft' + i] = $(('.img' + i), $this).css('left');
			//window[settings2.id + 'height' + i] = $(('.img' + i), $this).css('height');
			//window[settings2.id + 'width' + i] = $(('.img' + i), $this).css('width');
		};

		$(window).scroll(function() {
			settings2.maxScroll2 = $('body').height() - window.innerHeight;
			settings2.minScroll2 = settings2.maxScroll2 - 1500;
			if (window.pageYOffset >= 0)
			{
				windowDocumentOffset = window.pageYOffset;
			}
			else
			{
				windowDocumentOffset = document.documentElement.scrollTop;
			};
			
			if (windowDocumentOffset >= settings2.minScroll2 && windowDocumentOffset <= settings2.maxScroll2)
			{
			}
			else if (windowDocumentOffset <= settings2.minScroll2) 
			{
				windowDocumentOffset = settings2.minScroll2;
			}
			else if (windowDocumentOffset >= settings2.minScroll2) 
			{
				windowDocumentOffset = settings2.maxScroll2;
			}
			
			windowDocumentOffset = windowDocumentOffset - settings2.minScroll2;
			
			var bonus = 1500 - Math.round(windowDocumentOffset);
			if (window['windowDocumentOffsetCheck2'] == windowDocumentOffset)
			{
			}
			else
			{
				for (i=1; i<=imgCount; i++)
				{
					if (settings2['Image' + i + 'y2'] == 0)
					{
						window[settings2.id + 'newTop' + i] = parseFloat(window[settings2.id + 'startTop' + i]);
					}
					else
					{
						window[settings2.id + 'newTop' + i] = Math.round(parseFloat(window[settings2.id + 'startTop' + i]) + (parseFloat(windowDocumentOffset)*(settings2['Image' + i + 'y2'])*(settings2['Image' + i + 's2'])));
					}
					
					if (settings2['Image' + i + 'x2'] == 0)
					{
						window[settings2.id + 'newLeft' + i] = parseFloat(window[settings2.id + 'startLeft' + i]);
					}
					else
					{
						window[settings2.id + 'newLeft' + i] = Math.round(parseFloat(window[settings2.id + 'startLeft' + i]) + (parseFloat(windowDocumentOffset)*(settings2['Image' + i + 'x2'])*(settings2['Image' + i + 's2'])));
					}
					if (settings['Image' + i + 'd2'] == 0)
					{
						rotate = 0;
					}
					else
					{
						var rotate = parseFloat(windowDocumentOffset)*settings2['Image' + i + 'd2']*.1;
					}
					if (settings['Image' + i + 'o1'] == 0)
					{
						opacity = 1;
					}
					else
					{
						var opacity = Math.round(parseFloat(windowDocumentOffset)*settings2['Image' + i + 'o2'])/1000;
						
						if (opacity <= 0)
						{
							opacity = 0;
						}
						else if (opacity >= 1) {
							opacity = 1;
						};
					}
					/*if (settings2['Image' + i + 'r2'] == 0)
					{
						var height = window[settings2.id + 'height' + i];
						var width = window[settings2.id + 'width' + i];
						var yoffset = 0;
						var xoffset = 0;
					}
					else
					{
						var height = Math.round(((parseFloat(windowDocumentOffset)*settings2['Image' + i + 'r2'])/1000 + 1)*parseFloat(window[settings2.id + 'height' + i]));
						var width = Math.round(((parseFloat(windowDocumentOffset)*settings2['Image' + i + 'r2'])/1000 + 1)*parseFloat(window[settings2.id + 'width' + i]));
						var yoffset = Math.round((parseFloat(window[settings2.id + 'height' + i]) - height)/2);
						var xoffset = Math.round((parseFloat(window[settings2.id + 'width' + i]) - width)/2);
					}*/
					
					$(('.img' + i), $this).css({'bottom' : (window[settings2.id + 'newTop' + i]/* - bonus +yoffset*/) + 'px', 'left' : (window[settings2.id + 'newLeft' + i]/* + xoffset*/) + 'px', /*'height' : height, 'width' : width, */'-moz-transform' : 'rotate(' + rotate + 'deg)', 'opacity' : opacity});
				};
			}
			window['windowDocumentOffsetCheck2'] = windowDocumentOffset;
		});
	});
};
