// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var TopicForm = {
  editNewTitle: function(txtField) {
    $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
  }
}

var LoginForm = {
  checkLogin: function(txt) {
    if(txt.value.match(/^https?:\/\//)) {
      $('password_fields').hide();
    } else {
      $('password_fields').show();
    }
  }
}

var EditForm = {
  // show the form
  init: function(postId) {
    $('edit-post-' + postId + '_spinner').show();
    this.clearReplyId();
  },

  // sets the current post id we're editing
  setReplyId: function(postId) {
    $('edit').setAttribute('post_id', postId.toString());
    $('posts-' + postId + '-row').addClassName('editing');
    if($('reply')) $('reply').hide();
  },
  
  // clears the current post id
  clearReplyId: function() {
    var currentId = this.currentReplyId()
    if(!currentId || currentId == '') return;

    var row = $('posts-' + currentId + '-row');
    if(row) row.removeClassName('editing');
    $('edit').setAttribute('post_id', '');
  },
  
  // gets the current post id we're editing
  currentReplyId: function() {
    return $('edit').getAttribute('post_id');
  },
  
  // checks whether we're editing this post already
  isEditing: function(postId) {
    if (this.currentReplyId() == postId.toString())
    {
      $('edit').show();
      $('edit_post_body').focus();
      return true;
    }
    return false;
  },

  // close reply, clear current reply id
  cancel: function() {
    this.clearReplyId();
    $('edit').hide()
  }
}

var ReplyForm = {
  // yes, i use setTimeout for a reason
  init: function() {
    EditForm.cancel();
    $('reply').toggle();
    $('post_body').focus();
    // for Safari which is sometime weird
//    setTimeout('$(\"post_body\").focus();',50);
  }
}

Event.addBehavior({
  '#search,#monitor_submit': function() { this.hide(); }
})

function toggleInnerHTML(element, html1, html2)
{
  element = $(element);
  element.innerHTML = toggleText(element.innerHTML, html1, html2);
}

function toggleText(current_text, text1, text2, compare)
{
  var equal;
  if(typeof(compare) == 'function')
    equal = compare(current_text, text1)
  else
    equal = current_text == text1

  if(equal)
    return text2;
  else
    return text1;
}

function toggleWidget(id)
{
  new Effect.toggle(id + '_body', 'blind', {'duration':0.5,
    'afterFinish': function(){
      trigger = $(id + '_trigger')
      trigger.src = toggleText(trigger.src, '/images/wp_trigger_up.gif', '/images/wp_trigger_down.gif', function(text1, text2) {
                                                                                                          return new RegExp(text2 + "\\??.*$").test(text1)
                                                                                                        }
      );
    }
  })  
}

function switchContent(element, newContentUrl, reverce)
{
  reverce = reverce || false;
  var k = reverce ? 1 : -1
  element = $(element);
  var oldHeight = element.getHeight();
  element.style.overflow = "hidden";
  new Ajax.Request(newContentUrl, {
    'method':'get',
    'onComplete': function(req)
    {
      new Effect.MoveBy(element, 0, k*element.getWidth(), {
                'duration':0.5,
                'beforeStart':function(){element.makeClipping().makePositioned()},
                'afterFinish': function()
                {
                  element.style.left = (-1 * k * element.getWidth()).toString() + "px";
                  var par = $(element.parentNode);
                  par.style.height = par.getHeight().toString() + 'px'
                  element.style.height="auto";
                  Element.update(element, req.responseText);
                  var newHeight = element.getHeight();
                  new Effect.Parallel ([
                      new Effect.MoveBy(element, 0, k*element.getWidth(), {sync:true}),
                      new Effect.Scale(element, 100, {
                        sync:true,
                        scaleX:false,
                        scaleContent:false,
                        scaleMode:{originalHeight:newHeight, originalWidth:element.getWidth()},
                        scaleFrom: 100*oldHeight/newHeight,
                        afterFinish:function(){element.undoClipping().undoPositioned(); element.style.height="auto"; },
                        afterSetup:function(){par.style.height="auto";}
                      })
                    ],
                    {
                      'duration':0.5
                    }                          
                  );
                }
      });

    }
  });
}

function getCheckedValue()
{
  var i=0;
  for(i=0; i<arguments.length; i++)
  {
    try {
      if(arguments[i].checked)
        return arguments[i].value;
    } catch(exception) {}
  }
  return "";
}


function accessChanged(id)
{
  $(id+'_icon').src = '/images/' + getCheckedValue($(id + '_public'), $(id + '_friends'), $(id + '_private')) + '_access_icon.gif';
  new Effect.Fade(id + '_container', {duration:0.2})
}


Jeans = {activeRequestCount:0};

Ajax.Responders.register({
	onCreate: function(ajax) {
    if(ajax.options.invisible)
    {
      return; 
    }
    Jeans.activeRequestCount++;
		if($('global_preloader') && Jeans.activeRequestCount == 1)
    {
			new Effect.MoveBy('global_preloader', 76, 0, {duration:0.3, queue:'end', beforeStart:function(){
        $('global_preloader_container').show();
        $('global_preloader').show();
      }});
    }
	},
	onComplete: function(ajax) {
    if(ajax.options.invisible)
    {
      return;
    }
    Jeans.activeRequestCount--;
		if($('global_preloader') && Jeans.activeRequestCount == 0)
    {
			new Effect.MoveBy('global_preloader', -76, 0, {duration:0.3, queue:'end', afterFinish:function(){
        $('global_preloader').hide();
        $('global_preloader_container').hide()
      }});
    }
	}
});
