﻿/// <reference path="../includes/jquery/jquery-1.7.1.min.js" />
/// <reference path="../includes/jquery/fancybox/jquery.fancybox-1.3.4.js" />

$(document).ready(function () {
	//reframe("<%=mySett.my_site_root %>");
	$("a[rel^='prettyPhoto']").prettyPhoto({allow_resize: false});
	$("a").each(function () { if (this.href.indexOf("youtube.com") != -1) { $(this).prettyPhoto(); }; });
	$("img[id*='maxPhoto']").each(function () {
		$(this).load(function () {
			$(this).css("visibility", "visible");
			checkIfStartPreload(this);
		}).trigger('load');
	});
	$(".maxPhoto").each(function () { maxPhoto(this); });
	$("a.fancy").fancybox({
		'hideOnContentClick': true
	});
	$('#spotInline').fancybox();
	window.setTimeout(function(){$("#spotInline").fancybox().trigger('click')}, 3000);
	// [precaricamento immagini in aree del sito diverse da collezioni]
	if (preloadStr != "") {
		imgArr = preloadStr;
		startPreload2();
	}
	// [/precaricamento immagini in aree del sito diverse da collezioni]
});

function preloadMain(imgPbj) {
	var tstImage = new Image();
	tstImage.src = imgPbj.src.replace("small_", "");
	tstImage.onload = function () {
		// preload terminato
	}
}

$(window).resize(function () {
	$(".maxPhoto").each(function () { maxPhoto(this); });
});

var origWidth = 0; // indica la larghezza originale dell'immagine
var origHeight = 0; // indica l'altezza originale dell'immagine

function maxPhoto(photoObj) {
	// [visualizza gif preload]
	$("#middleBar").css("background-image", "url(" + siteRoot + "images/ajax-loader.gif)");
	$("#middleBar").css("background-position", "center");
	$("#middleBar").css("background-repeat", "no-repeat");
	// [/visualizza gif preload]
	if (parseInt($(photoObj).height()) > 100) {
		if (origWidth == 0) origWidth = $(photoObj).width();
		if (origHeight == 0) origHeight = $(photoObj).height();
		var maxAvailWidth = $(photoObj).parent().width();
		var maxAvailHeight = $(photoObj).parent().height();
		var actImgWidth = $(photoObj).width();
		var actImgHeight = $(photoObj).height();
		var newImgWidth = 0;
		var newImgHeight = 0;
		// calcolo la larghezza dell'immagine nel caso usassi per essa tutta l'altezza disponibile
		var newImgWidth = parseInt((maxAvailHeight * actImgWidth) / actImgHeight);
		if (newImgWidth > maxAvailWidth) {
			newImgWidth = maxAvailWidth;
			newImgHeight = parseInt((actImgHeight * newImgWidth) / actImgWidth);
		}
		else {
			newImgHeight = maxAvailHeight;
		}
		if (newImgHeight < origHeight && newImgWidth < origWidth) // <- se le nuova dimensione calcolata non supera la max. dimensioen possibile...
		{
			$(photoObj).width(newImgWidth);
			$(photoObj).height(newImgHeight);
		}
	}
	else
		window.setTimeout(function () { maxPhoto(photoObj); }, 100);
}

function checkIfStartPreload(imgObj) {
	// [verifica se è il momento di iniziare il preload e allo stesso tempo imposta l'array di preload]
	if (imgObj != null) {
		imgArr += imgObj.src.replace("small_", "") + "^";
		if ($('a[id*="stampaLnk"]').length > 0) {
			$('a[id*="stampaLnk"]').css("display", "none");
		}
	}
	anteNumb--;
	if (anteNumb <= 0) { startPreload(); }
	// [/verifica se è il momento di iniziare il preload e allo stesso tempo imposta l'array di preload]
}

function startPreload() {
	if ($('a[id*="stampaLnk"]').length > 0) {
		$('a[id*="stampaLnk"]').css("display", "inline");
	}
	// [attiva il processo di preload delle immagini principali]
	$(".maxPhoto").each(function () {
		if (IsImageOk(this)) {
			if (imgArr != "") {
				var preloadImage = new Image();
				preloadImage.src = imgArr.substring(0, imgArr.indexOf("^"));
				preloadImage.onload = new function () {
					if (IsImageOk(preloadImage)) {
						//alert(preloadImage.src);
						imgArr = imgArr.substring(imgArr.indexOf("^") + 1, imgArr.length);
						startPreload();
					}
					else {
						window.setTimeout("startPreload()", 1000);
					}
				};
			}
		}
		else {
			window.setTimeout("startPreload()", 1000);
		}
	});
	// [attiva il processo di preload delle immagini principali]
}

function startPreload2() {
	// [attiva il processo di preload delle immagini principali (in aree diverse da collezioni)]
	if (imgArr != "") {
		var preloadImage = new Image();
		preloadImage.src = imgArr.substring(0, imgArr.indexOf("^"));
		preloadImage.onload = new function () {
			if (IsImageOk(preloadImage)) {
				//alert(preloadImage.src);
				imgArr = imgArr.substring(imgArr.indexOf("^") + 1, imgArr.length);
				startPreload2();
			}
			else {
				window.setTimeout("startPreload2()", 1000);
			}
		};
	}
	// [attiva il processo di preload delle immagini principali]
}

function IsImageOk(img) {
	// During the onload event, IE correctly identifies any images that
	// weren't downloaded as not complete. Others should too. Gecko-based
	// browsers act like NS4 in that they report this incorrectly.
	if (!img.complete) {
		return false;
	}

	// However, they do have two very useful properties: naturalWidth and
	// naturalHeight. These give the true size of the image. If it failed
	// to load, either of these should be zero.
	if (typeof(img.naturalWidth) != "undefined" && img.naturalWidth == 0) {
		return false;
	}

	// No other way of checking: assume it's ok.
	return true;
}

