function hideAccountBox(){
  accountBox.hide();
  if(showAccountBox) showAccountBox.show();
}
// fix by Voytee
function getDaLink(element){
    var currentElement = element;
    while(currentElement.href == undefined){
	currentElement = element.up();
    }
    return currentElement;
}

function showFormAccountBox(event){
    var link = getDaLink(event.findElement());
    document.cookie = 'return_to=' + link.href;
    accountBox.show();
    showAccountBox.hide();
    event.stop();
    $('openid_identifier').activate();
}
function showFormAccountBox2(){
    var link = '/session/nowy'
    document.cookie = 'return_to=' + link;
    accountBox.show();
    showAccountBox.hide();
    $('openid_identifier').activate();
}

//Do zmiany. Pogadać z grafikami
function accountBoxShouldHide(event){
  var current_element = event.findElement();
  if(!account_box_structure.include(current_element)) hideAccountBox();
}

function addAccountBoxHandlers(){
  if(showAccountBox) showAccountBox.observe('click',showFormAccountBox);
  hideAccountBoxes.invoke('observe','click',hideAccountBox);
  Event.observe(window,'click',accountBoxShouldHide);
}

function hideAccountBoxLayer(){
  accountBox.hide();
}

function setAccountBoxGlobalVariable(){
    
    accountBox = $('account_box');
    accountBoxContent = $('account_box_content');
    account_box_structure = [accountBoxContent,accountBoxContent.descendants()].flatten();
    showAccountBox = $('show_account_box');
    hideAccountBoxes = $$('.hide_account_box');
}

function prepareAccountBox(){
  if(!$('account_box')) return false;
  setAccountBoxGlobalVariable()
  hideAccountBoxLayer();
  addAccountBoxHandlers()
  //if(mainSiteNewGroup = $('main_site_new_group')) mainSiteNewGroup.observe('click',showFormAccountBox2)
}

document.observe('dom:loaded',prepareAccountBox);
