$(document).ready(function() {
	
	/**********
	Project CROWNWeb Updates
	**********/
	
	$('#updates').submit(function() {
		var email = $('#email').val();
		$('input').removeClass('error');
		$('.input div.error').hide();
		
		$.get('resources/php/crownwebUpdates.php', {
			email: email,
			action: 'add',
			ajax: 'true'
		},
		// Callback data goes here
		function(data){
			if (data == 'true') {
				$('#updates').slideUp('slow', function() {
					$('#success').html('Thank you for registering for Project CROWNWeb Updates. Please check your email and click on the enclosed link to activate the updates').hide().slideDown('slow');
				});
			}
			else {
				$('#updates #email').addClass('error').siblings('.error').html(data).hide().slideDown('slow');
			}
		});
		
		return false;
	});

	/**********
	Deal with the CROWNWeb Conferences links
	**********/
	
	$('div.photo span.imgHover').hide();
	
	
	$('div.photo a').bind({
		'mouseenter focusin': function(){
			if ($.support.opacity){ 
				$(this).children('span.imgHover:not(:animated)').stop(true, true).fadeIn('fast');
			}
			else {
				$(this).children('span.imgHover').show();
			}
		},
		'mouseleave focusout': function(){
			if ($.support.opacity){ 
				$(this).children('span.imgHover').stop(true, true).fadeOut('fast');
			}
			else {
				$(this).children('span.imgHover').hide();
			}
		}
	});

	/**********
	Slideshow controls
	**********/
	
	var slideText = $('#slideText');
	var slides = $('#slides');
	var slideWidth = $('#slideContainer').width();
	
	// Get number slides
	var numTextSlides = $(slideText).children('li').length - 1;
	var numPicSlides = $(slides).children('li').length - 1;
	if (numTextSlides == numPicSlides) {
		var numSlides = numPicSlides;
	}
	else {
		var numSlides = 0;
	}
	
	$('#control span.total').html(numSlides + 1);
	
	// Variable for current slide
	var currentSlide = 0;
	
	$('.next a').click(function() {
		if (($(this).attr('class')) != 'deactivated') {
			// Change the text
			var nextSlide = currentSlide + 1;
			$(slideText).children('li').eq(currentSlide).fadeOut('fast', function() {
				$('#slideText li').eq(nextSlide).fadeIn('slow');
			});
			
			var newMargin = (nextSlide) * slideWidth * -1;
			
			$('#slides').animate({
				left: newMargin
			}, 1000);
			
			// Set current and next images
			currentSlide = nextSlide;
			if (currentSlide >= numSlides) {
				$(this).addClass('deactivated');
			}
			$('.prev a').removeClass('deactivated');
			
			$('#control span.current').html(currentSlide + 1);
			
		}
	});
	
	$('.prev a').click(function() {
		if (($(this).attr('class')) != 'deactivated') {
			// Change the text
			var prevSlide = currentSlide - 1;
			$(slideText).children('li').eq(currentSlide).fadeOut('fast', function() {
				$('#slideText li').eq(prevSlide).fadeIn('slow');
			});
			
			var newMargin = (prevSlide) * slideWidth * -1;
			
			$('#slides').animate({
				left: [newMargin, 'swing']
			}, 1000);
			
			// Set current and previous images
			currentSlide = prevSlide;
			if (currentSlide <= 0) {
				$(this).addClass('deactivated');
			}
			$('.next a').removeClass('deactivated');
			
			$('#control span.current').html(currentSlide + 1);
			
		}
	});
	
	/**********
	Latest news nav
	**********/
	var newsContent = $('#newsContent');
	
	$('a.news').live('click', function() {
		var url = $(this).attr('href');
		$(newsContent).slideUp('slow', function() {
			$(this).load(url, function() {
				$(this).slideDown('slow');
			});
		});
		return false;		
	});
	
	/**********
	Poll
	**********/
	
	var pollSubmit = $('#pollSubmit');
	var pollOptions = $('#pollForm input[type="radio"]');
	
	$(pollOptions).each(function() {
		// do something
	});
	
	if (!$(pollOptions).is(':checked')) {
		$(pollSubmit).hide();
	}
	
	if ($('#pollForm input.other[type="radio"]').is(':checked')) {
		$('#other').show();
	}
	else {
		$('#other').val('').hide();
	}

	$('#pollForm input[type="radio"]').change(function() {
		// If the user selects the "other" option
		if ($(this).hasClass('other')) {
			$('#other').slideDown('slow', function() {
				$(pollSubmit).slideDown('slow');
			});
		}
		else {
			// Slide down the poll submit button
			$(pollSubmit).slideDown('slow');
			$('#other').val('').slideUp('slow');
		}
		
	});
	
	$(pollSubmit).click(function() {
		var vote = $('input[name="option"]:checked').val();
		var other = $('#other').val();
		var poll = $('input[name="poll"]').val();
		var url = 'resources/php/pollUpdate.php?ajax=true&poll=' + poll + '&option=' + vote + '&other=' + other;
		
		$.get(url, function(data) {
			
			if (data == 'true') {
				
				// Fade out form
				$('#pollForm').slideUp('slow', function() {
					// Fade in results
					
					var results =  'poll_results.php?id=' + poll;
					
					$('#results').load(results).delay(500).hide().slideDown('slow');
				});
				
				// Set cookie so user can not vote again (unless the user is uber smart and knows how to delete cookies and then foils our plans to create the most accurate survey ever!!!)
				var cookie = 'poll' + poll;
				
				$.cookie(cookie, 'true', { expires: 365 });
			}
			else {
				alert('there was a problem :-(' + data);
			}
			
		});
		
		return false;
	});
});