/*jsl:ignoreall*/
/*jsl:option explicit*/
/*jsl:declare jQuery*/
/*jsl:declare $*/
/*jsl:declare document*/
/*jsl:declare window*/
var tabButtonClicked = false;

function changeTab(obj) {
	var old_tab = $(".tabs_content").find(".active");
	var new_tab = $(".tabs_content").find(obj.attr("href"));
	if (old_tab.attr("id") != new_tab.attr("id")) {
		old_tab.removeClass("active").fadeOut(500);
		new_tab.addClass("active").css('visibility','visible').fadeIn(500);
		$(".tabs_buttons").find(".active").removeClass("active");
		obj.addClass("active");
	}
}

function commaFormatted(amount) {
	var delimiter = ",";
	var n = amount;
	var a = [];
	while (n.length > 3) {
		var nn = n.substr(n.length - 3);
		a.unshift(nn);
		n = n.substr(0, n.length - 3);
	}
	if (n.length > 0) {
		a.unshift(n);
	}
	n = a.join(delimiter);
	amount = n;
	return amount;
}

var timeDelay = Math.round(Math.random() * 4 + 1);
var fullTime = 0;

function setCurrentSurveysAmount() {
	fullTime += timeDelay;
	var amount = completedSurveys + Math.round(responcesPerHour * fullTime / 3600);
	$("#completed-surveys-amount").html(commaFormatted(amount + ''));
	timeDelay = Math.round(Math.random() * 5 + 1);
	setTimeout(setCurrentSurveysAmount, timeDelay * 1000);
}

var animate = true;
var animateInterval = null;

function tabsAnimation(){
	if(animate) {
		var currentTab = $(".tabs_buttons a").index($(".tabs_buttons a.active"));
		changeTab($(".tabs_buttons a:eq(" + ((currentTab + 1) % 5) + ")"));
	}
}

$(document).ready(function() {
	$("#completed-surveys-amount").html(commaFormatted(completedSurveys + ''));
	if (responcesPerHour > 0)
		setTimeout(setCurrentSurveysAmount, timeDelay * 1000);
		$(".tabs_buttons").hover(function() {
			if (!tabButtonClicked && animate) {
				animate = false;
				clearInterval(animateInterval);
			}
		}, function() {
			if (!tabButtonClicked) {
				animate = true;
				animateInterval = setInterval(tabsAnimation, 3000);
			}
		})
	$(".tabs_buttons a").hover(function() {
		if (!tabButtonClicked) {
			changeTab($(this));
		}
	}, function() {}).click(function() {
		tabButtonClicked = true;
		if (animate) {
			animate = false;
			clearInterval(animateInterval);
		}
		changeTab($(this));
		return false;
	});

	$(".video > a").click(function() {
		TutorialVideo.setCurrentVideoByName("demo");
		TutorialVideo.showVideo();
		return false;
	}).hover(function() {
		$(this).closest('.tab').addClass('video_hover');
	}, function() {
		$(this).closest('.tab').removeClass('video_hover');
	});

	$(".sample_survey > a").hover(function() {
		$(this).closest('.tab').addClass('sample_survey_hover');
	}, function() {
		$(this).closest('.tab').removeClass('sample_survey_hover');
	});

	animateInterval = setInterval(tabsAnimation, 3000);
});
$(document).ready(function() {
	TutorialVideo.addTutorialVideoButton('demo');
});
