var g_accounts = $A([ ]);
if (window.console == undefined) { window.console = {log:function(){}}; }

function proceedError(transport)
{
  if (transport.status == 401)
    {
      window.location.href = "/login";
    }
    else
    {
      $('pageBody').innerHTML = transport.responseText;
      lightbox.prototype.deactivate();
      lightbox.prototype.close();
    }
}

function checkIsGermany(yesBundeslandId, notBundeslandId, country, historical)
{
  if(!$(historical).checked)
  {
    var selected = $(country).value;
    if(selected == 'DE' || selected == 'GE')
    {
      $(notBundeslandId).hide();
      $(yesBundeslandId).show();
      //if(arguments.length > 3 )  $(bundesInputId).value = '';
    }
    else
    {
      $(yesBundeslandId).hide();
      $(notBundeslandId).show();
    }
  }
}

function checkMarriageDevorce(devorceId, partershipType)
{
  var selected = $(partershipType).value;
  var id = devorceId.split('_');

  $('partnershipCaptionType_'+id[2]+'_1').show();
  $('partnershipCaptionType_'+id[2]+'_2').hide();
  $(devorceId).hide();

  if(selected == '1' || selected == '4')
  {
    $(devorceId).show();

    $('partnershipCaptionType_'+id[2]+'_3').show();
    $('partnershipCaptionType_'+id[2]+'_4').hide();

    //$('partnershipCaptionType_'+id[2]+'_5').show();
    //$('partnershipCaptionType_'+id[2]+'_6').show();

    if (selected == '1')
    {
      $('partnershipCaptionType_'+id[2]+'_5').show();
      $('partnershipCaptionType_'+id[2]+'_6').hide();
    }

    if (selected == '4')
    {
      $('partnershipCaptionType_'+id[2]+'_3').hide();
      $('partnershipCaptionType_'+id[2]+'_4').show();

      $('partnershipCaptionType_'+id[2]+'_5').hide();
      $('partnershipCaptionType_'+id[2]+'_6').show();
    }

  }

  if(selected == '3' || selected == '4')
  {
    $('marriage_witnesses_'+id[2]).hide();
    $('partnershipCaptionType_'+id[2]+'_1').hide();
    $('partnershipCaptionType_'+id[2]+'_2').show();
  }
  else
  {
    $('marriage_witnesses_'+id[2]).show();
  }
}



function checkBoxChoice(field)
{
  for (j = 0; j < document.login.gender.length; j++)
  {
    if (eval("document.login.gender[" + j + "].checked") == true)
    {
      document.login.gender[j].checked = false;
      if (j == field)
      {
        document.login.gender[j].checked = true;
      }
    }
  }
}

function new_video(vid, vkey, target)
{
  hiclip_vid = vid;
  hiclip_vkey = vkey;
  hiclip_write_to_id = target;
  var jsel = document.createElement('SCRIPT');
  jsel.type = 'text/javascript';
  jsel.src = 'http://www.hiclip.de/api/player_v10/'+'?hiclip_vid='+vid+"&hiclip_vkey="+hiclip_vkey+"&hiclip_write_to_id="+hiclip_write_to_id;
  document.body.appendChild(jsel);
}

function hideIfEmpty(toHide)
{
  var hide = true;
  for(var i =0;i < arguments.length-2;i++)
     hide = (hide && $F(arguments[i]) == '');
  if(arguments.length == 2)
      hide = (hide && $F(arguments[0]) == '');
  if(hide)
  {
    $(arguments[arguments.length-1]).hide();
  }
}

function hideIfChanged(toHide)
{
  if($(toHide))
  {
    $(toHide).hide();
  }
}



