var MAS = {
	current_step:0, //used on entry page to track which step the user is on
	current_survey_page:0, //used on survey to track which page the user is on
	survey_errors:0, // records the number of validation errors on survey validation
	has_selected_file: false, //used for validating that a user has selected a file to upload at the last step
	validator: {}, //saves the jQuery validator result
	quotes: Array("Graze where you will, you shall not house with me!", "This is the very painting of your fear.", "Men should be what they seem.", "Neither a borrower nor a lender be.", "Love look not with the eyes, but with the mind."),
	
	mycarousel_initCallback: function(carousel) {

			jQuery('#latest-right').bind('click', function() {
					carousel.next();
					return false;
			});

			jQuery('#latest-left').bind('click', function() {
					carousel.prev();
					return false;
			});
	},

	mycarousel_buttonNextCallback: function(carousel, button, enabled) {
			display('Next button is now ' + (enabled ? 'enabled' : 'disabled'));
	},
	
	mycarousel_buttonPrevCallback: function(carousel, button, enabled) {
			display('Prev button is now ' + (enabled ? 'enabled' : 'disabled'));
	},
	
	assignTogglers: function(){
		$("#competition .content-block h4 a.toggler").click(function(e){
			e.preventDefault();
			$(this).parent().next().toggle();
			if($(this).parent().next().is(':visible')){
				$(this).removeClass('arrow-up');
				$(this).addClass('arrow-down');
			}else{
				$(this).removeClass('arrow-down');
				$(this).addClass('arrow-up');
			}
		}).click();
	},
	
	initEntryForm: function(){
		$('#btn-back').hide();
		var val = Number($("input:radio[name='entry[quote_id]']:checked").val());
		this.swapStep0Image(val-1);
		var here = this;
		$('#btn-back').click(function(){
			here.current_step--;
			here.swapStep();
		});
		$('#btn-next, #btn-submit').click(function(){
			$('#entry-form').submit();
		});
		// pageRequired method taken from http://jquery.bassistance.de/validate/demo/multipart/
		$.validator.addMethod("pageRequired", function(value, element) {
			var $element = $(element);
			function match(index) {
				return MAS.current_step == index && $element.parents("#step-" + index).length;
			}
			if (match(1) || match(2) || match(3)) {
				return !this.optional(element);
			}
			return "dependency-mismatch";
		}, $.validator.messages.required);
		this.validator = $("#entry-form").validate({
			onfocusout: false,
			onkeyup: false,
			rules: {
				'entry[firstname]': {pageRequired:true},
				'entry[lastname]': {pageRequired:true},
				'entry[email]': {email:true, pageRequired:true},
				'entry[confirm]': {equalTo:'#email', email:true, pageRequired:true},
				'entry[phone][area_code]': {digits:true, minlength:2, pageRequired:true},
				'entry[phone][phone]': {digits:true, minlength:8, pageRequired:true},
				'entry[school]': {pageRequired:true},
				'entry[title]': {maxWords:10, pageRequired:true},
				'entry[description]': {maxWords:25, pageRequired:true},
				'entry[entry_check]': {pageRequired:true},
				'entry[format_check]': {pageRequired:true},
				'entry[terms_check]': {pageRequired:true},
				'uploaded_file_id': {pageRequired:true}
			},
			groups: {
				phone_group: 'entry[phone][area_code] entry[phone][phone]'
			},
			messages: {
				'entry[firstname]': "Please enter your first name",
				'entry[lastname]': "Please enter your last name",
				'entry[email]': "Please enter a valid email address",
				'entry[confirm]': "Please confirm your email address",
				'phone_group': "Please enter a valid phone number",
				'entry[phone][area_code]': {digits:"Please enter only digits for your area code", minlength:"Please enter 2 digits for your area code", pageRequired:"Please enter a valid phone number"},
				'entry[phone][phone]': {digits:"Please enter only digits for your phone number", minlength:"Please enter at least 8 digits for your phone number", pageRequired:"Please enter a valid phone number"},
				'entry[school]': "Please enter your school's name",
				'entry[title]': {maxWords:"Please enter a maximum of 10 words for your entry's title", pageRequired:"Please enter a title for your entry"},
				'entry[description]': {maxWords:"Please enter a maximum of 25 words for your entry's description", pageRequired:"Please enter a description for your entry"},
				'entry[entry_check]': "Please confirm you have checked your entry details",
				'entry[format_check]': "Please confirm you have obtained your parent/guardian's consent",
				'entry[terms_check]': "Please confirm you have read and agree to the official rules and Terms &amp; Conditions",
				'uploaded_file_id': "Please upload a valid file. Click \"Add File\", select a file, then click \"Start Upload\""
			},
			showErrors: function(map, list) {
				MAS.displayStepErrors(map);
			},
			submitHandler: function(form) {
				return MAS.formSubmit(form);
			}
		});
	},

	formSubmit: function(form){
		var retval = !(this.current_step < 3);
		if (this.current_step == 3) {
			var that = this,
				ops = {
					url: $(form).attr('action') + '.json',
					dataType: 'json',
					success: function(data){
						if (data.success) {
							that.current_step++;
							that.swapStep();
							return;
						}
						// TODO - better error handling
						alert('Failure');
					}
				};
			$(form).ajaxSubmit(ops);
			return retval;
		}
		this.current_step++;
		this.swapStep();
		if (this.current_step == 3) {
			var t = $('#step-3 fieldset').position().top;
//			alert(t);
			$('#flash-wrapper').css({'top': ((t+94)+'px')});
			$('#c-name').html($('#first_name').val() + " " + $('#last_name').val());
			$('#c-school').html($('#school').val());
//			if($('#subject').val() == '')
//			{
//				$('#c-subject-wrapper').hide();
//			} else {
//				$('#c-subject').html($('#subject').val());
//				$('#c-subject-wrapper').show();
//			}
			$('#c-subject').html($('#subject').val());
			$('#c-email').html($('#email').val());
			$('#c-dob').html($('#dob_day').val() + "/" + $('#dob_month').val() + "/" + $('#dob_year').val());
			$('#c-phone').html($('#area_code').val() + " " + $('#phone').val());
			$('#c-quote').html(this.quotes[Number($("input:radio[name='entry[quote_id]']:checked").val())-1]);
			$('#c-title').html($('#title').val());
			$('#c-description').html($('#description').val());
		}
		return retval;
	},

	displayErrors: function(map, ul, container) {
		var err = $(ul).empty();
		$.each(map, function(i, error) {
			$('<li/>').html(error).appendTo(err);
		});
		var display = err.find('li').length ? 'show' : 'hide';
		$('div.errors', container)[display]();
	},

	displayStepErrors: function(map) {
		this.displayErrors(map, '#errors-' + this.current_step, '#step-' + this.current_step);
	},

	swapStep: function(){
		for(var i = 0; i < 5; i++){
			$('#step-' + i).hide();
			$('#hints-' + i).hide();
		}
		$('#errors-' + this.current_step).empty();
		$('#step-' + this.current_step).show()
			.find('input, textarea, select').eq(0).focus().end()
			.find('div.errors').hide();
		$('#hints-' + this.current_step).show();
		var backBtnDisplay = (this.current_step > 0 && this.current_step < 4) ? 'show' : 'hide';
		var nextBtnDisplay = (this.current_step < 3) ? 'show' : 'hide';
		var submitBtnDisplay = (this.current_step == 3) ? 'show' : 'hide';
		$('#btn-back')[backBtnDisplay]();
		$('#btn-next')[nextBtnDisplay]();
		$('#btn-submit')[submitBtnDisplay]();
		if(this.current_step != 3){
			$('#flash-wrapper').css({'top' : '-500px'});
		}
		
		//upload/submit
		if(this.current_step == 4){
			$('#flash-wrapper').css({'top' : '-500px'});
		}

		this.trackPageview('competition/enter/page-' + (this.current_step + 1));
	},
	
	uploadFinished: function(data) {
		// TODO - make this safer
		data = eval("(" + data + ")");
		if (data.status && data.status > 0 || !data.res) {
			var error = new Array(2);
			error[0] = data.status;
			error[1] = data.message;
			return error;
			// return false;
		}
		$('#uploaded_file_id').val(data.res);
		return true;
	},

	swapStep0Image: function(n){
		$("img.step-0-image").hide();
		$('#img-' + n).show();
	},
	
	zoomToggle: function(){
		$('#overlay').toggle();
		$('#frame').toggle();
		$('select').toggle();
		MAS.stretchOverlay('overlay');
	},

	initTellFriend: function(){
		$('#btn-tell-friend-submit').click(function(e){
			e.preventDefault();
			$('#tell-a-friend').submit();
		});
		$('.tell-friend-close').click(function(e){
			e.preventDefault();
			$('#tell-friend-outer').hide();
			$('#tell-friend-thanks-outer').hide();
			$('#overlay').hide();
			$('select').show();
		});
		$('#btn-tell-friend').click(function(e){
			e.preventDefault();
			MAS.stretchOverlay('overlay');
			$('#overlay').show();
			$('#tell-friend-outer').show();
			$('select').hide();
			MAS.trackEvent('SocialMedia', 'sendtofriend');
		});
		$('#tell-more-friends').click(function(e){
			e.preventDefault();
			$('#tell-friend-outer').show();
			$('#tell-friend-thanks-outer').hide();
		});
		$('#tell-a-friend').validate({
			onfocusout: false,
			onkeyup: false,
			rules: {
				'stf[fromName]': 		{required: true},
				'stf[fromEmail]': 	{required: true, email: true},
				'stf[friendName]': 	{required: true},
				'stf[friendEmail]': {required: true, email: true},
				'stf[message]': 		{required: true, maxWords:250}
			},
			messages: {
				'stf[fromName]': 		{required:"Please provide your name"},
				'stf[fromEmail]': 	{required:"Please provide your email address", email:"Your email address is not valid"},
				'stf[friendName]': 	{required:"Please provide your friend's name"},
				'stf[friendEmail]': {required:"Please provide your friend's email address", email:"Your friend's email address is not valid"},
				'stf[message]': 		{required:"Please provide a message to your friend", maxWords:"Please only enter a maximum of 250 words for your message."}
			},
			showErrors: function(map, list) {
				MAS.displayTellFriendErrors(map);
			},
			submitHandler: function(form) {
				return MAS.tellFriendSubmit(form);
			}
		});
	},
	
	tellFriendSubmit: function(form){
		var ops = {
			url: $(form).attr('action') + '.json',
			dataType: 'json',
			success: this.tellFriendResponse
		};
		$(form).ajaxSubmit(ops);
		return false;
	},
	
	tellFriendResponse: function(data){
		if (!data.success) {
			alert('Sorry, there was an error sending your email. Please try again');
			return;
		}
		$('#friend_name').val('');
		$('#friend_email').val('');
		$('#tell-friend-outer').hide();
		$('#tell-friend-thanks-outer').show();
	},

	displayTellFriendErrors: function(map) {
		this.displayErrors(map, '#errors-tell-friend', '.tell-friend-inner');
	},

	voteSubmit: function (e) {
		e.preventDefault();
		var ops = {
			url: $(this).attr('action') + '.json',
			dataType: 'json',
			success: MAS.voteSuccess
		};
		$(this).ajaxSubmit(ops);
		return false;
	},

	voteClick: function () {
		var control = $(this).data('rating');
		$(control.rater).rating('readOnly', true);
		$(this).parents('form.rating-form')
			.find('input[name=vote]').val(this.value).end()
			.submit();
	},

	voteSuccess: function (data) {
		if (data.success) {
//			alert('Thanks for your vote!');
				$(".vote-thank-you").show();
			return;
		}
	},
	
	trackPageview: function (url) {
		try {
			pageTracker._trackPageview(url);
		} catch (e) {}
	},

	trackEvent: function (category, action, label, value) {
		try {
			pageTracker._trackEvent(category, action, label, value);
		} catch (e) {}
	},

	trackFlashEvent: function (params) {
		try {
			pageTracker._trackEvent.apply(pageTracker, params);
		} catch (e) {}
	},
	
	toggleSurvey: function(){
		var toggle = $('#survey-overlay').is(':visible');
		if(toggle == false){
			this.stretchOverlay('survey-overlay');
			for(var i = 1; i < 6; i++){
				$('#survey-' + i).hide();
			}
			$('#thanks').hide();
			$('#survey-outer').show();
			$('#survey-0').show();
			$('#survey-overlay').show();
			
		}else{
			$('#survey-overlay').hide();
		}
	},
	
	stretchOverlay: function(el){
		var containerHeight = $('.canvas').height();
		var windowHeight = document.body.clientHeight;
		var max = Math.max(containerHeight, windowHeight);
		$('#'+el).css('height' , (max + 'px'));
	},
	
	chooseSurvey: function(){
		this.current_survey_page = 0;
		$('#survey-form').submit();
		if(MAS.survey_errors > 0){
			MAS.survey_errors = 0; // clear errors
		} else { // no errors - move to next step
			var selected = $('#survey_user_type').val();
			for(var i = 0; i < 6; i++){
				$('#survey-' + i).hide();
			}
			$('#survey-' + (selected)).show();
						
			this.current_survey_page = parseInt(selected);

			switch(parseInt(MAS.current_survey_page))
			{
			case 1:
				MAS.trackPageview('survey/page-2-student');
				break;
			case 2:
				MAS.trackPageview('survey/page-2-teacher');
				break;
			case 3:
				MAS.trackPageview('survey/page-2-parent');
				break;
			case 4:
				MAS.trackPageview('survey/page-2-other');
				break;
			default:
				MAS.trackPageview('survey/page-2-unknown');
			}

			this.stretchOverlay('survey-overlay');
		}
	},
	
	surveyLastPage: function(){
		$('#survey-form').submit();
		if (MAS.survey_errors > 0) {
			MAS.survey_errors = 0; // clear errors
		}	else { // no errors - move to next step
			for(var i = 0; i < 6; i++){
				$('#survey-' + i).hide();
			}
			$('#survey-5').show();
			
			MAS.trackPageview('survey/page-3-submit');
			this.current_survey_page = 5;
			this.stretchOverlay('survey-overlay');
		}
	},
	
	validateSurvey: function(){
		$('#survey-form').submit();
	},

	initSurveyForm: function(){
		this.current_survey_page = 0; // reset current survey page
		MAS.trackPageview('survey/page-1-intro');
		
		// surveyPageRequired method taken from http://jquery.bassistance.de/validate/demo/multipart/
		$.validator.addMethod("surveyPageRequired", function(value, element) {
			var $element = $(element);
			function match(index) {
				return MAS.current_survey_page == index && $element.parents("#survey-" + index).length;
			}
			if (match(0) || match(1) || match(2) || match(3) || match(4) || match(5) ) {
				return !this.optional(element);
			}
			return "dependency-mismatch";
		}, $.validator.messages.required);
		
		this.validator = $("#survey-form").validate({
			onfocusout: false,
			onkeyup: false,
			rules: {
				// all
				'survey[first_name]'        : {surveyPageRequired:true},
				'survey[last_name]'         : {surveyPageRequired:true},
				'survey[email]'             : {surveyPageRequired:true, email:true},
				'survey[area_code]'         : {surveyPageRequired:true, minlength:2, digits:true},
				'survey[phone]'             : {surveyPageRequired:true, minlength:8, digits:true},
				'survey[user_type]'         : {surveyPageRequired:true},
				// students
				'survey[reason]'            : {maxWords:25},
				'survey[thoughts]'          : {maxWords:25},
				'survey[fun]'               : {maxWords:25},
				// teachers
				'survey[not_this_year]'     : {maxWords:25},
				'survey[learn]'             : {maxWords:25},
				// parents
				'survey[parent_why]'        : {maxWords:25},
				'survey[parent_think]'      : {maxWords:25},
				'survey[parent_fun]'        : {maxWords:25},
				// other
				'survey[other_why]'         : {maxWords:25},
				'survey[other_think]'       : {maxWords:25},
				'survey[other_interesting]' : {maxWords:25},
				// all
				'survey[theatre]'           : {surveyPageRequired:true}
			},
			messages: {
				// all
				'survey[first_name]'        : "Please enter your first name",
				'survey[last_name]'         : "Please enter your last name",
				'survey[email]'             : "Please enter a valid email address",
				'survey[area_code]'         : {digits:"Please enter only digits for your phone number's area code", minlength:"Please enter 2 digits for your phone number's area code", surveyPageRequired:"Please enter a valid phone number area code"},
				'survey[phone]'             : {digits:"Please enter only digits for your phone number", minlength:"Please enter at least 8 digits for your phone number", surveyPageRequired:"Please enter a valid phone number"},
				'survey[user_type]'         : "Please select 'who you are' from the dropdown list",
				// students
				'survey[reason]'            : {maxWords:"Please only enter a maximum of 25 words for why you do or don't plan to enter"},
				'survey[thoughts]'          : {maxWords:"Please only enter a maximum of 25 words for what do you think about Shakespeare's plays"},
				'survey[fun]'               : {maxWords:"Please only enter a maximum of 25 words for what you think would make Shakespeare more fun and interesting"},
				// teachers
				'survey[not_this_year]'     : {maxWords:"Please only enter a maximum of 25 words for reasons why you will not be encouraging your students to enter this year"},
				'survey[learn]'             : {maxWords:"Please only enter a maximum of 25 words for what do you think students learn most from Shakespeare's plays"},
				// parents
				'survey[parent_why]'        : {maxWords:"Please only enter a maximum of 25 words for why you will/will not be encouraging your child/children to enter the competition this year"},
				'survey[parent_think]'      : {maxWords:"Please only enter a maximum of 25 words for what you think about Shakespeare"},
				'survey[parent_fun]'        : {maxWords:"Please only enter a maximum of 25 words for what you thik would make Shakespeare more fun and interesting"},
				// other
				'survey[other_why]'         : {maxWords:"Please only enter a maximum of 25 words for why you will/will not be encouraging students to enter the competition this year"},
				'survey[other_think]'       : {maxWords:"Please only enter a maximum of 25 words for what you think about Shakespeare"},
				'survey[other_interesting]' : {maxWords:"Please only enter a maximum of 25 words for what you thik would make Shakespeare more fun and interesting"},
				// all
				'survey[theatre]'           : "Please select 'who you normally go to the theatre with' from the dropdown list"
			},
			showErrors: function(map, list) {
				MAS.displaySurveyPageErrors(map);
			},
			invalidHandler: function(form, validator) {
			MAS.survey_errors = validator.numberOfInvalids();
			},
			submitHandler: function(form) {
				MAS.formSurveySubmit(form);
			}
		});
	},

	formSurveySubmit: function(form){
		var retval = !(this.current_survey_page < 5);
		if (this.current_survey_page == 5) {
			var that = this,
				ops = {
					url: $(form).attr('action') + '.json',
					dataType: 'json',
					success: function(data, statusText){
						if (data.success) {
							if (MAS.survey_errors > 0) {
								MAS.survey_errors = 0; // clear errors
							} else { // no errors - move to next step
								for(var i = 0; i < 6; i++){
									$('#survey-' + i).hide();
								}
								//survey says....!
								$('#thanks').show();
								MAS.current_survey_page = 6; // special page for thank you
								MAS.trackPageview('survey/page-4-thanks');
							}
							return;
						}

						// TODO - better error handling
						alert('Failure, please try again.');
						MAS.trackPageview('survey/page-4-fail');
						
					}
				};
			$(form).ajaxSubmit(ops);
			
			if (MAS.survey_errors > 0) {
				MAS.survey_errors = 0; // clear errors
			}
			return false;
		}
		
		if (MAS.survey_errors > 0) {
			MAS.survey_errors = 0; // clear errors
		}
		return retval;
	},

	displaySurveyPageErrors: function(map) {
		this.displayErrors(map, '#errors-' + this.current_survey_page, '#survey-' + this.current_survey_page);
	}
	
};

