/**
 * @author Joe McGuire <joe@numiko.com>
 */
$( document ).ready(function() {
  commentContainers = $(".iac-comments");
  
  $( commentContainers ).each(function(){
    loadComments( this )
  })
});

function loadComments( commentContainer, page ){
    $( commentContainer ).html( '<div class="loading">Loading comments...</div>' );
	
	 $(commentContainer).css('width', '545px');

    options = getOptions( commentContainer );
    if( page != null ){
      options.page = page;
    }
    getQueryString( options );
    container = commentContainer;
    
    // if the signin block is showing, check whether this is an accidentally cached page
    // by getting the comments script to return the form if we are logged in
    if($(".signin").length == 1){
      options["getform"] = 1;
    }
    
    $.getJSON( Drupal.settings.iacComments.json + '?' + getQueryString( options ),
    function( data ){
      
      if(data.form != ""){
        
        // if we have the form too then replace what's already here
        
        $("#comments .signin").remove();
        $(".latest_comments_inner").prepend(data.form);
        
      }
    
      $( container ).empty(); // remove the loading message
	  
      $(container).css('width', '545px');

      $.each( data.comments, function( cid, commentMarkup ){
        $( container ).append( commentMarkup );
      });
      $( container ).append( data.pager );
      $( container ).find( 'div.pager a' ).each(function(){
        $( this ).click(function( event ){
          event.preventDefault();
          page = getPageFromHref( this );
          loadComments( container, page );
        });
      });

      $(container).css('width', '545px');
        
    });

}

function getOptions( commentContainer ){
  classes = commentContainer.className.split( ' ' );
  options = {};
  for( i = 0; i < classes.length; i++ ){
    // remove the numbers and see if we have any class matches
    switch( classes[ i ].replace( /[0-9]+/, '' ) ){
      case 'with-pager':
        options.pager = true; 
        break
      case 'comments-per-page-':
        options.commentsPerPage = parseInt( classes[ i ].match( /[0-9]+/ )[ 0 ] ); // just return the number from the class
        break;
      case 'with-content-title':
        options.contentTitle = true; 
        break;
      case 'with-content-type':
        options.contentType = true; 
        break;
      case 'for-node-':
        options.nid = parseInt( classes[ i ].match( /[0-9]+/ )[ 0 ] ); // just return the number from the class
        break;
    }
  }
  return options;
}

function getQueryString( options ){
  queryString = '';
  for( property in options ){
    queryString += property + '=' + options[property] + '&';
  }
  return queryString;
}

function getPageFromHref( anchor ){
  anchorHref = $( anchor ).attr( 'href' );
  if ( pageNum = anchorHref.match( /page=([0-9]+)/ ) ){
    return pageNum[1];
  }else{
    return null;
  }
}

/**
 * Returns an object with each nid as a property and each value a reference to the comment-count DOM object
 */
function getCommentContainers(){
  commentCountContainers = {};
  $( 'div.node' ).each( function(){
    nodeContainer = this;
    commentCountContainer = $( nodeContainer ).find( 'span.comment-count' );
    nid = getNidFromContainer( nodeContainer );
    if( nid != false && commentCountContainer.length === 1 ){
      commentCountContainers[nid] = commentCountContainer;
    }
  });
  return commentCountContainers;
}

/**
 * Replaces the current comment count with a loading graphic
 */
function addCommentCountLoading( commentCountContainers ){
  for( var nid in commentCountContainers ){
    img = new Image();
    img.src = Drupal.settings.fullPath + 'misc/progress.gif';
    img.alt = 'loading comment count';
    img.height = 10;
    img.width = 10;
    $( commentCountContainers[ nid ] ).html( img );
  }
}