
/*
 * Cookie Class
 * Code is copied from /home/httpd/sharedssi/WebToCall/WebToCall.js
 *
 * Why do we have a cookie class? We should use a standardized one.
 *   http://openjsan.org/doc/b/bu/burak/HTTP/Cookies/1.11/index.html
 */
function W2C_Cookie(){  }

W2C_Cookie.prototype = {
  create: function(name,value,days) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
  },

  read: function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
  },

  erase: function(name) {
    this.create(name,"",-1);
  }
}

  function edit_link_display_toggle() {
    this.cookie = new W2C_Cookie;
    this.edit_links = document.getElementsByName ("editLink");
    this.edit_link_divs = document.getElementsByName ("editLinkDiv");

    this.init = function () {
      var thisOne = this;
      var toggle_listener = this.toggle_listener;
      var cookie_value = this.cookie.read('f_edit_link_visible');
      this.f_edit_link_visible = (cookie_value == 'true' ? true : false);
      if (this.f_edit_link_visible == null) {
        this.f_edit_link_visible = false;
      }
      this.display_all();
    }

    this.display_all = function () {
      var toggle_widget = document.getElementById('toggle_widget');
      if (!toggle_widget) {
        return;
      }
      toggle_widget.innerHTML = 
        this.f_edit_link_visible ? 
          'Switch back to <strong>view</strong> mode' : 
          'Switch to <strong>modify</strong> mode';

      var edit_link_divs = this.edit_link_divs;
      var edit_link_divs_cnt = edit_link_divs.length;
      for (var i=0; i<edit_link_divs_cnt; i++) {
        edit_link_divs[i].style.display
          = this.f_edit_link_visible ? 'block' : 'none';
      }
    }

    this.toggle_listener = function (e, thisOne) {
      thisOne.f_edit_link_visible = !thisOne.f_edit_link_visible;
      thisOne.display_all ();
      thisOne.cookie.create (
        'f_edit_link_visible', thisOne.f_edit_link_visible, 1);
      return true;
    }
  }
  /**
   * Initialize modify/view mode toggle cookie tracker
   */ 
  var toggle_control = new edit_link_display_toggle();
  toggle_control.init();
  
  /* add listener for modify/view toggle */
  jQuery("#toggle_widget").click(function(){
    toggle_control.f_edit_link_visible = !toggle_control.f_edit_link_visible;
    toggle_control.display_all();
    toggle_control.cookie.create (
      'f_edit_link_visible', toggle_control.f_edit_link_visible, 1);
    return true;
  }); 

/**
 * Set up jQuery actions
 */
