$(document).ready(function() {

	$(".banner").jcarousel({
		auto:5,
		scroll:1,
		animation:'slow',
		wrap:'circular',
		easing:'swing'
	});
	
	$('.btnbox').click(function(){
		location.href = $('a:eq(0)', this).attr('href');
	});
	
	$('.members').equaliser();
	
	$('a[rel=nofollow]').attr('target','_blank');
    $('#colophon label').click(function(){
		var o = $('input', this).val();
	});
	
	$('a[rel=email]').each(function(){
		var ea = $(this).text();
		var ea = ea.replace(/#/,'@');
		var ea = ea.replace(/\*/g,'.');
		$(this).text(ea).attr('href','mailto:'+ea)
	});
	
	$('#eventsInternational').hide();
	$('#eventNav a[href=#eventsInternational]').parent().addClass('deselect')
	$('#eventNav a').click(function(){
		var h = $(this).attr('href');
		$('.eventList').hide();
		$(h).show();
		$('#eventNav a').parent().addClass('deselect');
		$(this).parent().removeClass('deselect');
		return false;
	});
	
	$('.boxInner').hide();
	$('.boxChapterDetail').hide();
	
	var chapter = readCookie('chapter');
	if(chapter){
		$('#boxChapter > h2').hide();
		$('#boxChapter').show();
		$('#'+chapter).show();
		$('#chapterReset').appendTo('#'+chapter).show();
		$('.boxChapterDetail > ul').each(function(){
			if ($(this).length == 0){
				$(this).prepend('<li>No upcoming events</li>');
			}
			$('li', this).slice(5).remove();
		});
	} else {
		$('.boxInner:eq(0)').show();
		$('#chapterSelect').show();
	}
	
	$('#chapterSelect button').click(function(){
		if($('#chapterSelect select option:selected').val() == ""){
			alert('Please select your local chapter');
			return false;
		} else {
			var chapter = $('#chapterSelect select option:selected').val();
			createCookie('chapter',chapter,365);
			$('#chapterSelect').hide();
			$('#'+chapter).fadeIn();
			$('#chapterReset').appendTo('#'+chapter).show();
			$('#boxChapter > h2').hide();
			$('.boxChapterDetail > ul').each(function(){
				if ($(this).length == 0){
					$(this).prepend('<li>No upcoming events</li>');
				}
				$('li', this).slice(5).remove();
			});
			return false;
		}
	});
	
	$('#menuNext').click(function(){
		var i = $('.boxWrapper > div').index($('.boxInner:visible'));
		var n = ((i+1) == $('.boxInner').length) ? 0 : i+1;
		$('.boxInner').hide().eq(n).show();
	});
	
	$('#menuPrevious').click(function(){
		var i = $('.boxWrapper > div').index($('.boxInner:visible'));
		var n = (i == 0) ? $('.boxInner').length-1 : i-1;
		$('.boxInner').hide().eq(n).show();
	});
	
	$('#chapterReset').click(function(){
		eraseCookie('chapter');
		$('.boxChapterDetail').hide();
		$('#chapterSelect').show();
		$('#boxChapter h2').show();
	});
	$('#subForm').submit(function (e) {
        e.preventDefault();
        $.getJSON(
        this.action + "?callback=?",
        $(this).serialize(),
        function (data) {
            if (data.Status === 400) {
                alert("Error: " + data.Message);
            } else { // 200
                alert("Success: " + data.Message);
            }
        });
    });
});
