function debug(msg) {
	debugDiv = $('debug');
	new Insertion.Bottom(debugDiv, msg + ' ## ');
}  

var currentlyOpened = null;
var currentlyMoving = null;

function signinOpen() {
	var f = function() {
		$('signinlink').onclick = function(){signinClose();};
		Field.focus('email');		
	}
	openPanel('signinPanel', f);
}
function signinClose() {
	var f = function() {
		$('signinlink').onclick = function(){signinOpen();};
	}
	closePanel('signinPanel', f);
}
function openPanel(elem, after) {
	if(currentlyMoving) return;
	elem = $(elem);
	if(currentlyOpened) {
	  if(currentlyOpened == elem) {
  		closePanel(currentlyOpened);
	  } else {
  		closePanel(currentlyOpened, function(){openPanel(elem, after);});	    
	  }
		return;
	}
	currentlyMoving = true;
	currentlyOpened = elem;
	Element.show(elem);
	var scalePercent = elem.offsetHeight * 100;
	elem.style.height = '1px';
	var f = function() {
		elem.style.height = '';
		currentlyMoving = false;
		if(after) after();
	}
	new Effect.Scale(elem, scalePercent, {scaleY:true, scaleX:false, scaleContent:false, duration:0.3, afterFinish:f});	
}
function closePanel(elem, after) {
	if(currentlyMoving) return;
	currentlyMoving = true;	
	elem = $(elem);
	var origHeight = elem.offsetHeight;
	var scalePercent = 100/origHeight;
	var f = function() {
		Element.hide(elem);
		elem.style.height = '';
		currentlyMoving = false;
		if(currentlyOpened != null && currentlyOpened == elem) currentlyOpened = null;		
		if(after) after();
	};
	new Effect.Scale(elem, scalePercent, {scaleY:true, scaleX:false, scaleContent:false, duration:0.3, afterFinish:f});	
}
function signOut() {
	new Ajax.Updater('signinPanelContent', '/home/logout_partial/', {asynchronous:true, evalScripts:true});					
}

var HoverPanel = Class.create();
HoverPanel.prototype = {  
  initialize: function(panelId, contentId) {
    // check if both panelId and contentId is available
    var p = $(panelId);
    var c = $(contentId);
    if(!p || !c) { // not good, remove both
      if($(p)) Element.remove(p);
      if($(c)) Element.remove(c);
      var html = '<div id="' + panelId + '"><div id="' + contentId + '"></div></div>';
      new Insertion.Bottom(document.body, html);    
      this.panel = $(panelId);
      this.content = $(contentId);
    } else {
      // good reuse it
      this.panel = p;
      this.content = c;
    }
    Element.hide(this.panel); // make sure it is hidden initially
    this.editionId = null;
    this.currentCall = null;
  },
  makeVisibleFor: function(parentElem, editionId) {
    if(this.editionId == editionId) return; // already visible
    if(this.currentCall) {
      this.currentCall.abort;
      this.currentCall = null;
    }
    this.editionId = editionId;
    var parentPos = Position.cumulativeOffset(parentElem);
    var parentDim = Element.getDimensions(parentElem);
    // pick the top right corner of parent
    var top = parentPos[1];
    var left = parentPos[0] + parentDim.width;
    this.panel.style.left = (left+5) + "px";
    this.panel.style.top = (top) + "px";
    Element.update(this.content, 'loading...');
    Element.setOpacity(this.panel, 0.0);
    Element.show(this.panel);
    new Effect.Opacity(this.panel, {duration:0.3, from:0.0, to:1.0})
    var slf = this;
    var aft = function() {
      slf.currentCall = null;
    }
    this.currentCall = new Ajax.Updater(this.content,
      '/list/edition_hover',
      { method: 'post', 
        parameters: 'edition_id=' + editionId,
        onComplete: aft});
  },
  hide: function() {
    var slf = this;
    var f = function() {
      Element.hide(slf.panel);      
    }
    new Effect.Opacity(this.panel, {duration:0.6, from:1.0, to:0.0, after:f})
    this.editionId = null;
  }
}
var myHover;
var hoverShowTimeout;
var hoverHideTimeout;
function coverHover(link, editionId) {  
  if(!myHover) {
    myHover = new HoverPanel('coverHoverPanel', 'coverHoverContent');
  }
  // reposition
  var f = function() {
    hoverShowTimeout = null;
    myHover.makeVisibleFor(link, editionId);
  }
  if(hoverShowTimeout) clearTimeout(hoverShowTimeout);
  if(hoverHideTimeout) clearTimeout(hoverHideTimeout);
  hoverShowTimeout = setTimeout(f, 160);
}
function coverHoverHide(link) {
  if(hoverShowTimeout) clearTimeout(hoverShowTimeout);
  if(hoverHideTimeout) clearTimeout(hoverHideTimeout);
  var f = function() {
    hoverHideTimeout = null;
    myHover.hide();
  }
  if(myHover) {
    hoverHideTimeout = setTimeout(f, 160);
  }
}