/*---------------------------------*/
/* General functions */
/*---------------------------------*/

// Balance right & left columns
function balanceColumns() {
	if($('#rightColumn').height() < $('#leftColumn').height()) {
		$('#rightColumn').height($('#leftColumn').height());
	}
	
	if($('#leftColumn').height() < $('#rightColumn').height()) {
		$('#leftColumn').height($('#rightColumn').height());
	}
}

// Subscription form validation
function validateSubscribeForm() {
	val = $('#ea').val();

	if(val == '' || val.indexOf('@') == -1 || val.indexOf('.') == -1) {
		alert('You must provide a valid e-mail address');
		$('#ea').focus();
		return false;
	} else {
		return true;
	}
}

// Fancybox custom title format
function formatFancyboxTitle(title, currentArray, currentIndex, currentOpts) {
	var titleHTML = '';
	titleHTML += '<div id="fancyboxTitle">';
	titleHTML += '<p>' + (title && title.length ? title : '' ) + '</p>';
	titleHTML += '</div>';

  return titleHTML;
}


/*---------------------------------*/
/* Initializacion functions */
/*---------------------------------*/

// Home page
function initHome() {
	// Cycle Featured Fairs banners
	$('#bannerFeaturedFair').cycle({
		fx : 'fade',
		timeout : 6000
	});

	// Cycle Project Space banner
	$('#projectSpaceBanner').cycle({
		fx : 'fade',
		timeout : 6000
	});

	// Balance left & right columns
	balanceColumns();
	
	// Move description & address to left & right column bottom respectively
	$('#address').vAlign();
	$('#address').offset({ top: ($('#address').offset().top + 13), lefta: $('#address').offset().left });
	$('#projectSpace').vAlign();
}

// Event page
function initEvent() {
	// Balance left & right columns
	balanceColumns();
	
	// Move address to right column bottom
	$('#address').vAlign();
	$('#address').offset({ top: ($('#address').offset().top + 13), lefta: $('#address').offset().left });

	// Configure jScrollPane form summary area
	$('#eventSummary').jScrollPane({
		scrollbarWidth	:	5,
		showArrows			: true
	});

	// Configure Fancyox for gallery images
	$('#eventGallery a[rel]').fancyboxCustom();
}

// Archives page
function initArchives() {
	// Balance left & right columns
	balanceColumns();

	// Move address to right column bottom
	$('#address').vAlign();
	$('#address').offset({ top: ($('#address').offset().top + 13), lefta: $('#address').offset().left });

	// Balance height of scroller canvas and items in the scroller
	var heightScroller = $('#leftColumn').outerHeight() - $('#archivesHeader').outerHeight() - $('#footer').outerHeight() - 15;
	$('#archivesScrollerCanvas').height(heightScroller);
	$('.archivesItem').height(heightScroller);
	
	// Configure scrollable area for archives
	$('#archivesScrollerCanvas').scrollable({
		size	:	1,
		clickable	:	false,
		prevPage	:	'.prevMain',
		nextPage	:	'.nextMain'
	}).navigator({
		navi	:	'div.navItem',
		naviItem	:	'a.active'
	}).mousewheel(400);

	// Configure scrollable area for archives navigation
	var archivesNav = $('#archivesNavigationScrollerCanvas').scrollable({
		size	:	12,
		clickable	:	false,
		prevPage	:	'.prevNav',
		nextPage	:	'.nextNav',
		api				:	true,
		onSeek		: function() { checkArchivesNavigation(this.getPageIndex(), this.getPageAmount()); },
	});

	// Hide 'Newer' & 'Earlier' navigation button
	$('#archivesNavigationNewer').fadeTo('fast', 0);

	if(archivesNav.getPageAmount() == 1) {
		$('#archivesNavigationEarlier').fadeTo('fast', 0);

		// Calculate inner elements total width
		var childrenWidth = 0;
		$('#archivesNavigationScrollerCanvas div div').each(function() { childrenWidth += $(this).outerWidth(true); });

		// Set total width for navigation links & float element right
		$('#archivesNavigationScrollerCanvas').width(childrenWidth);
		$('#archivesNavigationScrollerCanvas').css('float', 'right');
	}
}

// Artists page
function initArtists() {
	// Balance left & right columns
	balanceColumns();

	// Move address to right column bottom
	$('#address').vAlign();
	$('#address').offset({ top: ($('#address').offset().top + 13), lefta: $('#address').offset().left });

	// Configure jScrollPane for artists list
	$('#index').jScrollPane({
		scrollbarWidth	:	5,
		showArrows			: true
	});
}

