var SurveyCookie = {
  data: {},
  options: {expires: 1, domain: "", path: "", secure: false},
init: function(options, data) {
  SurveyCookie.options = Object.extend(SurveyCookie.options, options || {});
  var payload = SurveyCookie.retrieve();
        if(payload) {
            SurveyCookie.data = payload.evalJSON();
        }
        else {
            SurveyCookie.data = data || {};
        }
        SurveyCookie.store();
    },
    getData: function(key) {
        return SurveyCookie.data[key];
    },
    setData: function(key, value) {
        SurveyCookie.data[key] = value;
        SurveyCookie.store();
    },
    removeData: function(key) {
        delete SurveyCookie.data[key];
        SurveyCookie.store();
    },
    retrieve: function() {
        var start = document.cookie.indexOf(SurveyCookie.options.name + "=");

        if(start == -1) {
            return null;
        }
        if(SurveyCookie.options.name != document.cookie.substr(start, SurveyCookie.options.name.length)) {
            return null;
        }

        var len = start + SurveyCookie.options.name.length + 1;   
        var end = document.cookie.indexOf(';', len);

        if(end == -1) {
            end = document.cookie.length;
        } 
        return unescape(document.cookie.substring(len, end));
    },
    store: function() {
        var expires = '';

        if (SurveyCookie.options.expires) {
            var today = new Date();
            expires = SurveyCookie.options.expires * 86400000;
            expires = ';expires=' + new Date(today.getTime() + expires);
        }

        document.cookie = SurveyCookie.options.name + '=' + escape(Object.toJSON(SurveyCookie.data)) + SurveyCookie.getOptions() + expires;
    },
    erase: function() {
        document.cookie = SurveyCookie.options.name + '=' + SurveyCookie.getOptions() + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    },
    getOptions: function() {
        return (SurveyCookie.options.path ? ';path=' + SurveyCookie.options.path : '') + (SurveyCookie.options.domain ? ';domain=' + SurveyCookie.options.domain : '') + (SurveyCookie.options.secure ? ';secure' : '');      
    }
};
var jo_survey = Class.create({
	initialize: function(options) {
		this.options = Object.extend({
			surveyURL: null,
			delay: 1500,
			initialHeight: 160,
			initialWidth: 300,
			width: 650,
			height: 500,
			days: 5,
			debug: false
		}, options || {});
		document.observe("dom:loaded", this.initiate.bind(this));
	},
	initiate: function() {
		SurveyCookie.init({
			name: 'ActiveWebsiteSurveyCookie_' + window.location.host,
			expires: 30
		});
		this.container = null;
		this.viewportDimensions = null;
		this.containerContent = null;
		this.buildSurveyContainer();
		this.buildBtns();
		this.checkCookies();
	},
	buildSurveyContainer: function() {
		var bodyElement = $$('body')[0];
		var skin = '<div id="joSurvey" style="display:none;">';
		skin += '<div id="joSurveyClose"><a class="surveyAction" href="#" title="close survey">Exit this survey</a></div>';
		skin += '<div id="joSurveyContent">'+ '<p><strong>We are currently conducting a survey.</strong><br />We value your feedback, and would appreciate if you took a few moments to respond to some questions.</p>';
		skin += '<button class="surveyAction" id="surveyDont">Never</button><button class="surveyAction" id="surveyLater">Later</button><button class="surveyAction" id="surveyDo">Now</button>';
		skin += '</div></div>';
		bodyElement.insert({'bottom':skin});
	},
	buildBtns: function() {
		this.container = $('joSurvey');
		this.viewportDimensions = document.viewport.getDimensions();
		this.containerContent = $('joSurveyContent');
		this.container.select('.surveyAction').each(function(el) {
			if(el.id == 'surveyDont') {
				el.observe('click',this.noSurvey.bind(this));
			} else if(el.id == 'surveyLater') {
				el.observe('click',this.laterSurvey.bind(this));
			} else if (el.id == 'surveyDo') {
				el.observe('click',this.startSurvey.bind(this));
			} else {
				el.observe('click',this.closeSurvey.bind(this));
			}
		}.bind(this))
	},
	checkCookies: function() {
		var laterDate = SurveyCookie.getData('laterSurvey');
		var now = new Date().getTime();
		var noSurvey = SurveyCookie.getData('noSurvey');
		if(this.options.debug == true) {
			setTimeout(this.showIntro.bind(this), this.options.delay);			
		} else if(noSurvey == undefined || noSurvey == false) {
			if(laterDate == undefined || laterDate <= now) {
				setTimeout(this.showIntro.bind(this), this.options.delay);
			}
		}
	},
	showIntro: function() {
		var myY = (this.viewportDimensions.height - this.options.initialHeight) / 2;
		var myX = (this.viewportDimensions.width - this.options.initialWidth) / 2;
		this.container.setStyle({
			width: this.options.initialWidth + 'px',
			height: this.options.initialHeight + 'px',
			position: 'absolute',
			top: myY + 'px',
			left: myX + 'px'
		});
		this.container.appear({duration:0.5});
	},
	noSurvey: function(e){
		Event.stop(e);
		SurveyCookie.setData('noSurvey', true);
		SurveyCookie.removeData('laterSurvey');
		this.container.fade({duration:0.5});		
	},
	laterSurvey: function(e){
		Event.stop(e);
		var now = new Date().getTime();
		SurveyCookie.setData('laterSurvey', (now + (24*60*60*10*10*10*this.options.days)));
		this.container.fade({duration:0.5});
	},
	closeSurvey: function(e){
		Event.stop(e);
		this.container.fade({duration:0.5});
		if(Prototype.Browser.IE == true){
			if(parseFloat(navigator.appVersion.split("MSIE")[1]) < 7 ) {
				$$('select').each(function(el){ el.show(); });
			}
		}
	},	
	startSurvey: function(e){
		Event.stop(e);
		if(Prototype.Browser.IE == true){
			if(parseFloat(navigator.appVersion.split("MSIE")[1]) < 7 ) {
				$$('select').each(function(el){ el.hide(); });
			}
		}		
		SurveyCookie.setData('noSurvey', true);
		var myY = (this.viewportDimensions.height - this.options.height) / 2;
		var myX = (this.viewportDimensions.width - this.options.width) / 2;
		this.containerContent.hide();
		this.containerContent.update('<iframe id="surveyIframe" src="' + this.options.surveyURL + '"></iframe>');
		new Effect.Morph(this.container,{
			style: 'width:' + this.options.width + 'px; height:' + this.options.height + 'px; top:' + myY + 'px; left:' + myX + 'px;',
			duration: 0.8,
			afterFinish: function(){ this.containerContent.appear({duration:0.5}); }.bind(this)
		});		
	}
});