// [accid|all:]url[;[accid|all:]url]
function track(name)
{
  if(!window.urchinTracker || g_accounts.length <= 0)
    return;

  var reset_tracking = false;

  trlist = name.split(";");
  for(i=0; i < trlist.length; i++)
  {
    var names = trlist[i].split(":");
    if(names.length <= 1)
    { // -> old style ("/url")
      real_track(names[0], g_accounts[0], reset_tracking);
      reset_tracking = true;
    }
    else
    { // -> new style ("x:/url")
      accid = names[0];
      url = names[1];
      if(accid == "all")
      {
        for(accid=0; accid < g_accounts.length; accid++)
        {
          real_track(url, g_accounts[accid], reset_tracking);
          reset_tracking = true;
        }
      }
      else
      {
        accid = parseInt(accid);
        if(accid >= 0 && accid < g_accounts.length)
        {
          real_track(url, g_accounts[accid], reset_tracking);
          reset_tracking = true;
        }
      }
    }
  }
}



function real_track(url, acc, reset)
{
  if(reset)
    _uff = 0; // Reset for other account

  _userv = 2;
  _uacct = acc;
  urchinTracker(url);
}



function widgetCode(width, height, siteurl, publictoken, fullname)
{
  return '<div id="flashcontent" style="width: '+width+'px; height: '+height+'px;"><h1>Stammbaum '+fullname+'</h1><object width="100%" height="100%"><param name="movie" value="'+siteurl+'/flash/widget.swf"/><param name="flashvars" value="widgetDebugUrl='+siteurl+'/tree/getXMLTreeWidget/&widgetID='+publictoken+'/><param name="scale" value="noscale" /><param name="salign" value="lt" /><embed src="'+siteurl+'/flash/widget.swf" scale="noscale" salign="lt" width="100%" height="100%" type="application/x-shockwave-flash" flashvars="widgetDebugUrl='+siteurl+'/tree/getXMLTreeWidget/&widgetID='+publictoken+'"></embed></object><p>Finden Sie mich in Suchmaschinen unter:</p> <h2>Ahnenforschung '+fullname+'</h2><h2>Ahnen '+fullname+'</h2><h2>Familienforschung '+fullname+'</h2></div>';
}

function blurLink(theObject)
{
     theObject.blur();
}



function addOpenAds(zone)
{
  if (!document.phpAds_used)
    document.phpAds_used = ',';

  phpAds_random = new String (Math.random());
  phpAds_random = phpAds_random.substring(2,11);

  document.write ("<" + "script language='JavaScript' type='text/javascript' src='");
  document.write ("http://ads.familyone.de/adjs.php?n=" + phpAds_random);
  document.write ("&amp;what=zone:" + zone + "&amp;target=_blank");
  document.write ("&amp;exclude=" + document.phpAds_used);
  if (document.referrer)
     document.write ("&amp;referer=" + escape(document.referrer));
  document.write ("'><" + "/script>");
}

function showMessageOnSubmit(message, element)
{
  $(element).innerHTML = '<span id="loadingMessage">'+message+'</span>';
  return true;
}

function getViewportDimensions()
{
   var viewportwidth;
   var viewportheight;

 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth;
      viewportheight = window.innerHeight;
 }

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth;
       viewportheight = document.documentElement.clientHeight;
 }

 // older versions of IE

 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
       viewportheight = document.getElementsByTagName('body')[0].clientHeight;
 }
 return {width: viewportwidth, height: viewportheight};
}

function closeModalboxAndGoTo(url)
{
  window.location.href = url;
}

function openVideoBox(id)
{
  Modalbox.show('Upload video', '/videobox/'+id, {width: 600, height: 300});
}

function getHeightForPhoto(width)
{
  return Math.ceil((width-200)/100)*100;
}

function refreshInvitations(invType) // invType: tree / friends
{
  if(invType == 'friends')
  {
    divToUpdate = 'friendsInvitationsCounter';
  }
  else if(invType == 'tree')
  {
    divToUpdate = 'treeInvitationsCounter';
  }

    new Ajax.Updater({success: divToUpdate}, '/refreshInvitations/' + invType , {
      method: 'post',
      evalScripts: true,
      onFailure: function() { alert('Problem');}
    });
}

