
// -------------------------------------------------------------- //
// Copyright (c) 2009 TEC-IT Datenverarbeitung GmbH, http://www.tec-it.com
//
// Use or modify this file ONLY according to TEC-IT’s Standard 
// License Terms for Freeware!!!
//
// Verwenden oder ändern Sie diese Datei NUR entsprechend der 
// Standard-Lizenzbestimmungen für TEC-IT Freeware !!
// -------------------------------------------------------------- //

// -------------------------------------------------------------- //
// Javascript (Development Version) used by
// TECIT.Freeware.WebControls.Bookmark WebControl
// TECIT.Website.WebControls.Bookmark WebControl
// -------------------------------------------------------------- //

// -------------------------------------------------------------- //
// for production use, compress this script with
// http://javascriptcompressor.com/
// and copy it to the bookmark.js file
// -------------------------------------------------------------- //

var copyright = "Copyright (c) 2009 TEC-IT Datenverarbeitung GmbH # Use or modify this file ONLY according to TEC-IT’s Standard License Terms for Freeware #  See http://www.tec-it.com"

// http request variable
var _xmlHttp = null;

// button variables
var _bt = null;

// panel variables
var _pn = null;
var _pnPX = 0;
var _pnPY = 0;
var _pnW = 252;
var _pnHtml = null;
var count=0;
// timer variables
var _myTimer = null;

function EncUri(url)
{
  if (typeof encodeURIComponent == 'function')
  {
    // Use JavaScript built-in function
    // IE 5.5+ and Netscape 6+ and Mozilla
    return encodeURIComponent(url);
  }
  else 
  {
    // Netscape 4.x and IE5
    return escape(url);
  }
}

function ShowBookmark(id, url, language)
{ 
  if (_xmlHttp==null)
    _xmlHttp=GetXmlHttpObject();
  
  if (_xmlHttp==null || !document.getElementById)
    return false;
    
    // get panel and button elements by id
  _pn  = document.getElementById(id + '_Panel');
  _bt = document.getElementById(id + '_Link');
  
  if (_pnHtml == null)
  {
      // load bookmark and display
      url=url + "?url=" + EncUri(location.href);
      url=url + "&title=" + EncUri(document.title);
      url=url + "&language=" + language;
      _xmlHttp.onreadystatechange=StateChanged;
      _xmlHttp.open("GET",url,true);
      _xmlHttp.send(null);
  }
  
  // display panel with bookmark links
  DisplayPanel(_pnHtml);
  
  // clear time out
  SetHideTimer(false);
 
  return true;
}

function StateChanged() 
{ 
  if (_xmlHttp.readyState==4)
  {
    var html = _xmlHttp.responseText;
    
    if (html.indexOf('<!-- OK -->')==-1)
        html = '<h3>Sorry, service not available...</h3>';
    
    DisplayPanel(html);        
   
  }
}

function GetXmlHttpObject()
{
  var xmlHttp = null;
  
  // Firefox, Opera 8.0+, Safari
  try { xmlHttp = new XMLHttpRequest(); }
  catch (e)
  {
    // Internet Explorer
    try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e)
    {
      try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
      catch (e) {}
    }
  }  
  return xmlHttp;
}

function DisplayPanel(innerHTML)
{
  // check if style is supported by all browsers
  if (_pn && _pn.style)
  {
    if (innerHTML != null)
    {
        _pnHtml = innerHTML;
        _pn.innerHTML = innerHTML;
    }

    // set div position
    PositionPanel();
    
    // set div attributes
    _pn.style.width = '250px';
    _pn.style.visibility='visible';
  }
}

function PositionPanel()
{
    var pageWidth = PageWidth();
    _pnPX = FindPosX(_bt);
    _pnPY = FindPosY(_pn);
    
    if (pageWidth != null && _pnPX > 0)
    {
        if((_pnPX + _pnW) > pageWidth)
            _pnPX = pageWidth - _pnW;
        
        // set left style attribute
        _pn.style.left = _pnPX + 'px';
    }
}

function FindPosX(object)
{
    var curleft = 0;
    if(object.offsetParent)
        while(1) 
        {
          curleft += object.offsetLeft;
          if(!object.offsetParent)
            break;
          object = object.offsetParent;
        }
    else if(object.x)
        curleft += object.x;
    return curleft;
}

function FindPosY(object)
{
    var curtop = 0;
    if(object.offsetParent)
        while(1) 
        {
          curtop += object.offsetTop;
          if(!object.offsetParent)
            break;
          object = object.offsetParent;
        }
    else if(object.y)
        curtop += object.y;
    return curtop;
}
  
// calculate the current window width //
function PageWidth()
{
 if (window.innerWidth != null)
  return window.innerWidth;
 else if (document.documentElement && document.documentElement.clientWidth)
  return document.documentElement.clientWidth;
 else if (document.body != null)
  return document.body.clientWidth;
 else
  return null;
}

// calculate the current window height //
/*
function PageHeight() {
 if (window.innerHeight != null)
  return window.innerHeight;
 else if (document.documentElement && document.documentElement.clientHeight)
  return document.documentElement.clientHeight;
 else if (document.body != null)
  return document.body.clientHeight;
 else
  return null;
}*/

/*function Contains(event)
{
    if (document.all)
        return (!event.fromElement.contains(event.toElement) && !_pn.contains(event.toElement));
    
    // fallback for other browsers
    var mouseleft = event.clientX;
    var mousetop = event.clientY;
        
    return ((mousetop > _pnPY + _pn.offsetHeight)
            || (mouseleft < _pnPX)
            || (mousetop < _pnPY)
            || (mouseleft > _pnPX + _pnW));
}*/

function PanelMouseOut(event)
{
    // SetHideTimer( Contains(event) );    
    SetHideTimer(true);
   
    /*
    Why is "Contains(event)" no longer required?
    Moving over a link within the div (panel) still fires a mouse out event
    but immediately also a mouse over event follows, which clears the timer
    */
}

function PanelMouseOver(event)
{
    SetHideTimer(false);
}

function SetHideTimer(enable)
{
    if (enable == true)
    {
        if(_myTimer!=null)
            clearTimeout(_myTimer);    
        _myTimer = setTimeout("HideBookmark()",1500);
    }
    else
    {
        clearTimeout(_myTimer);
        _myTimer = null;
    }
}

function HideBookmark()
{
   _mouseOver = false;
   _pn.style.visibility='hidden';
   SetHideTimer(false);
}

function AddFavorite()
{
    url = location.href;
    title = document.title;
    message = 'In order to bookmark this site you need to do so manually through your browser.';
    try
    {
        // Mozilla Firefox
        // window.sidebar.addPanel was not used here because the bookmark
        // opens the link on the sidebar by default
        if (window.sidebar) 
          alert('Press Ctrl-D to bookmark this page in your favorites.');
        // IE Favorite
        else if (window.external && document.all) 
          window.external.AddFavorite(url,title);
        // Opera Hotlist 
        else if(window.opera && window.print) 
          alert('Press Ctrl-T to bookmark this page in your favorites.');
        else
          alert(message);
    }
    catch(err) 
    { 
        alert(message); 
    }
  
    return false;
}
