


function advance_trail_direct( m, id, userID, radius ){
  advance_trail_position( m+radius, id, userID, radius );
}


function advance_trail_forward( id, userID, radius ){
  var trail = document.getElementById('trailRow' + id);
  var allBooks = getElementsByClass('bookIconTrail', trail);
  m = -1;

  for( var i=0; i<allBooks.length; i++)
    {
      if( allBooks[i].firstChild.className == 'focusIcon')
	m = i;
    }

  if( m < i-radius-1)
    advance_trail_position( m+1, id, userID, radius, true );
}


function advance_trail_back( id, userID, radius ){
  var trail = document.getElementById('trailRow' + id);
  var allBooks = getElementsByClass('bookIconTrail', trail);
  var m = -1;

  for( var i=0; i<allBooks.length; i++)
    {
      if( allBooks[i].firstChild.className == 'focusIcon')
	m = i;
    }

  advance_trail_position( m-1, id, userID, radius, true );
}


function advance_trail_position( n,  id, userID, radius, notExpand ){
  var trail = document.getElementById('trailRow' + id);
  var allBooks = getElementsByClass('bookIconTrail', trail);

  if( n > 0 && n >= radius  )
    {
      allBooks[n].firstChild.className = 'focusIcon';
      
      save_trail_position(id, userID, n-radius);
      for( var i=0; i<allBooks.length; i++)
	{
	  if( i != n  )
	    {
	      allBooks[i].firstChild.className = 'icon';
	    }

	  if( i > n + radius  || i < n - radius )
	    {
	      allBooks[i].style.display='none';
	    }
	  else
	    allBooks[i].style.display='block';
	}

      if( notExpand == null )
	{
	  $('#trailDisplayType'+id).addClass('featured').find('.expandToggle'+id).html('[-]').unbind('click');
	  prime_toggles( id, true );
	}
    }
}



function save_trail_position(trailID, userID, pos){
  if(userID)
    {
      var httpObject = getHTTPObject();
      if(httpObject != null)
	{
	  httpObject.open("GET", "save_trail_position.php?trailID=" + trailID + "&userID=" + userID + "&pos=" + pos, true);
	  httpObject.send(null);
	  httpObject.onreadystatechange = function(){
	    if(httpObject.readyState == 4)
	      {
		var response = httpObject.responseText;
		//		if(response != 'true')
		//	  alert(response);
	      }
	  }
	}
    }
}


function get_trail_body(trailID, userID, radius, focus, edit, featured, widget, simple, facebook ){

      if(simple == null)
	simple = 'false';
      var httpObject = getHTTPObject();
      if(httpObject != null)
	{
	  var link = "load_trail_body.php?trailID=" + trailID + "&userID=" + userID + '&radius=' + radius + '&focus=' + focus + '&edit=' + edit + '&featured=' + featured + "&widget=" + widget + "&simple=" + simple + '&fb=' + facebook;

	  httpObject.open("GET", link, true);
	  httpObject.send(null);
	  httpObject.onreadystatechange = function(){
	    if(httpObject.readyState == 4)
	      {
		var response = httpObject.responseText;
		var place = document.getElementById('trailRow' + trailID);

		if(response.substr(0,3) != '<p>')
		  place.className = 'trailMiddleRow';
		place.innerHTML = response;

		if($('#fb').attr('fb')=='true')
		  $('#trailRow'+trailID).find('a').attr('target', '_new');

		if(!widget && !simple)
		  {
		    prime_toggles(trailID, featured);
		    var nl = '#notloaded'+trailID;
		    $(nl).removeClass('notloaded');//removeClass('notLoaded');
		    if($('.notloaded').length == 0)
		      hovertipInit();
		  }
	      }
	  }
	}
}

function crossings(id){
  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      httpObject.open("GET", "crossings.php?id=" + id, true);
      httpObject.send(null);
      httpObject.onreadystatechange = setOutput;
    }
}

function add_all_books( trailID, userID ){
  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      httpObject.open("GET", "add_all_books.php?trailID=" + trailID + "&userID=" + userID, true);
      httpObject.send(null);
      httpObject.onreadystatechange = ajaxQueryOutput;
    }
}

function addAllBooksOnChange(){
  4;
}



//////////////////// TAGS //////////////////////

function book_tags( type, bookID, userID, selectForm ){
  var index = selectForm.selectedIndex;
  if(index==0)
    return true;
  else if(index ==1)
    {
      var msg = "<p><b>New tag</b></p><p><input type='text' id='newBookTag' size=30><input id='submitBookTag' onclick=\"return_new_book_tag('" + type + "', '" + bookID + "', " + userID + ");\" type='submit' value='add tag'></p>";
      popup(msg);

      $('#popup').keydown(function(event){
	  switch (event.keyCode) {
	  case 13:
	    return_new_book_tag(type, bookID, userID);
	  }
	});


    }
  else
    {
      var newTag = encodeURIComponent(selectForm.options[index].firstChild.nodeValue);
      book_tag_change(type, bookID, userID, index, newTag);
    }
  selectForm.selectedIndex = 0;
}

