
var Newsletter = {

	el        : null,
	slides    : null,
	visible   : false,
	curOffset : null,
	curSlide  : 0,
	startOpen : false,
	
	init : function() {
		var el = $("#subscribe");
		el.css("display", "none");
		this.slides = $(".slide", el)
		this.slides.each(
			function(index) {
				var slide = $(this);
				if(index == 0) {
					slide.css("display", "block");
				} else {
					slide.css("display", "none");
				}
			}
		);
		if(this.startOpen) {
			this.open(this.curOffset);
			this.startOpen = false;
		}
		this.el = el;
	},
	
	open : function(button) {
	
		var el = this.el;
	
		if(!this.visible)  {
			
			if(el == null) {
				this.startOpen = true;
				this.curOffset = button;
				return false;	
			}
			
			el.css("display", "block");
			
			var pos = this._getPos(button);
			
			if(!this.curOffset || this.startOpen) {
				el.css({
					top : pos.y,
					left : pos.x,
					opacity : 0
				});
			}
			
			el.animate({
				top : pos.y,
				left : pos.x,
				opacity: 1			
			}, 200);
			
			this.visible = true;
			this.curOffset = button;
			
			this.showSlide(0);
			
		}
		
	},
	
	close : function() {
		this.el.fadeTo(200, 0, 
			function() {
				$(this).css("display", "none");
			}
		);
		this.visible = false;
	},
	
	subscribe : function(email) {
		var that = this;
		this.showSlide(1,
			function() {
				$.ajax({
					type: "POST",
					url:  rootPath+"subscribe.html",
					data: {email : email },
					dataType: "json",
					success: function(msg){
						switch(msg.status) {
							case 1:
								that.showSlide(2);
								break
							case -1:
								that.error("Invalid Email!");
								break;
							case 0:
								that.error("Already Added!");
								break;
						}
					},
					error : function (XMLHttpRequest, textStatus, errorThrown) {
						that.error("There was a problem subscribing :(");
					}
				});
			}
		);
		/*$.get("/RachelsOrganic/subscribe.aspx",
			{ email: email },
			function(data){
				alert(data);
			}
		);
		this.close();*/
	},
	
	error : function(text) {
		var slide = $(".error_text", this.slides[3]);
		slide.text(text);
		this.showSlide(3);	
	},
	
	showSlide : function(index, callback) {
		var cur = this.curSlide;
		var slides = this.slides;
		if(cur != index) {
			cur_slide = $(slides[cur]);
			index_slide = $(slides[index]);
			cur_slide.animate({
				opacity: 0		
			}, 200,
				function() {
					cur_slide.css("display", "none");
					index_slide.css({
						display : "block",
						opacity : 0
					});
					$(slides[index]).animate({
						opacity: 1
					}, 200,
						function() {
							if(typeof callback == "function") {
								callback();
							}
						}
					);
				}
			);
			this.curSlide = index;
		}
	},
	
	_getPos : function(offset) {
		
		if(!offset) 
			offset = this.curOffset;
	
		var pos = $(offset).offset();
		var offpos = $(this.el.get(0).offsetParent).offset();
		
		var ox = pos.left + 40;
		var oy = pos.top + 20;
		
		var html = $("html");
		var pen = (html.width() + html.get(0).scrollLeft) - (ox + this.el.width() + 10);
		
		ox = ox - offpos.left;
		oy = oy - offpos.top;

		if(pen < 0) {
			ox += pen;
		}
		
		return { x : ox, y : oy };
		
	}
	
}

$(document).ready(
	function() {
		Newsletter.init();
	}
);
