var rmclosed=0;

$(document).ready(function(){
	$('#home-but').bind( 'click', goToReferer );
	$('#prev').bind( 'click', prev );
	$('#next').bind( 'click', next );
	$('#refresh').bind( 'click', refresh );
	$('#tooltip').bind( 'mouseover', clearHide );
	$('#tooltip').bind( 'mouseout', hideTooltip );
	$('#authorAvatar').bind( 'mouseover', showTooltip );
	$('#authorAvatar').bind( 'mouseout', hideTooltip );

	if( !shallShowHint() )
	{
		$('#read-this').css( 'display', 'none' );
		rmclosed=1;
		resize();
	}

	initApp();
});

function markNextButton()
{

}

function unmarkNextButton()
{

}

function loadReaderInterface()
{
	$('#jogTitle').html( track.title );

	$('#authorAnchor').attr( 'href', 'http://www.jogtheweb.com/user/' + track.authorNickname );
	$('#authorAnchor').html( track.authorNickname );

	var steps = track.steps;
	var text = '';
	for( var i=0;i<steps.length;i++ )
	{
		var claz = 'stepEmbed';
		if( i%2 )
		{
			claz = claz + ' gray';
		}
		if( steps[i].type == '1' )
		{
			claz = claz + ' intercallaire';
		}
		text += '<p><a class="' + claz + '" href="javascript:void(0);">';
		text += steps[i].position + '. ' + steps[i].domain + '</a></p>';
	}

	$('#sidebar-items').append( text );

	$( '#authorAvatar' ).attr( 'src', track.imageUrl );
	$( '#authorAvatar2' ).attr( 'src', track.imageUrl );

	// Step menu
	$('#sidebar-items .stepEmbed').each(
		function( index ) {
			$(this).bind(
				'click',
				function() {
					selectStep( index+1 );
				}
			);
		}
	);
}

function resize()
{
	var clientHeight = $(window).height();
	$('#jogTheWebContentFrame').css('height', (clientHeight-152)+'px');
	if (rmclosed==0)
	{
		$('#sidebar-items').css('height', (clientHeight-310-50)+'px');
	}
	else
	{
		$('#sidebar-items').css('height', (clientHeight-120-50)+'px');
	}

}

function onSelectStep( oldPosition )
{
	var elPosition = oldPosition-1;
	$( '#sidebar-items .stepEmbed:eq(' + elPosition + ')' ).removeClass( 'active' );
	elPosition = stepIndex-1;
	$( '#sidebar-items .stepEmbed:eq(' + elPosition + ')' ).addClass( 'active' );

	var step = track.steps[elPosition];
	//var type = intval( step.type );
	var type = step.type;
	var titre = step.domain;
	var description = step.description;

	$('#stepNumer1').html( step.position );
	$('#stepNumer2').html( step.position );

	var text = '';

	if( type != 1 )
	{
		text += '<h1>' + titre + '</h1>';
		text += '<p><a href="' + step.url + '" target="_blank">' + step.url + '</a></p>';
		if( description.length >280 )
		{
			description = description.substr( 0,280 ) + ' [...] <a onclick="expandHeader()" href="javascript:void(0)">&gt;&gt;</a>';
		}
	    text += '<p>'+ description +'</p>';
	}
	else
	{
		text += '<h1>' + titre + '</h1><br /><br />';
	}
	text += '<br class="clear" />';
	$('#headerStepTitle').html( text );

	text = '<h1>' + step.domain + '</h1>';
	text += '<p><a href="' + step.url + '" target="_blank">' + step.url + '</a></p>';
	text += '<p>' + step.description + '&nbsp;&nbsp;<a onclick="collapseHeader()" href="javascript:void(0)">&lt;&lt;</a></p>';
	$('#stepContent').html( text );

	text = '<p class="ttitle">' + step.domain+ '</p>'
    text += '<p><a href="' + step.url + '" target="_blank">' + step.url + '</a></p>'
    text += '<p>' + step.description + '</p>'

	$( "#stepHint" ).html( text );
	collapseHeader();
	var addH = rmclosed == 0 ? $( '#read-this' ).height()+20 : 0;
	var scrollPos = $( '#sidebar-items .stepEmbed:eq(' + elPosition + ')' ).offset().top
					- $( '#header' ).height()
					- addH
					+ $('#sidebar-items').scrollTop()
					-115-50+19;
	$('#sidebar-items').animate({scrollTop:scrollPos}, 1000 );
}

function expandHeader(){
	$('#collapsed-header').slideUp('normal', function callback() {$("#expanded-header").slideDown("slow");});
}

function collapseHeader(){
	$('#expanded-header').slideUp('normal', function callback() {$("#collapsed-header").slideDown("slow");});
}

function closeReadme(){

	if( $('#nohint:checked').length ==1 )
	{
		dontShowHintNoMore();
	}

	$('#read-this').slideUp(300,
		function callback() {
			rmclosed=1;
			resize();
		}
	);
}
var ttip;

function showTooltip() {
	clearHide();
	$('#tooltip').fadeIn('slow');
}

function hideTooltip() {
	ttip=setTimeout("$('#tooltip').fadeOut('normal');", 700);
}
function clearHide() {
	clearTimeout(ttip);
}

function dontShowHintNoMore() {
	var expireDate = new Date();
	expireDate.setTime(expireDate.getTime() + 12*30*24*3600*1000);
	document.cookie = "nohint=1"
	   + ";expires=" + expireDate.toGMTString()
	   + ";path=/";
}

function shallShowHint() {
	var deb = document.cookie.indexOf( "nohint=")
	if (deb >= 0) {
	   deb += 7;	// Lenght of "nohint"
	   var fin = document.cookie.indexOf( ";" , deb )
	   if (fin < 0)
	   {
			fin = document.cookie.length;
	   }
	   var rep = unescape(document.cookie.substring(deb,fin));
	   return rep == 1 ? false : true;
	   }
	return true;
}
