var Quiz = {
    subjects : ["chronic","doctors","hospital","age","gender","marital","child","caretaker"],
    current : 0,
    url : '/ajax/quiz/',
    push : function (q,a)
    {
        $.get(Quiz.url, { "q": q, "a": a },function(data){
             if (data.status == 'error')
             {
                 alert(data.error);
             }
             else
             {
                var q = Quiz.subjects[Quiz.current];
                $("div[id='" + q + "']").removeClass('active');
                //alert(q);
                Quiz.current++;
                //alert(Quiz.current);
                var q = Quiz.subjects[Quiz.current];
                $("div[id='" + q + "']").addClass('active');
                //alert(q);
                Quiz.set();

                if (Quiz.current == 8)
        	    {
        	        setTimeout(function(){
        	            location.href= "/acquiz/";
                    }, 500);
        	    }
             }
        }, 'json');

    },
    set : function()
    {
        var q = Quiz.subjects[Quiz.current];
        $("input[subject='" + q + "']").unbind('click');
        $("input[subject='" + q + "']").click(function(){
    	    var a = $(this).attr('answer');
    	    if (!a)
    	    {
    	        a = $("option[subject='" + q + "']:selected").val();
    	    }

    	    Quiz.push(q,a);

        });
    }

};


$(document).ready( function() {


	/* --- FIVE ANSWERS POP-UPS ---------------------------------------------------------------------- */

	$("#five-answers .what-is-bubble").hover( function(){
		$("#five-answers .what-is-pane").show();
	});
	
	$("#five-answers .what-is-pane").mouseleave( function() {
		$(this).hide();
	});
	
	
	$('#five-answers ol li').hover(
		function() {
			var item = $(this).attr('id').replace('answer-','');
			$('#five-answers .pane.' + item).fadeIn('fast');
		},
		function() {
			$('#five-answers .pane').fadeOut();
		}
	);

	/* --- FIVE ANSWERS SIDEBAR ---------------------------------------------------------------------- */

	$('#fiveanswers-section h3').mouseenter( function() {
		$(this).parent().addClass('expanded');
		$(this).next('p').show();
	});

	$('#fiveanswers-section li').mouseleave( function() {
		$(this).filter('.expanded').children('p').hide();
		$(this).filter('.expanded').removeClass('expanded');
	});

	/* ----------------------------------------------------------------------------------------------- */


	// homepage - five questions
	
	$("#five-questions .next").click( function() {
		var current = $("#five-questions .question.active");
		var next = $("#five-questions .question.active").next(".question");
		if (next.attr("id") == null) {
			next = $("#five-questions .question:first");
		}
		current.fadeOut("normal", function() {
			current.removeClass("active");
			next.fadeIn("slow", function() {
				next.addClass("active");
			});
		});
	});

	/*
	var myLoop = 1;

	if (myLoop++ <= 5) {
		setTimeout('$.rotateQuestions', 500);
	}

	$.fn.rotateQuestions = function () {
		alert('ping');
		var current = $("#five-questions .question.active");
		var next = $("#five-questions .question.active").next(".question");
		if (next.attr("id") == null) {
			next = $("#five-questions .question:first");
		}
		current.fadeOut("slow", function() {
			current.removeClass("active");
			next.fadeIn("slow", function() {
				next.addClass("active");
			});
		});
	};
	*/

	// end five questions

	// CASE STUDIES PAGE - FILTERS

	$("#filters input:checkbox").click( function() {
		var cats = new Array();
		var show = new Array();
		$("#filters input:checkbox").each(function(){
			if ($(this).is(':checked'))
			{
				cats.push($(this).val());
			}
		});
		$.each(cats, function(k, v){
			$(".item").each(function(sk, sv){
				var cls = v.replace(" ", "-").replace("&", "-").toLowerCase();
				if ($(this).hasClass(cls)) {
					show.push($(this));
				}
			});
		});

		$(".item").hide();
		$.each(show, function(){
			$(this).fadeIn("slow");
		});
	});


	// Stacked - Scrolling

	// calculate total row width
	/*
	var rowWidth = 0;
	$("#stacked .row").each( function() {
		rowWidth = rowWidth + 930;
	});
	$("#stacked .list").width(rowWidth);

	var scrollPos = 0;

	$("#stacked .right-arrow").click ( function() {
		scrollPos = scrollPos + 930;
		if (scrollPos > rowWidth - 930) {
			scrollPos = rowWidth - 930;
		}
		$("#stacked .container").scrollLeft(scrollPos);
	});

	$("#stacked .left-arrow").click ( function() {
		scrollPos = scrollPos - 930;
		if (scrollPos < 0) {
			scrollPos = 0;
		}
		$("#stacked .container").scrollLeft(scrollPos);
	});
	*/


	//Stacked

	$("#stacked .row:first").addClass("active");
	$("#stacked .container").height($("#stacked .row:first").height());

	$("#stacked .right-arrow").click ( function() {
		currentRow = $("#stacked .row.active");
		nextRow = $("#stacked .row.active").next("#stacked .row");
		nextAfter = nextRow.next("#stacked .row");

		if (nextRow.attr("id") != null) {
			currentRow.fadeOut('slow', function() {
				newHeight = nextRow.height() + 10;
				$("#stacked .container").animate( { height : newHeight }, "normal", function() {
					nextRow.fadeIn('slow');
					currentRow.removeClass("active");
					nextRow.addClass("active");
					$("#stacked .right-arrow").fadeTo("normal",1);
					$("#stacked .left-arrow").fadeTo("normal",1);
				});
			});
		} else {
			$("#stacked .right-arrow").fadeTo("normal",.25);
		}
	});

	$("#stacked .left-arrow").click ( function() {
		currentRow = $("#stacked .row.active");
		prevRow = $("#stacked .row.active").prev("#stacked .row");
		prevBefore = prevRow.prev("#stacked .row");

		if (prevRow.attr("id") != null) {
			currentRow.fadeOut('slow', function() {
				newHeight = prevRow.height() + 10;
				$("#stacked .container").animate( { height : newHeight }, "normal", function() {
					prevRow.fadeIn('slow');
					currentRow.removeClass("active");
					prevRow.addClass("active");
					$("#stacked .left-arrow").fadeTo("normal",1);
					$("#stacked .right-arrow").fadeTo("normal",1);
				});
			});
		} else {
			$("#stacked .left-arrow").fadeTo("normal",.25);
		}
	});


	// FAQs

	$("#faqs-page .question").unbind("click").click( function() {
		if ($(this).next(".answer").is(':visible')) {
			$(this).next(".answer").slideUp();
		} else {
			$(".answer:visible").slideUp();
			$(this).next(".answer").slideDown();
		}
	});


	// quiz
	$("#submit").click(function(){
		pageTracker._trackPageview("/completesurvey");
	});


	// rss link
	$("#rss-link").click(function(){
		pageTracker._trackPageview("/news/rss");
	});

	/* --- 5 QUESTIONS PAGE ------------------------------------------------------ */
	
	
	
	$("#fivequestions-page .question").unbind("click").click( function() {
		if ($(this).next(".why").is(':visible')) {
			$(this).next(".why").slideUp();
		} else {
			$(".why:visible").slideUp();
			$(this).next(".why").slideDown();
		}
	});
	
	/* --------------------------------------------------------------------------- */

	Quiz.set();


});
