/*
fcBoxUp - versão beta 1.00
Copyright © 2010 Manoel Messias Santos Neto
All rights reserved.
-------------------------------------------------------------------------
Autor: Manoel Messias Santos Neto
E-mail: manoel@formaecor.com.br
Site: www.formaecor.com.br
Criado: 02/06/2010
-------------------------------------------------------------------------
Importante!! 
-------------------------------------------------------------------------
O uso deste software é 100% liberado para uso pessoal e comercial, desde
que mantidas as informações acima que indicam a autoria do mesmo.
-------------------------------------------------------------------------
Favor me comunicar quando fizer alterações e melhorias.
*/

jQuery.fn.fcBoxUp = function( opcoes )
{
	var padroes =
	{
		cURL: '',
		lReload: 'true',
		cHtml: '',
		cLargura: '760px',
		cAltura: '85%',
		lFundo: true,
		cCorFundo: '#000000',
		lScroll: false,
		nTransparencia: .7,
		lArrasta: false,
		cEfeito: 'show',
		nTempoEfeito: 400
	};
 
	var opcao = jQuery.extend( padroes, opcoes );

	// Chamada atrelada a um seletor
	if( this.length > 0 )
	{
		jQuery( this ).each( function()
	    {
			var cHRef = jQuery( this ).attr( "href" ), cSeletor = "a[href^='" + cHRef + "']";
			jQuery( cSeletor ).live( 'click', function(e)
			{
				jQuery.fn.fcBoxUp(
				{
					cURL: cHRef,
					lReload: opcao.lReload,
					cHtml: opcao.cHtml,
					cLargura: opcao.cLargura,
					cAltura: opcao.cAltura,
					lFundo: opcao.lFundo,
					cCorFundo: opcao.cCorFundo,
					lScroll: opcao.lScroll,
					nTransparencia: opcao.nTransparencia,
					lArrasta: opcao.lArrasta,
					cEfeito: opcao.cEfeito,
					nTempoEfeito: opcao.nTempoEfeito
				});
				return( false );
			});
		});
	}
	else
	{
		if( opcao.cURL != "" )
		{
			cfcBoxUpAtual = fcBoxUpAbre(opcao);
			return( cfcBoxUpAtual );
		}
		if( opcao.cHtml != "" )
		{
			cfcBoxUpAtual = fcBoxUpAbre(opcao);
			return( cfcBoxUpAtual );
		}
	}
}

