$(document).ready(function() {

	/* Apply fancybox to all images of all tabs */
	$("a.fancybox_image").fancybox({
		'cyclic' : true,
		'speedIn': 1000,
		'speedOut':1000
	});

	/* Add play/pause button */
	$("#fancybox-wrap").append('<input type="image" src="/img/play.png" id="play_pause_button" value="Play" onclick="toggleRotating(true);" />');
	
	/* Bind pagination link */
	$('.teamItem.teamPage a').each(function(i) {
		$(this).bind('click', function() {
		  go_to_page($(this).attr("page"));
		});
	});	
	
	/* Bind prev/next link */
	$('.prev-next-link').each(function(i) {
		$(this).bind('click', function() {
		  go_to_page($(this).attr("page"));
		});
	});
	

	$("a.iframe").fancybox({
	'width'		    : '75%',
	'height'	    : '75%',
        'autoScale'	    : false,
        'transitionIn'	    : 'none',
	'transitionOut'	    : 'none',
	'type'		    : 'iframe'
    });

});

/* toggle play/pause rotation fancybox slideshow */
var rotatingInterval = false;
function toggleRotating(fromButton) {
  if (rotatingInterval) {
		$("#fancybox-wrap #play_pause_button").attr("value","Play");
    clearInterval(rotatingInterval);
    rotatingInterval = false;
  } else {	
		$("#fancybox-wrap #play_pause_button").attr("value","Pause");
    rotatingInterval = setInterval($.fancybox.next, 4000);
    if (fromButton)
      $.fancybox.next();
  }
}
// vedi http://stackoverflow.com/questions/3976714/converting-string-true-false-to-boolean-value

/* random generator */
function randomNum(maxNum)
{
	return Math.floor(Math.random()*maxNum+1);
}

/* custom tabs transitions div for div */
function slideTabs(tabOld, tabNew, timeOff, timeOn)
{
	tabOld.children().fadeOut(timeOff);
	tabNew.children().delay(timeOff).fadeIn(timeOn);
}
function slideTabs2(tabOld, tabNew, maxDelay, timeOff, timeOn)
{
	tabOld.children().each(function(i) {
		var del = (i==1) ? 0 : randomNum(maxDelay);
		$(this).delay(del).fadeOut(timeOff);	
		tabNew.children().eq(i).delay(del+timeOff).fadeIn(timeOn);
	});
	//tabNew.children().each(function(i) {
	//	$(this).delay(timeOff+randomNum(maxDelay)).fadeIn(timeOn);
	//});
};
function flipTabs(tabOld, tabNew)
{
	tabOld.children().each(function(i) {
		$(this).delay(randomNum(50)*(i-1)).flip({
	    direction:'lr',
			content: i+" flipped!"
		});		
	});
};

/* Pagination */
function go_to_page(n)
{
	// se la n non è la pagina attiva
	if ($("#tabs-"+n).hasClass("hideTab"))
	{
		//slideTabs($(".showTab"), $("#tabs-"+n), 200, 200);
		slideTabs2($(".showTab"), $("#tabs-"+n), 1000, 500, 500);
		//flipTabs($(".showTab"), $("#tabs-"+n));
		$(".showTab").removeClass("showTab").addClass("hideTab");
		$("#tabs-"+n).removeClass("hideTab").addClass("showTab");
		$(".teamItem.teamPage").removeClass("teamPageActive");
		$(".teamItem.teamPage.page-"+n).addClass("teamPageActive");
		$("#prev-link").attr("page", prev_page(n));
		$("#next-link").attr("page", next_page(n));
	}
};

function max_page()
{
	return $('.teamItem.teamPage a').last().attr("page");
}

function prev_page(n)
{
	if (n == 1)
	{
		return max_page();
	}
	else
	{
		return parseInt(n)-1;
	}
};

function next_page(n)
{
	if (n == max_page())
	{
		return 1;
	}
	else
	{
		return parseInt(n)+1;
	}
}

