

/*
 * Javascript interaction events
 * -----------------------------------------------------------------------------------------------------------------------------------*/
$(function() {
	
	/*
	 * Remove  date input  label when label is clicked
	 * ----------------------------------------------------------------------------------------*/
	$('div.form-input label, div.form-textarea label').live('click',function() {
		var labelID = $(this).attr('for');
		$('#'+labelID).trigger('click');
		$(this).hide();
	});
	
	/*
	 * Remove  date input  label when input receives focus
	 * ----------------------------------------------------------------------------------------*/
	$('div.form-input input, div.form-textarea textarea').live('focus focusin',function(){
		$(this).siblings('label').hide();
	});
	
	/*
	 * Toggle  date input  label
	 * ----------------------------------------------------------------------------------------*/
	$('div.form-input input, div.form-textarea textarea').live('focusout',function(){
		var fieldText;
		
		if ( $(this).val()=='' || $(this).val()==null ) {
			
			// If the field is empty, show the label
			$(this).siblings('label').fadeIn();
			
		} else {
			
			// If the field is not empty, hide the default field text in the lavel
			$(this).siblings('label').hide();
		}
	});
	
	/*
	 * Build date chooser
	 * ----------------------------------------------------------------------------------------*/
	$('#date-input').datepicker({
		onSelect: function(dateText, inst) {
			$('div.date-input-container label').hide();
		},
		dateFormat: 'yy-mm-dd'
	});
	
	/*
	 * Toggle the name, date, phone number and description fields in the Booking form
	 * ----------------------------------------------------------------------------------------*/
	$('#email-address').live('focus',function(){
		var otherFields = $(this).parents('.form-container').children();
		otherFields.removeClass('hide');
		//if ( $('#booking .form-container .required').is(':visible') ) { $('#booking .form-container .required').hide().fadeIn() };
		if ( $('.booking-copy').is(':visible') ) { $('.booking-copy').hide() };
		console.log('test 2');
	});
	
	$('a#submit-booking-form').live('click',function(e){
		e.preventDefault();
		var container = $(this).parents('.form-container');
		if ( container.children('.name-input-container').hasClass('hide') ) {
			container.children().removeClass('hide');
			if ( $('#booking .form-container .required').is(':visible') ) { $('#booking .form-container .required').hide().fadeIn() };
			if ( $('.booking-copy').is(':visible') ) {
				$('.booking-copy').hide();
				console.log('test 1');
			}
			container.find('#email-address').focus();
			return;
		}
	});
	
	/*
	 * Turn the booking form back off
	 * ----------------------------------------------------------------------------------------*/
	$('a#close-booking-form').live('click',function(e){
		e.preventDefault();
		console.log('test3');
		if ( !$('.booking-copy').is(':visible') )  { $('.booking-copy').slideToggle(500) };
		$('div.name-input-container, div.date-input-container, div.description-textarea-container, div.close-form, div.phone-input-container').addClass('hide');
	});
	
	/*
	 * Bellydance class navigation toggling
	 * ----------------------------------------------------------------------------------------*/
	$('#second .menu li').live('click',function(){
		$('#second .menu li').removeClass('highlight');	
		$('#second .menu li').removeClass('shadow');		
		$('#second .menu li').addClass('text-shadow');			
		if ( !$(this).hasClass('highlight') ) {
			$(this).addClass('highlight');
			$(this).addClass('shadow');
			$(this).removeClass('text-shadow');
		}
	});
	
	/*
	 * Bellydance class content toggling
	 * ----------------------------------------------------------------------------------------*/
	$('#second .menu li').live('click',function(){
		$('#second .dance-class').hide();
		var classType = $(this).attr('rel').toLowerCase(); 		
		$('#second .dance-class.' + classType).fadeIn();
	 });
	
	$('a.price').live('click',function(e){
		e.preventDefault();
		var title = $(this).attr('title');
		var price = $(this).attr('rel');
		buyTicket(price,title);
	 });
	
	/*
	 * Testimonial navigation toggling
	 * ----------------------------------------------------------------------------------------*/
	$('#third .menu li').live('click',function(){
		$('#third .menu li').removeClass('highlight');	
		$('#third .menu li').removeClass('shadow');		
		$('#third .menu li').addClass('text-shadow');			
		$('#testimonials-right .ico-close').hide();
		if ( !$(this).hasClass('highlight') ) {
			$(this).addClass('highlight');
			$(this).addClass('shadow');
			$(this).removeClass('text-shadow');
			$('#testimonials-right .ico-close').fadeIn();
		}
	});
	
	/*
	 * Testimonial content toggling
	 * ----------------------------------------------------------------------------------------*/
	$('#third .menu li').live('click',function(){
		$('#third .testimonial').hide();
		var classType = $(this).attr('rel').toLowerCase(); 		
		$('#third .testimonial.' + classType).fadeIn();
	 });
	
	/*
	 * News navigation toggling
	 * ----------------------------------------------------------------------------------------*/
	$('#theend .menu li').live('click',function(){
		$('#theend .menu li').removeClass('highlight');	
		$('#theend .menu li').removeClass('shadow');		
		$('#theend .menu li').addClass('text-shadow');			
		if ( !$(this).hasClass('highlight') ) {
			$(this).addClass('highlight');
			$(this).addClass('shadow');
			$(this).removeClass('text-shadow');
		}
	});
	
	/*
	 * News content toggling
	 * ----------------------------------------------------------------------------------------*/
	$('#theend .menu li').live('click',function(){
		$('#theend .news').hide();
		var newsType = $(this).attr('rel').toLowerCase(); 		
		$('#theend .news.' + newsType).fadeIn();
	 });
	
		
	/*
	 * Initialize pagingFilter for Workshops
	 *------------------------------------------------------*/
	$('#dance-class-workshops').pagingFilter({
		itemsPerPage: 1,
		childName:'.workshop'
	});
	
	/*
	 * Initialize pagingFilter for the Testimonial subtypes
	 *------------------------------------------------------*/
	$('#testimonial-client').pagingFilter({
		itemsPerPage: 1,
		childName: '.person',
		alwaysShowNav: true,
		duration: 20000,
		transition: 1000
	});
	$('#testimonial-student').pagingFilter({
		itemsPerPage : 1,
		childName: '.person',
		alwaysShowNav: true,
		duration: 20000,
		transition: 1000
	});
	$('#testimonial-press').pagingFilter({
		itemsPerPage : 1,
		childName: '.person',
		alwaysShowNav: true,
		duration: 20000,
		transition: 1000
	});
	$('#testimonial-fan').pagingFilter({
		itemsPerPage : 1,
		childName: '.person',
		alwaysShowNav: true,
		duration: 20000,
		transition: 1000
	});
	$('#testimonial-default').pagingFilter({
		itemsPerPage : 1,
		childName: '.person',
		alwaysShowNav: true,
		duration: 20000,
		transition: 1000
	});
	
		
	/*
	 * Initialize pagingFilter for the Awards
	 *------------------------------------------------------*/
	$('#awards').pagingFilter({
		itemsPerPage : 5,
		alwaysShowNav: true,
		duration: 20000,
		transition: 1000
	});
	 
	 
	/*
	 * Scroll down
	 * ----------------------------------------------------------------------------------------*/
	$('#first .down').live('click',function(){
		$.scrollTo( $('#second'), 500 );
	});
	$('#second .down').live('click',function(){
		$.scrollTo( $('#third'), 500 );
	});
	$('#third .down').live('click',function(){
		$.scrollTo( $('#fourth'), 500 );
	});
	$('#fourth .down').live('click',function(){
		$.scrollTo( $('#theend'), 500 );
	});
	
	/*
	 * Scroll up
	 * ----------------------------------------------------------------------------------------*/
	$('#second .up').live('click',function(){
		$.scrollTo( $('#first'), 500 );
	});
	$('#third .up').live('click',function(){
		$.scrollTo( $('#second'), 500 );
	});
	$('#fourth .up').live('click',function(){
		$.scrollTo( $('#third'), 500 );
	});
	$('#theend .up').live('click',function(){
		$.scrollTo( $('#fourth'), 500 );
	});
	
	/*
	 * Scroll to section
	 * ----------------------------------------------------------------------------------------*/
	$('a#booking, a[rel="booking"]').live('click',function(e){
		e.preventDefault();
		$.scrollTo( $('#first'), 800 );
	});
	$('a#classes, a[rel="classes"]').live('click',function(e){
		e.preventDefault();
		$.scrollTo( $('#second'), 800 );
	});
	$('a#testimonials').live('click',function(e){
		e.preventDefault();
		$.scrollTo( $('#third'), 800 );
	});
	$('a#information').live('click',function(e){
		e.preventDefault();
		$.scrollTo( $('#fourth'), 800 );
	});
	$('a#blog').live('click',function(e){
		e.preventDefault();
		$.scrollTo( $('#theend'), 500 );
	});
	
	/*
	 * Initialize pagingFilter for the main video and the thumbs
	 *------------------------------------------------------*/
	$('#current_video').pagingFilter({
		itemsPerPage : 1,
		alwaysShowNav : true
	});
	
	$('#videos').pagingFilter({
		itemsPerPage : 6
	});    
	
	$('#video_container').centerme();     
	$('#form-response').centerme();

	/*
	 * Close video div
	 *------------------------------------------------------*/
	 $('#close-video').live('click',function(){
	 	$(this).parent().parent().fadeOut();
		
		// Stop other videos from playing
		var youtubeOff = $('.big-video:visible').attr('rel');
		var videoURL = $('#' + youtubeOff).attr('src');
		$('#'+youtubeOff).attr('src',videoURL);
	 });     

	/*
	 * Open video div
	 *------------------------------------------------------*/
	 $('.ico-video.ico-btn, .ico-video.ico-btn a, .booking-copy .video-thumb, a[rel="watch-videos"]').live('click',function(e){
/*		e.preventDefault();
		$videoBackground = $('#video-background');
	 	$videoBackground.hide().fadeIn();
		$videoBackground.css('visibility','visible'); */
	 });
	 
	 /*
	 * Close class div
	 *------------------------------------------------------*/
	 $('.dance-class .ico-close').live('click',function(){
		if ( !$(this).hasClass('workshop') ) { 
			$(this).parent().hide();
			$(this).parent().parent().find('.dance-class.default').fadeIn();
	   } else {
			$(this).parent().parent().hide();
			$(this).parent().parent().parent().find('.dance-class.default').fadeIn();
	   }
		
		$('#second .menu li').removeClass('highlight');	
		$('#second .menu li').removeClass('shadow');		
		$('#second .menu li').addClass('text-shadow');			
	 });     
	 
	 /*
	 * Close testimonial div
	 *------------------------------------------------------*/
	 $('#testimonials-right .ico-close').live('click',function(){
	 	$(this).hide();
		$(this).parent().find('.testimonial').hide();
		$(this).parent().find('.testimonial.default').fadeIn();
		
		$('#third .menu li').removeClass('highlight');	
		$('#third .menu li').removeClass('shadow');		
		$('#third .menu li').addClass('text-shadow');			
	 });        
	 
	 /*
	 * Close class div
	 *------------------------------------------------------*/
	 $('.news .ico-close').live('click',function(){
		$(this).parent().parent().hide();
		$(this).parent().parent().parent().find('.dance-class.default').fadeIn();
		$(this).parent().parent().find('.news.default').fadeIn();
		
		$('#second .menu li').removeClass('highlight');	
		$('#second .menu li').removeClass('shadow');		
		$('#second .menu li').addClass('text-shadow');			
	 });     
	 
	 /*
	 * Close the form response message div
	 *------------------------------------------------------*/
	$('#form-response-container').live('click',function(){	
		$(this).fadeOut();
	});
   
	 
	 /*
	 * Send the user to the mailing list
	 *------------------------------------------------------*/
	$('.ico-mailing-list').live('click',function(){
		openWindow('http://visitor.constantcontact.com/d.jsp?m=1102527925618&p=oi');
	});
	$('.ico-mailing-list a').live('click',function(e){
		e.preventDefault();
	});
});

