
/* Config */
var VelocidadeBox = 10; // Velocidade de animação do box, em milisegundos.
var OpacidadeMaximaBox = 0.7; // Opacidade / transparência máxima do box.
var Frames = 6 // Quantidade aproximada de frames do efeito

var PulsoComBox = Class.create({
	initialize: function() {
		this.PosTop = new Number;
		this.PosLeft = new Number;
		this.CurrentTop = new Number;
		this.CurrentLeft = new Number;
		this.BodyWidth = new Number;
		this.BodyHeight = new Number;
		this.BodyLeft = new Number;
		this.BodyTop = new Number;
		this.ObjWidth = 1;
		this.ObjHeight = 1;
		this.Velocidade = VelocidadeBox;
		this.OpacidadeMaxima = OpacidadeMaximaBox;
		this.Frames = Frames;
		this.FadeInOpacity = new Number;
		this.FadeOutOpacity = new Number;
		this.FadeInWidth = new Number;
		this.FadeOutWidth = new Number;
		this.FadeInHeight = new Number;
		this.FadeOutHeight = new Number;
		this.Body = new Object;
		this.Div = new Object;
		this.ElementClicked = new Object;
		this.ConteudoURL = new String;
		this.Script = new String;
		this.Title = new String;
		this.ConteudoWidth = Number;
		this.ConteudoHeight = Number;
		this.Iniciado = new Boolean;
		this.DivCriado = new Boolean;
		this.Contador = Number;
	},
	
	Show: function(Conteudo, Width) {
	
		if(Box.Iniciado==false) { Box.Init(); } 
		
		if(Width) { Box.ConteudoWidth = Width; }
		
		if(Conteudo) {
			if (Box.DivCriado==false) {
				
				Box.Div = new Element('div', { 'class':'EstiloDiv' } );
				Box.Div.setStyle({'position':'absolute'});
				Box.Body.insert(Box.Div).show();
				Box.DivCriado = true;
				
				Box.FadeIn(Conteudo);
								
			} else {
				
				Box.Div.show();
				Box.FadeIn(Conteudo);				
			}
		} else {
			alert('Conteúdo não informado!');
		}
		
	},
	
	Alert: function(strHTML, Script, Title) {
		PulsoComBoxAlert(strHTML);
		
		if (Script) Box.Script = Script;
		else Box.Script = '';
		
		if (Title) Box.Title = Title;
		else Box.Title = 'Clube do Lar';
	},
	
	Hide: function() {
		Box.Div.update().hide();
		Box.Div.setStyle({'width':'1px', 'height':'1px'});
		
		if($('PulsoComBoxDivConteudo')) {
			$('PulsoComBoxDivConteudo').remove();
		}
	}, 
	
	FadeIn: function(Conteudo) {
		
		Box.Div.update();
		Box.ObjWidth = 1; 
		Box.ObjHeight = 1; 
		Box.CurrentTop = -1;
		Box.CurrentLeft = -1;
		Box.ConteudoURL = Conteudo
		Box.BodyWidth = Box.getPageSize()['xScroll'];
		Box.BodyHeight = Box.getPageSize()['yScroll'];
		Box.BodyLeft = document.viewport.getScrollOffsets()[0];
		Box.BodyTop = document.viewport.getScrollOffsets()[1];
		
		PulsoComBoxFadeIn(); 
	},
	
	Init: function() {
		if(document.body) {
			$(document.body).observe('click', function(event) { 
				Box.PosLeft = Event.pointerX(event);
				Box.PosTop = Event.pointerY(event); 
				Box.ElementClicked = $(event.element()); 
			} );
			
			Box.Body = $(document.body);
			Box.Iniciado = true;
		}
	},
	
	getPageSize: function() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}
		
		return( { 
			'xScroll': xScroll, 
			'yScroll': yScroll, 
			'windowWidth': windowWidth, 
			'windowHeight': windowHeight, 
			'pageHeight': pageHeight 
		} );
	},
	
	GerarSWF: function($Container, $arquivo, $largura, $altura, $id) {
		var strHTML = '<object id="globalnav-object" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + $largura + '" height="' + $altura + '" id="' + $id + '" name="' + $id + '">' + 
			'<param name="movie" value="' + $arquivo + '" />' + 
			'<param name="FlashVars" value="loc=en_US&htmlApp=false&gatewayURL=gwurl" />' + 
			'<param name="quality" value="high" />' + 
			'<param name="wmode" value="transparent" />' + 
			'<embed id="globalnav-embed" src="' + $arquivo + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" flashvars="loc=en_US&htmlApp=false&gatewayURL=gwurl" bgcolor="#ffffff" menu="false" quality="high" salign="tl" scale="noscale" id="' + $id + '" width="' + $largura + '" height="' + $altura + '"></embed>' + 
			'</object>'
		var Div = new Element('div');
		$($Container).insert(Div).update(strHTML).show();	
	},
	
	FindPos: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return [curleft, curtop];
	},
	
	AjaxUpdater: function(ConteudoURL, Container) {
		
		if(!Container){Container='PulsoComBoxDivConteudo';}
		
		new Ajax.Request(ConteudoURL, {
			method:'get',
			
			onSuccess: function(transport) {
				new Ajax.Updater(Container, ConteudoURL, { 
						method:'get', 
						evalScripts:true, 
						parameters: { NoCahe: escape(new Date()) } 
				} );
			},
			
			onFailure: function() {
				alert('Desculpe, arquivo não encontrado.');
				Box.Hide();
			}
		} );
		
	}
		
})