jQuery(document).ready(function(){
  
  /** 
   * Edit drop-down menue set up 
   */

  /* Edit drop-down selectors */
  var editAction = "ul.editDrop li";
  var editActionWithArrow = editAction + ".arrow";
  var dropArrow = editAction + " a.dropArrow"; 
  var topAction = editAction + ".top";

  /* Hide edit action list items */
  jQuery(editAction).each(function(){
    jQuery(this).not(jQuery(topAction)).not(jQuery(editActionWithArrow)).slideUp(10);
  });
  
  /* Set toggle edit drop down ascention/descention */
  jQuery(dropArrow).click(function(){
    /* Ascend all list items except the first */
    jQuery(editAction).each(function(){
      jQuery(this).not(jQuery(editAction)[0]).not(jQuery(topAction)).slideToggle(100);
    });
    return false;
  });

  /**
   * BlogWiki edit form error prompt
   * @todo - get appropriate library and enable draggable()
  jQuery("div.errorPrompt").draggable();
   */
  

  /* BlogWiki publish widget */
  var widget = "fieldset#publishWidget";
  var widgetDate = widget + ' span.date';
  var widgetTime = widget + ' span.time';
  var widgetSetNowLink = widget + ' a.nowLink';
  var widgetDoneButton = widget + ' input.done';
  
  var widgetMonth = widget + ' select[@name="blogMonth"]';
  var widgetDay = widget + ' select[@name="blogDay"]';
  var widgetYear = widget + ' select[@name="blogYear"]';
  var widgetHour = widget + ' select[@name="blogHour"]';
  var widgetMin = widget + ' select[@name="blogMinute"]';
  var widgetAmpm = widget + ' select[@name="blogAmpm"]';
  
  var publishToggler = "fieldset#configIndex span.toggle";
  var publishTogglerCheckbox = publishToggler + " input";
  var publishedNotifier = publishToggler + ' span.datetime';
  
  var timestampTextSelector = {
    M : publishedNotifier + ' span.m',
    D : publishedNotifier + ' span.d',
    Y : publishedNotifier + ' span.y',
    hr : publishedNotifier + ' span.hr',
    min : publishedNotifier + ' span.min',
    ampm : publishedNotifier + ' span.ampm'
  };

  
  var unpublishedNotifier = publishToggler + ' span.unpublished';
  var dateTimeEditLink = publishToggler + ' a.editToggle';
  var author = 'span.author';
  var authorSelect = author + ' select[@name="authorId"]';
  var authorInput = author + ' span.authorInput';
  var authorCancel = author + ' input.cancel'
  /* published timestamp variables */
  var timestampText = {
    M : jQuery(timestampTextSelector['M']).html(),
    D : jQuery(timestampTextSelector['D']).html(), 
    Y : jQuery(timestampTextSelector['Y']).html(),
    hr : jQuery(timestampTextSelector['hr']).html(),
    min : jQuery(timestampTextSelector['min']).html(),
    ampm : jQuery(timestampTextSelector['ampm']).html()
  };
  
  var oldTimestamp = jQuery(publishedNotifier).html();
  var oldWidgetTime = jQuery(widgetTime).html();
  var oldWidgetDate = jQuery(widgetDate).html();
  
  /* hide widget here incase the user doesnt have javascript */
  jQuery(widget).hide();
  /* hide author input */
  jQuery(authorInput).hide();
  
  /* if checkbox is not checked, hide the date */
  if(!jQuery(publishTogglerCheckbox).attr("checked")){
    jQuery(publishedNotifier).hide();
    jQuery(dateTimeEditLink).hide();
    jQuery(unpublishedNotifier).show();
  }
  
  /* show widget with "Edit" link */
  jQuery(dateTimeEditLink).click(function(){
    jQuery(widget).slideToggle('fast');
    return false;
  });
  
  /* toggle notifier when checkbox is checked */
  jQuery(publishTogglerCheckbox).click(function(){
    var checked = jQuery(publishTogglerCheckbox).attr("checked");
    if(checked){
      jQuery(publishedNotifier).show();
      jQuery(dateTimeEditLink).show();
      jQuery(unpublishedNotifier).hide();
    }else{
      jQuery(publishedNotifier).hide();
      jQuery(dateTimeEditLink).hide();
      jQuery(unpublishedNotifier).show();
      jQuery(widget).hide();
    }
  });
  
  /* hide widget to hide when 'done' clicked */
  jQuery(widgetDoneButton).click(function(){
    jQuery(widget).fadeOut('fast');
  });
  
  // /* cancel and reset date changes */
  // jQuery(widget + " input.cancel").click(function(){
  //   jQuery(widget).fadeOut('fast');
  //   jQuery(widgetTime).html(oldWidgetTime);
  //   jQuery(widgetDate).html(oldWidgetDate);
  //   jQuery(publishedNotifier).html(oldTimestamp);
  // });
  
  /* show/hide Create author */
  jQuery(authorSelect).change(function(){
    //alert(jQuery('span.author select[@name="authorId"] option:selected').html());
    var authorIndex = jQuery(authorSelect).val();
    if(!authorIndex){
      jQuery(authorSelect).hide();
      jQuery(authorInput).show();
      jQuery(authorInput + ' input[@name="newAuthor"]').effect("highlight", {color:'#FFFFCF'}, 800);
    }
    return false;
  });
  
  /* hide create author fields */
  jQuery(authorCancel).click(function(){
    jQuery(authorSelect).show();
    jQuery(authorInput).hide();
    /* deselect all options */
    jQuery(authorSelect + " option").removeAttr("selected");
    /*select first option as default if user cancels Create new author */
    jQuery(authorSelect + " option[@value='1']").attr('selected', 'selected');
  });
  
  jQuery(widget + ' select').change(function(){
    var newVal = jQuery(this).children('option:selected').text();
    var selectClass = jQuery(this).attr('class');
    jQuery(publishedNotifier + ' span.' + selectClass).html(newVal);
    jQuery(publishToggler).effect("highlight", {}, 800);
  });
  
  // jQuery(widgetSetNowLink).click(function(){
  //     jQuery(widget).fadeOut('fast');
  //     var time = new Date();
  //     nowElements = {
  //       M : time.getMonth() + 1,
  //       D : time.getDate(),
  //       Y : time.
  //       hr :
  //       min :
  //       ampm :
  //     }
  //     for (var i in timestampTextSelector) {
  //       jQuery(timestampTextSelector[i]).html(
  //       alert(i);
  //     }
  // });
  
});



