

function Set_Cookie( name, value, expires, path, domain, secure )
{
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


	
var Questions = [
	{
		category: "anxiety",
		title: "When a coworker or someone in my family asks me something, I get so irritated and tense lately",
		answer: ""
	},
	{
		category: "anxiety",
		title: "I turn rather hostile and angry when my partner doesn`t offer me the care and attention that I need.",
		answer: ""
	},
	{
		category: "anxiety",
		title: "When I am in the middle of a disscusion, I always look for the other`s approval towards what I say.",
		answer: ""
	},
	{
		category: "anxiety",
		title: "Not once did I experience shortness of breath, elevated heart rate or chest pain on some idle working days, out of nothing.",
		answer: ""
	},
	{
		category: "anxiety",
		title: "Despite that my partner and I have a great relationship, I feel vulnerable most of the times and I foresee problems might appear between us.",
		answer: ""
	}
	,
	{
		category: "stress",
		title: "When I go to a reunion or any other social activity, I always walk around foreheads up no matter what I`m wearing.",
		answer: ""
	},
	{
		category: "stress",
		title: "I am in this often restless mood lately and I fear inexplicably, without actually knowing why.",
		answer: ""
	},
	{
		category: "stress",
		title: "Me and my partner don`t find time anymore to talk about our activities, our worries, our future plans as we did used to the past.",
		answer: ""
	},
	{
		category: "stress",
		title: "My partner complains about me being sad, quiet and pensive most of the time.",
		answer: ""
	},
	{
		category: "stress",
		title: "I usually end up any debate in changing my former views upon a topic and agreeing with contrary opinions that the others expressed.",
		answer: ""
	},
	{
		category: "self-contentment",
		title: "One moment I feel I`m having hot flashes, and the other there`s a sudden change and I get very cold, out of nothing.",
		answer: ""
	},
	{
		category: "self-contentment",
		title: "I have been diagnosed with high cholesterol and hypertension.",
		answer: ""
	},
	{
		category: "self-contentment",
		title: "I avoid walking naked around the house, even when my partner is home.",
		answer: ""
	},
	{
		category: "self-contentment",
		title: "Lately, when someone has been inviting me to some gathering event, I didn`t feel in the mood for going.",
		answer: ""
	},
	{
		category: "self-contentment",
		title: "Ever since I was a little child, people I know have surrounded me with encouragements and support.",
		answer: ""
	}
];
var CurrentQuestion = -1;
function InitQuestions(){
	CurrentQuestion = -1;
	for(var i = 0; i < Questions.length; i++){
		Questions[i].answer = "";
	}
	RandomizeQuestions();
}
function RandomizeQuestions(){
	var randTimes = Math.floor(Math.random()*30);
	var questionsLength = Questions.length;
	for(var i = 0; i < randTimes; i++){
		var what = Math.floor(Math.random()*questionsLength);
		var to = Math.floor(Math.random()*questionsLength);
		var aux = Questions[what];
		Questions[what] = Questions[to];
		Questions[to] = aux;
	}
}

function GetNextQuestion(answer){
	if(CurrentQuestion >= 0 && CurrentQuestion < Questions.length){
		if(answer != ""){
			_gaq.push(['_trackEvent', 'QUIZ', 'Question: '+Questions[CurrentQuestion].title, answer]);
		}
		Questions[CurrentQuestion].answer = answer;
	}
	
	CurrentQuestion++
	if(CurrentQuestion < Questions.length){
		return Questions[CurrentQuestion];
	}
	return false;
}
var StressQuizCategories = new Array("anxiety","stress","self-contentment");
function GetResponse(category){
	var nr_a = 0;
	var nr_b = 0;
	var nr_c = 0;
	
	for(var i = 0; i < Questions.length; i++){
		if(Questions[i].category == category){
			switch(Questions[i].answer){
				case "a": nr_a++
				break;
				case "b": nr_b++
				break;
				case "c": nr_c++
				break;
			}
		}
	}
	switch(category){
		case "anxiety":
			if(nr_a > 2){
				return category+"-low-level";
			}
			if(nr_b > 2){
				return category+"-average-level";
			}
			if(nr_c > 2){
				return category+"-high-level";
			}
			if(nr_a == 2 && nr_b == 2 && nr_c == 1){
				return category+"-low-average-level";
			}
			if(nr_a == 1 && nr_b == 2 && nr_c == 2){
				return category+"-average-high-level";
			}
			if(nr_a == 2 && nr_b == 1 && nr_c == 2){
				return category+"-low-high-level";
			}
		break;
		case "stress":
			if(nr_c > 2){
				return category+"-low-level";
			}
			if(nr_b > 2){
				return category+"-average-level";
			}
			if(nr_a > 2){
				return category+"-high-level";
			}
			if(nr_a == 2 && nr_b == 2 && nr_c == 1){
				return category+"-low-average-level";
			}
			if(nr_a == 1 && nr_b == 2 && nr_c == 2){
				return category+"-average-high-level";
			}
			if(nr_a == 2 && nr_b == 1 && nr_c == 2){
				return category+"-low-high-level";
			}
		break;
		case "self-contentment":
			if(nr_c > 2){
				return category+"-low-level";
			}
			if(nr_b > 2){
				return category+"-average-level";
			}
			if(nr_a > 2){
				return category+"-high-level";
			}
			if(nr_a == 2 && nr_b == 2 && nr_c == 1){
				return category+"-low-average-level";
			}
			if(nr_a == 1 && nr_b == 2 && nr_c == 2){
				return category+"-average-high-level";
			}
			if(nr_a == 2 && nr_b == 1 && nr_c == 2){
				return category+"-low-high-level";
			}
		break;
	}
}
function DisplayNextQuestion(answer){
	var question = GetNextQuestion(answer);
	if(question){
		$("#stressQuiz_question").html(question.title);
		$("#stressQuizProgressBar").progressbar({value:((CurrentQuestion+1)*100/Questions.length)});
		_gaq.push(['_trackEvent', 'QUIZ', 'Question Fed', question.title]);
	}
	return question;
}
function CreateQuizLayout(){
		$(document).bind("keyup", function(event){
			if (event.which == 27) { 
				DestroyQuiz();
			}
		});
		$("#stressQuizOverlay_container").remove();
		$("body").append("<div id='stressQuizOverlay_container' style='display:none'><div id='stressQuizOverlay' style='position: fixed; left: 0; top: 0; bottom: 0; right: 0; z-index: 1000000; filter: alpha(opacity=90); opacity: .90; background-color: #000; background-image: -webkit-radial-gradient(rgba(127, 127, 127, 1), rgba(127, 127, 127, 1) 35%, rgba(0, 0, 0, 1)); background-image: -moz-radial-gradient(rgba(127, 127, 127, 1), rgba(127, 127, 127, 1) 35%, rgba(0, 0, 0, 1));'><div id='stressQuizOverlayContent' style='position: fixed; z-index: 1000001; left: 50%; top: 50%; margin-left: -325px; margin-top: -180px; width: 650px; height: 360px; background-color: rgb(139, 0, 0); font-family: Arial; padding: 0pt; box-shadow: 0px 5px 80px rgb(80, 80, 80); border-radius: 10px 10px 10px 10px;'></div></div></div>")
		$("#stressQuizOverlayContent").html("<h3 style='text-align:center; font-size:16px; height:20px;'>Here`s a little Stress Quiz that you`ll enjoy going through!</h3><div id='stressQuizProgressBar' style='width:600px; margin-left:25px; height:20px; margin-top:-10px;'></div><div style='height:260px; scroll:auto; background: #FFF; margin-top:10px;text-align:center;'><table style='height:inherit; margin:0 auto; width:70%;'><td style='vertical-align: middle;text-align:center; font-size:24px;' id='stressQuiz_question'></td></table></div><div id='stressQuizAnswers' style='position:relative; text-align:center; font-size:18px; bottom:2px;height:20; margin-top:5px;'><input type='radio' name='stressQuizAnswer' id='stressQuizAnswer_a' value='a' /><label style='margin-left:30px; width:150px;background:#3f9f01; cursor: pointer;' for='stressQuizAnswer_a'>Never</label><input type='radio' name='stressQuizAnswer' value='b' id='stressQuizAnswer_b' /><label for='stressQuizAnswer_b' style='margin-left:30px;width:150px;background:#3f9f01; cursor: pointer;'>Sometimes</label><input type='radio' name='stressQuizAnswer' id='stressQuizAnswer_c' value='c' style='margin-left:30px;' /><label for='stressQuizAnswer_c' style='margin-left:30px;width:150px;background:#3f9f01; cursor: pointer;'>All the time</label><input type='button' style='color:red;margin-left:50px;' value='Close' onclick='DestroyQuiz()' /></div><div></div>");
		
		$("#stressQuizProgressBar").progressbar({value:((CurrentQuestion+1)*100/Questions.length)});
		InitQuestions();
		DisplayNextQuestion("");
		
		$("[name=stressQuizAnswer]").unbind("click").bind("click", function(){
			if(!DisplayNextQuestion($(this).val()) ){
				$("#stressQuizOverlayContent").html("<h3 style='text-align:center; font-size:16px; height:20px;'>Quiz Results</h3><div style='height:280px;  background: #FFF; margin-top:10px;text-align:center;'><table style='height:inherit; margin:0 auto; width:70%;'><tr><td style='height:280px;vertical-align: middle;overflow:scroll; text-align:center; font-size:12px;' ><p id='stressQuiz_question' style='height:260px;overflow-y:scroll; text-align:justify;'>Processing your answers...</p></td></tr></table></div><div id='stressQuizAnswers' style='position:relative; text-align:center; bottom:2px;height:20; margin-top:5px;'><input type='button' style='color:red;' value='Close' onclick='DestroyQuiz()' /></div>");
				for(var i = 0; i < StressQuizCategories.length; i++){
					var whatToLoad = GetResponse(StressQuizCategories[i]);
					_gaq.push(['_trackEvent', 'QUIZ', 'Result for '+StressQuizCategories[i], whatToLoad]);
					$("#stressQuiz_question").html("");
					$.ajax({
						type: "GET",
						url: "/quizanswers/"+whatToLoad+".html",
						success: function(html){
							$("#stressQuiz_question").append(html.replace(/\<br \/\>/g,"<br /><br />").replace(/\<br \/\>\<br \/\>\<br \/\>\<br \/\>/g,"<br /><br /><br />"));
							
							$("a[name=from_quiz]").unbind("click").bind("click", function(event){
								
								_gaq.push(['_trackEvent', 'QUIZ', 'Product reference', $(this).html()]);
							
								
							});
							Set_Cookie("stressQuiz","CLOSED",1);
						}
					});
				}
			}
			
		}).hide();
		$("label").css("cursor","pointer").hover(function(){$(this).addClass("ui-widget-header");},function(){$(this).removeClass("ui-widget-header");});
		$("#stressQuizOverlay_container").show();
		_gaq.push(['_trackEvent', 'QUIZ', 'Created', window.location.href]);
		
}
function DestroyQuiz(){
	$("#stressQuizOverlay_container").remove();
	Set_Cookie("stressQuiz","CLOSED",1);
	
	_gaq.push(['_trackEvent', 'QUIZ', 'Closed', CurrentQuestion+""]);
}
$(document).ready(function(){
	$("#take_the_quiz_button").unbind("click").bind("click", function(event){
		CreateQuizLayout();
	});
	$(".cart").find("a.button").bind("click", function(event){
		if($(this).attr("id") != "button-cart"){
		var theProductId = $(this).attr("onclick").replace("addToCart('","").replace("')","");
		_gaq.push(['_trackEvent', 'BASKET', 'ADD', theProductId]);
		}else{
			var theProductId = $("[name=product_id]").val();
			_gaq.push(['_trackEvent', 'BASKET', 'ADD', theProductId]);
		}
	});
});

