$(function() {
	popupAccessibili();
	ieFlickerFix()
	headerSearchButton($('input#fCercaHeader'));
	hrStyle($('hr[class!="hidden"]'));
	imgDescription($('img[class="Immagine_conDidascalia"]'));
	aBigImage($('a[class*="popup"]'));
	tableStyle($('table[class]'));
	collezioneGalleryBordoStyle($('ul#collezionedarteLista li div.bordo'));
	collezioneGalleryNascondiNavigazione($('div.image-box'));
	collezioneGalleryStyle($('div.collezione-previmage, div.collezione-nextimage'));
});

/**	ieFlickerFix
 * 
 *	Fix per il bug "IE Flickering" di IE6.
 *	http://www.hedgerwow.com/360/bugs/dom-fix-ie6-background-image-flicker.html
 * 
 */
function ieFlickerFix() {
	var m =  document.uniqueID		// Solo    IE   
		 &&  document.compatMode	// Solo >= IE 6 
		 && !window.XMLHttpRequest	// Solo <= IE 6 
		 &&  document.execCommand;
	try {
		if (!!m) {
			m("BackgroundImageCache", false, true)
		}
	} catch (oops) {
		// does nothing
	};
}



function headerSearchButton(inputElement) {
	$('<div></div>')
		.text($(inputElement).val())
		.attr({
				id: 'fCercaLink'
		})
		.hover(
			function() {
				$(this).toggleClass('fCercaLinkHover');
			}
		  , function() {
				$(this).toggleClass('fCercaLinkHover');
			}
		)
		.click(function() {
			$('form#fSearchHeader').submit();
			return false;
		})
		.insertBefore($(inputElement));
	$(inputElement).hide();
}
function hrStyle(hrCollection) {
	$(hrCollection).each(function() {
		$(this)
			.appendTo(
				$('<div></div>')
					.addClass('hr')
					.insertBefore($(this))
			)
			.hide();
	});
}
function imgDescription(imgCollection) {
	$(imgCollection).each(function() {
		$(this)
			.appendTo(
				$('<div></div>')
					.addClass('ImmagineConDidascalia')
					.css({
						'float': $(this).css('float')
					  , margin: $(this).css('margin')
					  , padding: '5px'
					  , width: $(this).width() + 'px'
					})
					.insertBefore($(this))
			)
			.css({
				margin: '0px 0px 5px 0px'
			});
		$(this)
			.after(
				$('<div></div>')
					.addClass('DidascaliaImmagine')
					.width('100%')
					.text($(this).attr('title') || $(this).attr('alt'))
			);
	});
}
function aBigImage(aCollection) {
	$(aCollection).each(function() {
		$(this).click(function() {
			//cambiare target
		});
	});
}
function tableStyle(tableCollection) {
	$(tableCollection).each(function() {
		if ($(this).hasClass('Tabella_FondazioneStandard1')) {
			tableStyle_standard1(this);
		} else if ($(this).hasClass('Tabella_FondazioneStandard2')) {
			tableStyle_standard2(this);
		} else if ($(this).hasClass('Tabella_FondazioneAlternata')) {
			tableStyle_alternata(this);
		}
	});
}
function tableStyle_standard1(tableElement) {
	rowCollection = tableElement.rows;
	$(rowCollection).each(function(rowIterator) {
		dataCollection = this.cells;
		$(dataCollection).each(function(dataIterator) {
			if (dataIterator < dataCollection.length - 1) {
				//$(this).addClass('CellaTabella_internaBordoDestro');
			} else {
				$(this).addClass('CellaTabella_ultimaColonnaBordoDestro');
			}

			if (rowIterator < rowCollection.length - 1) {
				$(this).addClass('CellaTabella_internaBordoInferiore');
			} else {
				$(this).addClass('CellaTabella_ultimaRigaBordoInferiore');
			}

			fillIfEmpty(this);
		});
	});
}
function tableStyle_standard2(tableElement) {
	rowCollection = tableElement.rows;
	$(rowCollection).each(function(rowIterator) {
		dataCollection = this.cells;
		$(dataCollection).each(function(dataIterator) {
			if (dataIterator < dataCollection.length - 1) {
				$(this).addClass('CellaTabella_internaBordoDestro');
			} else {
				$(this).addClass('CellaTabella_ultimaColonnaBordoDestro');
			}

			if (rowIterator < rowCollection.length - 1) {
				$(this).addClass('CellaTabella_internaBordoInferiore');
			} else {
				$(this).addClass('CellaTabella_ultimaRigaBordoInferiore');
			}

			fillIfEmpty(this);
		});
	});
}
function tableStyle_alternata(tableElement) {
	rowCollection = tableElement.rows;
	$(rowCollection).each(function(rowIterator) {
		dataCollection = this.cells;
		$(dataCollection).each(function(dataIterator) {
			$(this).addClass('CellaTabella_' + (((dataIterator + 1) % 2 == 0) ? 'pari' : 'dispari'));
			
			if (dataIterator = dataCollection.length - 1) {
				$(this).addClass('CellaTabella_internaBordoDestro');
			} else {
				$(this).addClass('CellaTabella_ultimaColonnaBordoDestro');
			}

			if (rowIterator < rowCollection.length - 1) {
				$(this).addClass('CellaTabella_internaBordoInferiore');
			} else {
				$(this).addClass('CellaTabella_ultimaRigaBordoInferiore');
			}
			
			fillIfEmpty(this);
		});
		$(this).addClass('RigaTabella_' + (((rowIterator + 1) % 2 == 0) ? 'chiara' : 'scura'));
	});
}