function selected_book_tags( type, userID, selectForm ){
  var index = selectForm.selectedIndex;
  if(index==0)
    {
      return true;
    }
  else if(index ==1)
    {
      var msg = "<p><b>New tag</b></p><p><input type='text' id='newBookTag' size=30><input onclick=\"return_new_book_tag('" + type + "', 'SELECTED', " + userID + ");\" type='submit' value='add tag'></p>";
      popup(msg);
      $('#popup').keydown(function(event){
	  switch (event.keyCode) {
	  case 13:
	    return_new_book_tag(type, 'SELECTED', userID);
	  }
	});

    }
  else
    {
      newTag = encodeURIComponent(selectForm.options[index].firstChild.nodeValue);
      $('.selected').each(function(){
	  var id = $(this).attr('objectId');
	  book_tag_change(type, id, userID, index, newTag);
	});
    }
  selectForm.selectedIndex = 0;

}

function book_tag_change( type, bookID, userID, index, newTag ){
  if(newTag != "___________")
    {
      var link = "add_book_tag_save.php?type=" + type + "&bookID=" + bookID + "&tag=" + newTag;

      var httpObject = getHTTPObject();
      if(httpObject != null)
	{
	  httpObject.open("GET", link, true);
	  httpObject.send(null);
	  httpObject.onreadystatechange = function(){
	    if(httpObject.readyState == 4)
	      {  
		response = httpObject.responseText;
		if(response == 'true')
		  {
		    if( newTag == 'Read' )
		      {
			delete_book_tag( type, bookID, 'Reading' );
			delete_book_tag( type, bookID, 'Planning to read' );
			change_book_icon( bookID, 'read');
		      }
		    else if( newTag == 'Reading' )
		      {
			delete_book_tag( type, bookID, 'Read' );
			delete_book_tag( type, bookID, 'Planning to read' );
			change_book_icon( bookID, 'reading');
		      }
		    else if( newTag == 'Planning to read' )
		      {
			delete_book_tag( type, bookID, 'Read' );
			delete_book_tag( type, bookID, 'Reading' );
			change_book_icon( bookID, 'unread');
		      }
		    
		    $('#tagsLine'+bookID).removeClass('emptyTags');

		    jQuery.fn.newTagsList = function(){
		      newTag = unencodeURIComponent(newTag);
		      if(type=='book')
			$(this).html( $(this).html() + "<div class='bookTag' name='" + newTag + "'><a href='search.php?search=books&SbpuserID=" + userID + "&Sbptag=" + newTag + "'>" + newTag + "</a> <a class='bor' onclick=\"delete_book_tag('book', '" + bookID + "', '" + newTag + "');\">x</a></span>");
		      else
			$(this).html( $(this).html() + "<div class='bookTag' name='" + newTag + "'><a href='search.php?search=trails&SptuserID=" + userID + "&Spttag=" + newTag + "'>" + newTag + "</a> <a class='bor' onclick=\"delete_book_tag('book', '" + bookID + "', '" + newTag + "');\">x</a></span>");
		    }

		    $('#tags'+bookID).newTagsList();
		    $('#tagsBox'+bookID).show();

		    //		    		    if(existingTags.innerHTML == '' || existingTags.innerHTML == ' (none)')
		  }
		else if(response == 'notloggedin')
		  window.location.reload();
		//else
		//		alert(response);
	      }
	  }
	}
    }
}

function return_new_book_tag( type, bookID, userID ){
  var input = document.getElementById('newBookTag');
  if(bookID=='SELECTED')
    {
      $('.selected').each(function(){
	  var id = $(this).attr('objectId');
	  return_new_book_tag_request( type, id, userID, input );
	});

    }
  else
    return_new_book_tag_request( type, bookID, userID, input );

}
function return_new_book_tag_request( type, bookID, userID, input ){
  $('#submitBookTag').attr('disabled', true);
  var link = "add_book_tag_save.php?type=" + type + "&bookID=" + bookID + "&tag=" + encodeURIComponent(input.value);

  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {  
	    response = httpObject.responseText;
	    if(response == 'true')
	      {
		var existingTags = document.getElementById('tags'+bookID);

		var selectOptions = document.getElementById('select'+bookID);
		if(!selectOptions)
		  {
		    var selectOptions = document.getElementById('selectSelected');
		  }

		if( type == 'book' )
		  existingTags.innerHTML += "<span class='bookTag' name='" + input.value + "'><a href='search.php?search=books&SpbuserID=" + userID + "&Sbptag=" + input.value + "'>" + input.value + "</a> <a class='bor' onclick=\"delete_book_tag('book', '" + bookID + "', '" + input.value + "');\">x</a></span>";
		else
		  existingTags.innerHTML += ("<span class='bookTag' name='" + input.value + "'><a href='search.php?search=trails&SptuserID=" + userID + "&Spttag=" + input.value + "'>" + input.value + "</a>  <a class='bor' onclick=\"delete_book_tag('trail', '" + bookID + "', '" + input.value + "');\">x</a></span>");

		var add =  selectOptions.innerHTML + "<option>" + input.value + "</option>";
		$('#select'+bookID).html(add);
		$('#tagsBox'+bookID).show();
		$('#popup').jqmHide();
	      }
	    else if(response == 'notloggedin')
	      window.location.reload();
	    else
	      input.parentNoinnerHTML="There was an error adding new tag." + response;
	  }
      }
    }
}

