$(document).ready(function() {
	var links = new Array();
	var ids = new Array();
	var slot = 0;
	var permalink = document.location.href;
		

	/** 
	 * Store all links in an array for key press navigation
	 */
    $("#subnav .entry, .entry_selected").each(function(i) { 
    		var id = $(this).attr( "id" );
    		ids[i] = id;
   	});	
   	
   	$("#subnav a").each(function(i) { 
    		var href = $(this).attr( "href" );
    		links[i] = href;
   	});	
   			
	/** 
	 * Override the default behavior of all subnav a elements so that, when
  	 * clicked, their href value is pushed onto the history hash
  	 * instead of being navigated to directly.
  	 * BBQ plugin takes care of deep linking and history by storing
     * each URL-change within a hash
  	 */
  	$("#subnav a").click(function(){
   		var href = $(this).attr( "href" );
   		  	
		// store link for Facebook and twitter
   		permalink = 'http://thomasnoller.com/' + href;
   		
   		// Push this URL "state" onto the history hash.
    	$.bbq.pushState({ url: href });
    	
      	// set 'focus' to subnav
    	enableKeyScroll = true;
    	
    	

    	// Prevent the default click behavior.
    	return false;
    	
    	//document.title = $(this '.title').text();
  	});
  	
  	/**
  	 * Register for key presses
  	 
  	$(document).keydown(function(e) {
  		var key = e.which;
      	var sid = currSection(window.location.href);
    	
		if(links.length > 1) {
      		switch(key) {
				case 40: // down arrow
					if(slot < ids.length - 1) slot = slot + 1;
					else slot = ids.length - 1;
					var href = links[slot];
					$.bbq.pushState({ url: href });
					showArticle(sid, links[slot]);
				break; 
				
				case 38: // up arrow
					if(slot > 0) slot = slot - 1;
					else slot = 0;
					var href = links[slot];
					$.bbq.pushState({ url: href });
					showArticle(sid, links[slot]);
				break;
				
				case 39: // right arrow
					return false;
				break; 
				
				case 37: // left arrow
					return false;
				break; 
			}
		}
			 
    });*/
     
    /**
  	 * Load entry
  	 */
    function showArticle(sid, aid) {
    	if(sid == 2) { // Photos
    		$('#stage').load("empty.html", function() {
  				$('#stage').load("photos.php?a=" + aid);  	
  			});
 		}else{ // Blog, Projects etc
    		$('#stage').load("empty.html", function() {
  				$('#stage').load("article.php?a=" + aid);
  			});
  		}
	}
  	
   	/**
  	 * Bind a callback that executes when document.location.hash changes
  	 * This function takes care of regular clicks
  	 */
  	$(window).bind("hashchange", function(e) {
    	var url = e.getState( "url" );
    	    	 
    	// split url into params
    	var sid = currSection(url);
    	var aid = currArticle(url);
    	
    	if(aid != "" && aid != null) {
    		// set back all nav buttons
   			$(".subsection_node div").each(function(i) { 
    			$(this).removeClass("entry_selected");
    			$(this).addClass("entry");
   			});	
    		
    		// hilight the selected div
    		$("#" + aid).removeClass("entry");
    		$("#" + aid).addClass("entry_selected");
    		showArticle(sid, aid);
   		}else{
   			return false;
   		}
  	});
  	
  	/**
  	 * Returns current section id
  	 */
  	 function currSection(url) {
    	var obj = $.deparam.querystring(url);
    	sid = obj.s;
    	return sid;
  	 }
  	 
  	 /**
  	 * Returns current article id
  	 */
  	 function currArticle(url) {
    	var obj = $.deparam.querystring(url);
    	aid = obj.a;
    	return aid;
  	 }
  	 
  	/** 
  	 * Since the event is only triggered when the hash changes, we need
     * to trigger the event now, to handle the hash the page may have
     * loaded with.
     */
  	$(window).trigger( "hashchange" );
  	
  	/** 
  	 * top pull down/up.
     */
	$('#nametag').toggle(
   		function(){
      		$('#brand').animate({
      			top: '+=50' 
			}, 300);
   		},
   		function(){
      		$('#brand').animate({
      			top: '-=50' 
      		}, 300);
   		});
   		
   	/*
   	* FB
   	*/
   	$('#fb').click(
   		function(evt){
   			t = $('#subnav .entry_selected .title').text();
   			window.open('http://www.facebook.com/sharer.php?u='+ encodeURIComponent(permalink)+'&t='+ t,'sharer','toolbar=0, status=0, width=600,height=500'); return false;
   			evt.preventDefault();
   		}
   	);
   	
   	/*
   	* Twitter
   	*/
   	$('#tw').click(
   		function(evt){
   			t = $('#subnav .entry_selected .title').text();
   			window.open('http://twitter.com/share?url='+ encodeURIComponent(permalink)+'&text='+ encodeURIComponent(t),'sharer','toolbar=0, status=0, width=600,height=500'); return false;
   			evt.preventDefault();
   		}
   	);
 });

  





