
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  ScrollToSimilar
//  Monitors the page position, and loads the Similar Listings 
//  module when the user scrolls far enough to see it.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var StSim = new ScrollToSimilar();
    StSim.Init();
    
function ScrollToSimilar()
{
    this.Timer = null;
    this.PlaceholderName = "scrolltosimilar_placeholder.gif";
    this.isLoaded = false;
    
    var My = this;
    
    ScrollToSimilar.prototype.Init = function()
    {
        this.Loadem();
        if (this.Timer != null)
        {
            window.clearInterval(this.Timer);
        }
        this.Timer = window.setInterval(function(){My.Loadem();},500);
        //window.status = "LdImgs Running";
    };
    
    ScrollToSimilar.prototype.SetLoaded = function()
    {
        this.isLoaded = true;
        window.clearInterval(this.Timer);
    };
    
    ScrollToSimilar.prototype.Loadem = function()
    {with(this){
    
        if (!ActionRequired)
        {
            return;
        }
        var imgs = document.getElementsByTagName("img");
        for (var x=0; x < imgs.length; x++)
        {
            if (imgs[x].src.toLowerCase().indexOf(PlaceholderName) > -1)
            {
                // get scrollHeight of window
                var WinBounds = new GetWindowBunds();
                
                if (WinBounds.isDefined)
                {
                    // get offsetTop of image
                    var imgTop = RecurseOffset(imgs[x]).offsetTop;
                    // add the hieght + padding, 40px
                    imgTop += parseInt(imgs[x].height,10) + 50;
                    var winViewBottom = WinBounds.VisibleTop + WinBounds.VisibleHeight;
                    if (imgTop < winViewBottom)
                    {
                        //TODO: Load SimilarListings
                        //alert("Load Now!!");
                        LoadSimilarListings();
                        SetLoaded();
                        //alert("loaded!");
                    }
                }
                else
                {
                    //TODO: Load SimilarListings
                    //alert("Load Now!! undefined");
                    LoadSimilarListings();
                    SetLoaded();
                    //alert("loaded!");
                }
            }
        }
    }};

    ScrollToSimilar.prototype.ActionRequired = function()
    {
        if (this.isLoaded == true)
        {
            return false;
        }
        var imgs = document.getElementsByTagName("img");
        for (var x=0; x < imgs.length; x++)
        {
            if (imgs[x].src.toLowerCase().indexOf(this.PlaceholderName) > -1)
            {
               return true;
            }
        }
        return false;
    };
}
    