/* SIO - Overlay
=============================================================================================*/

SIO.overlay = 
{
   show: function( insert_into )
   {
		if( $('#overlay').length == 0 )
		{
			var parent = insert_into || $('body');
			
			var overlay  = '<div id="overlay" class="hide">';
					 overlay += '<div id="overlay-box"></div>';
					 overlay += '<div id="overlay-bg"></div>';
				 overlay += '</div>';
			
			parent.append( overlay );
			
			$('#overlay').show();
			
			$('#overlay-bg').css( { opacity: 0, height: $('#main').height() } ).fadeTo( 'fast', 0.8 );
			
			$('#overlay').click( function( event ){ return SIO.overlay.hide( event ); } );
		}
   },
   
   hide: function( event )
   {
		if( event )
		{
			var target = $(event.target);
			if( target.is('.close') || target.is('#overlay-bg') )
			{
				SIO.overlay.do_hide();
				return false;
			}
		}
		else
		{
			SIO.overlay.do_hide();
		}
   },
   
	do_hide: function()
	{
		$('#overlay-box .field').removeClass('fail').attr('value', '');
		$('#overlay-preload .errorbox').hide();
		$('#overlay-preload .successbox').hide();
		$('#overlay-box').hide();
		$('#overlay-bg').fadeTo(100, 0, function(){ $('#overlay').remove(); } );
	},
	
   jig: function()
   {
		var width = $('#overlay-box .holder').width();
      var height = $('#overlay-box .holder').height();
		
		if( jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 )
		{
			window.scrollTo(0,0);
			$('#overlay-box').css( { marginLeft: '-'+(width/2)+'px', width: width, height: height } );
		}
		else
		{
			$('#overlay-box').css( { marginLeft: '-'+(width/2)+'px', marginTop: '-'+(height/2)+'px', width: width, height: height } );
		}
   },
	
   load_window: function( insert_into, source, fixed_position )
   {
		if ( fixed_position === undefined ) fixed_position = true;
		
		$('.errorbox').hide();
		$('.successbox').hide();
		
		SIO.overlay.show( insert_into );
		$('#overlay-box').html( source.clone( true ) );
		SIO.overlay.jig();
		
		if( fixed_position == false )
		{
			window.scrollTo(0,0);
			$('#overlay-box').css({'position': 'absolute', 'margin-top': '0px', 'top': '100px'});
		}
		
		$('#overlay-box .holder').show();
		
      return false;
   },
	
   load_ajax: function( insert_into, url, fixed_position )
   {
		if ( fixed_position === undefined ) fixed_position = true;
			
      SIO.overlay.show( insert_into );
		
		$('#overlay-box').html('<div class="loading full5">Loading...</div>');
		
      $.ajax({
         type: "POST", 
         url: url, 
         success: function(response)
         {
				$("#overlay-box").html(response);
           	SIO.overlay.jig();
				$('#overlay-box .holder').show();
				
				if( fixed_position == false )
				{
					window.scrollTo(0,0);
					$('#overlay-box').css({'position': 'absolute', 'margin-top': '0px', 'top': '100px'});
				}
				
         }
      });
      
   }
   
}
