/*
TLFCB Core JavaScript
By: Junaid Bhura
www.junaidbhura.com
*/

var currentSlide = 0;
var totalSlides = 0;
var sliderDelay = 6000;
var currentGallerySlide = 1;

$(document).ready(function(){
	// Main menu
	$('#nav li').mouseenter(function(){
		if($(this).parent().attr('id') == 'nav') {
			$('#nav li').removeClass('active');
			$('.submenu').css('visibility', 'hidden');
			
			$(this).addClass('active');
			$(this).find('.submenu').css('visibility', 'visible');
		}
	});
	$('#nav li').mouseleave(function(){
		if($(this).parent().attr('id') == 'nav') {
			$('#nav li').removeClass('active');
			$('.submenu').css('visibility', 'hidden');
		}
	});
	$('#nav a').each(function(){
		if($(this).attr('href') == '#')
			$(this).addClass('dead');
	});
	$('#nav a').click(function(){
		if($(this).attr('href') == '#')
			return false;
	});
	
	// Newsletter
	$('#newsletter-link').click(function(e){
		e.stopPropagation();
		$(this).parent().addClass('active');
		$('#newsletter-subscribe-container').css('visibility', 'visible');
		return false;
	});
	$('#mc-signup').attr('value', 'email');
	$('#mc-signup').focus(function(){
		if($(this).attr('value') == 'email')
			$(this).attr('value', '');
	});
	$('#mc-signup').blur(function(){
		if($(this).attr('value') == '')
			$(this).attr('value', 'email');
	});
	$('body').click(function() {
		$('#newsletter-subscribe-container').css('visibility', 'hidden');
		$('#newsletter-link').parent().removeClass('active');
		$('#mc-message').html('');
		$('#newsletter-loading').css('visibility', 'hidden');
		$('#mc-signup').attr('value', 'email');
	});
	$('#newsletter-subscribe-container').click(function(e){
		e.stopPropagation();
	});
	$('#mc-submit').click(function(){
		$('#mc-message').html('');
		$('#newsletter-loading').css('visibility', 'visible');
		$.ajax({
			type: "POST",
			url: $('#template_url').attr('value') + "/ajax_mailchimp.php",
			data: "email=" + $('#mc-signup').attr('value'),
			success: function(msg){
				$('#mc-message').html(msg);
				$('#newsletter-loading').css('visibility', 'hidden');
			},
			error: function(){
				$('#mc-message').html('There was a problem signing you up. Please try after some time.');
				$('#newsletter-loading').css('visibility', 'hidden');
			}
		});
	});
	
	// Home page slideshow
	if($('#home-page-slide').length != 0) {
		totalSlides = $('#total-slides').attr('value');
		
		if(totalSlides > 1) {
			changeSlide();
			setInterval('changeSlide()', sliderDelay);
		}
		else
			$('#home-slide-1').css('display', 'block');
	}
	
	// Search
	if($('#search-textbox').length != 0) {
		if($('#search-textbox').attr('value') == '')
			$('#search-textbox').attr('value', 'search');
		
		$('#search-textbox').focus(function(){
			if($(this).attr('value') == 'search')
				$(this).attr('value', '');
		});
		$('#search-textbox').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'search');
		});
	}
	
	// Calendar
	if($('#schedule-link').length != 0) {
		$('#schedule-link').fancybox({
			width: 700,
			height: 600,
			autoScale: false,
			autoDimensions: false,
			onComplete: function() {
				$('#cal-calendar').fullCalendar({
					events: $.fullCalendar.gcalFeed($('#schedule-feed').attr('value')),
					
					eventClick: function(event) {
						return false;
					},
					
					loading: function(bool) {
						if (bool) {
							$('#cal-calendar').hide();
							$('#cal-loading').show();
						}else{
							$('#cal-loading').hide();
							$('#cal-calendar').show();
						}
					}
				});
			}
		});
	}
	
	// FAQs
	if($('#faq-list').length != 0) {
		$('#faq-list .question').click(function(){
			if($(this).parent().is('.active')) {
				$('#faq-list .answer').slideUp();
				$('#faq-list li').removeClass('active');
			}
			else {
				$('#faq-list .answer').slideUp();
				$('#faq-list li').removeClass('active');
				
				$(this).parent().addClass('active');
				$(this).parent().find('.answer').slideDown();
			}
			
			return false;
		});
	}
	
	// Get in touch form
	if($('#contact-form').length != 0) {
		$('#name').attr('value', 'name');
		$('#email').attr('value', 'email');
		$('#message').attr('value', 'message');
		
		$('#name').focus(function(){
			if($(this).attr('value') == 'name')
				$(this).attr('value', '');
		});
		$('#name').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'name');
		});
		
		$('#email').focus(function(){
			if($(this).attr('value') == 'email')
				$(this).attr('value', '');
		});
		$('#email').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'email');
		});
		
		$('#message').focus(function(){
			if($(this).attr('value') == 'message')
				$(this).attr('value', '');
		});
		$('#message').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'message');
		});
	}

	// Donate now form
	if($('#contact-form').length != 0) {
		$('#firstname').attr('value', 'first name*');
		$('#lastname').attr('value', 'last name*');
		$('#donateremail').attr('value', 'email*');
		$('#phone').attr('value', 'phone number');
		$('#organisation').attr('value', 'organisation');
		$('#donation-amount').attr('value', 'donation amount*');
		
		$('#firstname').focus(function(){
			if($(this).attr('value') == 'first name*')
				$(this).attr('value', '');
		});
		$('#firstname').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'first name*');
		});

		$('#lastname').focus(function(){
			if($(this).attr('value') == 'last name*')
				$(this).attr('value', '');
		});
		$('#lastname').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'last name*');
		});
		
		$('#donateremail').focus(function(){
			if($(this).attr('value') == 'email*')
				$(this).attr('value', '');
		});
		$('#donateremail').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'email*');
		});
		
		$('#phone').focus(function(){
			if($(this).attr('value') == 'phone number')
				$(this).attr('value', '');
		});
		$('#phone').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'phone number');
		});

		$('#organisation').focus(function(){
			if($(this).attr('value') == 'organisation')
				$(this).attr('value', '');
		});
		$('#organisation').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'organisation');
		});

		$('#donation-amount').focus(function(){
			if($(this).attr('value') == 'donation amount*')
				$(this).attr('value', '');
		});
		$('#donation-amount').blur(function(){
			if($(this).attr('value') == '')
				$(this).attr('value', 'donation amount*');
		});
	}
	
	// Gallery
	if($('#gallery-list').length != 0) {
		$('#gallery-list ul li a').click(function(){
			var total = $('#gallery-list ul li').length;
			var number = $(this).attr('id').replace('gallery-link-', '');
			
			// If more than one item in gallery
			if(total > 0 && currentGallerySlide != number) {
				$('.gallery-slide').fadeOut();
				$('#gallery-slide-' + number).fadeIn();
				
				currentGallerySlide = number;
				
				checkGalleryNav();
			}
			return false;
		});
		
		$('#gallery-left').click(function(){
			currentGallerySlide --;
			
			$('.gallery-slide').fadeOut();
			$('#gallery-slide-' + currentGallerySlide).fadeIn();
			
			checkGalleryNav();
		});
		
		$('#gallery-right').click(function(){
			currentGallerySlide ++;
			
			$('.gallery-slide').fadeOut();
			$('#gallery-slide-' + currentGallerySlide).fadeIn();
			
			checkGalleryNav();
		});
		
		checkGalleryNav();
	}
});

