// JavaScript Document

$.fn.parallax = function(options) {
	return this.each(function(){
		settings = $.extend({
			id: 'light-holder',
     		width: 1900, // default width of the parallax
     		height: 1000, // default height of the parallax
			minScroll: 0, // beginning offset of the parallax
			maxScroll: 1500, // ending offset of the parallax

			Image1x: -.3, // how far in the x direction it should travel. Approx. -1 to 1
			Image1y: -.3, // how far in the y direction it should travel. Approx. -1 to 1
			Image1s: 1, // how fast overall it should travel. 0 to 3.
			Image1o: 0, // how fast the opacity should change. Should be 0 to 1.
			Image1d: 0, // how fast the change of degree should be. -3 to 3.
			Image1r: 0, // how fast the image scales. Should be 0 to 1.
			
			Image2x: 0,
			Image2y: -.3,
			Image2s: 1,
			Image2o: 0,	
			Image2d: 0,
			Image2r: 0,
			
			Image3x: .3,
			Image3y: -.3,
			Image3s: 1,
			Image3o: 0,	
			Image3d: 0,
			Image3r: 0,
			
			Image4x: .3,
			Image4y: 0,
			Image4s: 1,
			Image4o: 0,	
			Image4d: 0,
			Image4r: 0,
			
			Image5x: .3,
			Image5y: .3,
			Image5s: 1,
			Image5o: 0,	
			Image5d: 0,
			Image5r: 0,
			
			Image6x: 0,
			Image6y: .3,
			Image6s: 1,
			Image6o: 0,	
			Image6d: 0,
			Image6r: 0,
			
			Image7x: -.3,
			Image7y: .3,
			Image7s: 1,
			Image7o: 0,	
			Image7d: 0,
			Image7r: 0,
			
			Image8x: -.3,
			Image8y: 0,
			Image8s: 1,
			Image8o: 0,	
			Image8d: 0,
			Image8r: 0,
			
	 	}, options);
		
		var $this = $('#' + settings.id);
		$($this).css({
			width: settings.width,
			height: settings.height,
		});
		
		$('> *',$this).css({
			position: 'relative',
			width: settings.width,
			height: settings.height
		});
		
		var imgCount = $('img', $this).length;
		
		for (i=1; i<=imgCount; i++)
		{
			window[settings.id + 'startTop' + i] = $(('.img' + i), $this).css('top');
			window[settings.id + 'startLeft' + i] = $(('.img' + i), $this).css('left');	
			//window[settings.id + 'height' + i] = $(('.img' + i), $this).css('height');
			//window[settings.id + 'width' + i] = $(('.img' + i), $this).css('width');
		};
		
		//window.onscroll = scroll;
		$(window).scroll(function() {
			
			if (window.pageYOffset >= 0)
			{
				windowDocumentOffset = window.pageYOffset;
			}
			else
			{
				windowDocumentOffset = document.documentElement.scrollTop;
			};
			
			if (windowDocumentOffset >= settings.minScroll && windowDocumentOffset <= settings.maxScroll)
			{

			}
			else if (windowDocumentOffset <= settings.minScroll) 
			{
				windowDocumentOffset = settings.minScroll;
			}
			else if (windowDocumentOffset >= settings.maxScroll) 
			{
				windowDocumentOffset = settings.maxScroll;
			}
			
			windowDocumentOffset = windowDocumentOffset - settings.minScroll;
			
			if (window['windowDocumentOffsetCheck1'] == windowDocumentOffset)
			{
			}
			else
			{
				for (i=1; i<=imgCount; i++)
				{
					if (settings['Image' + i + 'y'] == 0)
					{
						window[settings.id + 'newTop' + i] = window[settings.id + 'startTop' + i];
					}
					else
					{
						window[settings.id + 'newTop' + i] = Math.round(parseFloat(window[settings.id + 'startTop' + i]) + (parseFloat(windowDocumentOffset)*(settings['Image' + i + 'y'])*(settings['Image' + i + 's'])));
					}
					
					if (settings['Image' + i + 'x'] == 0)
					{
						window[settings.id + 'newLeft' + i] = window[settings.id + 'startLeft' + i];
					}
					else
					{
						window[settings.id + 'newLeft' + i] = Math.round(parseFloat(window[settings.id + 'startLeft' + i]) + (parseFloat(windowDocumentOffset)*(settings['Image' + i + 'x'])*(settings['Image' + i + 's'])));
					}
					
					if (settings['Image' + i + 'd'] == 0)
					{
						rotate = 0;
					}
					else
					{
						var rotate = parseFloat(windowDocumentOffset)*settings['Image' + i + 'd']*.1;
					}
					
					if (settings['Image' + i + 'o'] == 0)
					{
						opacity = 1;
					}
					else
					{
						var opacity = 1 - (Math.round(parseFloat(windowDocumentOffset)*settings['Image' + i + 'o'])/1000);
						
						if (opacity <= 0)
						{
							opacity = 0;
						}
						else if (opacity >= 1) {
							opacity = 1;
						};
					}
					/*
					if (settings['Image' + i + 'r'] == 0)
					{
						var height = window[settings.id + 'height' + i];
						var width = window[settings.id + 'width' + i];
						var yoffset = 0;
						var xoffset = 0;
					}
					else
					{
						var height = Math.round(((parseFloat(windowDocumentOffset)*settings['Image' + i + 'r'])/1000 + 1)*parseFloat(window[settings.id + 'height' + i]));
						var width = Math.round(((parseFloat(windowDocumentOffset)*settings['Image' + i + 'r'])/1000 + 1)*parseFloat(window[settings.id + 'width' + i]));
						var yoffset = Math.round((parseFloat(window[settings.id + 'height' + i]) - height)/2);
						var xoffset = Math.round((parseFloat(window[settings.id + 'width' + i]) - width)/2);
					}*/
					$(('.img' + i), $this).css({'top' : (window[settings.id + 'newTop' + i]/* + yoffset*/) + 'px', 'left' : (window[settings.id + 'newLeft' + i]/* + xoffset*/) + 'px', /*'height' : height, 'width' : width, */'-moz-transform' : 'rotate(' + rotate + 'deg)', 'opacity' : opacity});
				};
			}
			window['windowDocumentOffsetCheck1'] = windowDocumentOffset;
		});
	});
};
