/* Author: 
	Peter Alt
	peter@tactable.com
	October 2011
*/

var debug = false;

$(document).ready(function() {
	
	$('#content .content').hide();
	$('#content .content').eq(0).show();
	
	if($.browser.msie)
		$('#subNavigation li.active').css("background-color", '#FFFFFF');

	VideoJS.setupAllWhenReady({
      controlsBelow: false, // Display control bar below video instead of in front of
      controlsHiding: true, // Hide controls when mouse is not over the video
      defaultVolume: 0.85, // Will be overridden by user's last volume if available
      flashVersion: 9, // Required flash version for fallback
      linksHiding: true // Hide download links when video is supported
    });

	//var verticalNavigation = ($('#main_navi').length > 0);
	//var verticalNavigation = true;
	
	$("#imagePages").scrollable({
		// basic settings
		vertical: true,
		// up/down keys will always control this scrollable
		keyboard: 'static',
		// assign left/right keys to the actively viewed scrollable
		onSeek: function(event, i) {
			horizontal.eq(i).data("scrollable").focus();
		}
	
	// Main Navigation definition
	}).navigator("#main_navi");
	
	// horizontal scrollables. each one is circular and has its own navigator instance
 	horizontal = $(".scrollable").scrollable({ circular: true, easing: 'swing' }).navigator(
	{
		navi: ".navi",
		naviItem: 'div'
	});
	
	if(!verticalNavigation)
		horizontal.eq(0).data("scrollable").getConf().keyboard = false;
		
	

// when page loads setup keyboard focus on the first horzontal scrollable
horizontal.eq(0).data("scrollable").focus();

// making the toolbox available globally
api = $("#imagePages").data("scrollable");

// Clone the navigation pager at startup
manageContentLayerNavigation();
manageSubtitle();

// Clone Navigation pager on every slide change
$(".scrollable").bind("onSeek", function() {
	manageContentLayerNavigation();
	manageSubtitle();
});

	if(verticalNavigation)
	{
		// Hacks to fire the cloning & resetting to first image 
		// when switching between projects
		$(document).keydown(function(e) {
		  //log(e.keyCode);
		  // Up & Down
		  if(e.keyCode == 40 || e.keyCode == 38)
		  {
		  	//manageContentLayerNavigation();
		  	horizontal.eq(api.getIndex()).data("scrollable").seekTo(0);
			showContentForPage();
		  }
		});
	}

// Little hack to display selected first pager when using keyboard navigation
// and circular mode
$(document).keyup(function(e) {
  //log(e.keyCode);
  // Right & Left
  if(e.keyCode == 39 || e.keyCode == 37)
  {
  	delay(function(){
      manageContentLayerNavigation();
    }, 500 );
  }
});

if($.browser.msie)
{
	$('#main_navi li').hover(function () {
		if(!$(this).hasClass('active'))
		{
	    	$(this).css("background-color", '#FFFFFF');
	    	$(this).css('color','#000000');
		}
	  },
	  function () {
	  	if(!$(this).hasClass('active'))
	  	{
	   	 	$(this).css("background-color", 'transparent');
	    	$(this).css('color','#FFFFFF');
	  	}
	  });
}

if(verticalNavigation)
{
	$('#main_navi li').click(function(){
		//manageContentLayerNavigation();
		horizontal.eq(api.getIndex()).data("scrollable").seekTo(0);
		showContentForPage();
	});
}

// Displays the according content for each project
showContentForPage = function showContentForPage()
{
	delay(function(){
      	$('#content .content').slideUp();
		$('#content .content').eq(api.getIndex()).slideDown();
    }, 200 );
	
}

// The cloning happens in here
function manageContentLayerNavigation()
{
	var contentLayers = $('.page .navi');
	var oldContent = contentLayers.eq(api.getIndex()).children('div').clone(true);
	$('#pager').empty().append(oldContent);
	if($.browser.msie)
	{
		$('#subNavigation li').css("background-color", 'transparent');
		$('#subNavigation li.active').css("background-color", '#FFFFFF');
	}
}

function manageSubtitle()
{
	var scrollIndex = horizontal.eq(0).data("scrollable").getIndex();
	if($('.items .subtitle').eq(scrollIndex).length == 0)
		$('#subtitle').hide();
	else
		$('#subtitle').show();
	var oldContent = $('.items .subtitle').eq(scrollIndex+1).clone(true).removeClass('hidden');
	$('#subtitle').empty().append(oldContent);	
}

//http://stackoverflow.com/questions/1909441/jquery-keyup-delay
var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();

$.easing.custom = function (x, t, b, c, d) {
	var s = 1.70158; 
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}



/*
idleTime = 0;
incrementInterval = 20; //secs
NextItemTimeout = 60; // secs
if(verticalNavigation)
{
    //Increment the idle time counter every minute.
    var idleInterval = setInterval("timerIncrement()", incrementInterval*1000); // 1 minute

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
        if(debug)
        	log('Mouse moved. Reset timeout');
    });
    $(this).keypress(function (e) {
        idleTime = 0;
        if(debug)
        	log('Key pressed. Reset timeout');
    });
}
*/
});
function timerIncrement() {
	idleTime = idleTime + 1;
    if(debug)
    	log('Timeout: '+idleTime*incrementInterval+'/'+NextItemTimeout);
    if (idleTime >= NextItemTimeout/incrementInterval) { // 20 minutes
        if(debug)
        	log('Next Image');
        if((api.getSize()-1) <= api.getIndex())
        	api.seekTo(0);
        else
        	api.next();
        setTimeout("showContentForPage()", 500);
    }
}

