var durShort = 150;
var durMed = 250;
var durLong = 500;
var durXLong = 750;
var filterLiHeight = 31;
var bottomY = 0;
var scrollIndex = 1;
var endReached = false;
var loadRequested = false;
var ieDefaultFadeIds = [".banner"];
var arrTypes = [".box"];
var elementsInView = [".box"];

setVisibleElements();

$(window).scroll(function() {
    
	
	 
	setVisibleElements();
	//this checks the page scroll

    if ((($(window).scrollTop() + $(window).height()) > ($(document).height() - 300)) && !loadRequested && $(".pageNav li").length > 0) {
		 $.ajax({type:'GET',url:"http://www.brandhatchery.com/clients/brandhatchery/site/getresources.php",
		 success:function(data){
		
			console.log(data);
		 }
	   });
     loadRequested = true;
	 scrollIndex++;
	 var href = "?page="+scrollIndex;
	 //load more content
	
	  $.ajax({type:'GET',url:href,
		 success:function(data){
		
			var obj = $(data);
			//console.log(obj.find('#left-column').html());
			$("#left-column").append(obj.find('#left-column').html());
			
			//setGrid();
			setVisibleElements();
			loadRequested = false;
			//scrollIndex++
			//$(el).stop().delay(Math.floor(Math.random() * 500)).animate({opacity: 1.0}, durLong, function() { removeIEFilter(this); });
		 }
	   });
		
    } else if (endReached) {
		//console.log("end reached");
      // hideTheEnd();
    };
});



function setVisibleElements() {
    // populate array with unique values corresponding to each element box
    var d = new Date();
    $.each(arrTypes, function(i, val) {
      $(val).each(function() {
        if ( !$(this).attr("data-unique-id") ) {
          $(this).attr("data-unique-id", parseInt(d.getTime()) + Math.floor(Math.random() * 100000));
        };
        evalVis($(this));
      });
    });
  }
  
  function evalVis(el) {
    var elTop = $(el).offset().top - $(window).scrollTop();
    var elHeight = $(el).height();
    // if between top of footer and top of window
    if (elTop + elHeight > 25 && elTop < $(window).height()) {
      
      if ($.inArray($(el).attr("data-unique-id"), elementsInView) < 0) {
        addToView(el);
      }
      
    } else {
      
      //if ($.inArray($(el).attr("data-unique-id"), elementsInView) >= 0) {
        removeFromView(el);
     // }
      
    }
  };

  
  function addToView(el) {
    elementsInView.push($(el).attr("data-unique-id"));
    revealElements(el);
  };
  
  function removeFromView(el) {
    hideElements(el);
    elementsInView = $.grep(elementsInView, function(val) { return val != $(el).attr("data-unique-id"); });
  };
  
   var prevElTime, newElTime;

  function revealElements(el) {
    if ( $(el).attr("id") && ($(el)[0].id.indexOf("swf") > -1) ) {
      return;
    }
    
    $(el).stop().delay(Math.floor(Math.random() * 500)).animate({opacity: 1.0}, durLong, function() { removeIEFilter(this); });
  };
  
  function hideElements(el) {
    if ( $(el).attr("id") && ($(el)[0].id.indexOf("swf") > -1) ) {
      return;
    }
    //$(el).stop().delay(Math.floor(Math.random() * 200)).animate({opacity: 0.0}, durLong);
    $(el).css("opacity", "0.0");
  };
  
  function removeIEFilter(el) {
    el.style.filter = null;
  }
  
  
function showTheEnd() {
   
    var i = Math.floor(Math.random() * 3);
    var offsetY = i * -120;
   
    bottomY = $(window).scrollTop() + $(window).height() - 135;
  
    endReached = true;
    
    
  }



