/*
      .-      .-                      .-:::-.`                        
     `NM:    .MM.                     yMMNNMMMmo`                     
     +MMd`   hMMs   .--.`  `..     ...yMM.  .sMMs `--.`     .--.`+.   
     dMMMo  /MMMN` yNmmNMm+-mMd- `yMN/yMM.   :MMysNmmNMm+`sNMmmMMMy`  
    -MMdMN.`mMdMM/ ``.-:yMM-`sMMyNMh. yMMsooyNMm.``.--sMMmMN:  :MMy   
    yMM`yMhyMm`mMd:hNNmdmMM/  /MMMs   yMMdhhyo: .yNNmdmMMNMMo``+MMs   
   `NMy `mMMN- +MMMMh`  /MM/ /NMmMMs` yMM.      NMd`  :MMooMMMMMd+`   
   +MM:  :MM+  .NMMMNo+sNMMsyMN+ -mMm:yMM.      dMNs+smMMmMMdoyyso-   
   +so    +o    +so+syso-/ysso.   `oso+so`       /syyo-:ys+oso++hMM:  
                                                         .dms++odMN.  
                                                         `/oyhhhs/` 
														 
	jQuery PageLoading
	Version : 1.0

	Author : PAGNOULLE Maxime (MaxPag)
	http://maxpag.eu
	http://codecanyon.net/user/MaxPag/
 
***************************************************************************/

jQuery.PadeLoading = function (Options) 
{
	// Default Settings
	var Settings = { 
      'Delay': 0,
	  'BgIMG' : '',
      'BgColor' : '#000000',
	  'BgInnerColor' : '#FFFFFF',
	  'BorderColor' : '#000000',
	  'ShadowColor' : '',
	  'LoaderIMG' : 'LoaderIMG.gif',
	  'zIndex' : 99
    };
	       
	// If options exist, replace the default Settings by the options set.
	if (Options) 
	{ 
		$.extend(Settings, Options);
	}
		
	function CenterLoaderIMG(DivToCenter) 
	{
		var IMG = new Image();
		var IMGSrc = Settings.LoaderIMG;

		$(IMG).load(function () 
		{
			var posTopOffset, posLeftOffset = '';
				
			posTopOffset = '-' + ((IMG.height / 2) + 15) + 'px';
				
			posLeftOffset = '-' + ((IMG.width / 2) + 15) + 'px';
				
			$(DivToCenter).css(
			{
				'position': 'fixed',
				'left' : '50%', 
				'top' : '50%', 
				'margin-top' : posTopOffset , 
				'margin-left' : posLeftOffset, 
				'z-index' : Settings.zIndex + 1 
			});
  				
			delete IMG;
			
		}).error(function () 
		{
   			 $(DivToCenter).css(
			 {
				 'position': 'fixed',
				 'left' : '50%', 
				 'top' : '50%', 
				 'margin-top' : 0 , 
				 'margin-left' : 0, 
				 'z-index' : Settings.zIndex + 1 
			});
				 
		}).attr(
		{ 
			src: IMGSrc
		});
				
	}
		
	function LoadingPage()
	{
		if(Settings.LoaderIMG != "")
		{
			$("body").append("<div id=\"OverMask\"></div><div id=\"LoaderDiv\"><img src=\"" + Settings.LoaderIMG + "\"></div>");
			
			if(Settings.BgIMG == "")
			{ 
				$("#OverMask").css(
				{
					'width' : '100%',
					'height' : '100%',
					'background-color': Settings.BgColor,
					'position': 'absolute',
					'left' : 0, 
					'top' : 0, 
					'position' : 'fixed',
					"z-index" : Settings.zIndex
				}).fadeTo("slow", 0.7);
			}
			else
			{
				$("#OverMask").css(
				{
					'width' : '100%',
					'height' : '100%',
					'background-image': 'url(' + Settings.BgIMG + ')',
					'position': 'absolute',
					'left' : 0, 
					'top' : 0, 
					'position' : 'fixed',
					"z-index" : Settings.zIndex
				}).fadeTo("slow", 0.7);
			}
			
			
			CenterLoaderIMG($("#LoaderDiv"));
			
			var Red, Green, Blue;
			
			if(Settings.BorderColor != "")
			{
				Red = HexToRed(Settings.BorderColor);
				Green = HexToGreen(Settings.BorderColor);
				Blue = HexToBlue(Settings.BorderColor);
			}
			else
			{
				Red = 0;
				Green = 0;
				Blue = 0;
			}
			
			if(Settings.Delay != 0)
			{
				$("#LoaderDiv").hide().css(
				{
					'background-color': Settings.BgInnerColor,
					'-moz-background-clip' : 'padding',
					'-webkit-background-clip' : 'padding',
					'background-clip' : 'padding-box',
					'border' : '10px solid rgba(' + Red + ', ' + Green + ', ' + Blue + ', 0.3)',
					'-webkit-border-radius' : '13px',
					'-moz-border-radius' : '13px',
					'border-radius' : '13px',
					'-webkit-box-shadow' : '0px 0px 5px' + Settings.ShadowColor,
					'-moz-box-shadow' : '0px 0px 5px' + Settings.ShadowColor,
					'box-shadow' : '0px 0px 5px' + Settings.ShadowColor, 
					'padding' : '5px'
					
				}).fadeIn('slow').delay(Settings.Delay).fadeOut('slow', function()
				{
					 $("#OverMask").fadeOut();
				});
			}
			else
			{
				$("#LoaderDiv").hide().css(
				{
					'background-color': Settings.BgInnerColor,
					'-moz-background-clip' : 'padding',
					'-webkit-background-clip' : 'padding',
					'background-clip' : 'padding-box',
					'border' : '10px solid rgba(' + Red + ', ' + Green + ', ' + Blue + ', 0.3)',
					'-webkit-border-radius' : '13px',
					'-moz-border-radius' : '13px',
					'border-radius' : '13px',
					'-webkit-box-shadow' : '0px 0px 5px' + Settings.ShadowColor,
					'-moz-box-shadow' : '0px 0px 5px' + Settings.ShadowColor,
					'box-shadow' : '0px 0px 5px' + Settings.ShadowColor, 
					'padding' : '5px'
					
				}).fadeIn('slow');
			}
		}
		else
		{
			alert("The LoaderIMG setting can not be empty !");
		}
	}
	
	function HexToRed(Value) 
	{ 
		return parseInt((CutDiese(Value)).substring(0,2),16);
	}
	function HexToGreen(Value) 
	{ 
		return parseInt((CutDiese(Value)).substring(2,4),16);
	}
	function HexToBlue(Value) 
	{ 
		return parseInt((CutDiese(Value)).substring(4,6),16);
	}
	function CutDiese(Value) 
	{ 
		return (Value.charAt(0)=="#") ? Value.substring(1,7) : Value;
	}
		
	$(function()
	{
		LoadingPage();
	});
	
}

jQuery.PadeLoaded = function () 
{
	$("#LoaderDiv").fadeOut('slow', function()
	{
		$("#OverMask").fadeOut();
	});
}
