(function($) {
	
	
	$(function() {
		
		if ($.browser.safari) {
			$('body').css('background-image','-webkit-gradient(linear, left top, left bottom, from(rgba(55,55,55,1.0)), to(rgba(15,15,15,1.0)))');
		}
		
		$('#twitter').append($("<span/>"));

		$('.screen-browser').screens().removeClass('screen-browser');
		setTimeout(function() {
		    $('.nav').navigation();		
	    },10);
	    
	    $('.m').m().removeClass('m');
	});

	var hash = window.location.hash.replace('#','');
	if (hash && hash!='') {
	    window.location.href = hash;
	}
	
})(jQuery);



(function($) {
	
	$.fn.m = function() {
        return this.each(function() {
            var $m = $(this);
            var t = this.getAttribute('data-to');
            $m.html(t+'&#64;&#114;&#101;&#101;&#100;&#101;&#114;&#46;&#99;&#104;');
            $m.attr('href','mailto:'+$m.text());
        });
        
	}
	
	$.fn.navigation = function() {
		
		
		var $selected = null;
		var $page = $('.page');
		
		var $indicator = $('.indicator');
		var diff = $indicator.height()/2;
		
		
		var _top = function($a) {
			var h = $a.outerHeight()/2;
			var pos = $a.offset();
			var top = pos.top - diff + h - $('#wrapper').offset().top;
			return top;
		}
				
		var pg = $page.attr('id');
        $selected = $('#navigation .'+pg);
        $indicator.show().css({top:_top($selected)+'px'});
        		
		
        // $indicator.css({
        //     display: 'block',
        //  top: _top(this.eq(0))+'px'
        // });
				
		var cached = {};
		
		cached[$selected.attr('href')] = $page;
				
		return this.each(function() {
									
			var $a = $(this);
			
			$a.click(function() {
					                
					
				$a.blur();
				
				var top = _top($a);
				
				$indicator.animate({
					top: top+'px'
				},{easing: 'easeOutCubic', duration: 300});
		
				if (this == $selected.get(0)) {
					// already loaded
					return false;
				}
				
				
				$selected = $a;
				
				var c = $a.attr('href').split('#')[1];
				var href = $a.attr('href');
				
				window.location.hash = href;
				
				var _show = function($c) {

    				var $old = $page;
                    if (!$old) $old = $('#app .page:visible');
                    
                    $page = $c;
                
                    //$page = $('#app .'+c);
                    $page.css({display: 'block', opacity: 0, left: '-450px'}).animate({left: '40px', opacity: 1},{easing: 'easeOutCubic', duration: 500});
    				if ($old) $old.animate({left: '-450px', opacity: 0},{easing: 'easeOutCubic', duration: 500});
                }
                
				if (cached[href]) {
				    _show(cached[href]);
				}
				else {
				    $.get(href, function(html) {
                        var $c = $(html);
                        $('#app').append($c);
                        cached[href] = $c;
                        _show($c);
                		$('.screen-browser').screens().removeClass('screen-browser');
                		$('.m').m().removeClass('m');
				    });
				}
                                
				
				
				return false;
			});
			
		});
		
	}
	
})(jQuery);



(function($) {
	
	$.fn.screens = function() {
		
		return this.each(function() {
 
			var $obj = $(this);
			
			var $images = $(this).find('img');
			
			var $prev = $("<a/>").attr('href','#prev').addClass('prev').html('&lsaquo;');
			var $next = $("<a/>").attr('href','#next').addClass('next').html('&rsaquo;');
						
			
			$obj.append($prev,$next);
			
			var current = 0;
			$prev.click(function() {
				
				if (current <= 0) {
					return false;
				}
				
				var $current = $images.eq(current);
				
				current--;
				
				if (current == 0) {
					$prev.fadeOut();
				}
				else {
					$prev.fadeIn();
				}
				$next.fadeIn();
				
				var $img = $images.eq(current);
				
				$img.css({
					left: '-320px'
				}).show();
 
				$img.animate({
					left: 0
				},300);
				
				$current.animate({
					left: '320px'
				},300);
				
				return false;
			});
			
			$next.click(function() {
				
				if (current+1 >= $images.length) {
					return false;
				}
				
				var $current = $images.eq(current);
				
				current++;
				if (current+1 == $images.length) {
					$next.fadeOut();
				}
				else {
					$next.fadeIn();
				}
				$prev.fadeIn();
				
				var $img = $images.eq(current);
				
				$img.css({
					left: '320px'
				}).show();
 
				$img.animate({
					left: 0
				},300);
				
				$current.animate({
					left: '-320px'
				},300);
				
				return false;
			});
			
 
		});
		
	}
	
})(jQuery);
 

