// The Affordable Art Fair UK website - jQuery functions

$(document).ready(function() {
	
// nav functionality
	$('nav li:has(ul)').hover(function() {
		if ($(this).hasClass('selected') == false) {
			$(this).toggleClass('hover');	
		}
	});

// home page slideshow
	$('#home-slideshow #slides').before('<div id="slideshow-controls-wrapper"><div id="slideshow-controls"></div></div>').cycle({
		fx:      'scrollLeft', 
		speed:    1000, 
		timeout:  5000,
		pager:	  '#slideshow-controls'
	});
	
// galleries page image + info
	$('#galleries-list ul:first-child li:first-child').addClass('selected'); // add 'selected' class to first gallery

	function showSelectedGallery() { // show gallery image + info of 'selected' gallery
		var currentId = $('#galleries-list ul li.selected').attr('id');
		$('div.gallery').hide();
		$('#info-'+currentId).show();
	}
	
	showSelectedGallery(); // run showSelectedGallery() function on page load
	
	$('#galleries-list ul li a').hover(function() { // toggle 'selected' class on hover
		if ($(this).parent().hasClass('selected') == false) {
			$('#galleries-list ul li.selected').removeClass('selected');
			$(this).parent().toggleClass('selected');
			showSelectedGallery();
		}
	});
	
	$('#next-gallery').click(function() { // move to next gallery when clicking 'next' arrow
		if (($('#galleries-list ul li.selected').is('li:last-child')) && ($('#galleries-list ul li.selected').parent().is('ul:last-child'))) {
			$('#galleries-list ul li.selected').removeClass('selected').parent().siblings('ul:first-child').children('li:first-child').addClass('selected'); 
		} else if ($('#galleries-list ul li.selected').is('li:last-child')) {
			$('#galleries-list ul li.selected').removeClass('selected').parent().next().children('li:first-child').addClass('selected'); 
		} else {
			$('#galleries-list ul li.selected').removeClass('selected').next().addClass('selected');
		}
		showSelectedGallery();
		return false;
	});
	
	$('#prev-gallery').click(function() { // move to previous gallery when clicking 'prev' arrow
		if (($('#galleries-list ul li.selected').is('li:first-child')) && ($('#galleries-list ul li.selected').parent().is('ul:first-child'))) {
			$('#galleries-list ul li.selected').removeClass('selected').parent().siblings('ul:last-child').children('li:last-child').addClass('selected'); 
		} else if ($('#galleries-list ul li.selected').is('li:first-child')) {
			$('#galleries-list ul li.selected').removeClass('selected').parent().prev().children('li:last-child').addClass('selected'); 
		} else {
			$('#galleries-list ul li.selected').removeClass('selected').prev().addClass('selected');
		}
		showSelectedGallery();
		return false;
	});
	
// images page
	$('#thumbnail-images img').hover(function() { // replace large image when hovering over the thumbnails
		$('#large-images div.large-image').hide();
		$('#large-'+($(this).attr('id'))).show();
	});

// text pages 'lightbox' images
	$('img.artwork-image').click(function() { // show grey overlay and relevant larger image
		$('#grey-overlay').show();
		$('#large-'+($(this).attr('id'))).show();
	});
	$('#grey-overlay, a.close-button').click(function() { // hide grey overlay and image (plus email updates form - see below) when clicking on close button or on grey overlay
		$('#grey-overlay, div.artwork-image-overlay, #email-updates-overlay').hide();
		return false;
	});
	
// email updates form
	$('.email-updates-link').click(function() { // show grey overlay and email updates form
		$('#grey-overlay').show();
		$('#email-updates-overlay').show();
		return false;
	});
	
	var formOptions = {
		target: 'email-updates-response',
		url: EMAIL_URL,
	    type: 'post',
        beforeSubmit: validateForm
	}; 
	$('#email-updates-form').ajaxForm(formOptions);
	
	function validateForm() {
		if ($('#email-confirm-checkbox:checked').val() == null) {
			alert('Please confirm that want to hear from the Affordable Art Fair');
			return false;
		} else {
			$('#email-updates-form-wrapper').hide();
			$('#email-updates-thank-you').show();
			return true;
		}
	}

	$('input.default-value').each(function() { // clear default input text on focus
		var default_value = this.value;
		$(this).focus(function() {
		    if(this.value == default_value) {
		    	this.value = '';
		    }
		});
		$(this).blur(function() { // show default input text on blur
		    if(this.value == '') {
		    	this.value = default_value;
		    }
		});
	});

});