// Lists the images from an album
function listAlbumImages(url, searchParam)
{
  albumList = $('albumsList');
  albumId = albumList.options[albumList.selectedIndex].value;

  if (searchParam)
  {
    searchStringValue = $('searchSid').value;
    isSearch = 1;
  }
  else
  {
    searchStringValue = '';
    isSearch = 0;
  }

  new Ajax.Updater({success: 'album'}, url,{
    method: 'post',
    evalScripts: true,
    parameters: { album: albumId, search: isSearch, searchString: searchStringValue },
    onFailure: function(){alert('Problem. Please try again later');},
    on401: function() {alert('Session expired');}
  });
}

function addEngineFirefox(url, icon, name, category)
{
  if ((typeof window.sidebar == "object") && (typeof
  window.sidebar.addSearchEngine == "function"))
  {
    window.sidebar.addSearchEngine(
      url				|| "http://www.familyone.de/tools/familyone.src",
      icon			|| "http://www.familyone.de/favicon.ico",
      name			|| "FamilyOne",
      category	|| "Social Networks");
    track('/tools/firefoxSearch');
  }
  else
  {
    alert("Leider ist diese Funktion in Ihrem Browser nicht verfügbar.");
  }
}

function addEngineIE(url)
{
  if(!!(window.attachEvent && !window.opera) && typeof document.body.style.maxHeight != "undefined" && typeof window.external == "object")
  {
    window.external.AddSearchProvider(
      url || "http://www.familyone.de/tools/familyone-ie.xml"
    );
    track('/tools/ieSearch');
  }
  else
  {
    alert("Leider ist diese Funktion in Ihrem Browser nicht verfügbar.");
  }
}

var IFrameUpload = Class.create();

IFrameUpload.prototype = {
  // Form.onSubmit event handler
  // Will create a new IFrame and set it as the "target" for the form
  onSubmit: function(ev) {
    if (ev)
      Event.stop(ev);

    this.form = $(form);
    this.iframes = $A();

    var iframe = this.createIframe();
    this.form.target =$(iframe).name;
    this.form.submit();
    return true;
  },

  // Creates an iframe
  createIframe: function() {
    var doc = this.form.ownerDocument;
    if($('iframeupload'))
      $('iframeupload').remove();

    var iframe = $(doc.createElement("IFRAME"));

    iframe.id = iframe.name="iframeupload"
    iframe.setStyle({ width: '1px', height: '1px', border: '0px' });
    var x = window.frames.length;
    doc.body.appendChild(iframe);
    var frame = window.frames[x];
    frame.name = iframe.name;
    //this.iframes.push({ IFrame: iframe, Window: frame });
    return frame;
  }
};

function iframeUpload(formId)
{
  window.form = formId;
  var fx = IFrameUpload.prototype.onSubmit.bind(IFrameUpload.prototype);
  fx();
}

function showFlash()
{
  frontFlash = $('middleContainer');
  if (frontFlash)
  {
    frontFlash.style.visibility = 'visible';
  }
}

function hideFlash()
{
  frontFlash = $('middleContainer');
  if (frontFlash)
  {
    frontFlash.style.visibility = 'hidden';
  }
}

function findX(obj)
{
  var curleft=0;
  if(obj.offsetParent)
  {
    while(obj.offsetParent)
    {
    curleft+=obj.offsetLeft;
    obj=obj.offsetParent;
    }
  }
  else if(obj.x)
    curleft+=obj.x;

  return curleft;
}

function findY(obj)
{
  var curtop=0;
  if(obj.offsetParent)
  {
    while(obj.offsetParent)
    {
      curtop+=obj.offsetTop;
      obj=obj.offsetParent;
    }
  }
  else if(obj.y)
    curtop+=obj.y;

  return curtop;
}

