function dynamicQuestionsCopy() {
	$("#Myself").click(function() {
		$("SPAN.suffererPronoun").each(function() {
			if (this.innerHTML == 'they') {
				this.innerHTML = 'you';
			} else if (this.innerHTML == 'their') {
				this.innerHTML = 'your';
			}
		});
	}).filter(":checked").click();
	$("#anotherAdult, #kid1217").click(function() {
		$("SPAN.suffererPronoun").each(function() {
			if (this.innerHTML == 'you') {
				this.innerHTML = 'they';
			} else if (this.innerHTML == 'your') {
				this.innerHTML = 'their';
			}
		});
	}).filter(":checked").click();
};

function formElements() {

	$('FORM#adultsProfilerForm SELECT').styledDropDown();

	$("FORM#adultsProfilerForm :input").addClass("required");

};

function imagesChange(){
	/* Question 1 */
	$("#ageQuestionContainer .dropDownMenu UL LI>A").click(function(){
		$("#ageQuestionContainer DIV.questionImage").spritify(0,0);		
	});
	if($("#ageQuestionContainer SELECT")[0].selectedIndex!=0){
		$("#ageQuestionContainer DIV.questionImage").spritify(0,0);
	}
	
	/* Question 2 */
	$("#stateQuestionContainer .dropDownMenu UL LI>A").click(function(){
		var optionIndex = $(this).data("optionIndex");
		if(optionIndex>0){
			$("#stateQuestionContainer DIV.questionImage").spritify(1,optionIndex-1);
		}
	});
	if($("#stateQuestionContainer SELECT")[0].selectedIndex!=0){
		$("#stateQuestionContainer DIV.questionImage").spritify(1,$("#stateQuestionContainer SELECT")[0].selectedIndex-1);
	}
	
	/* Question 3 */
	$("#symptomsQuestionContainer INPUT:checkbox").each(function(i){
		$(this).click(function(){
			$("#symptomsQuestionContainer DIV.questionImage").spritify(2,i);
		}).is(":checked") && $("#symptomsQuestionContainer DIV.questionImage").spritify(2,i);
	});
	
	/* Question 4 */
	$("#sufferMostQuestionContainer INPUT:radio").each(function(i){
		$(this).click(function(){
			$("#sufferMostQuestionContainer DIV.questionImage").spritify(3,i);
		}).is(":checked") && $("#sufferMostQuestionContainer DIV.questionImage").spritify(3,i);
	});
	
	/* Question 5 */
	$("#seasonQuestionContainer INPUT:radio").each(function(i){
		$(this).click(function(){
			$("#seasonQuestionContainer DIV.questionImage").spritify(4,i);
		}).is(":checked") && $("#seasonQuestionContainer DIV.questionImage").spritify(4,i);
	});
	
	/* Question 6 */
	$("#durationQuestionContainer INPUT:radio").each(function(i){
		$(this).click(function(){
			$("#durationQuestionContainer DIV.questionImage").spritify(5,i);
		}).is(":checked") && $("#durationQuestionContainer DIV.questionImage").spritify(5,i);
	});
	
	/* Question 7 */
	$("#formQuestionContainer INPUT:radio").each(function(i){
		$(this).click(function(){
			$("#formQuestionContainer DIV.questionImage").spritify(6,i);
		}).is(":checked") && $("#formQuestionContainer DIV.questionImage").spritify(6,i);
	});
	
	
};