function collezioneGalleryBordoStyle(divCollection) {
	$(divCollection).each(function() {
		var parentElement = this.parentNode;
		var textHeight = $('.autore', parentElement).height() 
			  + $('.titolo', parentElement).height() 
			  + $('.tecnica-misure', parentElement).height() 
			  + $('.testo', parentElement).height() 
			  + parseInt($('.testo', parentElement).css('padding-top')) 
			  + parseInt($('.testo', parentElement).css('padding-bottom'))
			  ;
		var imageHeight = ($('.image-box', parentElement).length ? $('.image-box', parentElement).height() : 0);
		if (textHeight < imageHeight) {
			$('.bordo', parentElement).width('490px')
		}
	});
}
function collezioneGalleryNascondiNavigazione(divCollection) {
	$(divCollection).each(function() {
		if ($('a', this).length > 1) {
			$('div.collezione-nextimage', this).removeClass('hidden');
		} else {
			$('div.collezione-previmage, div.collezione-nextimage', this).remove();
		}
	});
}
function collezioneGalleryStyle(divCollection) {
	$(divCollection).each(function() {
		var buttonClass = $(this).hasClass('collezione-previmage') ? 'collezione-previmage' : 'collezione-nextimage';
		$(this).hover(function() {
			$(this).css('background-image', "url('/skin/website/widgets/" + buttonClass + "-hover.png')");
		}, function() {
			$(this).css('background-image', "url('/skin/website/widgets/" + buttonClass + ".png')");
		}).click(function() {
			var galleryClass = $(this).parents('li').attr('class');
			sum = ($(this).attr('class').indexOf('prev') != -1) ? -1 : +1;
			var activeItem = $('.image-box a.' + galleryClass).filter('.active');
			activeItem.addClass('hidden').removeClass('active');
			var activeItemIndex = parseInt(activeItem.attr('id').substr(activeItem.attr('id').lastIndexOf('-') + 1));
			nextItemId = activeItem.attr('id').substr(0, activeItem.attr('id').lastIndexOf('-'));
			var maxItemIndex = parseInt(nextItemId.substr(nextItemId.lastIndexOf('-') + 1));
			var nextItemIndex;
			if (sum == -1) {
				nextItemIndex = Math.max(0, activeItemIndex + sum);
			} else {
				nextItemIndex = Math.min(maxItemIndex, activeItemIndex + sum);
			}
			nextItemId = '#' + nextItemId + '-' + nextItemIndex;
			$(nextItemId).removeClass('hidden').addClass('active');
			if (nextItemIndex == maxItemIndex) {
				$('li.' + galleryClass + ' .collezione-nextimage').addClass('hidden');
			} else if (nextItemIndex == 0) {
				$('li.' + galleryClass + ' .collezione-previmage').addClass('hidden');
			} else {
				$('li.' + galleryClass + ' .collezione-previmage').removeClass('hidden');
				$('li.' + galleryClass + ' .collezione-nextimage').removeClass('hidden');
			}
		});
	});
}

function isNearProgenitor(childElement, parentElement) {
	return ($(childElement).parents($(parentElement).nodeName).get() === parentElement);
}

function isEmpty(element) {
	return (!element.hasChildNodes()
			|| (element.hasChildNodes()
				&& element.childNodes.length == 1
				&& element.firstChild.nodeType == 3
				&& ((element.firstChild.textContent && jQuery.trim(element.firstChild.textContent) == '')
					|| (element.firstChild.innerText && jQuery.trim(element.firstChild.innerText) == ''))));
}

function fillIfEmpty(element) {
	if (isEmpty(element)) {
		$(element).text(' ');
	}
}

/*
function imageZoom(u, w, h) {
	windowParam = '';

	windowParam += (windowParam.length ? ',' : '') + 'height=' + h;
	windowParam += (windowParam.length ? ',' : '') + 'left=0';
	windowParam += (windowParam.length ? ',' : '') + 'location=no';
	windowParam += (windowParam.length ? ',' : '') + 'menubar=no';
	windowParam += (windowParam.length ? ',' : '') + 'resizable=no';
	windowParam += (windowParam.length ? ',' : '') + 'scrollbars=no';
	windowParam += (windowParam.length ? ',' : '') + 'status=no';
	windowParam += (windowParam.length ? ',' : '') + 'toolbar=no';
	windowParam += (windowParam.length ? ',' : '') + 'top=0';
	windowParam += (windowParam.length ? ',' : '') + 'width=' + w;

	openPopup(u, windowParam, '');
	return false;
}
*/