/*
 * Handle thumbnail clicks. Make it so that the video changes
 *------------------------------------------------------*/
$('#videos div.video-thumb a').live('click',function(e){
	e.preventDefault();
	
	// Stop other videos from playing
	var youtubeOff = $('.big-video:visible').attr('rel');
	var videoURL = $('#' + youtubeOff).attr('src');
	$('#'+youtubeOff).attr('src',videoURL);
	
	// Go to the correct video
	var videoNumber = $(this).attr('href');
	$('#current_video').pagingFilter.setPagingFilter($('#current_video'),videoNumber);
	
	// console.log('1.youtubeOFf',youtubeOff);
	// console.log('videoURL',videoURL);
	
});

/*
 * Form handling
 * -----------------------------------------------------------------------------------------------------------------------------------*/
$('#submit-booking-form').live('click',function(){
										
	if ( !($('#close-booking-form').is(':visible')) ) { return; }
	
	// Initialize and cleanse variables
	var emailAddress = escape($('#email-address').val());
	var phoneNumber = escape($('#phone-number').val());
	var eventDescription = escape($('#event-description').val());
	var fullName = escape($('#full-name').val());
	var dateInput = escape($('#date-input').val());
	var lastpage = $('#lastpage').val();
	//console.log('form submit');
	
	// Show messaging
	$('#form-response-container').hide().css('visibility','visible').fadeIn();
	$('#response-text').html('Sending your event request...');
	
	// Submit the form
	$.ajax({
		type: 'POST',
		url: 'process/booking.process.php',
		data: {
			emailAddress: emailAddress,
			phoneNumber: phoneNumber,
			eventDescription: eventDescription,
			fullName: fullName,
			dateInput: dateInput,
			lastpage: lastpage
		},
		error: function() {
			//alert(response);
			//console.log(data);
		},
		success: function(data) {
			
			// Reset the the form field borders
			$('#booking input').css('border','none');
			$('#booking textarea').css('border','none');
			
			// An error is returned
			if ( data.error ) {
				$('#response-text').html('ERROR:<br>');
				for ( var x in data.error )  {
					$('#response-text').append(data.error[x]);
					$('input[name="'+x+'"]').css('border','1px solid #f00');
					$('textarea[name="'+x+'"]').css('border','1px solid #f00');
					//console.log(data);
				};
			};
			
			// Everything went well
			if ( data.success ) {
				//console.log('success msg',data.success);
				$('#response-text').html(data.success);
				$('#booking input').val('');
				$('#booking textarea').val('');
				$('#close-booking-form').trigger('click');
			};
			timer = setTimeout('hideFormResponse("")', 2000);
		},
		dataType: 'json'
	});
	
	return false;
});


/*
 * FUNCTIONS
 * -----------------------------------------------------------------------------------------------------------------------------------*/

function hideFormResponse() {
	$('#form-response-container').fadeOut();
}

/*
 * Triggers the payment through the paypal API.
 * ----------------------------------------------------------------------------------------*/
function buyTicket (tPrice,tType) {
	f = document.paypalForm;
	f.amount.value = tPrice;
	f.item_name.value = tType;
	document.paypalForm.submit();
}

/*
 * Open a URL in a new window
 * ----------------------------------------------------------------------------------------*/
function openWindow(url) {
	// Change \"_blank\" to something like \"newWindow\" to load all links in the same new window
	var newWindow = window.open(url, '_blank');
	newWindow.focus();
	return false;
}
