/**
 * @author Chris Yap
 * SURVEY CONTROLLER for SFIAFF
 * Note: This object contains logic branches for SFIAFF.  The reason is the main site uses prototype whereas the SFIAFF site uses
 * jQuery.  Therefore we do not have access to prototype functionality on the SFIAFF site.  We should really fix this.
 */


var SurveyController = Class.create();

Object.extend(SurveyController.prototype, {
	initialize: function(surveyVersion){
		
		this.surveyVersion = (surveyVersion == null) ? '' : surveyVersion;

		this.currentClickCount = Cookie.get('caamSurveyClickCount');
		this.surveyLaunched = Cookie.get('caamSurveyFinished');
		
		if (this.surveyLaunched == 'yes') {
			return
		}
		
		if (this.currentClickCount == null) {
			this.setFirstCookie();
		}
		
		else {
			this.determineCount();
		}
		
		// set event to handle window resize and scroll
		this.scrollCache = this.handleWindowScroll.bindAsEventListener(this); // necessary to cache the function for proper unregistration later
		
		if (this.surveyVersion == 'sfiaff09') {
			Event.observe(parent.document, 'scroll', this.scrollCache);
		}
		
		else {
			Event.observe(window, 'scroll', this.scrollCache);	
		}
		
	},
	
	setFirstCookie: function() {
		Cookie.set('caamSurveyClickCount','0',null,'/','asianamericanmedia.org');
		Cookie.set('caamSurveyFinished','no',365,'/','asianamericanmedia.org');
	},
	
	determineCount: function() {
		this.currentClickCount = parseInt(this.currentClickCount);
		this.currentClickCount = this.currentClickCount + 1;
		
		if (this.currentClickCount >= 2) {
			this.launchSurvey();
		}
		
		else {
			this.incrementCookieCount();
		}
	},
	
	incrementCookieCount: function() {
		Cookie.set('caamSurveyClickCount',this.currentClickCount,null,'/','asianamericanmedia.org');
	},
	
	launchSurvey: function() {
		// launch survey here
		if (this.surveyVersion == 'sfiaff09') {
			parent.document.getElementById('caamSurvey').style.display = 'block';
		}
		
		else {
			$('caamSurvey').style.display = 'block';	
		}
		
		Cookie.set('caamSurveyClickCount',this.currentClickCount,null,'/','asianamericanmedia.org');
	},
	
	killSurvey: function() {
		Cookie.set('caamSurveyFinished','yes',365,'/','asianamericanmedia.org');
		
		if (this.surveyVersion == 'sfiaff09') {
			parent.document.getElementById('caamSurvey').style.display = 'none';
		}
		
		else {
			$('caamSurvey').style.display='none';	
		}
	},
	
	delaySurvey: function() {
		Cookie.set('caamSurveyClickCount','0',null,'/','asianamericanmedia.org');
		
		if (this.surveyVersion == 'sfiaff09') {
			parent.document.getElementById('caamSurvey').style.display = 'none';
		}
		
		else {
			$('caamSurvey').style.display='none';	
		}
	},

	handleWindowScroll: function(){
		
		// set message position based on viewport
		
		if (this.surveyVersion == 'sfiaff09') {
			var viewport_y_offset = parent.getScrollY();
			var viewport_height = parent.getWindowHeight();
			var module_height = parent.document.getElementById('caamSurvey').offsetHeight;
			parent.document.getElementById('caamSurvey').style.top = viewport_y_offset + (viewport_height/2) - module_height + 'px';
		}
		
		else {
			var viewport_y_offset = getScrollY();
			var viewport_height = document.viewport.getHeight();
			var module_height = $('caamSurvey').getHeight();
			$('caamSurvey').style.top = viewport_y_offset + (viewport_height/2) - module_height + 'px';	
		}
		

	}

});