/**
 * 
 * Classe de overlay
 * 
 */
function GaleriaOverlay()
{
	this.domElement = $( "<div id='AdjetivaOverlay' class='GaleriaOverlay'></div>" );
	$("body").append( this.domElement );
	
	this.background = new OverlayBackgroundGaleria();
	this.template = new OverlayTemplateGaleria();
	
	$(this.domElement).append( this.background.domElement );
	$(this.domElement).append( this.template.domElement );
	
	this.ajaxContentURL = "";
	this.windowTitle = "";
	
	this.template.startListeners();
	this.template.updateLayout();
	
	this.isActive = false;
}

GaleriaOverlay.prototype.show = function( ajaxContent, windowTitleText )
{
	if ( this.isActive )
	{
		return;
	}
	
	var that = this;
	
	this.ajaxContentURL = ajaxContent;
	this.windowTitle = windowTitleText;

	this.template.updateLayout();
	this.animateIn();
	
	this.isActive = true;
	
	/*
	this.template.domElement.bind("ANIMATE_IN", function(e) {
		that.template.loadAjax( that.ajaxContentURL );
		that.template.setTitle( that.windowTitle );
		that.updateLayout();
	})
	*/
	
	this.setupListeners();
	
	/*
	$(this.background.domElement).bind( "click", function(e){
		that.animateOut(e);
	} );
	
	$(this.template.domElement).bind( "CLOSE", function(e){
		that.animateOut(e);
	} );	
	
	$( window ).bind( "resize.AdjetivaOverlay", function(){
		that.updateLayout();
	});
	$( window ).bind( "scroll.AdjetivaOverlay", function(){
		that.updateLayout();
	});
	*/
}

GaleriaOverlay.prototype.setContent = function( newDomElement )
{
	$( "#AdjetivaOverlayTemplateBody", this.template.domElement ).empty();
	$( "#AdjetivaOverlayTemplateBody", this.template.domElement ).append( newDomElement );
}

GaleriaOverlay.prototype.setCaption = function( captionValue )
{
	$( "#AdjetivaOverlayTemplateCaption", this.template.domElement ).empty();
	$( "#AdjetivaOverlayTemplateCaption", this.template.domElement ).text( captionValue );
}

GaleriaOverlay.prototype.setupListeners = function()
{
	var that = this;
	
	$(this.background.domElement).bind( "click", function(e){
		that.animateOut(e);
	} );
	
	$(this.template.domElement).bind( "CLOSE", function(e){
		that.animateOut(e);
	} );	
	
	$( window ).bind( "resize.AdjetivaOverlay", function(){
		that.updateLayout();
	});
	$( window ).bind( "scroll.AdjetivaOverlay", function(){
		that.updateLayout();
	});}

GaleriaOverlay.prototype.close = function()
{
	$(this.domElement).css( { "display":"none" } );
	var that = this;
	$(this.background.domElement).unbind();
	$(this.template.domElement).unbind();
	$(this.domElement).unbind();
	$( window ).unbind( "resize.AdjetivaOverlay" );
	$( window ).unbind( "scroll.AdjetivaOverlay" );

	$(this.domElement).remove();
}

GaleriaOverlay.prototype.updateLayout = function()
{
	var widthValue = $(window).width();
	var heightValue = $(window).height() + $(window).scrollTop();
	if ( heightValue < $("#AdjetivaOverlayTemplate").height() )
	{
		heightValue = $("#AdjetivaOverlayTemplate").height();
	}
	
	$( this.domElement ).height( heightValue );
	$( this.domElement ).width( widthValue );
	
	this.background.updateLayout();
	this.template.updateLayout();
}

GaleriaOverlay.prototype.animateIn = function()
{
	var that = this;
	
	$(this.domElement).css( { "display":"block" } );
	this.updateLayout();
	$(this.background.domElement).bind( "ANIMATE_IN", function(e)
	{
		that.template.animateIn();
	} );
	this.background.animateIn();
}

GaleriaOverlay.prototype.animateOut = function()
{
	var that = this;
	
	$(this.background.domElement).bind( "ANIMATE_OUT", function(e){
		that.close();
	} );
	
	$(this.template.domElement).bind( "ANIMATE_OUT", function(e){
		that.background.animateOut();
	} );
	
	this.template.animateOut();
	this.isActive = false;
}

/**
 * 
 * Classe do background do overlay
 * 
 */
function OverlayBackgroundGaleria()
{
	this.domElement = $( "<div id='AdjetivaOverlayBackground'></div>" );
	$(this.domElement).fadeTo(0, 0)
}

