function ChatHelper()
{
  this.bWindowOpen  = false;
  this.bIntegrated  = false;
}
ChatHelper.prototype.openWindow = function()
{
  if(!this.bWindowOpen)
  {
    if(this.bIntegrated)
    {
      var iframe  = document.createElement('iframe');
      iframe.setAttribute('name', 'chatbox');
      iframe.setAttribute('id', 'chatbox');
      //IE compatible way of setting styles...
      iframe.style.width    = '312px';
      iframe.style.height   = '442px';
      iframe.style.overflow = 'hidden';
      iframe.style.border   = '0';
      iframe.style.padding  = '0';
      iframe.style.margin   = '0';
      iframe.style.position = 'absolute';
      iframe.style.top      = '0';
      iframe.style.left     = '0';
      iframe.setAttribute('scrolling', 'no');
      iframe.setAttribute('src', '/chat/index.html');
      document.getElementById('callchatbox').appendChild(iframe);
      this.bWindowOpen = true;
      document.cookie = 'chatact=1';
    }
    else
    {
      this.w = window.open('/chat/index.html', 'LiveHelpChat', 'width=312px,height=542px,status=no,toolbar=no,menubar=no,location=no');
      this.w.focus();
      this.bWindowOpen = true;
    }
  }
  else
  {
    if(!this.bIntegrated)
    {
      if(!this.w.closed)
      {
        this.w.focus();
      }
      else
      { //its closed, user wants to open it again!
        this.bWindowOpen = false;
        this.openWindow();
      }
    }
  }
}
ChatHelper.prototype.closeWindow = function()
{
  if(this.bWindowOpen)
  {
    if(this.bIntegrated)
    {
      var iframe = document.getElementById('chatbox');
      document.getElementById('callchatbox').removeChild(iframe);
      this.bWindowOpen = false;
      document.cookie = 'chatact=0';
    }
    else
    {
      this.w.window.close();
      this.bWindowOpen = false;
    }
  }
}
ChatHelper.prototype.getStartMessage = function()
{
  var aCookie = document.cookie.split(';');
  for(i=0,ii=aCookie.length; i<ii; i++)
  {
    aCookieEntry = aCookie[i].split('=');
    if('chaturl' == aCookieEntry[0] || ' chaturl' == aCookieEntry[0])
    {
      if(aCookieEntry[1] == location.href)
      {
        return '';
      }
    }
  }
  var sMessage = 'URL: ' + location.href;
  document.cookie = 'chaturl='+location.href;
  return sMessage;
}
ChatHelper.prototype.advertiseChat = function()
{
  try
  { //check if jQuery has been loaded
    var jQueryIsLoaded = jQuery;
    jQueryIsLoaded = true;
  }
  catch(e)
  {
    jQueryIsLoaded = false;
  }
  if(jQueryIsLoaded)
  {
    if(!this.bWindowOpen && jQuery('#livehelpchat').is(':visible'))
    {
      jQuery('#livehelpchat').fadeOut('500');
      jQuery('#livehelpchat').fadeIn('500');
    }
    if(!jQuery('#livehelpchat').is(':visible'))
    {
      clearInterval(interval);
    }
  }
}
var oCH = new ChatHelper();
var sURL = location.href;
if(0 <= sURL.indexOf('geschenkidee.ch'))
{
  document.domain="geschenkidee.ch";
}
if(0 <= sURL.indexOf('ideecadeau.ch'))
{
  document.domain="ideecadeau.ch";
}

function reopenWindow()
{
  var aCookie = document.cookie.split(';');
  for(i=0, ii=aCookie.length; i<ii; i++)
  {
    aCookieEntry = aCookie[i].split('=');
    if('chatact' == aCookieEntry[0] || ' chatact' == aCookieEntry[0])
    {
      if(parseInt(aCookieEntry[1]))
      {
        oCH.openWindow();
      }
    }
  }
}
setTimeout('reopenWindow()', 2000);
var interval = setInterval('oCH.advertiseChat()', 10000);

