// TODO: Remove this once the UI is feature complete
function notImplemented() {
  alert('Not implemented');
  return false;
}

Object.extend(Function.prototype, {
  delay: function() {
    var __method = this, args = $A(arguments), timeout = args.shift() * 1000;
    return window.setTimeout(function() {
      return __method.apply(__method, args);
    }, timeout);
  }
})

var FlashNotice = {
  flash: function() {
    this.show();
    setTimeout(this.hide, 7000);
  },
  show: function() {
    new Effect.Appear('flash_notice', {duration:0.4});
  },
  hide: function() {
    new Effect.Fade('flash_notice', {duration:0.2});
  },
  activate: function() {
    $('flash_notice').addClassName('active');
    this.flash();
  }
}


var CatalogChoice = {
  checkForActivation: false,
  isActivated: false,
  init: function() {
    if ($('flash_notice') && $('flash_notice').hasClassName('active')) { 
      FlashNotice.flash();
    }
    
    // Activation stuff
    if ($(document.body).hasClassName('unactivated')) {
      CatalogChoice.checkForActivation = true;
    } else {
      CatalogChoice.isActivated = true;
    }
  },
  updateActivationStatus: function() {
    new Ajax.Request('/user/is_activated');
  }
}

Event.observe(window, 'load', CatalogChoice.init);

var introSwitcher = {
  fixDimensions: function() { return false; } 
}

var PhotoShow = Class.create();
PhotoShow.prototype = {
  initialize: function(element, options) {
    this.element = $(element);
    this.options = Object.extend({className: 'photo', duration: 3}, options);
    //this.photos = document.getElementsByClassName(this.options.className, this.element);
    this.photos = $(element).childElements();

    this.preparePhotos();
    this.registerCallback();
  },

  preparePhotos: function() {
    this.currentPhoto = this.photos.first();
    this.element.style.position = 'relative';
    this.element.style.height = this.photos.max(function(photo) {
      var visible = Element.visible(photo), height;
      Element.setStyle(photo, {position: 'absolute', width: '100%', left: '0px'});
      if (!visible) Element.show(photo);
      height = Element.getHeight(photo);
      if (!visible) Element.hide(photo);
      return height;
    }).toString() + 'px';
  },

  nextPhoto: function() {
    return this.photos[(this.photos.indexOf(this.currentPhoto) + 1) % this.photos.length];
  },

  registerCallback: function() {
    window.setTimeout(this.tick.bind(this), this.options.duration * 1000);
  },

  tick: function() {
    var currentPhoto = this.currentPhoto, nextPhoto = this.nextPhoto();

    new Effect.Parallel([
      new Effect.Fade(currentPhoto, {sync: true}),
      new Effect.Appear(nextPhoto, {sync: true})
    ], {
      duration: 2,
      afterFinish: (function(effect) {
        this.currentPhoto = nextPhoto;
        this.registerCallback();
      }).bind(this)
    })
  }
}


Ajax.Responders.register({
  onCreate: function() {
    if($('busy_ajax') && Ajax.activeRequestCount>0) {
      Effect.Appear('busy_ajax',{duration:0.5});
    }
  },
  onComplete: function() {
    if($('busy_ajax') && Ajax.activeRequestCount==0)
      Effect.Fade('busy_ajax',{duration:0.5});
  }
});

function closeWindow() {
  window.open('','_parent','');
  window.close();
}

////
// used to toggle an element
// (provides the link text swapping of 'View ...' and 'Hide ...')
function toggleLink(id, link) {
	text = link.innerHTML;
	new_text = (text.indexOf('View') == 0 ? text.gsub(/^View/, 'Hide') : text.gsub(/^Hide/, 'View'));
	$(link).update(new_text);
	$(id).toggle();
}