OverlayBackgroundGaleria.prototype.animateIn = function()
{
	this.updateLayout();
	
	$( this.domElement ).fadeTo( 300, 0.8, function(){
		$(this).trigger("ANIMATE_IN");
	} )
}

OverlayBackgroundGaleria.prototype.animateOut = function()
{
	$( this.domElement ).fadeTo( 300, 0, function(){
		$(this).trigger("ANIMATE_OUT");
	} )
}

OverlayBackgroundGaleria.prototype.updateLayout = function()
{
	$( this.domElement ).height( $(window).height() + $(window).scrollTop() );
	$( this.domElement ).width( $(window).width() );
}

/**
 * 
 * Classe com a template do overlay
 * 
 */
function OverlayTemplateGaleria()
{
	var that = this;
	this.loadingAnimation = document.createElement("img");
	this.loadingAnimation.src = "ajax-loader.gif";
	this.loadingAnimation.id = "AdjetivaOverlayTemplateLoadingAnimation";
	
	var domElementString = "";
	domElementString += "<div id='AdjetivaOverlayTemplate'>";
	domElementString += "	<div id='AdjetivaOverlayTemplateBgContainer'>";
	domElementString += "		<div id='AdjetivaOverlayTemplateBody'></div>";
	domElementString += "		<div class='galeriaBtnPrevious'></div>";
	domElementString += "		<div class='galeriaBtnNext'></div>";	
	domElementString += "		<div id='AdjetivaOverlayTemplateCaption'></div>";
	domElementString += "		<div id='AdjetivaOverlayTemplateHeader'>";
	domElementString += "			<p id='AdjetivaOverlayTitulo'></p>";
	domElementString += "			<a id='AdjetivaOverlayBtnFechar'>fechar</a>";
	domElementString += "		</div>";
	domElementString += "	</div>";
	domElementString += "</div>";
	
	this.domElement = $( domElementString );
	this.animateOut( 0.0000000000000001 );
}

OverlayTemplateGaleria.prototype.startListeners = function()
{
	var that = this;
	
	$( "#AdjetivaOverlayBtnFechar" ).bind( "click", function(e){
		$(that.domElement).trigger( "CLOSE" );
	} );
}

OverlayTemplateGaleria.prototype.animateIn = function( speed )
{
	var animationSpeed = speed || 1000;
	
	var animationProperties = { 
        top: 40 + "px"
    };
	
	// ser for o IE6, que usa position absolute para simular position fixed, não anima. Entra seco.
	if ( $("#AdjetivaOverlayTemplate").css("position") == "absolute" )
	{
		this.updateLayout();
		$(this.domElement).trigger("ANIMATE_IN");
		return;
	}
	
	$( this.domElement ).animate( animationProperties, animationSpeed, "easeInOutCubic", function(){
		$(this).trigger("ANIMATE_IN");
	} )
}

OverlayTemplateGaleria.prototype.animateOut = function( speed )
{
	var animationSpeed = speed || 1000;
	
	var animationProperties = { 
        top: $(window).height() + $(window).scrollTop()
    };
	
	$( this.domElement ).animate( animationProperties, animationSpeed, "easeInOutCubic", function(){
		// limpa o conteudo da template
		$("#AdjetivaOverlayTemplateBody" ).html( "" );
		$(this).trigger("ANIMATE_OUT");
	} )
}

OverlayTemplateGaleria.prototype.updateLayout = function()
{
	var marginLeftX = $("body").innerWidth()/2 - $("#AdjetivaOverlayTemplate").width()/2 + "px"
	$( this.domElement ).css( {
		"marginLeft": marginLeftX
	} );
	
	if ( $("#AdjetivaOverlayTemplate").css("position") == "absolute" )
	{
		$( this.domElement ).css( {
			top: $(window).scrollTop() + 40 // adiciona 50 para o IE
		} );
	}
}

OverlayTemplateGaleria.prototype.loadAjax = function(url)
{
	$("#AdjetivaOverlayTemplateBody").append( this.loadingAnimation );
	
	// para carregamento via ajax
	$("#AdjetivaOverlayTemplateBody", this.domElement).load(url);
}

OverlayTemplateGaleria.prototype.loadIframe = function(url)
{
	$("#AdjetivaOverlayTemplateBody").append( this.loadingAnimation );
	
	// para carregamento via IFrame
	$("#AdjetivaOverlayTemplateBody", this.domElement).html( "<iframe id='AdjetivaOverlayContentIframe' frameborder='0' ></iframe>" );
	$("#AdjetivaOverlayContentIframe", this.domElement).attr( "src", url );
}

OverlayTemplateGaleria.prototype.setTitle = function( title )
{
	$("p#AdjetivaOverlayTitulo" ).html( title );
}



