/**
 * Sidebar JS functionality
 * Auth: Chris Reeves, POKE London, 2010-06-24 (iPhone 4 day!)
 */

// JV Namespace (to be abstracted when / if we need a proper app structure)
JV = {}

// JV Sidebar Namespace
JV.sidebar = {}

// Execute methods to be run on init
JV.sidebar.init = function() {
	// Hide sidebar nav items until clicked
	$("#sidebar .sidebar-subnav").hide();
	// Show first fast facts item
	$('#sidebar #fast-facts li:first').show();
	// Bind Events
	JV.sidebar.bind_events();
	// Start Fast facts roation
	JV.sidebar.rorate_fast_facts(true);
}

JV.sidebar.bind_events = function() {
	$('#sidebar a#email').bind('click', this.toggle_email_field);
	$('#sidebar .email-form').bind('submit', this.submit_email_form);
	$('#subscribe-submit').bind('click', function() { $('#sidebar .email-form').submit(); return false; });
	$("#sidebar #sidebar-nav a.block").toggle(
	      function () {
	        	$(this).addClass('open');
				$(this).next().show();
	      },
	      function () {
	        	$(this).removeClass('open');
				$(this).next().hide();
	});
}

// Toggle Email Field
JV.sidebar.toggle_email_field = function() {
	if($('#sidebar .email-fieldset').css('display') == 'none') {
		$('#sidebar .email-fieldset').slideDown();
		$('#sidebar a#email').addClass('open');
	} else {
		$('#sidebar .email-fieldset').slideUp();
		$('#sidebar a#email').removeClass('open');
	}
	return false;
}

// Submit Email Form
JV.sidebar.submit_email_form = function() {
	data = {
		'name': $('#name-field').attr('value'),
		'email': $('#email-field').attr('value')
	}
	$.post('/subscribe.php', data, function(response) {
        var sidebar = $('#sidebar');
		if(response.errors) {
			sidebar.find('.errors').text(response.error_message).show();
		} else {
            var fieldset = sidebar.find('fieldset')
            var p = $('<p>');
            p.addClass('success');
            p.text('Your email has been accepted');
            fieldset.append(p);
            fieldset.find('input, #subscribe-submit').hide();
            setTimeout("JV.sidebar.toggle_email_field()", 5000);
		}
	}, 'json');
	return false;
}

// Rotates Fast Facts
JV.sidebar.rorate_fast_facts = function(set_height) {
	list = $('#sidebar #fast-facts ul');
	if(set_height) {
		heights = Array();
		$.each(list.find('li'), function() {
			heights.push($(this).height());
		});
		largest = 0;
		for(height in heights) {
			if(heights[height] > largest) {
				largest = heights[height];
			}
		}
		list.css('height', (largest+80) + 'px');
	}
	setTimeout(function() {
		curr = false;
		$.each(list.find('li'), function() {
			if($(this).is(":visible")) {
				curr = $(this);
			}
		});
		if(curr) {
			curr.fadeOut('fast', function() {
				if(curr.next().length != 0) {
					curr.next().fadeIn();
				} else {
					list.find('li:first').fadeIn();
				}
			});
		} else {
			list.find('li:first').fadeIn();
		}
		JV.sidebar.rorate_fast_facts(false);
	}, 8000);
}

// On Document ready, run sidebar app
$(document).ready(function() {
	JV.sidebar.init();
});
