$(document).ready(function() { survey.init(); });

var survey = {
	
	init : function() {
		if( $('#survey-popup').is('div') ) {
			$('#survey-overlay').width( $(document).width() );
			$('#survey-overlay').height( $(document).height() );
			$('#tweets').css({
				overflow: 'hidden',
				width: '0px'
			});
			$('#survey-overlay').fadeTo(250,0.6, function() {
				$('#survey-popup').fadeIn(250);
			});
			
			$('#survey-page-1 a.btn-no:first').click( survey.hide );
			$('#survey-page-1 a.btn-yes:first').click( survey.page2 );	
			$('#survey-submit').click( survey.submit );			
		}
	},
	
	submit : function() {
		var valid = true;
		$('#survey-popup form:first div.survey-question').each(function() {
			if( !$(this).find('input[type="radio"]:checked').is('input') ) { valid = false; }
		});
		
		if( valid ) {
		
			var data = $('#survey-popup form:first').serialize()
			$.ajax({
				url: '/enkat-fran-lafa/',
				data: data,
				dataType: 'html',
				type: 'post',
				success : function(data) {
					data = jQuery.trim(data);
					$('#survey-page-2').html(data);
					$('#survey-page-2').addClass('survey-page-last');
					$('#close-survey').click( survey.hide );
				}
				
			});
		
		} else {
			$('#survey-popup p.survey-error').fadeIn(250);
		}
		
		return false;
	},
	
	page2 : function() {
		$('#survey-page-1').hide();
		$('#survey-page-2').show();
		return false;
	},
	
	hide : function() {
		$('#survey-popup').fadeOut(250, function() {
			$(this).remove();
			$('#survey-overlay').fadeOut(250, function() {
				$('#tweets').css({
					overflow: 'visible',
					width: 'auto'
				});
				$(this).remove();
			});
		});

		// Set cookie
  	var exdate = new Date();
  	var exdays
  	exdate.setDate(exdate.getDate() + 365);
  	var c_value = 'yes; expires='+exdate.toUTCString();
  	document.cookie = 'survey' + "=" + c_value;		
		
		return false;
	}

}