// Artist page
function initArtist(arrImages, arrLabels, arrLinks) {
	// General set-up for the section
	var imageCount = 0;
	var a_images = arrImages;
	var a_links = arrLinks;
	var a_titles = arrLabels;

	// Configure jScrollPane for artist text
	$('#artistSummary').jScrollPane({
		scrollbarWidth	:	5,
		showArrows			: true
	});

	// Configure Fancyox for artist gallery images
	$('#artistGallery a[rel]').fancyboxCustom();

	// Next image action
	$('#nextImage').click(function() { 
		if(imageCount < a_images.length - 1) {
			imageCount++;
			var url = 'admin/scripts/recortarImagen.php?imagen=../../' + a_images[imageCount] + '&ancho=996&alto=440&modo=crop'; 
			var wrap = $("#imageFade").fadeTo(0, 0); 
			var img = new Image(); 

			img.onload = function() { 
				wrap.fadeTo('medium', 1); 
				wrap.find('img').attr('src', url); 
			};

			img.src = url;
			$('#imageCounter').html(imageCount + 1);
			$('#croppedImage').attr('href', a_links[imageCount]);
			$('#croppedImage').attr('title', a_titles[imageCount]);
		}

		checkNavButtons(imageCount, a_images.length);
	})

	// Previous image action
	$('#previousImage').click(function() { 
		if(imageCount > 0) {
			imageCount--;

			var url = 'admin/scripts/recortarImagen.php?imagen=../../' + a_images[imageCount] + '&ancho=996&alto=440&modo=crop'; 
			var wrap = $('#imageFade').fadeTo(0, 0); 
			var img = new Image(); 

			img.onload = function() { 
				wrap.fadeTo('medium', 1); 
				wrap.find('img').attr('src', url); 
			}; 

			img.src = url; 
			$('#imageCounter').html(imageCount + 1);
			$('#croppedImage').attr('href', a_images[imageCount]);
			$('#croppedImage').attr('title', a_titles[imageCount]);
		}

		checkNavButtons(imageCount, a_images.length);
	})

	checkNavButtons(imageCount, a_images.length);
}


/*---------------------------------*/
/* Check functions */
/*---------------------------------*/

// Check status og gallery navigation buttons
function checkNavButtons(imageCount, totalImages) {
	if(imageCount > 0){
		$('#previousImage').fadeTo('medium', 1);
	} else {
		$('#previousImage').fadeTo('medium', 0.2);
	}

	if(imageCount < totalImages - 1){
		$('#nextImage').fadeTo('medium', 1);
	} else {
		$('#nextImage').fadeTo('medium', 0.2);
	}
}

function checkArchivesNavigation(currentPage, totalPages) {
	if(currentPage == 0) {
		$('#archivesNavigationNewer').fadeTo('medium', 0);
	} else {
		$('#archivesNavigationNewer').fadeTo('medium', 1);
	}

	if(currentPage == totalPages - 1) {
		$('#archivesNavigationEarlier').fadeTo('medium', 0);
	} else {
		$('#archivesNavigationEarlier').fadeTo('medium', 1);
	}
}


/*---------------------------------*/
/* jQuery extensions */
/*---------------------------------*/

// Vertically align function
(function($) {
	$.fn.vAlign = function() {
		return this.each(function(i) {
			// Determine correction according to browser and version
			if(jQuery.browser.msie && jQuery.browser.version < 8) {
				correction = 47;
			} else {
				correction = 1;
			}

			var container = $('#container').outerHeight();
			var object = $(this).outerHeight();
			var footer = $('#footer').outerHeight();
			var newOffset = container - footer - object - correction;
			$(this).offset({ top: newOffset, left: $(this).offset().left });
		});
	};
})(jQuery);

// Custom Fancybox function
(function($) {
	$.fn.fancyboxCustom = function() {
		return this.each(function(i) {
			$(this).fancybox({
				'padding'							:	3,
				'speedIn'							:	600, 
				'speedOut'						:	200, 
				'titlePosition'				:	'over',
				'titleFormat'					: formatFancyboxTitle,
				'showCloseButton'			:	false,
				'overlayOpacity'			:	0.8,
				'overlayColor'				:	'#000',
				'hideOnContentClick'	: true,
				'onComplete'					:	function() {
					$("#fancybox-wrap").hover(function() {
						$("#fancybox-title").show();
					}, function() {
						$("#fancybox-title").hide();
					});
				}
			});
		});
	};
})(jQuery);