/* Instancia globalmente a classe */
var Box = new PulsoComBox();

var PulsoComBoxAlert = function(strHTML) {

    if(Box.Iniciado == false) { Box.Init(); } 
		
	if (Box.DivCriado == false) {
		
		Box.Div = new Element('div', { 'id':'PulsoComBoxDivBackGround' });
		Box.Body.insert(Box.Div).show();
		Box.DivCriado = true;
						
	} else { Box.Div.show(); }
	
	Box.Div.setStyle({
        'position':'absolute',
        'backgroundColor':'#000000',
        'top':'0px',
        'left':'0px',
        'width': Box.getPageSize()['xScroll'] + 'px',
        'height': Box.getPageSize()['yScroll'] + 'px'
    });
	
    Box.ConteudoWidth = 400;
    Box.ConteudoHeight = 120;
    
    Box.Contador = 0;
    FadeInAlert(strHTML);
	
}

var FadeInAlert = function(strHTML) {
    if(Box.Contador <= 7) {
    
        $('PulsoComBoxDivBackGround').setStyle({'opacity':'0.' + Box.Contador});
        Box.Contador += 2;
        window.setTimeout('FadeInAlert(\'' + strHTML.toString().replace("'","\\'") + '\')', 1);
        
    } else { 
    
        var DivConteudo = new Element('div', { 'id':'PulsoComBoxDivConteudo' });
	    DivConteudo.setStyle( {
		    'position':'absolute',
		    'top': parseInt( (Box.getPageSize()['windowHeight'] - Box.ConteudoHeight) / 2 ) + document.viewport.getScrollOffsets()['top'] + 'px',
		    'left': parseInt( (Box.getPageSize()['xScroll'] - Box.ConteudoWidth) / 2 )  + 'px',
		    'width': Box.ConteudoWidth + 'px', 
		    'minHeight': Box.ConteudoHeight + 'px'
	    } );

	    DivConteudo.innerHTML = '' + 
	        '<div class="titulo">' + Box.Title + '</div>' + 
	        '<p>' + strHTML + '</p>' + 
	        '<div class="rodape"><a href="javascript:Box.Hide();' + Box.Script + '">[OK]</a></div>';

	    Box.Body.insert(DivConteudo).show();
    }
}

