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

// This is a Prototype extension designed to work with cookies.
// Full code http://wiki.script.aculo.us/scriptaculous/show/Cookie
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

Effect.BackgroundScroll = Class.create();
Object.extend(Object.extend(Effect.BackgroundScroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element.currentBackgroundPosition)
      this.element.currentBackgroundPosition = [0, 0];
    this.offsets = arguments[1];
    this.start(arguments[2] || {});
  },
  setup: function() {
    this.current = this.element.currentBackgroundPosition;
    this.delta = [
      this.offsets[0] - this.current[0],
      this.offsets[1] - this.current[1]];
  },
  update: function(position) {
    this.element.currentBackgroundPosition = [
      Math.round(this.current[0] + (this.delta[0]*position)),
      Math.round(this.current[1] + (this.delta[1]*position)) ];
    this.element.style.backgroundPosition = 
      this.element.currentBackgroundPosition[0] + 'px ' +
      this.element.currentBackgroundPosition[1] + 'px';
  }
});

Effect.ScrollTo = Class.create();
Object.extend(Object.extend(Effect.ScrollTo.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    this.start(arguments[1] || {});
  },
  setup: function() {
    Position.prepare();
    var offsets = Position.cumulativeOffset(this.element);
    if(this.options.offset) offsets[1] += this.options.offset;
    var max = window.innerHeight ? 
      window.height - window.innerHeight :
      document.body.scrollHeight - 
        (document.documentElement.clientHeight ? 
          document.documentElement.clientHeight : document.body.clientHeight);
    this.scrollStart = Position.deltaY;
    this.delta = (offsets[1] > max ? max : offsets[1]) - this.scrollStart;
  },
  update: function(position) {
    Position.prepare();
    var offset = this.scrollStart + (position*this.delta);
    if(Engine.isMSIE6) {
      document.body.scrollTop = offset;
    }
    else
      window.scrollTo(Position.deltaX, offset)
  }
});

var X_REF = -945, Y_REF = -390;

function slide(x, y, url, css_class) {
  new Effect.Fade('panel_content', {duration: 0.3, afterFinish: function(){
    new Effect.BackgroundScroll('panel', [X_REF*x, Y_REF*y], {duration: 1.0, afterFinish: function(){
      $('panel_content').className = css_class;
      new Effect.Shrink('new_actions', {duration: 0.3, afterFinish: function(){
        new Effect.Appear('spinner', {duration: 0.3, from: 0.0, to: 0.8});
        $('new_actions_wrapper').className = css_class;
        new Ajax.Request(url, {asynchronous:true, evalScripts:true, method:'get', onComplete:function(request){
          new Effect.Appear('panel_content',{duration:0.5, afterFinish: function(){new Effect.Fade('spinner', {duration: 0.3, from: 0.8, to: 0.0});}});
        }});
      }});
    }});
  }});
}

function tMCE_init(l, t, e) {
  if (t == 1){
    tinyMCE.execCommand('mceAddControl', false, e);
    Element.writeAttribute(l, {"onclick": "tMCE_init(this, 0, 'group_description');; return false;"});
  }else{
    tinyMCE.execCommand('mceRemoveControl', false, e); 
    Element.writeAttribute(l, {"onclick": "tMCE_init(this, 1, 'group_description');; return false;"});
  }
  return false;
}

function respond_to_parent(url) {
  new Effect.Appear('spinner',{duration:0.3, from:0.0, to:0.8, afterFinish: function(){
    new Ajax.Updater('main', url, {asynchronous:true, evalScripts:true, method:'get'});
    new Effect.Fade('spinner',{duration:0.3, from:0.8, to:0.0});
  }});
  return false;
}
