var user_functions = new Array();
user_functions['abc'] = 0;

var is_ie6 = false;

if (navigator.appName == 'Microsoft Internet Explorer')
{
  var ua = navigator.userAgent;
  var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  if (re.exec(ua) != null)
    rv = parseFloat( RegExp.$1 );
    if (rv == 6)
    is_ie6 = true;
}

function analytics_track_pageview(url) {
	if(typeof pageTracker != 'undefined') {
		pageTracker._trackPageview(url);			
	}
	if(typeof secondTracker != 'undefined') {
		secondTracker._trackPageview(url);	
	}
	return false;
}		

hide_div = function hide_div(div_name) {
  if (document.getElementById) { // DOM3 = IE5, NS6
    if (document.getElementById(div_name))
    document.getElementById(div_name).style.display = 'none';
  }
  else { // IE 4
    eval("document.all."+div_name+".style.display = 'none'");
  }
}

element_exists = function element_exists(elem_name)
{
  return document.getElementById(elem_name)!=null;
}

scroll_to_element = function scroll_to_element(theElement)
{
  element = document.getElementById(theElement)
  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(element != null){
    selectedPosX += element.offsetLeft;
    selectedPosY += element.offsetTop;
    element = element.offsetParent;
  }
  window.scrollTo(selectedPosX,selectedPosY-50);
}

