var heroSlot = {
	current : 0,
	busy : false,

	init : function() {
		heroSlot.setCycle();

		$('#hero-slot .nav.right').click(function(){
			if(!heroSlot.busy)
				$('#hero-slot UL').cycle('next');

			return false;
		});

		$('#hero-slot .nav.left').click(function(){
			if(!heroSlot.busy) {
				$('#hero-slot UL').cycle({
					fx :'scrollRight',
					startingSlide : heroSlot.current,
					before : heroSlot.onBefore,
					after : heroSlot.onAfterPrev
				});

				$('#hero-slot UL').cycle('prev');
			}

			return false;
		});
	},

	setCycle : function() {
		$('#hero-slot UL').cycle({
			fx : 'scrollLeft',
			timeout : 5000,
			pause : true,
			startingSlide : heroSlot.current,
			before : heroSlot.onBefore,
			after : heroSlot.onAfter
		});
	},

	onBefore : function(curr, next, opt) {
		heroSlot.busy = true;
	},

	onAfter : function(curr, next, opt) {
		heroSlot.current = opt.currSlide;
		heroSlot.busy = false;
	},

	onAfterPrev : function(curr, next, opt) {
		heroSlot.busy = false;

		if (heroSlot.current != opt.currSlide) {
			heroSlot.current = opt.currSlide;
			heroSlot.setCycle();
		}
	}
}

var signup = {
	init : function() {
		$('#signup-form INPUT.text').each(function() {
			$(this).data('orig', $(this).val());
		});

		$('#signup-form').submit(function() {
			var data = {
				name : ($('#signup-name').val() != $('#signup-name').data('orig')) ? $('#signup-name').val():'',
				email : ($('#signup-email').val() != $('#signup-email').data('orig')) ? $('#signup-email').val():''
			};

			$.ajax({
				url : '/newsletter-signup',
				data : data,
				dataType : 'json',
				success : function(errors) {
					var i = 0;

					$('#signup-form INPUT.text').each(function() {
						var id = $(this).attr('id');

						if(errors[id]) {
							$(this).addClass('error');
							$('#'+id+'-error').text(errors[id]);
							i++;
						}
						else {
							$(this).removeClass('error');
							$('#'+id+'-error').text('');
						}
					});

					if(!i) {
						$('#footer-signup .signup').empty();
						$('#footer-signup .signup').html('<label class="success"><span>Thank you!</span> You have been registered to receive our newsletter.</label>');
						cufon.refresh();
					}
				}
			});

			return false;
		});

		$('#signup-form INPUT.text').live('blur', function() {
			if(!$(this).val()) {
				$(this).val($(this).data('orig'));
				$(this).removeClass('focus');
			}
		});

		$('#signup-form INPUT.text').live('focus', function() {
			if($(this).val() == $(this).data('orig')) {
				$(this).val('');
				$(this).addClass('focus');
			}
		});
	}
}

$(document).ready(function(){
	heroSlot.init();
	signup.init();

	$("#header-nav LI").hover(
		function() {$(this).find("ul.subnav").stop(true, true).fadeIn('fast').show();},
		function() {$(this).find("ul.subnav").stop(true, true).fadeOut('1000');}
	);
});
