// Make the first button (Compassionate) active (default)
var	active_button_id = 3;

var index = 2;
var $pane = null;
var timer = null;
var r_idx = null;
var $pane = null;
var feed_count = null;
var MAX_COUNT_IN_PANE = 4;
var is_scrolling = true;
var last_feed_id = null;


function load_question_and_answers( last_answer_id )
{
	// Reset answer div
	$("#answers").css("top", "0px");

	// Show pre loader
	$("#answers").html('<div id="loader"><img src="/images/murdoch-loader.gif" alt="Loading ..."></div>');

	clearInterval(timer);
	$.getJSON("/ajax/load_feed_question_and_answers.php",{id:active_button_id, last_feed_id:last_answer_id}, function(data){

		$("div#answer_area h1").html(data.html_question);
		$("input#btn_submit").attr("src", "/images/feed/buttons/submit_" + active_button_id + ".gif");
		$("#answers").html(data.html_answers);
		feed_count = data.count_answers;
		new_last_answer_id = data.last_answer_id;

		// set the feed_question id in the form
		$("#feed_question_id").val(active_button_id);

		$pane = $("#answers");
		$pane.removeAttr("class");
		$pane.addClass('scroll-pane category_' + active_button_id);

		// Inititialise the scroll pane if we have enough to scroll
		$("#answer_area #ctrl_button").hide();
		if( feed_count > MAX_COUNT_IN_PANE )
		{
			$("#answer_area #ctrl_button").show();
			$pane.jScrollPane({scrollbarWidth:0, scrollbarMargin:0, animateTo:true, animateStep:6, maintainPosition:false});
			index = 1;
	 		$pane[0].scrollTo("#p1", true);
			timer=setInterval("scroller(new_last_answer_id)", 3000);

			// set the flag thet we have scrolling active
			is_scrolling = true;
			var ctrl_button_src = "/images/feed/buttons/btn_pause_" + active_button_id +".gif";
			$("#answer_area #ctrl_button img").attr("src", ctrl_button_src);
		}
    });

}

function select_button()
{
		var tag = "ul#categories .category_" + active_button_id;

		make_inactive($(tag));

		active_button_id = $(this).attr("btn_id");
		make_active($(this));

		// empty the answer line
		$("#free_thought_feed_feed_answer").val("");

		// Update play/pause button

		var ctrl_button_src = "/images/feed/buttons/btn_pause_" + active_button_id +".gif";
		$("#answer_area #ctrl_button img").attr("src", ctrl_button_src);

		// Omniture tracking
		var s=s_gi(s_account);
		switch (active_button_id)
		{
			case "1": s.pageName=s.pageName.replace(new RegExp(/freethinker|main page|creativity(:submit)?|visionary(:submit)?|compassionate(:submit)?|practical(:submit)?/g),'creativity'); break;
			case "2": s.pageName=s.pageName.replace(new RegExp(/freethinker|main page|creativity(:submit)?|visionary(:submit)?|compassionate(:submit)?|practical(:submit)?/g),'visionary'); break;
			case "3": s.pageName=s.pageName.replace(new RegExp(/freethinker|main page|creativity(:submit)?|visionary(:submit)?|compassionate(:submit)?|practical(:submit)?/g),'compassionate'); break;
			case "4": s.pageName=s.pageName.replace(new RegExp(/freethinker|main page|creativity(:submit)?|visionary(:submit)?|compassionate(:submit)?|practical(:submit)?/g),'practical'); break;
		}
		s.t();

		load_question_and_answers(0);
	}


var skip_animate = false
function scroller( last_answer_id )
{
	var targetString = "#p" + index;

	$("#answers")[0].scrollTo(targetString, skip_animate);
	skip_animate = false;
	index++;

	// Reset if loop end is reached
	if( index > feed_count - 3 )
	{
		$pane[0].scrollTo("#p1", true);
		load_question_and_answers(last_answer_id);
	}
}

function make_active(element)
{
	if( !element.css('background-image').match("-act.gif") )
	{
		element.css('background-image', element.css('background-image').replace(".gif", "-act.gif"));
	}
}

function make_inactive(element)
{
	element.removeAttr('style');
}


$(document).ready(function() {

	// Click event for category buttons
	$("ul#categories li").click(select_button);


	// Click event for play/pause button
	$("#answer_area #ctrl_button img").click(function()
	{
		var ctrl_button_src;

		if( is_scrolling == true )
		{
			ctrl_button_src = "/images/feed/buttons/btn_play_" + active_button_id +".gif";
			is_scrolling = false;
			clearInterval(timer);
		}
		else
		{
			ctrl_button_src = "/images/feed/buttons/btn_pause_" + active_button_id +".gif";
			is_scrolling = true;
			timer=setInterval(scroller, 3000);
		}

		$(this).attr("src", ctrl_button_src);

	});


	// Hover event for the buttons
	$("ul#categories li").hover(function () {
        if( $(this).attr("btn_id") != active_button_id )
        {
			make_active($(this));
        }
      },
      function () {
        if( $(this).attr("btn_id") != active_button_id )
        {
			make_inactive($(this));
        }
      }
    );



	// To avoid a Safari/Firefox bug with the background image not being loaded in time
	// Call the function to set the Creativity tab as active, 500ms after page load
	setTimeout(function() { make_active($("ul#categories li.category_" + active_button_id)); }, 500);


	// Load question and answer feed and initialise the scroll pane
	load_question_and_answers(0);
});