$(function(){
	
	contentHeight();
	
	banner_rotation();
	
	galleries();

	$("a.gallery-image").fancybox({
		//'transitionIn'	:	'elastic',
		//'transitionOut'	:	'elastic',
		//'speedIn'		:	600, 
		//'speedOut'		:	200, 
		//'overlayShow'	:	false
	});
	
	jQuery.preloadCssImages();
	
	formObserve();
	
});

function contentHeight() {
	if($('#right').height() > 540)
		$('#right').addClass('max');
}

function banner_rotation() {
	if($('.image_gallery .item').length > 0) {
		setInterval('rotate_banner()', 3000);
	}
}

function rotate_banner() {
	$('.image_gallery').each(function(){
		var visible = false;
		var found = null;
		var total = $(this).find('.item').size();
		$(this).find('.item').each(function(i){
			if($(this).is(":visible")) {
				visible = true;
				found = i;
				$(this).fadeOut(1000);
			}
			if(visible && i == parseInt(found) + 1) {
				$(this).fadeIn(1000);
			}
		});
		if(found + 1 == total) {
			$(this).find('.item:first').fadeIn();
		}
	});
}

function formObserve() {
	$('form.ajax').live('submit', function(event){
		event.preventDefault();
		$('#form-loading').show();
		$.ajax({
			url: $(this).attr('action'),
			type: 'POST',
			data: $(this).serialize(),
			success: function(data){
				$('#form-loading').hide();
				$('#ajax-contact-holder').html(data);
				contentHeight();
			}
		});
	});
}

function galleries() {
	$('a.fancybox').fancybox({
		//'transitionIn'		: 'none',
		//'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
}
