Event.observe(window, 'load', page_load);

function page_load()
{
  var aryPopups = $$("a.popup");
 
  aryPopups.each
  (
	function(lnk)
	{
		lnk.setAttribute("url", lnk.getAttribute("href"));
		lnk.setAttribute("href", "javascript:void(0);");

		lnk.observe
		(
		  'click',
		  function()
		  {
			lnk.blur();
			popupCenter(lnk.getAttribute("url"), "", lnk.getAttribute("options"));
		  }
		);
	}
  );
  
  loadAccordions();
  addTrimOnBlurTextboxes("input[type='text']");
}

function popupCenter(url, strName, strOptions)
{
  var iWidth = window.screen.width;
  var iHeight = window.screen.width;
  var aryOptions = $A(strOptions.split(","));
  
  aryOptions.each
  (
    function(strOption)
    {
      var aryOption = strOption.split("=");
      
      if(aryOption[0].toLowerCase() == "width")
        iWidth = parseInt(aryOption[1]);
      else if(aryOption[0].toLowerCase() == "height")
        iHeight = parseInt(aryOption[1]);
    }
  );
  
  var iLeft = (window.screen.width - iWidth) / 2;
  var iTop = (window.screen.height - iHeight) / 2;
  
  strOptions += ",left=" + iLeft;
  strOptions += ",top=" + iTop;

  var popUp = window.open(url, strName, strOptions);
  popUp.focus();
  
  return popUp; 
}

function loadAccordions()
{
  if($('vertical_container'))
  {
    var navAccordion = new accordion
    (
      'vertical_container', 
      {
        resizeSpeed : 5,
        classNames : 
        {
          toggle : 'vertical_accordion_toggle',
          toggleActive : 'vertical_accordion_toggle_active',
          content : 'vertical_accordion_content vertical_accordion_content_closed'
        },
        onEvent : 'click',
        direction : 'vertical'
      }
    );
    
    if(typeof(open_menu) != "undefined" && !isNaN(parseInt(open_menu)))
    {
      var top_level = $$('#vertical_container .vertical_accordion_toggle')[open_menu];
      navAccordion.activate(top_level);

     if(typeof(current_menu_item) != "undefined" && !isNaN(parseInt(current_menu_item)))
     { 
        var second_level = top_level.next('div.vertical_accordion_content').getElementsBySelector('li')[current_menu_item];
        second_level.addClassName('selected');
      }  
	 
    }
	
  }
}

function addTrimOnBlur(txt)
{
  txt.observe
  (
    'blur',
    function()
    {
      txt.value = trim(txt.value);
    }
  );
}

function addTrimOnBlurTextboxes(strTextboxesCssRule)
{
 $$(strTextboxesCssRule).each
  (
    function(txt)
    {
      addTrimOnBlur(txt);
    }
  );
}

function trim(str)
{
  if(typeof(str) == "string")
    str = str.replace(/^\s+/, "").replace(/\s+$/, "");
    
  return str;
}

function redirect(href)
{
  if(href && typeof(href) == "string")
    window.location.href = href;
}
