var ec = window.ff || {};

ec.common = (function($){
	
	function init() {
		initNewsletterSubscribe();
		initSilverbackProfiles();
	}
	
	function initNewsletterSubscribe() {
		$('#lnk-newsletter-signup').click(function(ev){
			ev.preventDefault();
			$('#newsletter-form').slideDown();
			$('#newsletter-form input.email').one('focus',function(){
				$(this).val('');
			});
			$('#newsletter-form form').submit(function(ev){
				ev.preventDefault();
				var $this = $(this);
				$.ajax({
		            type: "POST",
		            url: $this.attr('action'),
					data: $this.serializeArray(),
		            dataType: 'html',
		            success: function(html){
						$this.parent().html(html);
		            },
		            error: function(XMLHttpRequest, textStatus, errorThrown){
		                alert("Oops, sorry there was an error, please try again... ");
		            }
		        });
				return false;
			});
			return false;
		});
	}
	
	function initSilverbackProfiles()
	{
		$('a.ag-profile').click(function(ev){
			ev.preventDefault();
			var $this = $(this);
			$('div.site-overlay').fadeIn();
			$.ajax({
	            type: "GET",
	            url: $this.attr('href'),
	            dataType: 'html',
	            success: function(html){
					$('body').append($(html).css('display','none'));
					$('div.silverback-profile').css({
						'top': $(window).scrollTop()+150+'px',
						'display': 'block'
					});
					$('div.silverback-profile a.close').one('click',function(ev){
						ev.preventDefault();
						$('div.silverback-profile').remove();
						$('div.site-overlay').fadeOut();
						return false;
					});
	            },
	            error: function(XMLHttpRequest, textStatus, errorThrown){
	                alert("Oops, sorry there was an error, please try again... ");
	            }
	        });
			return false;
		});
	}
	
	return {
		init: init
	};
})(jQuery);

jQuery(document).ready(ec.common.init);