var PulsoComBoxFadeIn = function() {
	
	 /* var strAlert = 'Box.ObjWidth != BodyWidth: ' + (Box.ObjWidth != Box.BodyWidth) + '\n';
	 strAlert += 'Box.ObjHeight != BodyHeight: ' + (Box.ObjHeight != Box.BodyHeight) + '\n';
	 strAlert += 'Box.PosTop > Box.BodyTop: ' + (Box.PosTop > Box.BodyTop) + '\n';
	 strAlert += 'Box.PosLeft > Box.BodyLeft: ' + (Box.PosLeft > Box.BodyLeft) + '\n';
	 alert(strAlert); */
	 
	if(Box.ObjWidth != Box.BodyWidth || Box.ObjHeight != Box.BodyHeight || Box.PosTop > Box.BodyTop || Box.PosLeft > Box.BodyLeft) {
		
		// Posição TOP (Y) do Div
		// Box.PosTop: posição top do cursor no momento do click;
		// Box.CurrentTop: proporção (em px) q o div se movimentará em direção ao topo;
		// Box.BodyTop: posição do topo da tela em relação ao scroll.
		if (Box.CurrentTop == -1) { Box.CurrentTop = parseInt( (Box.PosTop - Box.BodyTop) / Box.Frames); } 
		else { (Box.PosTop - Box.CurrentTop >= Box.BodyTop) ? Box.PosTop -= Box.CurrentTop : Box.PosTop = Box.BodyTop; }
		
		/* strAlert = 'Box.PosTop: ' + Box.PosTop + '\n';
		strAlert += 'Box.CurrentTop: ' + Box.CurrentTop + '\n';
		strAlert += 'Box.BodyTop: ' + Box.BodyTop + '\n';
		strAlert += 'Box.PosTop - Box.CurrentTop: ' + (Box.PosTop - Box.CurrentTop) + '\n';
		alert(strAlert); */
		
		
		// Posição LEFT (X) do Div
		if (Box.CurrentLeft == -1) { Box.CurrentLeft = parseInt( (Box.PosLeft - Box.BodyLeft) / Box.Frames); } 
		else { (Box.PosLeft - Box.CurrentLeft >= Box.BodyLeft) ? Box.PosLeft -= Box.CurrentLeft : Box.PosLeft = Box.BodyLeft; }
		
		// Largura (width) do Div não deve ultrapassar as dimensões da tela p/ não dar scroll 
		// horizontal durante a transição
		if (Box.ObjWidth + 200 <= Box.BodyWidth) { 
			if( (Box.ObjWidth + 200 + Box.PosLeft) > Box.BodyWidth) { Box.ObjWidth = Box.BodyWidth - Box.PosLeft; } 
			else { Box.ObjWidth += 200 - 50; }
		} else { Box.ObjWidth = Box.BodyWidth; Box.PosLeft = 0; }
		
		// Altura (height) do Div não deve ultrapassar as dimensões da tela p/ não dar scroll 
		// vertical durante a transição.
		if (Box.ObjHeight + 100 <= Box.getPageSize()['windowHeight']) { 
			if( (Box.ObjHeight + 100 + Box.PosTop) > Box.BodyHeight) { Box.ObjHeight = Box.BodyHeight - Box.PosTop; } 
			else { Box.ObjHeight += 100 - 20; }
			if(Box.ObjHeight + Box.PosTop > Box.BodyHeight - 10) { Box.ObjHeight = Box.BodyHeight - Box.PosTop - 10; }
		} else { Box.ObjHeight = Box.BodyHeight; Box.PosTop = 0;  }
		
		/* strAlert = 'Box.ObjHeight: ' + Box.ObjHeight + '\n';
		strAlert += 'Box.BodyHeight: ' + Box.BodyHeight + '\n';
		strAlert += 'Box.getPageSize()[\'windowHeight\']: ' + Box.getPageSize()['windowHeight'] + '\n';
		alert(strAlert); */
		
		// Tranparência / Opacidade gradual do Div (para Mozilla e IE7)
		var varOpacity = parseFloat(Box.ObjWidth / Box.BodyWidth);
		if (varOpacity > Box.OpacidadeMaxima) { varOpacity = Box.OpacidadeMaxima; }
		
		Box.Div.setStyle( {
			'width': Box.ObjWidth + 'px', 
			'height': Box.ObjHeight + 'px',
			'top': Box.PosTop + 'px',
			'left': Box.PosLeft + 'px',
			'opacity': varOpacity
		} );
		
		window.setTimeout('PulsoComBoxFadeIn();', Box.Velocidade);
		
	} else {
		
		//Após o fundo carregado:
		
		if (Box.ConteudoWidth==0) { Box.ConteudoWidth = 600; }

		var DivConteudo = new Element('div', { 'id':'PulsoComBoxDivConteudo' });
		DivConteudo.setStyle( {
			'position':'absolute',
			'top': (document.viewport.getScrollOffsets()['top'] + 20) + 'px',
			'left': parseInt( (Box.getPageSize()['xScroll'] - Box.ConteudoWidth) / 2 )  + 'px',
			'width': Box.ConteudoWidth + 'px', 
			'text-align':'center'
		} );

		Box.Body.insert(DivConteudo).hide();

		Box.AjaxUpdater(Box.ConteudoURL);

		Box.Body.insert(DivConteudo).show();
		
	}
}
