/**
 * _project.js
 * commented for ideas and learning
 * by Ismo Vuorinen (ivuorinen@gmail.com) 20090114
 */
 
$(document).ready(function() {

 	// Get all the h3-elements, give them their own id and
	// print a nice link list to #quicklinks-list
	$("#r").find("h3").each(function(i) { // Search for h3 elements inside div#r
		lnk = $(this).html(); // Save the text inside h3 (<h3>this text</h3>) as lnk
		$(this).attr( "id", lnk ); // We set the id for every h3
		$("#quiclinks-list").append( "<li><a href=\"\#" + lnk + "\">" + lnk + "</a></li>" );
	});
	
	$("#quicklinks").css("display", "block"); // We have javascript on, so display it
	$("#quicklinks-container").slideUp(); // Show user it's there, and contains something
	$("#quicklinks-toggler a").click(function () { // Clicking the link toggles the contents
		$("#quicklinks-container").slideToggle(); // Toggle function
    });
    
    /////////
    
    // scrollTo effect to the quicklinks
    // @see http://demos.flesler.com/jquery/scrollTo/
    // @see http://demos.flesler.com/jquery/localScroll/
    $('#r').attr({scrollTop:0,scrollLeft:0});
    $.localScroll.hash({ target: '#r', axis:'y', queue:true, duration:1500 });
    var $last = $([]);
    $.localScroll({
		target: '#r', axis:'y', queue:true, duration:1000, hash:true,
		onBefore:function( e, anchor, $target ){//'this' is the clicked link
			$last.removeClass('scrolling');
			$last = $(this).addClass('scrolling');
			if( this.blur )
				this.blur();//remove the awful outline
		},
		onAfter:function( anchor ){ $last.removeClass('scrolling'); }
	});
	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
	$('#quiclinks-list a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 1500, { easing:'elasout' });
		$(this.hash).find('h3').text( this.title );
		return false;
	});

    
	/////////
	
    // <a class="lightbox"> opens a nice box for screenshots
    $('a.lightbox').lightBox();
	
	
});