var Cookies = {
	write: function(name, value, days) {
		var expires = "";
		
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = "; expires=" + date.toGMTString();
		}
		
		document.cookie = name + "=" + value + expires + "; path=/";
	},
		
	read: function(name) {
		var searchName = name + "=";
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; ++i) {
			var c = cookies[i];
			while (c.charAt(0) == ' ') {
				c = c.substring(1, c.length);
			}
			if (c.indexOf(searchName) == 0) {
				return c.substring(searchName.length, c.length);
			}
		}
		
		return null;
	},
		
	erase: function(name) {
		writeCookie(name, "", -1);
	}
};

var SiteSurvey = {
	countCookieName: 'site_survey_count',
	shownCookieName: 'site_survey_shown',
	hitLimit: 4,
		
	init: function() {
		var count = this.touchCookie();
		if (this.testCount(count)) {
			this.showSurvey();
		}
	},
		
	touchCookie: function() {
		var shown = Cookies.read(this.shownCookieName);
		if (shown) {
			return false;
		}
		var count = Cookies.read(this.countCookieName);
		count = count ? ++count : 1;
		Cookies.write(this.countCookieName, count);
		return count;
	},
	
	testCount: function(count) {
		return count >= this.hitLimit;
	},
	
	showSurvey: function() {
		var surveyUrl = '';
		window.open("http://www.surveymonkey.com/s/JZ6SYTY");
		/*document.observe('lightview:loaded', function() {
			Lightview.show({
				href: surveyUrl,
				rel: 'iframe'
			});
		});
		*/
		Cookies.write(this.shownCookieName, 1);
	}
};

document.observe('dom:loaded', function() {
	SiteSurvey.init();
});