/*
 * Use this object to execute a callback function after a specified time
 * during a "session".
 * author: dz_fl@dmb.at (231448Bsep11)
 *
 * public functions:
 *  - survey.launch(callback, [timer]);
 *      callback: (required) a function (can be anonymous)
 *                that will be executed after a certain
 *                time span
 *      timer:    (optional) waiting time in minutes before
 *                the execution of the callback function
 *  - survey.set_cookie_prefix(prefix);
 *      prefix:   (required) prefix the cookie keys
 */
var survey = (function() {
	var callback, survey_performed, survey_start;
	var survey_init = true;
	var survey_timer = 3; // 3 Minutes default
	var cookie_prefix = "9c7U_"; // default cookie prefix
	
	// set_cookie/get_cookie are shamelessly inspired from $.cookie (https://github.com/carhartl/jquery-cookie)
	function set_cookie(key, value, options) {
		options = options || {}; // Use default options
		if (value == null) // Delete cookie
			options.expires = -1;
		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}
		value = String(value);
		document.cookie = [
			encodeURIComponent(cookie_prefix + key), '=',
			options.raw ? value : encodeURIComponent(value),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join('');
	}
	function get_cookie(key) {
		return (result = new RegExp('(?:^|; )' + encodeURIComponent(cookie_prefix + key) + '=([^;]*)').exec(document.cookie)) ? decodeURIComponent(result[1]) : null;
	}
	// As soon as the callback is executed the user will never be bothered again with this survey, except he deletes the cookie!
	function survey_started() {
		// remove
		set_cookie("survey_start", null);
		set_cookie("survey_performed", true, {expires: 999, path: '/', domain: document.domain});
	}
	return {
		launch: function(callback, timer) {
			// Check if the survey was done already! 'callback' is required and must be a function
			var survey_performed = get_cookie("survey_performed");
			if (callback && typeof callback == 'function' && (survey_performed == null || survey_performed != 'true' ) && survey_init) {
				survey_timer = ((timer && Number(timer) > 0) ? Number(timer) : survey_timer) * 60*1000;
				survey_start = (result = get_cookie("survey_start")) ? new Date(Number(result)) : new Date();
										
				var now = new Date();
				var survey_timer_left = now - survey_start;
				if ( survey_timer_left > survey_timer ) {
					// this is a visitor who left before the survey was triggered -> restart
					survey_start = now;
				} else if ( (survey_timer_left + 10000) > survey_timer) {
					// this would trigger to soon after page load, add 10 seconds to the timer
					survey_start = new Date(now.getTime() + 10000);
				}
				set_cookie('survey_start', survey_start.getTime(), {expires: 7, path: '/', domain: document.domain});
				survey_end = new Date(survey_start.getTime() + (survey_timer));
				
				setTimeout( // Do the magic! ;)
					// wrap the callback with an anonymous function so we can add our own callback here as well
					function() {
						callback();
						survey_started();
					},
					survey_end - now
				);
				survey_init = false;
				return true;
			}
			return false;
		},
		set_cookie_prefix: function(prefix) {
			if (survey_new)
				cookie_prefix = prefix || cookie_prefix;
		},
		set_survey_performed: function() {
			set_cookie("survey_performed", true, {expires: 999, path: '/', domain: document.domain});
		}
	}	
})();
