/**
 * Portfolio bar variables
 */
var buttonShow, buttonHide, minHeight, maxHeight;

/**
 * Functions that moves the portfolio bar to maintain it at the bottom of the window
 */
function initPortfolio(bs, bh, mn, mx) {
  buttonShow = bs;
  buttonHide = bh;
  minHeight = mn;
  maxHeight = mx;
  
  if ($('portfolio_drop_area')) {
    window.onscroll = setPosition;
    window.onresize = setPosition;
  
    var clientHeight = document.documentElement.clientHeight;
    if (clientHeight == 0) {
      clientHeight = document.body.clientHeight;
    }
  
    total = clientHeight - $('portfolio_drop_area').offsetHeight;
    $('portfolio_drop_area').style.top = total + "px";
  }
}

function setPosition(e) {
  var clientHeight = document.documentElement.clientHeight;
  if (clientHeight == 0) {
    clientHeight = document.body.clientHeight;
  }
  scrollOffset = getScrollXY();
  total = clientHeight + scrollOffset[1] - $('portfolio_drop_area').offsetHeight;
  $('portfolio_drop_area').style.top = total + "px";
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

/**
 * Function that toggles the portfolio bar on and off
 */
function togglePortfolio() {
  var url = '/portfolio/set-state';
  var pf = $('portfolio');
  var pfda = $('portfolio_drop_area');
  
  if (pf.style.height == maxHeight+"px" || pf.style.height == false) {  
    hidePortfolioBar();
   
    if (pfda) {
      var newTop = pfda.offsetTop + 98;
      $('footer').removeClassName('pad');
      $('footer').addClassName('smallpad');
      pfda.style.height = minHeight+'px';
      pfda.style.top = newTop + 'px';
    }
   
    $('togglePortfolio').innerHTML = buttonShow;
    
    new Ajax.Request(url, {
      method: 'get',
      parameters: {
        state: 0
      }
    });
  } else {   
    showPortfolioBar();

    if (pfda) {
      var newTop = pfda.offsetTop - 98;
      $('footer').removeClassName('smallpad');
      $('footer').addClassName('pad');
      pfda.style.height = maxHeight+'px';
      pfda.style.top = newTop + 'px';
    }
    
    $('togglePortfolio').innerHTML = buttonHide;
    
    new Ajax.Request(url, {
      method: 'get',
      parameters: {
        state: 1
      }
    });
  }
}


/**
 * Function to deal with dragging an item
 */
var itemDraggables = [];
function dragItem(e, id) {
  var pd = $('item_'+id+'_draggable'); // item draggable

  pd.style.top = (Event.pointerY(e) - 30) + "px";
  pd.style.left = (Event.pointerX(e) - 20) + "px";
  
  pd.show();
  
  //Draggables.activate(itemDraggables[id]);
  //Draggables.updateDrag(e);
  itemDraggables[id].initDrag(e);
  itemDraggables[id].startDrag(e);
}


/**
 * Functions to scroll along items on the portfolio bar
 */
var nextPortfolioItem = 4;
var totalPortfolioItems;
function nextItemInPortfolio() {
  var hide = nextPortfolioItem - 3;
  Effect.Fade('portfolio_item_'+ hide, {
    duration: 0.2,
    queue: 'front'
  });
  Effect.Appear('portfolio_item_'+ nextPortfolioItem, {
    duration: 0.4,
    queue: 'end'
  });
  $('arrow_left').show();
  
  if (nextPortfolioItem == totalPortfolioItems) {
    $('arrow_right').hide();
  }
  nextPortfolioItem++;
}

function prevItemInPortfolio() {
  var hide = nextPortfolioItem - 1;
  Effect.Fade('portfolio_item_'+ hide, {
    duration: 0.2,
    queue: 'front'
  });
  Effect.Appear('portfolio_item_'+ (nextPortfolioItem - 4), {
    duration: 0.4,
    queue: 'end'
  });
  $('arrow_right').show();
  
  if (nextPortfolioItem - 5 == 0) {
    $('arrow_left').hide();
  }
  nextPortfolioItem--;
}


/**
 * Function to deal with Items being drag/dropped onto the portfolio bar
 */
function addItemToPortfolio(id) {
  var url = '/portfolio/add';
  var pf = $('portfolio');
  var popDown = false;
  
  new Ajax.Updater('items', url, {
    method: 'post',
    parameters: {
      id: id
    },
    evalScripts: true,
    onCreate: function() {
      if (!(pf.style.height == maxHeight+"px" || pf.style.height == false)) {
        showPortfolioBar();
        popDown = true;
      }
    },
    onComplete: function() {
      if (popDown) {
        setTimeout("hidePortfolioBar()", 1600);
      }
    }
  });
}


/**
 * Function to show the portfolio bar by sliding it up
 */
function showPortfolioBar() {
  var pf = $('portfolio');
  new Effect.Morph(pf, {
    style: {
      'height': maxHeight+'px'
    },
    duration: 0.6
  });
}


/**
 * Function to hide the portfolio bar by sliding it down
 */
function hidePortfolioBar() {
  var pf = $('portfolio');
  new Effect.Morph(pf, {
    style: {
      'height': minHeight+'px'
    },
    duration: 0.6
  });
}

/**
 * Function to remove a item from the portfolio
 */
function removeItemFromPortfolio(id, count) {
  Effect.DropOut('portfolio_item_'+count, {
    afterFinish: function() {
      var url = '/portfolio/remove';
      new Ajax.Updater('items', url, {
        method: 'post',
        parameters: {
          id: id
        },
        evalScripts: true
      });
    }
  });
}