/* +----------------------------------------------------+ */
/* | Main JavaScript used on this site                  | */
/* +----------------------------------------------------+ */

/**
 * onLoad events 
 */
$(document).ready(function() {
	$('body').removeAttr('no-js');
	replace_email();
	form_helper();
	scrolling_link_helper();
	tour_helper();
});

/**
 * Kill parent framesets, except Wordpress preview frame 
 */
if (top.location != self.location) {
	if (self.location.indexOf('preview=1') == -1) {
		top.location.replace(self.location.href);
	}
}

/**
 * Replace certain text with email address and clickable mailto links
 */
function replace_email()
{
	var domain  = 'sphericalimages' + '.' + 'com';
	var address = 'enquiries' + '@' + domain;
	var email_placeholders = '.email_placeholder';
	$(email_placeholders).each(function() {
		var mailto = $('<a>').attr('href', 'mailto:' + address);
		$(mailto).append(address);
		$(this).append(mailto).show();
	});
}

/**
 * Handles showing/hiding tour categories in sidebar
 */
function project_nav_helper()
{
	var link = '#project_nav > ul > li > a';
	var container = '#project_nav ul ul.submenu';
	$(container).hide();
	// Menu interaction
	$(link).click(function(event) {
		if ($(this).hasClass('open')) return;
		$(container).filter('.open').slideUp();
		$('.open').removeClass('open');
		$(this).addClass('open').next(container).slideDown().addClass('open');
		event.preventDefault();
	});
	// Keep list open if it contains the current page
	$(container + ' a').filter('.current').closest(container).show().addClass('open');
}

/**
 * Functionality needed for various forms
 */
function form_helper()
{
	// Turn off HTML5 validation.
	$('form').attr('novalidate', true);
	// Show placeholder text in subscribe and search forms for older browsers
	// (Contact form placeholders handled by Contact Form 7 JavaScript)
	var fields = [ '#search input[type="search"]', '#newsletter input[type="email"]', ];
	if (!has_placeholder_support()) {
		old_browser_form_helper(fields);
	}
}

/**
 * Check if user's browser supports the HTML5 placeholder attribute.
 */
function has_placeholder_support() 
{
	var input = document.createElement('input');
	return ('placeholder' in input);
}

/**
 * Helper function for browsers that don't support HTML5 placeholder text.
 */
function old_browser_form_helper(fields)
{
	$.each(fields, function(index, field) {
		$(field).val($(field).attr('placeholder'));
		$(field).focus(function() {
			if ($(this).val() == $(field).attr('placeholder')) { 
				$(this).val(''); 
			}
		});
		$(field).blur(function() {
			if ($(this).val() == '') { 
				$(this).val($(field).attr('placeholder')); 
			}
		});
	});
}

/**
 * Functionality used on single post pages
 */
function single_helper()
{
	comment_form_helper();
}

/**
 * Adds default text to comment form and removes it upon submission
 */
function comment_form_helper()
{
	var fields = [
		'#comment_author', '#comment_email', '#comment_url', '#comment_comment'
	]
	if (!has_placeholder_support()) {
		old_browser_form_helper(fields);
	}
	// Validate
	$('#commentform').submit(function() {
		return validate_message(fields);
	});
}

function validate_message(fields)
{
	// Start with a clean slate
	$('#commentform input').removeClass('error');
	$('#commentform textarea').removeClass('error');
	// Validate fields
	var validation_status = true;
	$.each(fields, function(index, field) {
		// First check if field exists (logged in users don't have name/email/etc)
		if ($(field).length == 0) {
			// Skip to next iteration
			return true;
		}
		// Now catch fields that don't require validation
		if (field == '#comment_url') {
			if ($(field).val() == $(field).attr('placeholder')) {
				$(field).val('');
			}
			// Skip to next iteration
			return true;
		}
		// Validate the rest
		if (($(field).val() == '') || ($(field).val() == $(field).attr('placeholder'))) {
			$(field).addClass('error');
			validation_status = false;
		}
	});
	return validation_status;
}

/**
 * Provides smooth scrolling for in-page links
 */
function scrolling_link_helper()
{
	var links = 'a.scroll';
	$(links).click(function(event) {
		var target = $(this.hash);
		target = target.length && target || $('[name=' + this.hash.slice(1) +']');
		if (target.length) {
			var offset = target.offset().top;
			$('html,body').animate({ scrollTop: offset }, 800);
		}
		event.preventDefault();
	});
}

/**
 * Pulls virtual tours out of page content and inserts above into display container.
 */
function tour_helper()
{
	var tour = 'div.iframe-wrapper iframe, div.pp-embed';
	if ($(tour).length == 0) return;
	$('#viewer').empty().append($(tour));
}

/**
 * Display latest Tweets
 */
function get_twitter_feed()
{
	$('#twitter_feed').tweet({
		username: 'sphericalimages', join_text: '', avatar_size: 0, count: 2, 
		loading_text: '<img src="/wp/wp-content/themes/sphericalimages/images/loading.gif" alt="" class="loading" />'
	});
}




