﻿/**
 * Site.AjaxLoader class
 */
 
Site.AjaxLoader = function()
{
    
}

Site.AjaxLoader.LastClickX;

Site.AjaxLoader.LastClickY;

/**
 * Tracks all mouse movement coordinates
 */
Site.AjaxLoader.TrackLastClickCoordinates = function()
{
    $( document ).mousemove( function( e )
    {
        Site.AjaxLoader.LastClickX = e.pageX;
        Site.AjaxLoader.LastClickY = e.pageY;
    });
}

Site.AjaxLoader.ContainerId = "ajax-loader-container";

Site.AjaxLoader.Init = function()
{
    Site.AjaxLoader.TrackLastClickCoordinates();
    
    $( "body" ).ajaxStart( function()
    {
        Site.AjaxLoader.Start();
    });
    
    $( "body" ).ajaxStop( function()
    {
        Site.AjaxLoader.Stop();
    });
}

/**
 * Gets called on every ajax request.
 * Automatically cancels a request if another one is still in progress
 */
Site.AjaxLoader.Start = function( sender, args )
{    
    Site.AjaxLoader.StartAnimation();
}

Site.AjaxLoader.StartAnimation = function()
{
    // Sets initial position
    Site.AjaxLoader.Position( Site.AjaxLoader.LastClickY, Site.AjaxLoader.LastClickX );
    Site.AjaxLoader.StartTrackingMouseMovements();
    Site.AjaxLoader.Show();
}

Site.AjaxLoader.StopAnimation = function()
{
    Site.AjaxLoader.Hide();
    Site.AjaxLoader.StopTrackingMouseMovements();
}

Site.AjaxLoader.Stop = function( sender, args )
{
    Site.AjaxLoader.StopAnimation();
}

Site.AjaxLoader.Show = function()
{
    $( "#" + Site.AjaxLoader.ContainerId ).show();
}

Site.AjaxLoader.Hide = function()
{
    $( "#" + Site.AjaxLoader.ContainerId ).hide();
}

Site.AjaxLoader.StartTrackingMouseMovements = function()
{
    $( document ).bind( "mousemove", Site.AjaxLoader.TrackMouseMovements );
}

Site.AjaxLoader.StopTrackingMouseMovements = function()
{
    $( document ).unbind( "mousemove", Site.AjaxLoader.TrackMouseMovements );
}

Site.AjaxLoader.TrackMouseMovements = function( e )
{
    Site.AjaxLoader.Position( (e.pageY), (e.pageX) );
}

/**
 * Positions the loader
 */
Site.AjaxLoader.Position = function( top, left )
{
    $( "#" + Site.AjaxLoader.ContainerId ).css(
    {
        top: top + 0 + "px",
        left: left + 13 + "px"
    });
}