$(document).ready( function() {
    $.attachAtlas('cbnctn_AllergyProfiler_1');
	$.attachSpotlightTag('1608566','manag537','aller057');
	

	formElements();
	dynamicQuestionsCopy();
	
	imagesChange();
	
	
	
	var validator = $("#adultsProfilerForm").validate({
		errorPlacement: function(error, element) {},
		highlight: function(element, errorClass) {
			$(element).parents(".questionContainer").addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
			$(element).parents(".questionContainer").removeClass(errorClass);
		},
		focusCleanup: true,
		submitHandler:function(form){
			if(!$(form).hasClass('disabled')){						
				$(form).addClass('disabled');
				$(form).ajaxSubmit({
					dataType:  'json', 
					success:   function (result) {
						$(form).removeClass('disabled');
						$.log(result);
						if(result.result != false){
							var profilerResult = result.result;
							if(profilerResult.recommenedProducts.length>0){
								$("#productContainer1").find(".productTitle").html(profilerResult.recommenedProducts[0].name);
								$("#productContainer1").find(".productImageContainer IMG")
									.attr('src',profilerResult.recommenedProducts[0].imageURL)
									.reflect( {	height :.25, opacity :.25 });
								//$("#productContainer1").find(".productTitleline").html(profilerResult.recommenedProducts[0].titleLine);
								$("#productContainer1").find(".productDescription").html(profilerResult.recommenedProducts[0].shortDesc);
								$("#productContainer1").find("A.recoProdUrl").attr("href","./../products/"+profilerResult.recommenedProducts[0].id+".jspa");
								$("#productContainer1").find("A.recoProdsUrl").attr("href","./../products/index.jspa#"+profilerResult.recommenedProducts[0].category);
								$("#productContainer1 SUP.footnote").footnotes(function(fnotes){
									var footnotesHtml = ""
									for(var i=0;i<fnotes.length;i++){
										footnotesHtml+="<sup>"+fnotes[i].index+"</sup>"+fnotes[i].title+"<br/>";
									}
									$("#productContainer1").find(".footNote").html(footnotesHtml);
								});
								//$("#productContainer1").find(".footNote").html(profilerResult.recommenedProducts[0].footnote);
							}
							if(profilerResult.recommenedProducts.length>1){
								$("#productContainer2").find(".productTitle").html(profilerResult.recommenedProducts[1].name);
								$("#productContainer2").find(".productImageContainer IMG")
									.attr('src',profilerResult.recommenedProducts[1].thumbLargeURL)
									.reflect( {	height :.25, opacity :.25 });
								//$("#productContainer2").find(".productTitleline").html(profilerResult.recommenedProducts[1].titleLine);
								$("#productContainer2").find(".productDescription").html(profilerResult.recommenedProducts[1].shortDesc);
								$("#productContainer2").find("A.recoProdUrl").attr("href","./../products/"+profilerResult.recommenedProducts[1].id+".jspa");
								$("#productContainer2").find("A.recoProdsUrl").attr("href","./../products/index.jspa#"+profilerResult.recommenedProducts[1].category);
								$("#productContainer2 SUP.footnote").footnotes(function(fnotes){
									var footnotesHtml = ""
									for(var i=0;i<fnotes.length;i++){
										footnotesHtml+="<sup>"+fnotes[i].index+"</sup>"+fnotes[i].title+"<br/>";
									}
									$("#productContainer2").find(".footNote").html(footnotesHtml);
								});
								//$("#productContainer2").find(".footNote").html(profilerResult.recommenedProducts[1].footnote);
								$("#productContainer2").show();

							}else{
								$("#productContainer2").hide();
							}
							if(profilerResult.tips.length>0){
								$("#profilerTipContainer1").find(".tipText").html(profilerResult.tips[0].text).truncate(110);
								$("#profilerTipContainer1").find(".tipName").html(profilerResult.tips[0].title);
							}
							if(profilerResult.tips.length>1){
								$("#profilerTipContainer2").find(".tipText").html(profilerResult.tips[1].text).truncate(110);
								$("#profilerTipContainer2").find(".tipName").html(profilerResult.tips[1].title);
							}
							
							/* Visual Profiler */
							var vpImages = [{'i':0,'j':0}, /* Question 1 */
							                {'i':1,'j':$("#stateQuestionContainer SELECT")[0].selectedIndex-1}]; /* Question 2 */
							
							/* Question 3 */
							$("#symptomsQuestionContainer INPUT:checkbox").each(function(i){
								$(this).is(":checked") && vpImages.push({'i':2,'j':i});
							});
							
							/* Question 4 */
							$("#sufferMostQuestionContainer INPUT:radio").each(function(i){
								$(this).is(":checked") && vpImages.push({'i':3,'j':i});
							});
							
							/* Question 5 */
							$("#seasonQuestionContainer INPUT:radio").each(function(i){
								$(this).is(":checked") && $(this).is(":checked") && vpImages.push({'i':4,'j':i});
							});
							
							/* Question 6 */
							$("#durationQuestionContainer INPUT:radio").each(function(i){
								i!=2 && $(this).is(":checked") && vpImages.push({'i':5,'j':i});
							});
							
							/* Question 7 */
							$("#formQuestionContainer INPUT:radio").each(function(i){
								i!=3 && $(this).is(":checked") && vpImages.push({'i':6,'j':i});
							});
							
							
							for(var i=0;i<vpImages.length;i++){
								$("#visualProfile").append($("<DIV></DIV>").addClass("visualProfileImage").spritify(vpImages[i].i,vpImages[i].j));
								
							}
							$("#visualProfile").append($("<DIV></DIV>").addClass("ClearFloat"));
							/* Visual Profiler END */
							
							
							$("#profiler").hide();
							$("#profilerResults").show();
						}
					}
				});
			}
		}
	});
	
});