// initialize various buttons and behaviours
jQuery(document).ready(function(){
	// used in index.tpl - allows for word 'search' to be show when no input in search input field
	$("#search-query").fieldtag(); 
	
	if($("#home-carousel").length > 0){
			jQuery("#home-carousel").jcarousel({
					scroll: 1,
//			auto: 5,
//			wrap: 'last',
					initCallback: MAS.mycarousel_initCallback,
			buttonNextCallback:   MAS.mycarousel_buttonNextCallback,
			buttonPrevCallback:   MAS.mycarousel_buttonPrevCallback,
					// This tells jCarousel NOT to autobuild prev/next buttons
					buttonNextHTML: null,
					buttonPrevHTML: null
			});
	}
	//used on competition page
	if($('#competition').length > 0){
		MAS.assignTogglers();
	}
	
	//used on entry page
	if($('#btn-back').length > 0){
		MAS.initEntryForm();
	}
	
	if($('#img-player').length > 0){
		$('#img-player, #frame-close').click(function(e){
			e.preventDefault();
			MAS.zoomToggle();
		});
	}

	if($('input.rating').length > 0){
		$.extend($.fn.rating.options, {
			required: true,
			callback: MAS.voteClick
		});
		$('form.rating-form').submit(MAS.voteSubmit);
		$('input:radio.rating').rating();
	}

	if($('#btn-tell-friend-submit').length > 0){
		MAS.initTellFriend();

		// Social media links
		$('a[rel=social]').click(function(e){
			e.preventDefault();
			var type = this.hash.replace(/^#/, '');
			social[type]();
		});
	}

	//used on view entry page
	/*
	if($('.scroll-pane').length > 0){
		// Has to be wrapped in a window load to stop a random display bug
		$(window).load(function(){
			$('.scroll-pane').jScrollPane({scrollbarWidth:26});
			$('.jScrollPaneDrag').css({'height' : '30px'});
		});
	}
	*/
	
	if($('#btn-search-go').length > 0){
		$('#btn-search-go').hover(function(){
			$(this).css({'background-position' : 'bottom left'});
		}, function(){
			$(this).css({'background-position' : 'top left'});
		});
	}

	if($('#form_related_sort').length > 0){
		$('#form_related_sort').change(function(){
			var href = location.href,
				regex = /([\?&])sort=(rating|views|recent)/;
			if (regex.test(href)) {
				href = href.replace(regex, '$1sort=' + $(this).val());
			} else {
				href += href.indexOf('?') > -1 ? '&' : '?';
				href += 'sort=' + $(this).val();
			}
			location.href = href;
		});
	}
	
	if($('#paging_options').length > 0){
		$('#paging_options').change(function(){
			var href = location.href,
				regex = /([\?&])show=(\d*)/;
			if (regex.test(href)) {
				href = href.replace(regex, '$1show=' + $(this).val());
			} else {
				href += href.indexOf('?') > -1 ? '&' : '?';
				href += 'show=' + $(this).val();
			}
			location.href = href;
		});
	}

	// Tracking links
	$.each([
		['#feedback', 'feedback'],
		['#logo-bell', 'logo_bell'],
		['#logo-optus', 'logo_optus'],
		['#enter-top', 'competition_enternow_top'],
		['#enter-btm', 'competition_enternow_btm'],
		['#toggler', 'competition_prizes'],
		['#hiw-toggler', 'competition_howitworks'],
		['#whocanenter', 'competition_whocanenter'],
		['#fortheteachers', 'competition_fortheteachers'],
		['#judges', 'competition_judges'],
		['#competition-rules-link', 'competition_rulesfaqs'],
		['#entry-info', 'gallery_entryinfo'],
		['#facebook', 'facebook'],
		['#myspace', 'myspace'],
		['#twitter', 'twitter'],
		
		['#home', 'header/home'],
		['#competition', 'header/competition'],
		['#gallery', 'header/gallery'],
		['#scene', 'header/make-a-scene-3d'],
		['#footer-optus', 'footer/copyright'],
		['#footer-privacy', 'footer/privacy-policy'],
		['#survey-terms', 'survey/terms']
	], function (i, track) {
		$(track[0]).click(function () {
			MAS.trackPageview(track[1]);
		});
	});
	
	//survey
	if($('#feedback').length > 0){
		$('#feedback').click(function(){
			$('#survey-overlay').show();
			$('#survey-outer').show();
			$('select').hide();
			$('#survey-inner select').show();	
			MAS.initSurveyForm();
		});
		$('.survey-close').click(function(e){
			e.preventDefault();
			$('#survey-outer').hide();
			$('#survey-overlay').hide();
			$('select').show();
			document.forms['survey-form'].reset();  // reset the form to avoid validation errors in pages beyond the 1st page
			$('div.errors').hide(); // hide errors until new ones come up
	
			switch(parseInt(MAS.current_survey_page))
			{
			case 0:
				MAS.trackPageview('survey/page-1-intro-close');
				break;
			case 1:
				MAS.trackPageview('survey/page-2-student-close');
				break;
			case 2:
				MAS.trackPageview('survey/page-2-teacher-close');
				break;
			case 3:
				MAS.trackPageview('survey/page-2-parent-close');
				break;
			case 4:
				MAS.trackPageview('survey/page-2-other-close');
				break;
			case 5:
				MAS.trackPageview('survey/page-3-submit-close');
				break;
			case 6:
				MAS.trackPageview('survey/page-4-thanks-close');
				break;
			default:
				MAS.trackPageview('survey/page-unknown-close');
			}
			
		});
	}
	
	
}); 

var resizeTimer = null;
$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(function(){
		MAS.stretchOverlay('survey-overlay');
	}, 100);
});