jsleep = function jsleep(delay)
{
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

setpos = function setpos(div_name, mvLeft,mvTop) {
  obj = document.getElementById(div_name);
  obj.style.position = "absolute";
  obj.style.top = mvTop+'px';
  obj.style.left = mvLeft+'px';
}

get_pos = function get_pos(div_name) {
  obj = document.getElementById(div_name);
  if (!obj) return 0;
  var curleft = curtop = 0;
  if (obj) {
  //if (obj.offsetParent) {
    do {
  	  curleft += obj.offsetLeft;
	    curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return [curleft,curtop];
  } 
}

get_pos_by_element = function get_pos_by_element(element) {
  obj = element;
  if (!obj) return 0;
  var curleft = curtop = 0;
  if (obj) {
  //if (obj.offsetParent) {
    do {
      //alert(obj.offsetLeft);
          curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return [curleft,curtop];
  }
}
//toggle_div = function toggle_div(div_name) {
//if (document.getElementById) { // DOM3 = IE5, NS6
//  if (document.getElementById(div_name).style.display == 'none')
    //document.getElementById(div_name).style.display = 'block';
  //else
//    document.getElementById(div_name).style.display = 'none';
//}
//}

div_visible = function div_visible(div_name) {
  if (document.getElementById(div_name).style.display != 'none') 
  return true;
  else return false;
}

show_div = function show_div(div_name, dtype) {
  if (dtype == null) dtype = 'block';
  if (document.getElementById) { // DOM3 = IE5, NS6
    //alert('show div:'+div_name);
    if (document.getElementById(div_name))
    document.getElementById(div_name).style.display = dtype;
  }
  else {
    if (document.layers) { // Netscape 4
      document.hideShow.visibility = 'visible';
    }
    else { // IE 4
      eval("document.all."+div_name+".style.display = '"+dtype+"'");
    }
  }
}

function show_popup(hoveritem,popupitem)
{
  var hp = document.getElementById(popupitem);
  var a = get_pos_by_element(hoveritem);

  hp.popupid = setTimeout(function() {
	  if (/*@cc_on!@*/false) return;

	  // Set position of hover-over popup
	  //alert(a[1]);
	  hp.style.left = '40px';
	  hp.style.top = a[1] + hoveritem.getHeight() - 5 +'px';//hoveritem.offsetTop + 48;

	  // Set popup to visible
	  hp.style.visibility = "visible";	
  }, 1000);
}

function hide_popup(popupitem)
{
	var hp = document.getElementById(popupitem);
	clearTimeout(hp.popupid);
	hp.style.visibility = "hidden";
}

focus_element = function focus_element(element_name) {
  if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById(element_name).focus();
  }
}

show_inline = function show_inline(div_name) {
  if (document.getElementById) { // DOM3 = IE5, NS6
    //alert('show div:'+div_name);
    document.getElementById(div_name).style.display = 'inline';
  }
  else {
    if (document.layers) { // Netscape 4
      document.hideShow.visibility = 'visible';
    }
    else { // IE 4
      eval("document.all."+div_name+".style.display = 'inline'");
    }
  }
}

window.onerror = function(e)  {
 if (e &&  e.preventDefault) e.preventDefault();
 if (e &&  e.stopPropagation) e.stopPropagation();
 return false;
};

set_application_a_title_text = function set_application_a_title_text(new_title)
{
  element = document.getElementById('application_a_title_text');
  if (element)
  {
    element.innerHTML = '<a href="#" onclick="return false;">'+new_title+'</a>';
//    hide_div('application_a_title');
  }
}

account_update_load_info = function account_update_load_info(did, extra)
{
//  alert('foo');
  var email_part = '';
  if (document.getElementById('account_fl_'+did))
  {
    if (did == 'a')
    {
      if (document.getElementById('account_fl_'+did).email_a.value != '')
      email_part = '/email/'+ document.getElementById('account_fl_'+did).email_a.value;
    }
    if (did == 'b')
    {
      if (document.getElementById('account_fl_'+did).email_b.value != '')
      email_part = '/email/'+ document.getElementById('account_fl_'+did).email_b.value;
    }
  }
  new Ajax.Updater('account_load_info_' + did, '/account/load_info'+ email_part + extra, {asynchronous:false,evalScripts:true});
}

var anchor;

startLyteFrame = function startLyteFrame(title, url) {
  if (anchor) {
    //alert('foo1');
    myLytebox.start(anchor, false, true);
    return false;
  }
//  if (document.getElementById('lytebox_a')) {
//    alert('found element');
//    anchor = document.getElementById('lytebox_a');
//  }
//  else
//  {
    anchor = this.document.createElement('a');
    anchor.setAttribute('rev', 'width: 900px; height: 480px; scrolling: auto;');
    anchor.setAttribute('id', 'lytebox_a');
    anchor.setAttribute('title', title);
    anchor.setAttribute('href', url);
    anchor.setAttribute('rel', 'lyteframe');
    //alert(anchor.getAttrbute('title'));
  //}
  myLytebox.start(anchor, false, true);
  return false;
}

startLytebox = function startLytebox(href, title, rel) 
{
  alert('foo');
  var el = document.getElementById('lytebox_misc');
//        el.href  = href;
//            el.title = title;
//                el.rel = rel;
//  alert(myLytebox);     
//  myLytebox.start(el);
}


get_t_sort = function get_t_sort(setv)
{
 if (setv)
  {
    hide_div('c_up');
    hide_div('c_dn');
  }
  if (!div_visible('t_up') && !div_visible('t_dn'))
  {
    if (!setv) return '';
    show_inline('t_up');
    return '/title_s/1';
  } else
  if (div_visible('t_up'))
  {
    if (!setv) return '/title_s/1';
    hide_div('t_up');
    show_inline('t_dn');
    return '/title_s/2';
  }
  else
  {
    if (!setv) return '/title_s/2';
    hide_div('t_dn');
    return '';
  }
}

get_c_sort = function get_c_sort(setv)
{
  if (setv)
  {
    hide_div('t_up');
    hide_div('t_dn');
  }

  if (!div_visible('c_up') && !div_visible('c_dn'))
  {
    if (!setv) return '';
    show_inline('c_up');
    return '/company_s/1';
  } else
  if (div_visible('c_up'))
  {
    if (!setv) return '/company_s/1';
    hide_div('c_up');
    show_inline('c_dn');
    return '/company_s/2';
  }
  else
  {
    if (!setv) return '/company_s/2';
    hide_div('c_dn');
    return '';
  }
}

get_sort = function get_sort(setv, idd, var_prefix)
{
  if (setv)
  {
    if (idd != 't') { hide_div('t_up'); hide_div('t_dn'); } 
    if (idd != 'c') { hide_div('c_up'); hide_div('c_dn'); } 
    if (idd != 'd') { hide_div('d_up'); hide_div('d_dn'); } 
    if (idd != 'm') { hide_div('m_up'); hide_div('m_dn'); } 
  }
  var idup = idd+'_up';
  var iddn = idd+'_dn';
  if (!div_visible(idup) && !div_visible(iddn))
  {
    if (!setv) return '';
    show_inline(idup);
    return '/'+var_prefix+'_s/1';
  } else
  if (div_visible( (idup) ))
  {
    if (!setv) return '/'+var_prefix+'_s/1';
    
    hide_div(idup);
    show_inline(iddn);
    return '/'+var_prefix+'_s/2';
  }
  else
  {
    if (!setv) return '/'+var_prefix+'_s/2';
    hide_div(iddn);
    return '';
  }
  
}

// CONTEXT MENU:

var _replaceContext = false;    // replace the system context menu?
var _mouseOverContext = false;    // is the mouse over the context menu?
var _noContext = false;       // disable the context menu?
var _divContext = $('divContext');  // makes my life easier


//function $(id) { return document.getElementById(id); }

function InitContext()
{
  _divContext.onmouseover = function() { _mouseOverContext = true; };
  _divContext.onmouseout = function() { _mouseOverContext = false; };

  $('aDisable').onclick = DisableContext;
  $('aEnable').onclick = EnableContext;

  //document.body.onmousedown = ContextMouseDown;
  //document.body.oncontextmenu = ContextShow;
}

// call from the onMouseDown event, passing the event if standards compliant
function ContextMouseDown(event)
{
  if (_noContext || _mouseOverContext)
    return;
  _divContext = $('divContext');
  // IE is evil and doesn't pass the event object
  //if (event == null)
    //event = window.event;

  // we assume we have a standards compliant browser, but check if we have IE
  //var target = event.target != null ? event.target : event.srcElement;

  // only show the context menu if the right mouse button is pressed
  //   and a hyperlink has been clicked (the code can be made more selective)
  if (event.button == 2)// && target.tagName.toLowerCase() == 'a')
    _replaceContext = true;
  else if (!_mouseOverContext)
    _divContext.style.display = 'none';
}

function CloseContext()
{
  _divContext = $('divContext');
  _mouseOverContext;
  _divContext.style.display = 'none';
}

// call from the onContextMenu event, passing the event
// if this function returns false, the browser's context menu will not show up
function ContextShow(event)
{
  if (_noContext || _mouseOverContext)
    return;
  _divContext = $('divContext');
  // IE is evil and doesn't pass the event object
  if (event == null)
    event = window.event;

  // we assume we have a standards compliant browser, but check if we have IE
  var target = event.target != null ? event.target : event.srcElement;

  if (_replaceContext)
  {
    //$('aContextNav').href = target.href;
    //$('aAddWebmark').href = 'http://luke.breuer.com/webmark/?addurl=' +
      //encodeURIComponent(target.href) + '&title=' +
      //encodeURIComponent(target.innerHTML);

    // document.body.scrollTop does not work in IE
    var scrollTop = document.body.scrollTop ? document.body.scrollTop :
      document.documentElement.scrollTop;
    var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft :
      document.documentElement.scrollLeft;

    // hide the menu first to avoid an "up-then-over" visual effect
    _divContext.style.display = 'none';
    _divContext.style.left = event.clientX + scrollLeft + 'px';
    _divContext.style.top = event.clientY + scrollTop + 'px';
    _divContext.style.display = 'block';
    _divContext.style.zIndex = 10;

    _replaceContext = false;

    return false;
  }
}

function DisableContext()
{
  _noContext = true;
  CloseContext();
  $('aEnable').style.display = '';

  return false;
}

function EnableContext()
{
  _noContext = false;
  _mouseOverContext = false; // this gets left enabled when "disable menus" is chosen
  $('aEnable').style.display = 'none';

  return false;
}

// ssykmap module javascript
  var ssbf = function ssbf(obj,prefix,prev_row_id)
  {
	  obj.blur();
	  if (div_visible(prefix+'_ssykmap_child_toggle_'+prev_row_id))
    { 
      document.getElementById(prefix+'_ssykmap_child_toggle_plusminus'+prev_row_id).src = '/skin/images/tree_plus.png';
      hide_div(prefix+'_ssykmap_child_toggle_'+prev_row_id);
    }
    else
    {
      if (!element_exists(prefix+'_brl'+prev_row_id))
      {
        new Ajax.Updater(prefix+'_ssykmap_child_toggle_'+prev_row_id, '/ssykmap/list_professions/prefix/'+prefix+'/br_id/'+prev_row_id, {asynchronous:false, evalScripts:true});
      }
      document.getElementById(prefix+'_ssykmap_child_toggle_plusminus'+prev_row_id).src = '/skin/images/tree_minus.png';
      show_div(prefix+'_ssykmap_child_toggle_'+prev_row_id); 
    }
    return false;
  }

  var ssbf2 = function ssbf2(obj, prefix, prev_row_id)
  {
    obj.blur(); 
    if (div_visible(prefix+'_ssykmap_child_toggle_'+prev_row_id))
    { 
      document.getElementById(prefix+'_ssykmap_child_toggle_plusminus'+prev_row_id).src='/skin/images/tree_plus.png';
      hide_div(prefix+'_ssykmap_child_toggle_'+prev_row_id);
    }
    else
    {
      document.getElementById(prefix+'_ssykmap_child_toggle_plusminus'+prev_row_id).src='/skin/images/tree_minus.png';
      show_div(prefix+'_ssykmap_child_toggle_'+prev_row_id); 
    }
    return false;
  }
