var activeID;
var windowRef;
var lastPos;

$(function() {   

  windowRef = $( window );

  $( ".entry-title" ).click(function(){
  
    activeID = $( this ).data( "id" );
    
    loadContent( activeID );
  
  });
  
  $(window).bind('hashchange',function(event){
    var hash = location.hash.replace('#','');
    if(hash == '') $(window).scrollTop(lastPos);
  });
  
  $( document ).ready( function(){
  
    $( ".post" ).each(function(){
      $( this ).attr( "original_height", $( this ).height() );
    });
    
    var directID = getDirectID();
    
    if( directID != -1 )
    {
      setTimeout( function(){ loadContent( directID ) }, 10 )
    }
  
  } )
  
  $( ".close_post" ).click( function(){
        
    lastPos = $(window).scrollTop();
    location.hash = '';

    var postDIV = $( this ).parent().parent().parent().parent();
    
    postDIV.find( ".body_copy_container" ).fadeOut(100, function(){
    
      var pageFoundN = 0;
    
      postDIV.find( ".page" ).each(function(){
        if( pageFoundN == 0 )
        {
          $( this ).empty();
          $( this ).attr( "style", "" );
        }else{
          $( this ).remove();
        }
        
        pageFoundN++;
      })
    
    });
    
    postDIV.animate( {height:postDIV.attr( "original_height" )} );
    postDIV.find( ".close_post" ).fadeOut();
    $( "nav" ).fadeOut();
  } )
  
  
  $( window ).resize(function(){
  
    positionMeta();
  
  });

  
  positionMeta();
  
  
});

function openPost( id )
{
  var thisPost = $( "#post-" + id );
  var everythingElse = $( ".post:not(#post-" + id + ")" );
  var bodyHeight = $( "#post-" + id ).find( ".body_copy_container").height() + Number( $( "#post-" + id ).attr( "original_height" ) );
  
  thisPost.animate({height:bodyHeight}, function(){
      
    //$( this ).find( ".body_copy_container").height( bodyHeight );
    $('body').animate({scrollTop:thisPost.offset().top - 250});
    $( this ).find( ".close_post" ).fadeIn();
    
    
    
  });
  everythingElse.each( function(){

    $( this ).find( ".body_copy_container" ).fadeOut(100, function(){
      
      var pageFoundN = 0;
    
      $( this ).find( ".page" ).each(function(){
        if( pageFoundN == 0 )
        {
          $( this ).empty();
          $( this ).attr( "style", "" );
        }else{
          $( this ).remove();
        }
        
        pageFoundN++;
      })    
    });

    $( this ).animate( {height:$( this ).attr( "original_height" )}, {queue:true} );
    $( this ).find( ".close_post" ).fadeOut();
  } )
  
  jQuery.fancybox.hideActivity();
  
}

function loadContent( id )
{
  jQuery.fancybox.showActivity();

  $.ajax({
		url: templateURL + "/post_load.php",
		data:{
			post_id: id
		},
		complete: function( data ){
						
			populateContent( id, data );
			
		}
	});
}

function populateContent( id, data )
{
  var postHTML = data.responseText;

  postHTML = postHTML.replace( /\.\.\/wp-content\/uploads/g, templateURL + "/../../../wp-content/uploads" );

  $( "#post-" + id + " .body_copy_container" ).html( postHTML );
  
  var imageCount = ( $( "#post-" + id + " .body_copy_container" ).find( "img" ).length );
  var loadedCount = 0;
  
  jQuery( "#post-" + id + " a.fancybox-iframe" ).fancybox();
  
  if( imageCount == 0 )
  {
    $( "#post-" + id ).find( ".body_copy_container" ).fadeIn();
    openPost( id );
  }else{
    $( "#post-" + id + " .body_copy_container" ).find( "img" ).load(function(){
      loadedCount++;
      
      if( loadedCount == imageCount )
      {
        $( "#post-" + id ).find( ".body_copy_container" ).fadeIn();
        openPost( id );
      }
    
      
    });

  }
  
  
  
}

function positionMeta()
{
  var windowWidth = windowRef.width();

  $( ".meta_verbs" ).css( { "padding-left":( ( windowWidth / 2 ) - 425 ) + "px" } )
}

function getDirectID() {
	var currentURL = window.location.href;
	var directID = currentURL.substring(currentURL.indexOf("#") + 1);
	
  if( currentURL.indexOf("#") == -1 || directID == "" )
	{
	 return -1;
	}
	
	return( directID );
}