function delete_all_book_tags( bookID ){
  popup("<p>Removing from Your Books...</p>");
  link = "remove_from_saved_books.php?bookID=" + bookID;
  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {  
	    response = httpObject.responseText;
	    if(response == 'true')
	      {
		window.location.reload();
	      }
	    else if(response == 'notloggedin')
	      {
		window.location.reload();
	      }
	  }
      }
    }
}

function delete_book_tag( type, bookID, tag ){
  var existingTags = document.getElementById('tags'+bookID);
  var link = "remove_book_tag_save.php?type=" + type + "&bookID=" + bookID + "&tag=" + encodeURIComponent(tag);


  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {  
	    response = httpObject.responseText;
	    if(response == 'true')
	      {
		var kids = existingTags.getElementsByTagName('span');
		for( var i=0; i<kids.length; i++ )
		  {
		    if(kids[i].getAttribute('name') == tag)
		      kids[i].parentNode.removeChild(kids[i]);
		  }
  	      }
	    else if(response == 'notloggedin')
	      window.location.reload();

	    //	    else
	    //	      alert(response);
	  }
      }
    }
}

function delete_trail( userID, trailID, alone ){
  var message = "<p>Are you sure you want to delete this trail?</p><p class='options'><a class='jqmClose option' onclick=\"delete_trail_go(" + userID + ", " + trailID + ", " + alone + "); \" >Yes</a> <a class='option jqmClose''>No</a></p>";
  popup(message);
  save_message_reset();
}


function delete_trail_go( userID, trailID, alone ){
  link = 'delete_trail.php?trailID=' + trailID;
  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {
	    var response = httpObject.responseText;
	    if(response == 'true')
	      window.location.reload();
	    else if(response == 'notloggedin')
	      window.location.reload();
	    else
	      popup('<p>There was an error deleting the trail.</p>');
	  }
      }
    }
}

function prime_toggles( id, featured ) {
  if(featured)
    {
      $('.expandToggle'+id).toggle(function(){
	  $('#trailDisplayType'+id).removeClass('featured').find('.expandToggle'+id).html('[+]');
	},
	function(){
	  $('#trailDisplayType'+id).addClass('featured').find('.expandToggle'+id).html('[-]');
	});
    }
  else
    {
      $('.expandToggle'+id).toggle(function(){
	  $('#trailDisplayType'+id).addClass('featured').find('.expandToggle'+id).html('[-]');
	},
	function(){
	  $('#trailDisplayType'+id).removeClass('featured').find('.expandToggle'+id).html('[+]');
	});
    }
}


function change_book_icon( bookID, newIcon ){
  if(bookID != '')
    {
      var icons = getElementsByClass(bookID, document, 'img');
      var newImage = 'images/icon_' + newIcon + '.png';
      
      for( var i=0; i<icons.length; i++)
	{
	  icons[i].setAttribute('src', newImage);
	}
    }
}

/*********** COMMENTS ***********/

function display_comments( trailID ){
  popupAjax("comments_display.php?trailID=" + trailID, 'comments');
}

function post_comment( trailID ){
  var comment = document.getElementById('newComment');
  if(comment)
    {
      comment = encodeURIComponent(comment.value);

      link = 'comment_save.php';
      params = "trailID=" + trailID + "&comment=" + comment;

      popup("<p>Saving comment...</p>");
      var httpObject = getXmlHTTPObject();
      if(httpObject != null)
	{
	  httpObject.open("POST", link, true);
	  httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	  httpObject.setRequestHeader("Content-length", params.length);
	  
	  httpObject.send(params);
	  
	  httpObject.onreadystatechange = function(){
	    if(httpObject.readyState == 4 )//  && httpObject.status == 200 )
	      {
		response = httpObject.responseText;
		if(response == 'true')
		  window.location.reload();		
	      }
	  }
	}
    }
  else
    popup('<p>Please enter a comment.</p>');

}

function rating_hover(i){
  var stars = $('#rating');
  for( var j = 1; j < 6; j++ )
    {
      if(j <=i )
	stars.find('#star'+j).attr('src', 'images/star_red.jpg');
      else
	stars.find('#star'+j).attr('src', 'images/star_empty.jpg');
    }
}

function rating_click(i, trailID){
      var httpObject = getHTTPObject();
      if(httpObject != null)
	{
	  var link = 'rating_update.php?rating=' + i + '&trailID=' + trailID;

	  httpObject.open("GET", link, true);
	  httpObject.send(null);
	  httpObject.onreadystatechange = function(){
	    if(httpObject.readyState == 4)
	      {
		response = httpObject.responseText;
		if(response == 'true')
		  window.location.reload();
		else
		  alert(response);
	      }
	  }
	}
}


function rating_reset(){
  var stars = $('#rating');
  for( var j = 1; j < 6; j++ )
    stars.find('#star'+j).attr('src', stars.find('#star'+j).attr('real'));
}

