// JavaScript Document
$(document).ready(function(){	
	// aplica somente se imagens encontradas
	if(typeof(POPUPLOAD) !== 'undefined') {
		// se popup foi fechado menos vezes do que pode ser apresentado e
		// se tempo de espera desde o ultimo fechamento já é maior que o tempo mínimo de espera previsto
		// então vai rodar a rotina do popup
		var tempdate = new Date();
		
		var splitDate=POPUPSTARTDATE.split('-');
		tempdate.setFullYear(splitDate[0]);
		tempdate.setMonth(splitDate[1]-1);
		tempdate.setDate(splitDate[2]);
		tempdate.setHours(0);
		tempdate.setMinutes(0);
		tempdate.setSeconds(0);
		var popupStartTime = Math.floor(tempdate.getTime()/1000);
		
		var splitDate=POPUPFINISHDATE.split('-');
		tempdate.setFullYear(splitDate[0]);
		tempdate.setMonth(splitDate[1]-1);
		tempdate.setDate(splitDate[2]);
		tempdate.setHours(0);
		tempdate.setMinutes(0);
		tempdate.setSeconds(0);
		var popupFinishTime = Math.floor(tempdate.getTime()/1000);
		
		// cancela carregamento caso a data corrente esteja fora do período previsto para exibição
		if(popupStartTime > popupServerCurtime || popupFinishTime < popupServerCurtime) {
			POPUPAUTOLOAD = false; 
		}
		//--------------------------------------------------------
		
		// caso o total de fechamentos atingiu o limite, o popup não é mais carregado automaticamente
		if(popupTotalTimesClosed >= POPUPMAXTIMESCLOSED) {
			POPUPAUTOLOAD = false;		
		} else {
			// neste ponto o popup ainda não atingiu o limite de fechamentos, então
			// verifica se já passou o tempo de espera necessário desde o último fechamento
			// antes de realizar a próxima exibição
			if(popupTotalDowntime <= POPUPWAITINGTIME) {
				POPUPAUTOLOAD = false;		
			}
		}

		if(POPUPLOAD == true) {
			$('#logo').qtip({
				content: {
					 title: {
							text: POPUPTITLE,
							button: POPUPBUTTON
					 },
						text:  POPUPCONTENT // content for popup
				},
				position: {
					 target: $(document.body), // Position it via the document body...
					 corner: POPUPCORNER // ...at the center of the viewport
				},
				show: {
					 ready: POPUPAUTOLOAD, // false means not show on ready automatically, true will show on ready itself... autoLoadLayer is a local variable loaded on php script
					 when: POPUPEVENT, // Show it on click
					 solo: true, // And hide all other tooltips
         effect: {
            type: POPUPEFFECTTYPE,
            length: POPUPEFFECTRUNTIME
         },					 
				},
				hide: false,
				style: {
					 width: { max: POPUPMAXWIDTH, min: POPUPMINWIDTH },
					 padding: POPUPPADDING,
					 border: {
							width: POPUPBORDERWIDTH,
							radius: POPUPBORDERRADIUS,
							color: POPUPBORDERCOLOR
					 },
					 name: POPUPSTYLE
				},
				api: {
					 beforeShow: function() {
							// aplica blanket somente se solicitado
							if(POPUPAPPLYBLANKET) {
								// Fade in the modal "blanket" using the defined show speed
								$('#qtip-blanket').fadeIn(this.options.show.effect.length);
							}
					 },
					 beforeHide: function() {
						 // função para registro do exato instante do fechamento do popup
						 setPopupClosingTime();
						 // Fade out the modal "blanket" using the defined hide speed
						 if(POPUPAPPLYBLANKET) {
							 $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
						 }
					 }
				}
			});
		
			function setPopupClosingTime() {
				$('<div id="qtip-background"><iframe src="./lib/popup.close.snip.php"></iframe></div>')
					.appendTo(document.body) // Append to the document body
					.hide();
			}
		}
	}
});
