jQuery(document).ready(function() {
	
	// Wrapper
	(function($) {
		// Properties
		this.timeout = 120;
		this.maxout = 500;
		this.loop = setInterval(function() { this.check(); }, 1000);
		this.test = undefined;

		// On load
		var expires = new Date();
		expires.setDate(expires.getDate()+360);
		$.cookie("nov_survey", ($.cookie("nov_survey") === null) ? "0" : $.cookie("nov_survey"), { path: "/", expires: expires });

		// On unload
		$(window).unload(function() {
			var expires = new Date();
			expires.setDate(expires.getDate()+360);
			$.cookie("nov_survey", (parseInt($.cookie("nov_survey")) !== this.maxout && parseInt($.cookie("nov_survey")) < this.timeout) ? this.test : $.cookie("nov_survey"), { path: "/", expires: expires });
		});
		
		// On resize
		$(window).resize(function() {
			$("#cover").css("width", $(window).width());
			$("#cover").css("height", $(window).height());
		});
		
		// On scroll
		$(window).scroll(function() {
			var scrollTop = document.documentElement.scrollTop || window.scrollTop || document.body.scrollTop;
			scrollTop = (scrollTop > 0) ? ((scrollTop)+($("#surveyModal").height()/2)+80)+"px" : "50%";
			
			$("#surveyModal").css("top", scrollTop);
		});

		// Check method
		this.check = function() {
			// Do not activate on chapter sites
			if(location["pathname"].indexOf("chapters") > -1) {
				clearInterval(this.loop);
				return true;
			}
			
			// Update the counter and stop the interval if the cookie is maxed out
			this.test = (typeof this.test === "undefined")? (isNaN(parseInt($.cookie("nov_survey")))) ? 0 : parseInt($.cookie("nov_survey")) : this.test+1;
			
			if(this.test === this.maxout) {
				clearInterval(this.loop);
				return true;
			}
			
			// Show modal
			if(this.test > this.timeout) {
				clearInterval(this.loop);
				
				// Create and append the content modal        
				var content = $("<div id='surveyModal'></div>");
				$("body").prepend(content);
				
				// Load in the content
				$("#surveyModal").load("/survey.html");
				
				// The animation and click events need some work...
				var scrollTop = document.documentElement.scrollTop || window.scrollTop;
				scrollTop = (scrollTop > 0) ? ((scrollTop)+($("#surveyModal").height()/2)+80)+"px" : "50%";
				$("#surveyModal").animate({top: scrollTop, marginTop: "-200px"}, "slow", "linear", function() {
				
					$("body").prepend("<div id='cover'>&nbsp;</div>");
					$("#cover").css("filter", "alpha(opacity=55)");
					$("#cover").css("width", $(window).width());
					$("#cover").css("height", $(window).height());
					$("#cover").fadeIn("slow");
					
					$(".NoThanks").click(function() {
						$("#cover").fadeOut("slow");
						$("#surveyModal").hide();
						return false;
					});
					
					$(".BeginSurvey").click(function() {
						window.open('http://www.zoomerang.com/Survey/?p=WEB229U9NMJ2RG','_blank', "modal=yes,alwaysRaised=yes, scrollbars=yes,toolbar=0, width=670, height=670, status=0");
						$("#cover").fadeOut("slow");
						$("#surveyModal").hide();
						return false;
					});
					
				});
				
				var expires = new Date();
				expires.setDate(expires.getDate()+360)
				$.cookie("nov_survey", "500", { path: "/", expires: expires });
			}
			
			return true;
		}
		
		this.check();
	})(jQuery);
});
