// $Id$
/*
/*  Project: IJF
**  Module: ijf_journal_view
**  Filename: ijf_ajax.js
**  Author: Andrew Galbraith
**  Creation Date:  SEP 2008
**  Description:  Javascript to handle AJAX.
**
**  *------------REVISIONS---------------
 *  DATE      Author      Lines       Desc
 *
 *
 *
**/
 


/** START FUNCTION $(document).ready--------------------------------------------
 *  DESC: Function to set up the page and assign events to functions.
 *  ACCPT:
 *  RETRS:
 */
$(document).ready(
  function () {
	     	
    $('.journal-volume-icon').click(toggle_nav_journal);
	$('.article-view-post-comment a').click(click_post_comment);
	$('.article-view-reply-comment a').click(click_reply_comment);


  }//END FUNC
);//END - $(document).ready-----------------------------------------------------



/** START FUNCTION toggle_nav_journal--------------------------------------------------
 *  DESC: Function toggle the view of each node to display teaser & comments
 *        or hide teaser & comments.
 *  ACCPT:
 *  RETRS:
 */
toggle_nav_journal = function () {
	
	var id = $(this).attr('id').replace(/journal_volume_icon_/, '');

	
	if ($(this).attr('class')=='journal-volume-icon journal-volume-collapsed')
	{
		$('#volume_issues_'+id).slideDown('slow',
			function () {
							
							}//endfunc
    	);
		$(this).addClass('journal-volume-expanded');
		$(this).removeClass('journal-volume-collapsed');
		
	}
	else
	{
		
		$('#volume_issues_'+id).slideUp('slow',
			function () {
      		}//endfunc
    	);
		
		$(this).addClass('journal-volume-collapsed');
		$(this).removeClass('journal-volume-expanded');
		
	}//end ifelse

  return false;
}//END FUNC - toggle_nav_journal-------------------------------------------------------

/** START FUNCTION click_tag----------------------------------------------------
 *  DESC: Function called by the click event of tag icon to create & display
 *        tag pop-up window.
 *  ACCPT:
 *  RETRS:
 */
click_post_comment = function () {
  var id = $(this).attr('id');
  //Calls the createDialog function to display the window & overlay faded div
  Drupal.createDialog(this, 'post-comment-popup', 'ijf_journal_comments/ajax/comment/'+ id, init_post_comment, 'right');
  return false;
}//END FUNC - click_tag---------------------------------------------------------


/** START FUNCTION click_reply_comment----------------------------------------------------
 *  DESC: Function called by the click event of tag icon to create & display
 *        tag pop-up window.
 *  ACCPT:
 *  RETRS:
 */
click_reply_comment = function () {
  var id = $(this).attr('id');
  var id = id.split("_");
  //Calls the createDialog function to display the window & overlay faded div

  Drupal.createDialog(this, 'post-comment-popup', 'ijf_journal_comments/ajax/comment/'+ id[0]+"/"+id[1], init_post_comment, 'left');
  return false;
}//END FUNC - click_tag---------------------------------------------------------


/** START FUNCTION -----------------------------------------------------update_comment_author
 *  DESC: Function called when mail value is changed to update the Name field.
 *  ACCPT:
 *  RETRS:
 */
update_comment_author = function () {
	
  $.get(Drupal.settings.paths.base +'ijf_journal_comments/ajax/comment_author/'+$("#edit-mail").val(), function(data){
  $("#edit-name").val(data);
});
}//END FUNC - update_comment_author---------------------------------------------------------


/** START FUNCTION init_tag-----------------------------------------------------
 *  DESC: Function called when tag pop-window is being setup to set the submit
 *        button function to be called - submit_tag.
 *  ACCPT:
 *  RETRS:
 */
init_post_comment = function () {
  $('form', this).submit(
    function () {
      $(this).ajaxSubmit(submit_post_comment);
      return false;
    }//endfunc
  );   
  	$("#edit-mail").change(update_comment_author);
}//END FUNC - init_tag----------------------------------------------------------

/** START FUNCTION submit_tag---------------------------------------------------
 *  DESC: Function called after tag-popup form has been submitted.
 *  ACCPT: data - string of formatted html of the form to be displayed in pop-up
 *  RETRS:
 */
submit_post_comment = function (data) {
  //if no errors in the html form then close the overlay & popup
  if (data.search(/messages error/)<0)
  {
    $('#tag-popup-dialog-window').remove();
  }//endif

  $('.dialog-window .dialog-content').each(
    function () {
      if (data.length) {
        $(this).html(data);
        $('form', this).submit(
          function () {
            $(this).ajaxSubmit(submit_post_comment);
            return false;
          }//endfunc
        );
      }//endif
    }//endfunc
  );
}//END FUNC - submit_tag--------------------------------------------------------




/** START FUNCTION Drupal.createDialog------------------------------------------
 *  DESC: Function called to create an Ajax pop-up box over a faded overlay div.
 *  ACCPT:  el - Node dom object
 *          tag - id of the windows to be generated
 *          url - url of that node
 *          callback -
 *  RETRS:
 */
Drupal.createDialog = function (el, tag, url, callback, direction) {
  if (tag == 'undefinded') {
    tag = 'default';
  }//endif
  var dialog = tag +'-dialog-window';
  sh = $('html').height();
  sw = $('html').width();
  pos = Drupal.absolutePosition(el);


  $('body').append('<div id="'+ dialog +'" class="dialog-window" style="display: none;"><div class="dialog-title"><a href="#" class="dialog-close">Close</a></div><div class="dialog-content"></div></div>');
  if (direction == 'right')
 	 $('#'+ dialog).css({ 'position': 'absolute', 'top': pos.y+'px', 'right': (sw-pos.x)+'px','z-index': '100' });
  else
  	$('#'+ dialog).css({ 'position': 'absolute', 'top': pos.y+'px', 'left': (pos.x)+'px','z-index': '100' });

  $('#'+ dialog +' .dialog-content').load(Drupal.settings.paths.base + url, callback);
  $('#'+ dialog +' .dialog-close').click(
    function () {
      $('#'+ dialog).remove();
	  return false;
    }//endfunc
  );
  $('#'+ dialog).fadeIn("slow");


}//END FUNC - Drupal.createDialog-----------------------------------------------