function changeSlide() {
	currentSlide++;
	
	if(currentSlide > totalSlides)
		currentSlide = 1;
	
	$('.home-slide').fadeOut('slow');
	$('#home-slide-' + currentSlide).fadeIn('slow');
}

function validateContactForm() {
	if($('#name').attr('value') == '' || $('#name').attr('value') == 'name') {
		alert('Please enter your name');
		$('#name').focus();
		return false;
	}
	if($('#email').attr('value') == '' || $('#email').attr('value') == 'email') {
		alert('Please enter your email');
		$('#email').focus();
		return false;
	}
	
	var flag = false;
	$('#subject option:selected').each(function(){
		if($(this).attr('value') != '')
			flag = true;
	});
	if(!flag) {
		alert('Please select a subject');
		return false;
	}
	
	if($('#message').attr('value') == '' || $('#message').attr('value') == 'message') {
		alert('Please enter a message');
		$('#message').focus();
		return false;
	}
	
	return true;
}

function validateDonateForm() {
	if($('#firstname').attr('value') == '' || $('#firstname').attr('value') == 'first name*') {
		alert('Please enter your first name');
		$('#firstname').focus();
		return false;
	}
	if($('#lastname').attr('value') == '' || $('#lastname').attr('value') == 'last name*') {
		alert('Please enter your last name');
		$('#lastname').focus();
		return false;
	}
	if($('#donateremail').attr('value') == '' || $('#donateremail').attr('value') == 'email*') {
		alert('Please enter your email');
		$('#donateremail').focus();
		return false;
	}
	if($('#donation-amount').attr('value') == '' || $('#donation-amount').attr('value') == 'donation amount*') {
		alert('Please enter the donation amount');
		$('#donation-amount').focus();
		return false;
	}
	
	var flag = false;
	$('#country option:selected').each(function(){
		if($(this).attr('value') != '')
			flag = true;
	});
	if(!flag) {
		alert('Please select a country');
		return false;
	}

	var flag = false;
	$('#donation-category option:selected').each(function(){
		if($(this).attr('value') != '')
			flag = true;
	});
	if(!flag) {
		alert('Please select a category of donation');
		return false;
	}
	
	return true;
}

function checkGalleryNav() {
	var total = $('#gallery-list ul li').length;
	
	if(currentGallerySlide < total)
		$('#gallery-right').css('visibility', 'visible');
	else
		$('#gallery-right').css('visibility', 'hidden');
	
	if(currentGallerySlide > 1)
		$('#gallery-left').css('visibility', 'visible');
	else
		$('#gallery-left').css('visibility', 'hidden');
}
