//event handlers
$j(document).ready(function() {

	var split = "";
	var validates = "";
	var linkRel = "";
	var linkClass = "";
	var linkObj = "";
	var eMessages = "";
	var overlay = $j('#survey_messages_overlay');
	var popup_content = $j('#popup_content');
	var messagebox = $j('#message_content');
	var toValidate = "";
	var validateChildren = "";
	var typeError = true;

	getFunction = function(linkRel) {
		split = linkRel.split('-');
		split = parseInt(split[1])-1;
		split = split.toString();
		validates = "validate_" + split;
		return validates;
	};

	switchScreens = function(linkObj) {
		linkRel = $j(linkObj).attr('rel');
		if(linkRel != "end") {
			eMessages = getFunction(linkRel);
			if(!functionExists(eMessages)) {
				popup_content.addClass(linkRel);
			}
			else {
				messages = eMessages+"()";
				fn = eval(messages);
				if(fn != true) {
					flashMessage(fn, true);
				}
				else {
					popup_content.addClass(linkRel);
				}
			}
		}
		
	}

	flashMessage = function(message, typeError) {
		if(!typeError) {
			messagebox.css('color', '#000000');
		}
		else {
			messagebox.css('color', '#ff0000');
		}
		messagebox.html(message);
		overlay.show();
		setTimeout(
			function() {overlay.hide()},
			4000
		);
	}

	continueSurvey = function(linkObj) {
		linkClass = $j(linkObj).attr('class');
		if(linkClass == "y") {
			$j('#popup_content').addClass("screen-1");
		}
		else {
			$j("#popup_survey_overlay").css('display', 'none');
			if(linkClass == "n")
				$j.get('/surveys/setGoAwayCookie');
			else
				$j.get('/surveys/setLaterCookie');
		}
	}

	functionExists = function(functionName) {
		if (typeof functionName == 'string'){
			return (typeof this.window[functionName] == 'function');
		} else{
			return (functionName instanceof Function);
		}
	}

	/*
	* If any of these functions return true than validation will be successful
	* Any other values will be treated as error messages
	*/
	validate_1 = function() {
		toValidate = $j('#screen-1_validate');
		validateChildren = toValidate.find('input[type=checkbox][checked]');
		if(validateChildren.length < 1) {
			validateChildren = toValidate.find('input[type=text][value!=""]');
			if(validateChildren.length < 1) {
				return "Please select an option";
			}
			else { return true; }
		}
		else {
			return true;
		}
	}

	validate_2 = function() {
		toValidate = $j('#screen-2_validate');
		validateChildren = toValidate.find('input[name="data[Survey][2][1][text]"]:checked');
		if(validateChildren.length > 0) {
			if(validateChildren.val() == "Yes") {
				validateChildren = toValidate.find('input[name="data[Survey][2][2][text]"]:checked').val();
				if(validateChildren == "Yes") {
					validateChildren = $j('#how_did_you_find input:checked').length + $j('#how_did_you_find input[type=text][value!=""]').length;
					if(validateChildren < 2) {
						return "Please fill out remaining fields";
					}
					else{return true;}
				}
				else{return true;}
			}
			else{return true;}
		}
		else{return "Please select an option";}
	}

	validate_3 = function() {
		toValidate = $j('#screen-3_validate');
		validateChildren = toValidate.find('input[type="checkbox"]:checked').length;
		if(validateChildren < 1) {
			return "Please select an option";
		}
		else {return true;}
	}

	validate_4 = function() {
		toValidate = $j('#screen-4_validate');
		validateChildren = toValidate.find('input[type="radio"]:checked').length;
		if(validateChildren < 3) {
			return "Please fill out all fields";
		}
		else {return true;}
	}

	validate_5 = function() {
		toValidate = $j('#screen-5_validate');
		validateChildren = toValidate.find('input[type=radio]:checked');
		if(validateChildren.length < 1)
			return "Please select an option";
		else {
			if(validateChildren.val() == "No") {
				validateChildren = toValidate.find('input[type=text][value!=""]').length;
				if(validateChildren < 1)
					return "Please fill out the text field";
				else
					return true;
			}
			else
				return true;
		}
	}

	validate_6 = function() {
		toValidate = $j('#screen-6_validate');
		validateChildren = toValidate.find('input[type=radio]:checked');
		if(validateChildren.length < 1)
			return "Please select an option";
		else {
			if(validateChildren.val() == "No") {
				validateChildren = toValidate.find('input[type=text][value!=""]').length;
				if(validateChildren < 1)
					return "Please fill out the text field";
				else
					return true;
			}
			else
				return true;
		}
	}

	validate_7 = function() {
		toValidate = $j('#screen-7_validate');
		validateChildren = toValidate.find('input[type=radio]:checked');
		if(validateChildren.length < 1)
			return "Please select an option";
		else 
			return true;
	}

	validate_8 = function() {
		toValidate = $j('#screen-8_validate');
		validateChildren = toValidate.find('input[type=radio]:checked').length + toValidate.find('input[type=checkbox]:checked').length;
		if(validateChildren < 2) {
			return "Please select an option";
		}
		else{return true;}
	}

	validate_9 = function() {
		toValidate = $j('#screen-9_validate');
		validateChildren = toValidate.find('input[type=radio]:checked').length + toValidate.find('input[type=text][value!=""]').length;
		if(validateChildren < 2) {
			return "Please fill out all fields";
		}
		else{return true;}
	}

	validate_10 = function() {
		toValidate = $j('#screen-10_validate');
		validateChildren = toValidate.find('input.required[value!=""]');
		if(validateChildren.length >= 4) {
			validateChildren = toValidate.find('input.select:selected');
			return true;
		}
		else {
			return "Please fill out all fields marked with a *";
		}
	}
	
	stopSurvey = $j.cookie('stopSurvey');
	do_later = $j.cookie('doLater');
	access = $j.cookie('access');
	surveyStart = $j.cookie('surveyStart');
	if(surveyStart > 3) {
		if(access == null && stopSurvey == null) {
			if(do_later != true) {
				$j("#popup_survey_overlay").css('display', 'block');
			}
		}
	}
	
	$j("#survey_recover").click(function(){
		$j("#popup_survey_overlay").css('display', 'block');
	});
	
	$j('#yn_links a').click(function(e){
		e.preventDefault();
		continueSurvey(this);
	});

	$j('#popup_content a.next_button').click(function(e){
		e.preventDefault();
		switchScreens(this);
	});
	
	$j('a.close_window').click(function(e){
		e.preventDefault();
		$j("#popup_survey_overlay").css('display', 'none');
		$j.get('/surveys/setGoAwayCookie');
	});
	
	$j('#surveyForm').ajaxForm(function() {
		flashMessage("Thank you. Saving...");
		setTimeout(function() {
			$j('#popup_content').addClass("screen-12");
		},
		4000);
	});
	
	var did_you_find = $j('#did_you_find');
	did_you_find.hide();
	$j('#Survey211').click(function() {
		did_you_find.show();
	});
	
	var stories_section = $j('#stories');
	stories_section.hide();
	$j('#Survey811').click(function() {
		stories_section.show();
	});
	$j('#Survey812').click(function() {
		$j('#popup_content').addClass("screen-10");
	});

	$j('#if_not_stories').hide();
	$j('#Survey832').click(function() {
		$j('#if_not_stories').show();
	});
	$j('#Survey831').click(function() {
		$j('#popup_content').addClass("screen-10");
	});	
	
	var if_not_stories = $j('#easy_to_find');
	if_not_stories.hide();
	$j('#Survey512').click(function() {
		if_not_stories.show();
	});
	$j('#Survey511').click(function() {
		$j('#popup_content').addClass("screen-6");
	});
	
	var get_lost = $j('#get_lost');
	get_lost.hide();
	$j('#Survey611').click(function() {
		get_lost.show();
	});
	$j('#Survey612').click(function() {
		$j('#popup_content').addClass("screen-7");
	});	
	
	var tick_box_functions = $j('#tick_box_functions');
	tick_box_functions.hide();
	$j('#Survey421').click(function() {
		tick_box_functions.show();
	});
	$j('#Survey422').click(function() {
		$j('#popup_content').addClass("screen-5");
	});
	
	$j('#Survey212').click(function() {
		$j('#popup_content').addClass("screen-3");
	});
	var how_did_you_find = $j('#how_did_you_find');
	how_did_you_find.hide();
	$j('#Survey221').click(function() {
		how_did_you_find.show();
	});
	$j('#Survey222').click(function() {
		$j('#popup_content').addClass("screen-3");
	});
	
});