// Variables
var section_name 		= ".jparallax-section";
var section_count 		= 0;
var section_limits		= [];
var current_sections 	= "";
var scrolled				= false;
var initial_scroll 		= 0;
var animate				= true;

// Scroll Values
var scrollMin 		= 0;
var scrollMax 		= 0;
var scrollPos 		= 0;
var scrollTop  		= 0;

$(document).ready(function(e) {
    init();
});

$(window).scroll(function(e) {
    calc();
});


function scrollValues() {
	scrollMax 	= $(document).height();
	scrollPos 	= $(document).scrollTop();
	// Calculate
	calc();
}

function init() {
	// Increment val
	var zIndex = 999;
	$(section_name).each(function(index, element) {
        // Set z-index
		$(this).css("z-index", zIndex);
		zIndex = zIndex - 1;
    });
	// Element loop
	$(".jparallax-element").each(function(index, element) {
		$(this).attr("data-elemtop", parseInt($(this).css("top").replace("px", "")));
		$(this).attr("data-elemmin", parseInt($(this).css("top").replace("px", "")) - parseInt($(this).attr("data-radius")));
		$(this).attr("data-elemmax", parseInt($(this).css("top").replace("px", "")) + parseInt($(this).attr("data-radius")));
	});
	// Section loop
	$(section_name).each(function(index, element) {
		var bgPosition = $(this).css("background-position");
		if (bgPosition == 'undefined' || bgPosition == null) {
			var backgroundX = $(this).css("background-position-x");
			var backgroundY = $(this).css("background-position-y");
		} else {
			bgPosition = bgPosition.replace("%", "").replace("%", "").split(" ")
			var backgroundY 	= parseInt(bgPosition[1]);
			var backgroundX 	= parseInt(bgPosition[0]);
		}
		
		//$(this).attr("data-elemtop", parseInt($(this).css("top").replace("px", "")));
		$(this).attr("data-elemtop", backgroundY);
		$(this).attr("data-bgX", backgroundX);
		$(this).attr("data-elemmin", parseInt($(this).css("top").replace("px", "")) - parseInt($(this).attr("data-radius")));
		$(this).attr("data-elemmax", parseInt($(this).css("top").replace("px", "")) + parseInt($(this).attr("data-radius")));
		// Chrome Fix
		$(this).css("background-position", "0 0");
		
	});
}

function calc() {
	scrollMax 	= $(document).height();
	scrollPos 	= $(document).scrollTop();
	
	$(section_name).each(function(index, element) {
        if (isScrolledIntoView($(this))) {
			$(this).addClass("active");	
		} else {
			$(this).removeClass("active");
		}
    });
	
	/*$(section_name+".active").each(function(index, element) {
		var position = $(this).position();
		
		if ($(this).attr("id") == "s1") {
			var scrollPos = $(document).scrollTop() - position.top;
		} else {
			var scrollPos = $(document).scrollTop() + $(window).height() - position.top;
		}
		// Get values
		elemTop = parseInt($(this).attr("data-elemtop"));
		elemMin = parseInt($(this).attr("data-elemmin"));
		elemMax = parseInt($(this).attr("data-elemmax"));
		elemPos = parseInt(parseFloat(scrollPos / scrollMax) * (elemMax - elemMin)) + elemTop;
		
		$(this).css("background-position-y", elemPos + "%");
	});*/
	
	// Scroll Top
	scrollTop = $(document).scrollTop();
	scrollCheck = parseInt(scrollTop / 105);
	//console.debug("Scroll Top: " + scrollTop);
	//console.debug("Scroll Check: " + scrollCheck);
	// Check scroll

	
	if (scrollCheck != prevScrollCheck) {
		$(".active .jparallax-element").each(function(index, element) {
			// Get parent position
			var parentPosition = $(this).parent().position();
			// If first li
			
			// eymen
			/*
			if ($(this).parent().attr("id") == "s1") {
				var scrollPos2 = scrollTop - parseInt(parentPosition.top);
			} else {
				var scrollPos2 = (scrollTop + $(window).height()) - parseInt(parentPosition.top);
			}	
			*/
			// eymen finished
			
			
			//tamer: 13.01
			
			if ($(this).parent().attr("id") == "s1") {
				var scrollPos2 = (105 *scrollCheck) - parseInt(parentPosition.top);
			} else {
		
				var scrollPos2 = ((105 *scrollCheck) + $(window).height()) - parseInt(parentPosition.top);
			
			
			}	
			//tamer-fine
		
			
			
			
			// Get values
			elemTop = parseInt($(this).attr("data-elemtop"));
			elemMin = parseInt($(this).attr("data-elemmin"));
			elemMax = parseInt($(this).attr("data-elemmax"));

			elemPos = parseInt(parseFloat(scrollPos2 / scrollMax) * (elemMax - elemMin)) + elemTop;
			// Re-position
			
			// console.debug("elemPos: " + elemPos);
			//console.debug("(scrollPos2 / scrollMax): " + (scrollPos2 / scrollMax));
			//console.debug("paresd: " + parseFloat(scrollPos2 / scrollMax));
			//console.debug("int: " +parseFloat(scrollPos2 / scrollMax) * (elemMax - elemMin) );
			//console.debug("int: " + parseInt(parseFloat(scrollPos2 / scrollMax) * (elemMax - elemMin))) ;
			
			if (animate === false) {
				$(this).css("top", elemPos + "px");
			} else {
				$(this).animate({
				  top: elemPos + "px",
				},{"queue": false, "duration": 300}, function() {
				});
			}
		});
	// Assign new scrollCheck
	prevScrollCheck = scrollCheck;
	}
}

var prevScrollCheck = 0;
var scrollCheck = 0;

function show(elem) {
	$(elem).show();
}

$(document).ready(function(e) {
   $("#logo").click(function(e) {
    	scrollToTest();
	}); 
});

function scrollToTest() {
	$(this).scrollTo(3648, 10000);	
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemzTop = $(elem).offset().top;
    var elemBottom = elemzTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemzTop <= docViewBottom));
}
