/**************************************************
 * 
 * Script for interactive vCard.
 * -----------------------------
 * Needs Prototype (http://www.prototypejs.org) and 
 * scriptaculous (http://script.aculo.us/) to work.
 * 
 * -- copyright 2009 --
 * 
 * @author Karsten Rieger
 * 
 **************************************************/

/*
 * Register event listeners for startup and resize. 
 */
Event.observe(window, 'load', initializePage);
Event.observe(window, 'resize', centerVCard);
/*
 * This comes in handy if we forgot some logging
 * messages to be removed. Browsers without firebug
 * installed would throw an exception.
 */
if (typeof(console) == "undefined") {
    var console = {
      log: Prototype.emptyFunction,
      debug: Prototype.emptyFunction,
      info: Prototype.emptyFunction,
      warn: Prototype.emptyFunction,
      error: Prototype.emptyFunction
    };
}
/*
 * Called after page is loaded to center
 * the card in viewport, reopen panel if
 * the page was bookmarked and set the 
 * opacity for the footer.
 */
function initializePage(){
    var footer = $('footer');
    if(footer){
        footer.setOpacity(0.7);
    }
    centerVCard();
    var hash = location.hash.replace('#','');
    if (hash != '') {
        if(hash == "about"){
            togglePanel('bottom');
        }else if(hash == "contact"){
            togglePanel('left');
        }else if (hash == "links"){
            togglePanel('right');
        }
    }
}
/*
 * Called on tsartup and everytime the
 * resize event was triggered by the browser.
 */
function centerVCard(){
   var vCard = $('vCard');
   var main = $('main');

   var leftPanelOffset = $('left').getStyle('left') == "-200px" ? 200 : 0;
   var rightPanelOffset = $('right').getStyle('left') == "-200px" ? 200 : 0;
   var bottomPanelOffset = $('bottom').getStyle('bottom') == "-200px" ? 200 : 0;
   
   new Effect.Morph(vCard, {
        'style':{
            'top' :  document.viewport.getHeight()/2 - (main.getHeight() + bottomPanelOffset)/2 - main.positionedOffset().top+ "px",
            'left' : document.viewport.getWidth()/2  - (vCard.getWidth() +  rightPanelOffset)/2 + "px"
        },
        transition: Effect.Transitions.spring
   });
}
/*
 * Drives the handle identified by the given id
 * out or in, if the handle is out already.
 * Calls centerVCard after animation is finished.
 * 
 * @param {String} handle
 */
function togglePanel(handle){
    if (handle == "left") {
        var panel = $('left');
        var button = $('menuItem1');
        var pos = (panel.getStyle('left') == "-200px") ? "17px" : "-200px"; 
               
        new Effect.Morph(panel, {
            'style': {
                'left': pos
            },
            afterFinish: centerVCard
        } );
        button.toggleClassName('active');
    } else if (handle == "right") {
        var panel = $('right');
        var button = $('menuItem3');
        var pos = (panel.getStyle('right') == "-200px") ? "10px" : "-200px";
        
        new Effect.Morph(panel, {
            'style': {
                'right': pos
            },
            afterFinish: centerVCard
        } );
        button.toggleClassName('active');
    } else if (handle == "bottom") {
        var panel = $('bottom');
        var button = $('menuItem2');
        var pos = (panel.getStyle('bottom') == "-200px") ? "15px" : "-200px";
        
        new Effect.Morph(panel, {
            'style': {
                'bottom': pos
            },
            afterFinish: centerVCard
        } );
        button.toggleClassName('active');
    }
    
}