// Função que monta e abre o box popup
function fcBoxUpAbre(opcao)
{
	var ofcBoxUp = jQuery( ".fcBoxUp" );
	var nfcBoxUp = ofcBoxUp.length;	
	var cfcBoxUpAtual = "fcBoxUp" + ( nfcBoxUp + 1 );	
	
	opcao.cURL = opcao.cURL.replace( "#", "%23" );
	if( opcao.cURL.search( '[?]' ) >= 0 )	
	{
		opcao.cURL = opcao.cURL + "&boxup=" + cfcBoxUpAtual;	
	}
	else
	{
		opcao.cURL = opcao.cURL + "?boxup=" + cfcBoxUpAtual;		
	}
	
	if( !opcao.lScroll )
	{
		jQuery("body").css( { "overflow" : "hidden" } );
	}
	var nLarguraDoc = jQuery(document).width();
	var nAlturaDoc = jQuery(document).height();
	var nLarguraJanela = jQuery(window).width();
	var nAlturaJanela = jQuery(window).height();

	if( opcao.cLargura.search( "%" ) >= 0 )
	{
		aLargura = opcao.cLargura.split( "%" );
		opcao.cLargura = ( nLarguraJanela * ( aLargura[0] / 100 ) ) + "px";
	}
	if( opcao.cAltura.search( "%" ) >= 0 )
	{
		aAltura = opcao.cAltura.split( "%" );
		opcao.cAltura = ( nAlturaJanela * ( aAltura[0] / 100 ) ) + "px";
	}

	if( opcao.lFundo )
	{
		if( nAlturaDoc >= nAlturaJanela )
		{	
			jQuery( "<div id='" + cfcBoxUpAtual + "Fundo' style='position:absolute; display:none; top:0px; left:0px; z-index:" + ( 10000 + nfcBoxUp ) + "; background:" + opcao.cCorFundo + "; width:" + nLarguraDoc + "px; height:" + nAlturaDoc + "px;'></div>" ).appendTo("body");
		}
		else
		{	
			jQuery( "<div id='" + cfcBoxUpAtual + "Fundo' style='position:absolute; display:none; top:0px; left:0px; z-index:" + ( 10000 + nfcBoxUp ) + "; background:" + opcao.cCorFundo + "; width:" + nLarguraDoc + "px; height:" + nAlturaJanela + "px;'></div>" ).appendTo("body");
		}
		jQuery( "#" + cfcBoxUpAtual + "Fundo" ).fadeTo( 0, opcao.nTransparencia );
		jQuery( "embed, object, select" ).hide();
	}
	jQuery( "<div id='" + cfcBoxUpAtual + "' class='fcBoxUp' style='bottom:-4000px; position:absolute; z-index:" + ( 10000 + nfcBoxUp + 2 ) + "; width:" + opcao.cLargura + "; height:" + opcao.cAltura + ";'></div>" ).appendTo( "body" );
	jQuery( "<div id='" + cfcBoxUpAtual + "Contido' class='fcBoxUpContido' style='width:" + opcao.cLargura + "; height:" + opcao.cAltura + ";'></div>" ).appendTo( "#" + cfcBoxUpAtual );
	jQuery( "<div id='" + cfcBoxUpAtual + "Close' class='fcBoxUpClose';></div>" ).appendTo( "#" + cfcBoxUpAtual );
	
	jQuery( "<input name='fcBoxUpReload' id='fcBoxUpReload' type='hidden' value='N'>" ).appendTo( "body" );
	
	nAltura = jQuery( "#" + cfcBoxUpAtual ).height();
	nLargura = jQuery( "#" + cfcBoxUpAtual ).width();
	nAlturaContido = jQuery( "#" + cfcBoxUpAtual + "Contido" ).outerHeight();
	nLarguraContido = jQuery( "#" + cfcBoxUpAtual + "Contido" ).outerWidth();
	nAltura = nAltura + ( nAlturaContido - nAltura );
	nLargura = nLargura + ( nLarguraContido - nLargura );

	jQuery( "#" + cfcBoxUpAtual ).height( nAltura );
	jQuery( "#" + cfcBoxUpAtual ).width( nLargura );
	
	nAlturaIFrame = jQuery( "#" + cfcBoxUpAtual  ).outerHeight();
	nLarguraIFrame = jQuery( "#" + cfcBoxUpAtual ).outerWidth();

	jQuery( "<iframe id='" + cfcBoxUpAtual + "IFrame' class='fcBoxUpIFrame' scrolling='no' width='" + nLarguraIFrame + "' height='" + nAlturaIFrame + "' marginheight='0' marginwidth='0' frameborder='0' style='bottom:-4000px; position:absolute; z-index:" + ( 10000 + nfcBoxUp + 1 ) + ";'></iframe>" ).appendTo( "body" );

	centralizaObj( "#" + cfcBoxUpAtual + 'IFrame', nfcBoxUp * 20 );
	centralizaObj( "#" + cfcBoxUpAtual, nfcBoxUp * 20 );

	jQuery( "#" + cfcBoxUpAtual + 'IFrame' ).hide();
	jQuery( "#" + cfcBoxUpAtual ).hide();		

	if ( opcao.cEfeito == 'show' )
	{
		jQuery( "#" + cfcBoxUpAtual + 'IFrame' ).show( opcao.nTempoEfeito );
		jQuery( "#" + cfcBoxUpAtual ).show( opcao.nTempoEfeito );
	}		
	else if ( opcao.cEfeito == 'fade' )
	{
		jQuery( "#" + cfcBoxUpAtual + 'IFrame' ).fadeIn( opcao.nTempoEfeito );
		jQuery( "#" + cfcBoxUpAtual ).fadeIn( opcao.nTempoEfeito );
	}
	else
	{
		jQuery( "#" + cfcBoxUpAtual + 'IFrame' ).slideDown( opcao.nTempoEfeito );
		jQuery( "#" + cfcBoxUpAtual ).slideDown( opcao.nTempoEfeito );
	}

	if( opcao.cURL != "" )
	{
		jQuery( "<iframe id='" + cfcBoxUpAtual + "URL' class='fcBoxUpURL' src='" + opcao.cURL + "' width='100%' height='100%' frameborder='0' marginheight='0' marginwidth='0' scrolling='auto'></iframe>" ).appendTo( "#" + cfcBoxUpAtual + "Contido" );
	}
	else if( opcao.cHtml != "" )
	{
		// Antes de transportar o elemento cHtml para dentro da div fcBoxUpContido
		// insiro antes dele a div fcBoxUpHtml para servir de âncora quando fechar 
		// e tiver que transportar o elemento cHtml de volta
		$( "<div id='" + cfcBoxUpAtual + "Html' title='" + opcao.cHtml + "'></div>" ).insertBefore( jQuery( opcao.cHtml ) );
		jQuery( "#" + cfcBoxUpAtual + "Contido" ).append( jQuery( opcao.cHtml ) );
		jQuery( opcao.cHtml ).show();
	}

	if( opcao.lArrasta )
	{
		jQuery( "#" + cfcBoxUpAtual ).css( { "cursor" : "move" } );

		jQuery(function()
		{
			jQuery( "#" + cfcBoxUpAtual ).draggable(
			{
				start: function()
				{
					jQuery( "#" + cfcBoxUpAtual + 'URL' ).hide();
				},
				drag: function()
				{
					var boxPosicao = jQuery( "#" + cfcBoxUpAtual ).position();
					jQuery( "#" + cfcBoxUpAtual + "IFrame" ).css( "top", boxPosicao.top );
					jQuery( "#" + cfcBoxUpAtual + "IFrame" ).css( "left", boxPosicao.left );
				},
				stop: function()
				{
					var boxPosicao = jQuery( "#" + cfcBoxUpAtual ).position();
					jQuery( "#" + cfcBoxUpAtual + "IFrame" ).css( "top", boxPosicao.top );
					jQuery( "#" + cfcBoxUpAtual + "IFrame" ).css( "left", boxPosicao.left );					
					jQuery( "#" + cfcBoxUpAtual + 'URL' ).show();
				}
			});
		});
	}
	jQuery( "#" + cfcBoxUpAtual + "Close" ).css( { "cursor" : "pointer" } );
	if( opcao.lFundo )
	{
		jQuery( "#" + cfcBoxUpAtual + "Close" ).click( function(){ jQuery( "embed, object, select" ).show(); jQuery.fn.fcBoxUp.close( cfcBoxUpAtual, opcao.lReload ); } );
	}
	else
	{
		jQuery( "#" + cfcBoxUpAtual + "Close" ).click( function(){ jQuery.fn.fcBoxUp.close( cfcBoxUpAtual, opcao.lReload ); } );
	}
	return( cfcBoxUpAtual );
}

