// Prevent IE6 Flickering
try { window.document.execCommand('BackgroundImageCache', false, true); } catch(e) { } 
// First loop all the elements in the page that open a gallery
// Primeiro encontramos todos os elementos da página que vão abrir uma galeria
window.onload = function() {
	// For each element that has a class name of gallery
	
};
var ddGalleryObject = function() {
	var galleries = new Array();
	var elementsCreated = false;
	var currentGallery = -1;
	var currentIndex = -1;
	var progressIndicator = new Image();
	this.showThumbnails = true;
	this.backgroundColor = "#444";
	this.modalBackgroundColor = "#999";
	this.modalBackgroundOpacity = 80;
	this.navigationCssStyle = "color:#ddd; font-family:Arial, Verdana; font-size:12px;  text-decoration:none;";
	this.navigationSpliterCssStyle = "color:#ddd; font-family:Arial, Verdana; font-size:12px;  text-decoration:none;";
	this.nextText = "seguinte &gt;&gt;";
	this.previousText = "&lt;&lt; anterior";
	this.photoText = "foto";
	this.photoOfText = "de";
	this.slideshowCssStyle = "color:#fff; font-family:Arial, Verdana; font-size:11px; text-decoration:none;";
	this.closeButtonCssStyle = "color:#ddd; font-family:Arial, Verdana; font-size:11px; text-decoration:none;";
	this.titleCssStyle = "color:#fff; font-family:Arial, Verdana; font-size:12px";
	this.currentSlideCssStyle = "color:#ccc; font-family:Arial, Verdana; font-size:9px";
	this.closeButtonText = "fechar [x]";
	this.resizeTimeout = null;
	this.slideShowInterval = null;
	this.slideShowFrameTime = 3000;
	this.addGallery = function(imageArray, galleryName){
		var _gallery = new ddGallery();
		_gallery.galleryImages = imageArray;
		if (galleryName)
			_gallery.galleryName = galleryName;
		galleries.push(_gallery);
		// Preload the progress indicator
		// Fazer Preload do indicador de progresso para mostrar alguma coisa enquanto as imagens carregam
		// 
		// Before preloading it we need to know the image path
		// Mas antes temos que saber qual o caminho da imagem de preload
		var siteScripts = window.document.getElementsByTagName("script");
		for (i=0; i < siteScripts.length; i++) { if (siteScripts[i].src.indexOf('ddGallery.js') > 0) { progressIndicator.src = siteScripts[i].src.substr(0, siteScripts[i].src.length - 12) + "gallery-loader.gif"; } }
		
	}
	this.showGallery = function(galleryName, startAtImageIndex) {
		if (!elementsCreated) {
			this.createGallery();
			elementsCreated = true;
		}
		var galleryBackground = document.getElementById("galleryBackground");
		var galleryInterface = document.getElementById("galleryInterfaceWrapper");
		galleryBackground.style.display = 'block';
		galleryInterface.style.display = 'block';
		currentIndex = (startAtImageIndex) ? (startAtImageIndex - 1) : -1;
		// Find the index of the gallery that we are showing
		for (i = 0; i < galleries.length; i++) { if (galleries[i].galleryName == galleryName) { currentGallery = i; } }
		this.showNextImage();
		// Create the thumbnails
		// Criar os thumbnails
		if (this.showThumbnails) {this.createThumbs();}
	};
	this.hideGallery = function() {
		var galleryBackground = document.getElementById("galleryBackground");
		var galleryInterface = document.getElementById("galleryInterfaceWrapper");
		galleryBackground.style.display = 'none';
		galleryInterface.style.display = 'none';
	};
	this.showNextImage = function() {
		currentIndex ++;
		if (currentIndex >= galleries[currentGallery].galleryImages.length){currentIndex=0;}
		this.showImage();
		
	};
	this.showPreviousImage = function() {
		currentIndex --;
		if (currentIndex < 0){currentIndex=0;}
		this.showImage();
		
	};
	this.showImage = function(imageIndex){
		if (!isNaN(imageIndex)) { currentIndex = imageIndex; };
		var btnNext = document.getElementById("btnNavigationRight");
		var btnPrevious = document.getElementById("btnNavigationLeft");
		var galleryImage = document.getElementById("galleryImageArea");
		var titleArea = document.getElementById("galleryTitleArea");
		var gInterface = document.getElementById("galleryInterfaceWrapper");
		var currentSlide = document.getElementById("currentSlide");
		titleArea.innerHTML = galleries[currentGallery].galleryImages[currentIndex][1];
		currentSlide.innerHTML = this.photoText + " " + (currentIndex + 1).toString() + " " + this.photoOfText + " " + galleries[currentGallery].galleryImages.length.toString();
		var image = new Image();
		image.src = galleries[currentGallery].galleryImages[currentIndex][0];
		if (image.width > 0) {
			galleryImage.style.backgroundImage = "url(" + galleries[currentGallery].galleryImages[currentIndex][0] + ")";
			var interfaceNewWidth = (image.width + 20 < 240) ? 240 : (image.width);
			var interfaceNewHeight = image.height + 20;
			daydreamGallery.adjustPosition([interfaceNewWidth, interfaceNewHeight]);
		}
		else {
			galleryImage.style.backgroundImage = "url(" + progressIndicator.src + ")";
			daydreamGallery.adjustPosition([200, 100]);
			document.getElementById("galleryImageArea").style.width = '200px';
			document.getElementById("galleryImageArea").style.height = '100px';
			image.onload = function(){
				galleryImage.style.backgroundImage = "url(" + galleries[currentGallery].galleryImages[currentIndex][0] + ")";
				var interfaceNewWidth = (image.width + 20 < 240) ? 240 : (image.width);
				var interfaceNewHeight = image.height + 20;
				daydreamGallery.adjustPosition([interfaceNewWidth, interfaceNewHeight]);
			};
		}
		btnPrevious.style.visibility = (currentIndex > 0) ? "visible" : "hidden";
		btnNext.style.visibility = (currentIndex < galleries[currentGallery].galleryImages.length - 1) ? "visible" : "hidden";
		document.getElementById("navigationSplitter").style.visibility = (currentIndex > 0 && currentIndex < galleries[currentGallery].galleryImages.length - 1) ? "visible" : "hidden";
	}
	this.adjustPosition = function(newImageSize) {
		var thumbsArea = document.getElementById("galleryThumbsArea");
		if (navigator.userAgent.indexOf("Firefox")!=-1){
			thumbsArea.style.visibility = "hidden";
		}
		if (this.resizeTimeout) { window.clearTimeout(this.resizeTimeout); }
		var galleryImage = document.getElementById("galleryImageArea");
		var gInterface = document.getElementById("galleryInterfaceWrapper");
		var currentWidth = galleryImage.offsetWidth;
		var currentHeight = galleryImage.offsetHeight;
		var widthFactor = (newImageSize[0] - currentWidth) / 30;
		if (widthFactor < 1 && widthFactor > 0) {widthFactor = 1;} if (widthFactor > -1 && widthFactor < 0) {widthFactor = -1;}
		var heightFactor = (newImageSize[1] - currentHeight) / 30;
		if (heightFactor < 1 && heightFactor > 0) {heightFactor = 1;} if (heightFactor > -1 && heightFactor < 0) {heightFactor = -1;}
		var newWidth = (widthFactor > 0 ? (currentWidth + widthFactor) > newImageSize[0] : (currentWidth + widthFactor) < newImageSize[0]) ? newImageSize[0] : parseInt(currentWidth + widthFactor);
		var newHeight = (heightFactor > 0 ? (currentHeight + heightFactor) > newImageSize[1] : (currentHeight + heightFactor) < newImageSize[1]) ? newImageSize[1] : parseInt(currentHeight + heightFactor);
		gInterface.style.width = newWidth.toString() + "px";
		galleryImage.style.width = newWidth.toString() + "px";
		galleryImage.style.height = newHeight.toString() + "px";
		if (newWidth != newImageSize[0] || newHeight != newImageSize[1]) {
			this.resizeTimeout = window.setTimeout(function(){
				daydreamGallery.adjustPosition(newImageSize)
			}, 10);
		} else { thumbsArea.style.visibility = "visible"; }
		daydreamGallery.reposition();
	};
	this.slideShow = function() {
		var btnSlideShow = document.getElementById('btnSlideShow');
		if (!this.slideShowInterval) {
			this.slideShowInterval = window.setInterval(function(){
				daydreamGallery.showNextImage();
			}, this.slideShowFrameTime);
			btnSlideShow.innerHTML = "[ || ] slideshow";
		} else {
			this.slideShowInterval = window.clearInterval(this.slideShowInterval);
			btnSlideShow.innerHTML = "[ &gt; ] slideshow";
		}
	};
	this.reposition = function(){
		var galleryBackground = document.getElementById("galleryBackground");
		var gInterface = document.getElementById("galleryInterfaceWrapper");
		var ScrollTop = document.body.scrollTop;
		if (ScrollTop == 0) {
			ScrollTop = window.pageYOffset ? window.pageYOffset : ((document.body.parentElement) ? document.body.parentElement.scrollTop : 0);
		}
		galleryBackground.style.height = document.body.offsetHeight.toString() + "px";
		galleryBackground.style.width = document.body.offsetWidth.toString() + "px";
		gInterface.style.left = ((ddGetDocumentSize().width - gInterface.offsetWidth) / 2).toString() + "px";
		gInterface.style.top = (((ddGetDocumentSize().height - gInterface.offsetHeight) / 2) + ScrollTop).toString() + "px";
	}
	this.createThumbs = function() {
		var thumbsArea = document.getElementById("galleryThumbsArea");
		var thumbs = "";
		for (i = 0; i < galleries[currentGallery].galleryImages.length; i++){
			thumbs += "<a href=\"#\" onmouseout=\"this.style.backgroundColor = 'transparent';\" onmouseover=\"this.style.backgroundColor = '#fff';\" onclick=\"daydreamGallery.showImage(" + i.toString() + ");\" style=\"display:block; float:left; padding:2px;\"><img alt=\"\" style=\"width:30px; height:24px; border:0px none;\" src=\"" +  galleries[currentGallery].galleryImages[i][0] + "\" /></a>";
		}
		thumbsArea.innerHTML = thumbs + "<div style=\"clear:both; line-height:0px\"></div>";
	};
	this.createGallery = function() {
		// Create the elements that compose the gallery
		// Criar os vários elementos de HTML que compõem a galeria
		var background = document.createElement("div");
		background.setAttribute("id", "galleryBackground");
		background.style.display = "none";
		background.style.opacity = this.modalBackgroundOpacity * 0.01;		
		background.style.filter = "alpha(opacity=" + this.modalBackgroundOpacity.toString() + ")";
		background.style.width = "100%";
		background.style.height = ddGetDocumentSize().height.toString() + "px";
		document.body.style.height = "100%";
		background.style.backgroundColor = this.modalBackgroundColor;
		background.style.position = "absolute";
		background.style.top = "0px";
		background.style.left = "0px";
		background.style.zIndex = 10000;
		document.body.appendChild(background);
		// Now that we have the background we can create the interface
		// Agora que temos o background podemos criar a interface
		var placeHolder = document.createElement("div");
		var galleryInterfaceWrapper = "<div id=\"galleryInterfaceWrapper\" style=\"display:none; padding:10px; background:" + this.backgroundColor + "; z-index:10001; width:240px; position:absolute\">";
		var interfaceCenter = "<div id=\"interfaceCenter\" style=\"margin:0px auto;\">"
		var headerString = "<div style=\"height:15px\"><a id=\"btnSlideShow\" style=\"display:block; float:left; " +
			this.slideshowCssStyle + "\" href=\"#\" onclick=\"daydreamGallery.slideShow();\">[ &gt; ] slideshow</a><a id=\"btnClose\" href=\"#\" onclick=\"daydreamGallery.hideGallery();\" style=\"display:block; float:right; " + this.closeButtonCssStyle + "\">" + this.closeButtonText + "</a></div>";
		var galleryImageArea = "<div id=\"galleryImageArea\" style=\"background-repeat:no-repeat; background-position:center center;\"></div>";
		var galleryNavigation = "<div id=\"galleryTitleArea\" style=\"padding-bottom:5px; " + this.titleCssStyle + "\"></div>";
		var thumbsArea = "<div id=\"galleryThumbsArea\" style=\"padding-bottom:10px\"></div>";
		var navigationArea = "<div><div id=\"currentSlide\" style=\"display:block; float:left;" + this.currentSlideCssStyle + "\"></div><a id=\"btnNavigationRight\" href=\"#\" onclick=\"daydreamGallery.showNextImage();\" style=\"visibility:hidden; float:right; " + this.navigationCssStyle + "\">" + this.nextText + "</a><span id=\"navigationSplitter\"style=\"visibility:hidden; float:right; " + this.navigationSpliterCssStyle + "\">&nbsp;&nbsp;|&nbsp;&nbsp;</span><a id=\"btnNavigationLeft\" href=\"#\" onclick=\"daydreamGallery.showPreviousImage();\" style=\"visibility:hidden; float:right; " + this.navigationCssStyle + "\">" + this.previousText + "</a><div style=\"clear:both; line-height:0px;\"></div></div>";
		placeHolder.innerHTML = galleryInterfaceWrapper + interfaceCenter +
			headerString + galleryImageArea + galleryNavigation + thumbsArea + navigationArea +
			"</div></div>";
		document.body.appendChild(placeHolder);
		window.onresize = function(){ daydreamGallery.reposition(); }
		window.onscroll = function(){ daydreamGallery.reposition(); }
	}
};
var lastGalleryId = 0;
var ddGallery = function(){
	this.galleryImages = new Array();
	lastGalleryId++;
	this.galleryName = "ddgallery_" + lastGalleryId.toString();
	this.galleryCurrentIndex = 0;
};
function ddGetScrollTop() { var ScrollTop = document.body.scrollTop; if (ScrollTop == 0) { ScrollTop = window.pageYOffset ? window.pageYOffset : ((document.body.parentElement) ? document.body.parentElement.scrollTop : 0); }; return ScrollTop; }
function ddGetDocumentSize() { var myWidth = 0, myHeight = 0; if( typeof( window.innerWidth ) == 'number' ){ myWidth = window.innerWidth; myHeight = window.innerHeight; }else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){ myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight; }else if( document.body && ( document.body.clientWidth || document.body.clientHeight) ){ myWidth = document.body.clientWidth; myHeight = document.body.clientHeight; }return { width: myWidth, height: myHeight}; }var daydreamGallery = new ddGalleryObject();