// Function to add keyboard navigation to menu
function add_tab_navigation() {
	$('#nav a').focus( function() {
		$(this).parents('li').addClass('hover');
	});
	$('#nav a').blur( function() {
		$(this).parents('li').removeClass('hover');
	});
}

// Function to add banner cycle effect (uses jQuery Cycle plugin)
function add_banner_cycle() {
	if( $('#banner').length != 0 ) {
		$('.cycle-box + .cycle-box').css('display', 'block');
		$('#pager').empty(); // remove fallback #pager content
		$('.pager-item-text').hide();
		$('#banner .wrap').cycle({
			activePagerClass: 'current-pager-item',
			pager: '#pager',
			pagerAnchorBuilder: function(idx, dom) {
				return '<a href="#" id="pager-item-' + idx + '">' + $('.pager-item-text', dom).first().text() + '</a>';
			}
		});
	}
}

// Function to add ticker cycle effect (uses jQuery Cycle plugin)
function add_ticker_cycle() {
	if( $('#ticker').length != 0 ) {
		$('#ticker p').css('width', '100%'); // fix width
		$('#ticker .wrap').cycle({
			fx: 'scrollLeft',
			pause: 1, // pause on hover
			//speed: 2000,
			timeout: 7000
		});
	}
}

// Function to add Twitter cycle effect (uses jQuery Cycle plugin)
function add_twitter_cycle() {
	if( $('#twitter-container').length != 0 ) {
		//$('.twitter-box').css('width', '100%'); // fix width
		$('#twitter-container').cycle({
			pause: 1 // pause on hover
		});
	}
}

// Function to add tab effect
function add_tab_effect() {
	var cls = 'tab-current';
	var cls_sel = '.' + cls;
	var links = $('.tab-link');
	var content = $('.tab-content');
	$('.tab-content + .tab-content').hide();
	links.first().addClass('tab-current');
	links.click( function(event) {
		var hr = $(this).attr('href');
		event.preventDefault();
		$('.tab-content:visible').fadeOut('', function() {
			$(hr).fadeIn();
		});
		links.removeClass(cls);
		$(this).addClass(cls);
	});
}

// Function to write an error message
function error_message(message) {
	var error_id = 'contact-form-error';
	if( message == '' ) {
		$('#' + error_id).remove();
	} else {
		var existing = $('#' + error_id);
		if( existing.length != 0 ) {
			existing.text(message);
		} else {
			var error = $('<p></p>');
			error.text(message);
			error.attr('id', error_id);
			error.addClass('error');
			$('#contact-form').before(error);
		}
	}
}

// Function to validate form input
function validate_form() {
    var errors = [];
    var fields = $('#contact-form input, #contact-form textarea, #login-form input');
    fields.each( function() {
        if( $(this).hasClass('required') && $(this).val() == '' ) {
            errors.push( $(this).parent().find('input[type="hidden"].error-message').first().val() );
        } else if( $(this).hasClass('required-email') ) {
            var filter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
            var chars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
            if( (!filter.test( $(this).val() )) || $(this).val().match(chars) ) {
                errors.push( 'a valid email address' );
            }
        }
    });
    if( errors.length != 0 ) {
        message = 'Please enter ';
        message += errors[0];
        if( errors.length > 1 ) {
            if( errors.length > 2 ) {
                for( i=1; i<(errors.length-1); i++ ) {
                    message += ', ';
                    message += errors[i];
                }
            }
            message += ' and ';
            message += errors[errors.length-1];
        }
        message += '.';
        error_message(message);
        return false;
    }

}

// Function to activate form validation
function activate_validation() {
    var form = $('#contact-form');
    if ( form.length != 0 ) {
        form.submit( function() {
            return validate_form();
        });
		form.bind('reset', function() {
			error_message('');
		});
    }
}

// Functions to run on load
$(document).ready( function() {

	// Add keyboard navigation
	add_tab_navigation();

	// Add banner cycle effect
	add_banner_cycle();

	// Add twitter cycle effect
	add_twitter_cycle();

	// Add ticker cycle effect
	add_ticker_cycle();

	// Add tab effect
	add_tab_effect();

	// Activate form validation
	activate_validation();

});