// Função pública que fecha o box popup
jQuery.fn.fcBoxUp.close = function( cfcBoxUp, lReload )
{
	if ( lReload )
	{
		if( jQuery( "#fcBoxUpReload" ).val() == "S" )
		{
			document.forms[0].submit();
		}
	}
	// Se o tipo de conteúdo for Html
	if( jQuery( "#" + cfcBoxUp + "Html" ) )
	{
		// Recupera o nome do elemento cHtml para
		// tranportá-lo de volta a seu lugar de origem
		var cHtml = jQuery( "#" + cfcBoxUp + "Html" ).attr( "title" );
		jQuery( cHtml ).insertAfter( jQuery( "#" + cfcBoxUp + "Html" ) );
		jQuery( cHtml ).hide();
		jQuery( "#" + cfcBoxUp + "Html" ).remove();
	}
	jQuery( "#" + cfcBoxUp + "cURL" ).remove();
	jQuery( "#" + cfcBoxUp ).remove();
	jQuery( "#" + cfcBoxUp + "Close" ).remove();	
	jQuery( "#" + cfcBoxUp + "Contido" ).remove();
	jQuery( "#" + cfcBoxUp + "IFrame" ).remove();	
	jQuery( "#" + cfcBoxUp + "Fundo" ).remove();	
	jQuery("body").css( { "overflow" : "auto" } );
}

// Função pública que atualiza a variável #fcBoxUpReload
// que indica que a janela pai deve ser recarregada após submit
jQuery.fn.fcBoxUp.reload = function()
{
	jQuery( "#fcBoxUpReload" ).val( "S" );
}
